-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettingsStore.ts
More file actions
61 lines (56 loc) · 2.05 KB
/
Copy pathsettingsStore.ts
File metadata and controls
61 lines (56 loc) · 2.05 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
import { DefaultSettingsData } from '@/constants/settingsDefaults';
import { loadPersistedState, savePersistedState } from '@/services/persistence';
import type { SettingsData } from '@/types/ui/settings';
import { isSettingsData } from '@/types/ui/settings/typeGuards';
import { create } from 'zustand';
export const SETTINGS_STORAGE_KEY = 'graphvm.settings.v1';
type SettingsStore = SettingsData & {
setToast: (toast: SettingsData['ui']['toast']) => void;
setDisableElementsInfoPanel: (value: boolean) => void;
setTheme: (theme: SettingsData['ui']['theme']) => void;
setArrangeOn: (arrangeOn: SettingsData['graph']['arrangeOn']) => void;
setLimits: (limits: SettingsData['graph']['limits']) => void;
setDefaultPaddingOnActions: (value: number) => void;
setShortcuts: (shortcuts: SettingsData['shortcuts']) => void;
};
const initial = loadPersistedState({
storageKey: SETTINGS_STORAGE_KEY,
fallbackState: DefaultSettingsData,
isValidState: isSettingsData,
});
export const useSettingsStore = create<SettingsStore>()((set) => ({
ui: initial.ui,
graph: initial.graph,
shortcuts: initial.shortcuts,
setToast: (toast) => {
set((s) => ({ ui: { ...s.ui, toast } }));
},
setDisableElementsInfoPanel: (value) => {
set((s) => ({ ui: { ...s.ui, disableElementsInfoPanel: value } }));
},
setTheme: (theme) => {
set((s) => ({ ui: { ...s.ui, theme } }));
},
setArrangeOn: (arrangeOn) => {
set((s) => ({ graph: { ...s.graph, arrangeOn } }));
},
setLimits: (limits) => {
set((s) => ({ graph: { ...s.graph, limits } }));
},
setDefaultPaddingOnActions: (value) => {
set((s) => ({ graph: { ...s.graph, defaultPaddingOnActions: value } }));
},
setShortcuts: (shortcuts) => {
set({ shortcuts });
},
}));
useSettingsStore.subscribe((state) => {
savePersistedState({
storageKey: SETTINGS_STORAGE_KEY,
state: {
ui: state.ui,
graph: state.graph,
shortcuts: state.shortcuts,
},
});
});