-
Notifications
You must be signed in to change notification settings - Fork 67
/
GuidedTour.provider.tsx
82 lines (69 loc) 路 2.35 KB
/
GuidedTour.provider.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
import {noop} from 'lodash';
import React, {createContext, useCallback, useContext, useMemo, useState} from 'react';
import Joyride from 'react-joyride';
import {useLocation} from 'react-router-dom';
import StepContent from 'components/GuidedTour/StepContent';
import {CallbackByGuidedTour, StepsByGuidedTour} from 'components/GuidedTour/steps';
import {switchTestRunMode} from 'components/GuidedTour/testRunSteps';
import {IGuidedTourState} from 'constants/GuidedTour';
import HomeAnalyticsService from 'services/Analytics/HomeAnalytics.service';
import GuidedTourService from 'services/GuidedTour.service';
const {onGuidedTourClick} = HomeAnalyticsService;
interface IContext {
isGuidedTourRunning: boolean;
onStart(): void;
setGuidedTourStep(index: number): void;
}
const Context = createContext<IContext>({
isGuidedTourRunning: false,
onStart: noop,
setGuidedTourStep: noop,
});
interface IProps {
children: React.ReactNode;
}
export const useGuidedTour = () => useContext(Context);
const GuidedTourProvider = ({children}: IProps) => {
const pathname = useLocation().pathname;
const tourByPathname = GuidedTourService.getByPathName(pathname);
const [guidedTourState, setGuidedTourState] = useState<IGuidedTourState>({
callback: noop,
run: false,
stepIndex: 0,
steps: [],
});
const onStart = useCallback(() => {
if (!tourByPathname) return;
onGuidedTourClick(); // Analytics
switchTestRunMode(0);
setGuidedTourState(prev => ({
...prev,
callback: CallbackByGuidedTour[tourByPathname](setGuidedTourState),
run: true,
stepIndex: 0,
steps: StepsByGuidedTour[tourByPathname],
}));
}, [tourByPathname]);
const setGuidedTourStep = useCallback((stepIndex: number) => {
setGuidedTourState(prev => ({...prev, stepIndex}));
}, []);
const value = useMemo(
() => ({isGuidedTourRunning: guidedTourState.run, onStart, setGuidedTourStep}),
[guidedTourState.run, onStart, setGuidedTourStep]
);
return (
<Context.Provider value={value}>
<Joyride
callback={guidedTourState.callback}
continuous
disableOverlay
run={guidedTourState.run}
stepIndex={guidedTourState.stepIndex}
steps={guidedTourState.steps}
tooltipComponent={StepContent}
/>
{children}
</Context.Provider>
);
};
export default GuidedTourProvider;