|
1
|
"use client" |
|
2
|
|
|
3
|
import React, { useEffect } from "react" |
|
4
|
import { useLearningPath } from "@/providers/LearningPath" |
|
5
|
import { useNotifications } from "@/providers/Notification" |
|
6
|
import { LearningPathIcon } from "../../components/LearningPath/Icon" |
|
7
|
import { LearningPathFinish } from "../../components/LearningPath/Finish" |
|
8
|
import { LearningPathSteps } from "../../components/LearningPath/Steps" |
|
9
|
|
|
10
|
export const useCurrentLearningPath = () => { |
|
11
|
const { path, currentStep, updatePath, endPath } = useLearningPath() |
|
12
|
const step = path?.steps[currentStep] |
|
13
|
const { |
|
14
|
addNotification, |
|
15
|
generateId, |
|
16
|
removeNotification, |
|
17
|
updateNotification, |
|
18
|
} = useNotifications() || {} |
|
19
|
|
|
20
|
// used when a notification closed (finished or not) |
|
21
|
const handleClose = (notificationId: string, shouldEndPath = true) => { |
|
22
|
if (shouldEndPath) { |
|
23
|
setTimeout(() => { |
|
24
|
endPath() |
|
25
|
}, 500) |
|
26
|
} |
|
27
|
removeNotification?.(notificationId) |
|
28
|
} |
|
29
|
|
|
30
|
// used when the learning path is completely finished |
|
31
|
// shows the finish step, if the path has any |
|
32
|
const handleFinish = (notificationId: string) => { |
|
33
|
if (path?.finish) { |
|
34
|
updateNotification?.(notificationId, { |
|
35
|
title: path.finish.step.title, |
|
36
|
text: path.finish.step.description, |
|
37
|
type: "custom", |
|
38
|
layout: "default", |
|
39
|
CustomIcon: ( |
|
40
|
<LearningPathIcon |
|
41
|
className="!w-2 !h-2" |
|
42
|
imgClassName="!w-1.5 !h-1.5" |
|
43
|
/> |
|
44
|
), |
|
45
|
children: ( |
|
46
|
<LearningPathFinish |
|
47
|
{...path.finish} |
|
48
|
onRating={() => |
|
49
|
setTimeout(() => { |
|
50
|
handleClose(notificationId, false) |
|
51
|
}, 1500) |
|
52
|
} |
|
53
|
/> |
|
54
|
), |
|
55
|
}) |
|
56
|
endPath() |
|
57
|
} else { |
|
58
|
handleClose(notificationId) |
|
59
|
} |
|
60
|
} |
|
61
|
|
|
62
|
const LearningStep = (notificationId: string) => { |
|
63
|
return <LearningPathSteps onFinish={() => handleFinish(notificationId)} /> |
|
64
|
} |
|
65
|
|
|
66
|
// create a notification when a path is initialized |
|
67
|
useEffect(() => { |
|
68
|
if (path && !path.notificationId) { |
|
69
|
const id = generateId?.() |
|
70
|
|
|
71
|
if (!id) { |
|
72
|
return |
|
73
|
} |
|
74
|
|
|
75
|
addNotification?.({ |
|
76
|
title: path.label, |
|
77
|
text: step?.description, |
|
78
|
onClose: () => handleClose(id), |
|
79
|
layout: "empty", |
|
80
|
id, |
|
81
|
children: LearningStep(id), |
|
82
|
className: "flex flex-col", |
|
83
|
}) |
|
84
|
updatePath({ |
|
85
|
notificationId: id, |
|
86
|
}) |
|
87
|
} |
|
88
|
}, [path]) |
|
89
|
|
|
90
|
// update an existing notification when the step changes |
|
91
|
useEffect(() => { |
|
92
|
if (path && path.notificationId && step) { |
|
93
|
updateNotification?.(path.notificationId, { |
|
94
|
text: step?.description, |
|
95
|
children: LearningStep(path.notificationId), |
|
96
|
}) |
|
97
|
} |
|
98
|
}, [step]) |
|
99
|
} |