-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzustand.ts
89 lines (82 loc) · 1.99 KB
/
zustand.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { create } from 'zustand'
import { dummyMemos } from './apis/dummyMemos'
import { MemoModel } from './components/molecules/Memo'
export interface MemoStoreModel {
memos?: MemoModel[]
setMemos: (memos?: MemoModel[]) => void
}
// zustand 글로벌 스토어 hook
// 메모스토어
export interface StoreModel {
memos?: MemoModel[]
setMemos: (memos?: MemoModel[]) => void
}
export const useMemoStore = create<MemoStoreModel>((set) => ({
memos: dummyMemos,
setMemos: (memos) => set({ memos }),
}))
// 메모 히스토리 스토어
export interface MemoHistoryModel {
memoHistory: string[]
index: number
pushHistory: (newHistory: string) => void
backHistory: () => void
nextHistory: () => void
resetHistory: () => void
}
export const useMemoHistoryStore = create<MemoHistoryModel>((set) => ({
memoHistory: [],
index: -1,
pushHistory: (newHistory) => {
set(({ memoHistory, index }) => ({
memoHistory: memoHistory.slice(0, index + 1).concat(newHistory),
index: index + 1,
}))
},
backHistory: () => {
set(({ index, memoHistory }) => {
if (memoHistory[index - 1]) {
return {
index: index - 1,
}
} else {
return {}
}
})
},
nextHistory: () => {
set(({ index, memoHistory }) => {
if (memoHistory[index + 1]) {
return {
index: index + 1,
}
} else {
return {}
}
})
},
resetHistory: () => {
set({
memoHistory: [],
index: -1,
})
},
}))
// 테마 스토어
export interface ThemeStoreModel {
theme: 'dark' | undefined
set: (theme: 'dark' | undefined) => void
}
export const useThemeStore = create<ThemeStoreModel>((set) => ({
theme: undefined,
set: (theme) => set({ theme }),
}))
// 초기 스플래시 스토어
export type SplashStoreType = {
initial?: boolean
set: (initial?: boolean) => void
}
export const useSplashStore = create<SplashStoreType>((set) => ({
initial: undefined,
set: (initial) => set({ initial }),
}))