forked from awesomestvi/navet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard-entities-store.ts
More file actions
70 lines (66 loc) · 2.44 KB
/
dashboard-entities-store.ts
File metadata and controls
70 lines (66 loc) · 2.44 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
import { create } from 'zustand';
import { createJSONStorage, persist } from 'zustand/middleware';
interface DashboardEntitiesState {
hiddenEntityIds: string[];
onboardingCompleted: boolean;
replaceDashboardEntitiesState: (state: {
hiddenEntityIds: string[];
onboardingCompleted: boolean;
}) => void;
completeOnboarding: (entityIds: string[], startBlank: boolean) => void;
markOnboardingCompleted: () => void;
hideEntity: (entityId: string) => void;
showEntity: (entityId: string) => void;
showAllEntities: () => void;
resetHiddenEntities: () => void;
reopenOnboarding: () => void;
}
export const useDashboardEntitiesStore = create<DashboardEntitiesState>()(
persist(
(set) => ({
hiddenEntityIds: [],
onboardingCompleted: false,
replaceDashboardEntitiesState: ({ hiddenEntityIds, onboardingCompleted }) =>
set({ hiddenEntityIds, onboardingCompleted }),
completeOnboarding: (entityIds, startBlank) =>
set({
hiddenEntityIds: startBlank ? entityIds : [],
onboardingCompleted: true,
}),
markOnboardingCompleted: () => set({ onboardingCompleted: true }),
hideEntity: (entityId) =>
set((state) => ({
hiddenEntityIds: state.hiddenEntityIds.includes(entityId)
? state.hiddenEntityIds
: [...state.hiddenEntityIds, entityId],
})),
showEntity: (entityId) =>
set((state) => ({
hiddenEntityIds: state.hiddenEntityIds.filter((id) => id !== entityId),
})),
showAllEntities: () => set({ hiddenEntityIds: [] }),
resetHiddenEntities: () => set({ hiddenEntityIds: [] }),
reopenOnboarding: () => set({ onboardingCompleted: false }),
}),
{
name: 'navet-dashboard-entities',
storage: createJSONStorage(() => localStorage),
merge: (persistedState, currentState) => {
const persisted = (persistedState as Record<string, unknown> | null) ?? {};
const hiddenEntityIds = Array.isArray(persisted.hiddenEntityIds)
? persisted.hiddenEntityIds.filter((item): item is string => typeof item === 'string')
: [];
const onboardingCompleted =
typeof persisted.onboardingCompleted === 'boolean'
? persisted.onboardingCompleted
: hiddenEntityIds.length > 0;
return {
...currentState,
...persisted,
hiddenEntityIds,
onboardingCompleted,
};
},
}
)
);