Open History to see the last commit
1import { create } from "zustand";
2
3import {
4 getThemeDefinition,
5 type ThemeAppearance,
6 type ThemeHalves,
7 type ThemePreference,
8} from "../../themePalette";
9
10export type ThemeEditorSessionInput = {
11 editingThemeId: string | null;
12 seedThemeId: string | null;
13 seedName: string | null;
14 initialAppearance: ThemeAppearance;
15};
16
17export type ThemeEditorSession = ThemeEditorSessionInput & {
18 id: number;
19};
20
21type ThemeEditorStore = {
22 session: ThemeEditorSession | null;
23 openThemeEditor: (session: ThemeEditorSessionInput) => void;
24 closeThemeEditor: () => void;
25};
26
27let nextSessionId = 0;
28
29export const useThemeEditorStore = create<ThemeEditorStore>((set) => ({
30 session: null,
31 openThemeEditor: (session) => set({ session: { ...session, id: ++nextSessionId } }),
32 closeThemeEditor: () => set({ session: null }),
33}));
34
35export function toggleThemeEditorForTheme(input: {
36 theme: ThemePreference;
37 themeHalves: ThemeHalves | null;
38 initialAppearance: ThemeAppearance;
39}): void {
40 const store = useThemeEditorStore.getState();
41 if (store.session) {
42 store.closeThemeEditor();
43 return;
44 }
45
46 const baseThemeId = getThemeDefinition(input.theme)?.id ?? null;
47 const activeThemeId = input.themeHalves?.[input.initialAppearance] ?? baseThemeId;
48 const seedThemeId = activeThemeId ? (getThemeDefinition(activeThemeId)?.id ?? null) : null;
49 store.openThemeEditor({
50 editingThemeId: null,
51 seedThemeId,
52 seedName: null,
53 initialAppearance: input.initialAppearance,
54 });
55}