-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAchievementContext.jsx
More file actions
112 lines (96 loc) · 2.99 KB
/
AchievementContext.jsx
File metadata and controls
112 lines (96 loc) · 2.99 KB
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
import React, {
createContext,
useContext,
useEffect,
useCallback,
} from 'react';
import usePersistentState from '../hooks/usePersistentState';
import { useToast } from '../hooks/useToast';
import { ACHIEVEMENTS } from '../config/achievements';
import { TrophyIcon } from '@phosphor-icons/react';
const AchievementContext = createContext();
export const useAchievements = () => {
return useContext(AchievementContext);
};
export const AchievementProvider = ({ children }) => {
// Store achievements as { [id]: { unlocked: boolean, unlockedAt: string } }
const [unlockedAchievements, setUnlockedAchievements] = usePersistentState(
'unlocked-achievements',
{},
);
const [, setReadPosts] = usePersistentState('read-posts', []);
const [showAchievementToast, setShowAchievementToast] = usePersistentState(
'show-achievement-toasts',
true,
);
const { addToast } = useToast();
const toggleAchievementToast = () => {
setShowAchievementToast((prev) => !prev);
};
// Helper to unlock an achievement
const unlockAchievement = useCallback(
(id) => {
// Check if valid achievement ID
const achievement = ACHIEVEMENTS.find((a) => a.id === id);
if (!achievement) return;
// Check if already unlocked
if (unlockedAchievements[id]?.unlocked) return;
const now = new Date().toISOString();
setUnlockedAchievements((prev) => ({
...prev,
[id]: { unlocked: true, unlockedAt: now },
}));
if (showAchievementToast) {
// Trigger Toast
addToast({
title: 'Achievement Unlocked!',
message: achievement.title,
duration: 4000,
icon: <TrophyIcon weight="duotone" className="text-amber-400" />,
type: 'gold',
links: [
{ label: 'Settings', to: '/settings' },
{ label: 'Trophy Room', to: '/achievements' },
],
});
}
},
[
unlockedAchievements,
setUnlockedAchievements,
showAchievementToast,
addToast,
],
);
const trackReadingProgress = (slug) => {
if (!slug) return;
setReadPosts((prev) => {
if (prev.includes(slug)) return prev; // Already read
const newReadPosts = [...prev, slug];
const count = newReadPosts.length;
if (count >= 1) unlockAchievement('novice_reader');
if (count >= 5) unlockAchievement('avid_reader');
if (count >= 10) unlockAchievement('bookworm');
if (count >= 20) unlockAchievement('scholar');
if (count >= 30) unlockAchievement('milord');
return newReadPosts;
});
};
// Automatically unlock 'hello_world' on mount if not already
useEffect(() => {
unlockAchievement('hello_world');
}, [unlockAchievement]);
return (
<AchievementContext.Provider
value={{
unlockedAchievements,
unlockAchievement,
trackReadingProgress,
showAchievementToast,
toggleAchievementToast,
}}
>
{children}
</AchievementContext.Provider>
);
};