generated from json-schema-org/repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathstores.ts
69 lines (61 loc) · 1.63 KB
/
stores.ts
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
import { create } from "zustand";
type Store = {
editor: any;
monaco: any;
setEditor: (editor: any) => void;
setMonaco: (monaco: any) => void;
};
export const useEditorStore = create<Store>()((set) => ({
editor: null,
monaco: null,
setEditor: (editor) => set({ editor }),
setMonaco: (monaco) => set({ monaco }),
}));
type UserSolutionsByLesson = {
[key: string]: string | null;
};
type UserSolutionStore = {
userSolutionsByLesson: UserSolutionsByLesson;
saveUserSolutionForLesson: (
chapter: number,
lesson: number,
code: string,
) => void;
getSavedUserSolutionByLesson: (
chapter: number,
lesson: number,
) => string | null;
clearAllCode: () => void;
};
export const useUserSolutionStore = create<UserSolutionStore>()((set, get) => ({
userSolutionsByLesson:
typeof window !== "undefined"
? JSON.parse(localStorage.getItem("codeData") ?? "{}")
: {},
saveUserSolutionForLesson: (
chapter: number,
lesson: number,
code: string,
) => {
const key = `${chapter}.${lesson}`;
set((state) => {
const NewUserSolutionsByLesson = {
...state.userSolutionsByLesson,
[key]: code,
};
localStorage.setItem(
"codeData",
JSON.stringify(NewUserSolutionsByLesson),
);
return { userSolutionsByLesson: NewUserSolutionsByLesson };
});
},
getSavedUserSolutionByLesson: (chapter: number, lesson: number) => {
const key = `${chapter}.${lesson}`;
return get().userSolutionsByLesson[key] ?? null;
},
clearAllCode: () => {
localStorage.removeItem("codeData");
set({ userSolutionsByLesson: {} });
},
}));