|
1
|
"use client" |
|
2
|
|
|
3
|
import { getLearningPath } from "@/utils/learning-paths" |
|
4
|
import React, { createContext, useContext, useEffect, useState } from "react" |
|
5
|
import { LearningPathFinishType } from "@/components/LearningPath/Finish" |
|
6
|
import { useAnalytics, useIsBrowser } from "docs-ui" |
|
7
|
import { usePathname, useRouter } from "next/navigation" |
|
8
|
|
|
9
|
export type LearningPathType = { |
|
10
|
name: string |
|
11
|
label: string |
|
12
|
description?: string |
|
13
|
steps: LearningPathStepType[] |
|
14
|
finish?: LearningPathFinishType |
|
15
|
notificationId?: string |
|
16
|
} |
|
17
|
|
|
18
|
export type LearningPathStepType = { |
|
19
|
title?: string |
|
20
|
description?: string |
|
21
|
descriptionJSX?: JSX.Element |
|
22
|
path?: string |
|
23
|
} |
|
24
|
|
|
25
|
export type LearningPathContextType = { |
|
26
|
path: LearningPathType | null |
|
27
|
setPath: (value: LearningPathType) => void |
|
28
|
currentStep: number |
|
29
|
setCurrentStep: (value: number) => void |
|
30
|
startPath: (path: LearningPathType) => void |
|
31
|
updatePath: (data: Pick<LearningPathType, "notificationId">) => void |
|
32
|
endPath: () => void |
|
33
|
nextStep: () => void |
|
34
|
hasNextStep: () => boolean |
|
35
|
previousStep: () => void |
|
36
|
hasPreviousStep: () => boolean |
|
37
|
goToStep: (stepIndex: number) => void |
|
38
|
isCurrentPath: () => boolean |
|
39
|
goToCurrentPath: () => void |
|
40
|
baseUrl?: string |
|
41
|
} |
|
42
|
|
|
43
|
type LearningPathProviderProps = { |
|
44
|
children?: React.ReactNode |
|
45
|
baseUrl?: string |
|
46
|
} |
|
47
|
|
|
48
|
const LearningPathContext = createContext<LearningPathContextType | null>(null) |
|
49
|
|
|
50
|
export const LearningPathProvider: React.FC<LearningPathProviderProps> = ({ |
|
51
|
children, |
|
52
|
baseUrl, |
|
53
|
}) => { |
|
54
|
const [path, setPath] = useState<LearningPathType | null>(null) |
|
55
|
const [currentStep, setCurrentStep] = useState(-1) |
|
56
|
const { isBrowser } = useIsBrowser() |
|
57
|
const pathname = usePathname() |
|
58
|
const router = useRouter() |
|
59
|
const { track } = useAnalytics() |
|
60
|
|
|
61
|
const startPath = (path: LearningPathType) => { |
|
62
|
setPath(path) |
|
63
|
setCurrentStep(-1) |
|
64
|
if (isBrowser) { |
|
65
|
localStorage.setItem( |
|
66
|
"learning-path", |
|
67
|
JSON.stringify({ |
|
68
|
pathName: path.name, |
|
69
|
currentStep: -1, |
|
70
|
}) |
|
71
|
) |
|
72
|
} |
|
73
|
|
|
74
|
track({ |
|
75
|
event: { |
|
76
|
event: `learning_path_${path.name}`, |
|
77
|
options: { |
|
78
|
url: pathname, |
|
79
|
state: `start`, |
|
80
|
}, |
|
81
|
}, |
|
82
|
}) |
|
83
|
} |
|
84
|
|
|
85
|
useEffect(() => { |
|
86
|
if (path && currentStep === -1) { |
|
87
|
nextStep() |
|
88
|
} |
|
89
|
}, [path]) |
|
90
|
|
|
91
|
const endPath = () => { |
|
92
|
const didFinish = currentStep === (path?.steps.length || 0) - 1 |
|
93
|
const reachedIndex = currentStep === -1 ? 0 : currentStep |
|
94
|
track({ |
|
95
|
event: { |
|
96
|
event: `learning_path_${path?.name}`, |
|
97
|
options: { |
|
98
|
url: pathname, |
|
99
|
state: !didFinish ? `closed` : `end`, |
|
100
|
reachedStep: |
|
101
|
path?.steps[reachedIndex]?.title || |
|
102
|
path?.steps[reachedIndex]?.description || |
|
103
|
path?.steps[reachedIndex]?.descriptionJSX || |
|
104
|
reachedIndex, |
|
105
|
}, |
|
106
|
}, |
|
107
|
}) |
|
108
|
setPath(null) |
|
109
|
setCurrentStep(-1) |
|
110
|
if (isBrowser) { |
|
111
|
localStorage.removeItem("learning-path") |
|
112
|
} |
|
113
|
} |
|
114
|
|
|
115
|
const hasNextStep = () => currentStep !== (path?.steps.length || 0) - 1 |
|
116
|
|
|
117
|
const nextStep = () => { |
|
118
|
if (!path || !hasNextStep()) { |
|
119
|
return |
|
120
|
} |
|
121
|
const nextStepIndex = currentStep + 1 |
|
122
|
setCurrentStep(nextStepIndex) |
|
123
|
const newPath = path.steps[nextStepIndex].path |
|
124
|
if (isBrowser) { |
|
125
|
localStorage.setItem( |
|
126
|
"learning-path", |
|
127
|
JSON.stringify({ |
|
128
|
pathName: path.name, |
|
129
|
currentStep: nextStepIndex, |
|
130
|
}) |
|
131
|
) |
|
132
|
} |
|
133
|
if (pathname !== newPath && newPath) { |
|
134
|
router.push(newPath) |
|
135
|
} |
|
136
|
} |
|
137
|
|
|
138
|
const hasPreviousStep = () => currentStep > 0 |
|
139
|
|
|
140
|
const previousStep = () => { |
|
141
|
if (!path || !hasPreviousStep()) { |
|
142
|
return |
|
143
|
} |
|
144
|
|
|
145
|
const previousStepIndex = currentStep - 1 |
|
146
|
setCurrentStep(previousStepIndex) |
|
147
|
const newPath = path.steps[previousStepIndex].path |
|
148
|
if (isBrowser) { |
|
149
|
localStorage.setItem( |
|
150
|
"learning-path", |
|
151
|
JSON.stringify({ |
|
152
|
pathName: path.name, |
|
153
|
currentStep: previousStepIndex, |
|
154
|
}) |
|
155
|
) |
|
156
|
} |
|
157
|
if (pathname !== newPath && newPath) { |
|
158
|
router.push(newPath) |
|
159
|
} |
|
160
|
} |
|
161
|
|
|
162
|
const goToStep = (stepIndex: number) => { |
|
163
|
if (!path || stepIndex >= path.steps.length) { |
|
164
|
return |
|
165
|
} |
|
166
|
|
|
167
|
setCurrentStep(stepIndex) |
|
168
|
const newPath = path.steps[stepIndex].path |
|
169
|
if (isBrowser) { |
|
170
|
localStorage.setItem( |
|
171
|
"learning-path", |
|
172
|
JSON.stringify({ |
|
173
|
pathName: path.name, |
|
174
|
currentStep: stepIndex, |
|
175
|
}) |
|
176
|
) |
|
177
|
} |
|
178
|
if (pathname !== newPath && newPath) { |
|
179
|
router.push(newPath) |
|
180
|
} |
|
181
|
} |
|
182
|
|
|
183
|
const isCurrentPath = () => { |
|
184
|
if (!path || currentStep === -1) { |
|
185
|
return false |
|
186
|
} |
|
187
|
|
|
188
|
return pathname === path.steps[currentStep].path |
|
189
|
} |
|
190
|
|
|
191
|
const goToCurrentPath = () => { |
|
192
|
if (!path || currentStep === -1 || !path.steps[currentStep].path) { |
|
193
|
return |
|
194
|
} |
|
195
|
|
|
196
|
router.push(path.steps[currentStep].path!) |
|
197
|
} |
|
198
|
|
|
199
|
const updatePath = (data: Pick<LearningPathType, "notificationId">) => { |
|
200
|
if (!path) { |
|
201
|
return |
|
202
|
} |
|
203
|
setPath({ |
|
204
|
...path, |
|
205
|
...data, |
|
206
|
}) |
|
207
|
} |
|
208
|
|
|
209
|
const initPath = () => { |
|
210
|
if (isBrowser) { |
|
211
|
// give query parameters higher precedence over local storage |
|
212
|
const queryPathName = new URLSearchParams(location.search).get("path") |
|
213
|
const queryPath = queryPathName |
|
214
|
? getLearningPath(queryPathName) |
|
215
|
: undefined |
|
216
|
if (queryPath) { |
|
217
|
startPath(queryPath) |
|
218
|
} else { |
|
219
|
const storedPath = localStorage.getItem("learning-path") |
|
220
|
if (storedPath) { |
|
221
|
const storedPathParsed = JSON.parse(storedPath) |
|
222
|
const currentPath = getLearningPath(storedPathParsed?.pathName) |
|
223
|
if (currentPath) { |
|
224
|
setPath(currentPath) |
|
225
|
setCurrentStep(storedPathParsed?.currentStep || 0) |
|
226
|
} |
|
227
|
} |
|
228
|
} |
|
229
|
} |
|
230
|
} |
|
231
|
|
|
232
|
useEffect(() => { |
|
233
|
if (isBrowser && !path) { |
|
234
|
initPath() |
|
235
|
} |
|
236
|
}, [isBrowser]) |
|
237
|
|
|
238
|
return ( |
|
239
|
<LearningPathContext.Provider |
|
240
|
value={{ |
|
241
|
path, |
|
242
|
setPath, |
|
243
|
currentStep, |
|
244
|
setCurrentStep, |
|
245
|
startPath, |
|
246
|
updatePath, |
|
247
|
endPath, |
|
248
|
nextStep, |
|
249
|
hasNextStep, |
|
250
|
previousStep, |
|
251
|
hasPreviousStep, |
|
252
|
goToStep, |
|
253
|
isCurrentPath, |
|
254
|
goToCurrentPath, |
|
255
|
baseUrl, |
|
256
|
}} |
|
257
|
> |
|
258
|
{children} |
|
259
|
</LearningPathContext.Provider> |
|
260
|
) |
|
261
|
} |
|
262
|
|
|
263
|
export const useLearningPath = () => { |
|
264
|
const context = useContext(LearningPathContext) |
|
265
|
|
|
266
|
if (!context) { |
|
267
|
throw new Error( |
|
268
|
"useLearningPath must be used within a LearningPathProvider" |
|
269
|
) |
|
270
|
} |
|
271
|
|
|
272
|
return context |
|
273
|
} |