summaryrefslogtreecommitdiff
path: root/apps/web/src/stores/editor-store.ts
blob: 5c4856d359cb60b8a9aea71859377f9000c0e8d0 (plain) (blame)
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 { persist } from "zustand/middleware";
import type { TPlatformLayout } from "@/types/editor";
import { DEFAULT_CANVAS_PRESETS } from "@/constants/project-constants";
import type { TCanvasSize } from "@/types/project";

interface LayoutGuideSettings {
	platform: TPlatformLayout | null;
}

interface EditorState {
	isInitializing: boolean;
	isPanelsReady: boolean;
	canvasPresets: TCanvasSize[];
	layoutGuide: LayoutGuideSettings;
	setInitializing: (loading: boolean) => void;
	setPanelsReady: (ready: boolean) => void;
	initializeApp: () => Promise<void>;
	setLayoutGuide: (settings: Partial<LayoutGuideSettings>) => void;
	toggleLayoutGuide: (platform: TPlatformLayout) => void;
}

export const useEditorStore = create<EditorState>()(
	persist(
		(set) => ({
			isInitializing: true,
			isPanelsReady: false,
			canvasPresets: DEFAULT_CANVAS_PRESETS,
			layoutGuide: {
				platform: null,
			},
			setInitializing: (loading) => {
				set({ isInitializing: loading });
			},

			setPanelsReady: (ready) => {
				set({ isPanelsReady: ready });
			},

			initializeApp: async () => {
				set({ isInitializing: true, isPanelsReady: false });

				set({ isPanelsReady: true, isInitializing: false });
			},

			setLayoutGuide: (settings) => {
				set((state) => ({
					layoutGuide: {
						...state.layoutGuide,
						...settings,
					},
				}));
			},

			toggleLayoutGuide: (platform) => {
				set((state) => ({
					layoutGuide: {
						platform: state.layoutGuide.platform === platform ? null : platform,
					},
				}));
			},
		}),
		{
			name: "editor-settings",
			partialize: (state) => ({
				layoutGuide: state.layoutGuide,
			}),
		},
	),
);