Open History to see the last commit
1import { lazy, Suspense, useCallback, useSyncExternalStore } from "react";
2
3import { useTheme } from "../../hooks/useTheme";
4import {
5 getThemeDefinition,
6 subscribeToCustomThemes,
7 type ThemeAppearance,
8 type ThemeDefinition,
9} from "../../themePalette";
10import { stackedThreadToast, toastManager } from "../ui/toast";
11import { useThemeEditorStore } from "./themeEditorStore";
12
13const ThemeEditorPanel = lazy(() =>
14 import("./ThemeEditorPanel").then((module) => ({ default: module.ThemeEditorPanel })),
15);
16
17function useThemeDefinition(id: string | null | undefined) {
18 return useSyncExternalStore(
19 subscribeToCustomThemes,
20 () => (id ? (getThemeDefinition(id) ?? null) : null),
21 () => null,
22 );
23}
24
25export function ThemeEditorHost() {
26 const session = useThemeEditorStore((store) => store.session);
27 const closeThemeEditor = useThemeEditorStore((store) => store.closeThemeEditor);
28 const { theme, setTheme, themeHalves, refreshTheme } = useTheme();
29 const editingTheme = useThemeDefinition(session?.editingThemeId);
30 const seedTheme = useThemeDefinition(session?.seedThemeId);
31
32 const handleSaved = useCallback(
33 (
34 savedTheme: ThemeDefinition,
35 { created, mergedAppearance }: { created: boolean; mergedAppearance?: ThemeAppearance },
36 ) => {
37 if (mergedAppearance) {
38 if (!setTheme(savedTheme.id)) {
39 toastManager.add(
40 stackedThreadToast({
41 type: "error",
42 title: "Could not save your theme",
43 description: "Browser storage is unavailable, so the change was not kept.",
44 }),
45 );
46 return false;
47 }
48 toastManager.add(
49 stackedThreadToast({
50 type: "success",
51 title: `${savedTheme.label} updated`,
52 description: `Its ${mergedAppearance} palette was added.`,
53 }),
54 );
55 return true;
56 }
57 if (!created) {
58 // The edited theme may be showing through the base preference or either
59 // half of the mix; the preference itself is untouched (a setTheme here
60 // would clear the mix), the palette just needs re-applying.
61 const wasActive =
62 getThemeDefinition(theme)?.id === savedTheme.id ||
63 themeHalves?.light === savedTheme.id ||
64 themeHalves?.dark === savedTheme.id;
65 if (wasActive) refreshTheme();
66 toastManager.add(
67 stackedThreadToast({
68 type: "success",
69 title: `${savedTheme.label} saved`,
70 description: wasActive ? "Your changes are now active." : "Your changes are saved.",
71 }),
72 );
73 return true;
74 }
75
76 if (!setTheme(savedTheme.id)) {
77 toastManager.add(
78 stackedThreadToast({
79 type: "error",
80 title: "Could not save your theme",
81 description: "Browser storage is unavailable, so the change was not kept.",
82 }),
83 );
84 return false;
85 }
86 toastManager.add(
87 stackedThreadToast({
88 type: "success",
89 title: `${savedTheme.label} created`,
90 description: "It’s now active.",
91 }),
92 );
93 return true;
94 },
95 [refreshTheme, setTheme, theme, themeHalves],
96 );
97
98 if (!session) return null;
99
100 return (
101 <Suspense fallback={null}>
102 <ThemeEditorPanel
103 editingTheme={editingTheme}
104 initialAppearance={session.initialAppearance}
105 key={session.id}
106 onOpenChange={(open) => {
107 if (!open) closeThemeEditor();
108 }}
109 onSaved={handleSaved}
110 open
111 restoreTheme={refreshTheme}
112 seedName={session.seedName ?? undefined}
113 seedTheme={seedTheme}
114 />
115 </Suspense>
116 );
117}