-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathTourProvider.tsx
187 lines (162 loc) · 5.58 KB
/
TourProvider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import type React from "react";
import { createContext, useRef, useCallback, useReducer } from "react";
import type { Step } from "react-joyride";
/**
* TourProvider manages the state and actions for product tours in the application.
* It provides the core mechanics for:
* - Managing tour states (running, stopped, current step, etc.)
* - Handling tour actions (start, stop, next step, etc.)
* - Maintaining references to multiple tours that can be registered
*
* The actual UI implementation is handled by the <Tour> component which uses this provider
* to manage its state and behavior. The Tour component wraps react-joyride to render
* the actual tour UI elements.
*/
interface TourState {
key: string;
run: boolean;
continuous: boolean;
loading: boolean;
stepIndex: number;
steps: Step[];
}
export interface TourAction {
type: "START" | "RESET" | "STOP" | "GOTO" | "NEXT" | "RESTART";
payload?: any;
}
const reducer = (state: TourState, action: TourAction): TourState => {
switch (action.type) {
case "START":
return { ...state, run: true };
case "RESET":
return { ...state, stepIndex: 0 };
case "STOP":
return { ...state, run: false };
case "NEXT": {
const nextIndex = state.stepIndex + 1;
if (nextIndex < state.steps.length) return { ...state, stepIndex: nextIndex };
console.log(`Tour ${state.key} has no more steps`);
return state;
}
case "GOTO":
return { ...state, ...action.payload };
case "RESTART":
return {
...state,
stepIndex: 0,
run: true,
loading: false,
key: new Date().toISOString(),
};
default:
return state;
}
};
export const userTourStateReducer = (): [TourState, React.Dispatch<TourAction>] =>
useReducer(reducer, createInitialState([]));
const createInitialState = (steps: Step[]): TourState => ({
key: new Date().toISOString(),
run: false,
continuous: true,
loading: false,
stepIndex: 0,
steps: steps.map((step) => ({
...step,
// TODO: although html is predefined by LSE assets, to avoid XSS, we should better sanitize it by allowing only simple html tags like <b>, <i>, <u> etc.
title: typeof step.title === "string" ? <div dangerouslySetInnerHTML={{ __html: step.title }} /> : step.title,
content:
typeof step.content === "string" ? <div dangerouslySetInnerHTML={{ __html: step.content }} /> : step.content,
})),
});
type ProductTourState = "ready" | "skipped" | "completed";
const updateProductTourState = async (
api: any,
name: string,
state: ProductTourState,
interactionData: Record<string, any> = {},
) => {
return await api.callApi("updateProductTour", {
params: {
name,
},
body: {
state,
interaction_data: interactionData,
},
});
};
interface TourContextType {
registerTour: (name: string, dispatch: React.Dispatch<TourAction>) => void;
unregisterTour: (name: string) => void;
startTour: (name: string) => void;
setTourViewed: (name: string, isSkipped: boolean, interactionData: Record<string, any>) => void;
restartTour: (name: string) => void;
}
export const TourContext = createContext<TourContextType | null>(null);
// TODO: once useAPI is unified and moved into core library we need to come back and clean this up as it will not be necessary to pass it in to the provider
export const TourProvider: React.FC<{
children: React.ReactNode;
useAPI: () => { callApi: (apiName: string, params: Record<string, any>) => any };
}> = ({ children, useAPI }) => {
const api = useAPI();
const toursRef = useRef<Record<string, React.Dispatch<TourAction>>>({});
const registerTour = (name: string, dispatch: React.Dispatch<TourAction>) => {
toursRef.current[name] = dispatch;
};
const startTour = useCallback(
async (name: string) => {
const dispatch = toursRef.current[name];
if (!dispatch) {
console.error("Dispatch for tour", name, "not found");
return;
}
const response = await api.callApi("getProductTour", { params: { name } });
if (response?.$meta?.status !== 200) {
console.error("Error fetching tour data", response);
return;
}
if (response.awaiting) {
console.info(`Tour "${name}" is awaiting other tours`);
return;
}
if (!response.steps?.length) {
console.info(`No steps found for tour "${name}"`);
return;
}
if (response.state === "completed" || response.state === "skipped") {
console.info(`Tour "${name}" is already completed`);
return;
}
const state = createInitialState(response.steps);
dispatch({ type: "GOTO", payload: { ...state, run: true } });
},
[api],
);
const setTourViewed = useCallback(
(name: string, isSkipped: boolean, interactionData: Record<string, any> = {}) => {
// TODO: currently we don't have per-tour complete state, so we just update the global state
updateProductTourState(api, name, isSkipped ? "skipped" : "completed", interactionData);
},
[api],
);
const restartTour = useCallback(
(name: string) => {
const dispatch = toursRef.current[name];
if (!dispatch) {
console.error("Dispatch for tour", name, "not found");
return;
}
dispatch({ type: "RESTART" });
updateProductTourState(api, name, "ready");
},
[api],
);
const unregisterTour = (name: string) => {
delete toursRef.current[name];
};
return (
<TourContext.Provider value={{ registerTour, unregisterTour, startTour, setTourViewed, restartTour }}>
{children}
</TourContext.Provider>
);
};