Open History to see the last commit
1import { lazy, Suspense, 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 = (
33 savedTheme: ThemeDefinition,
34 { created, mergedAppearance }: { created: boolean; mergedAppearance?: ThemeAppearance },
35 ) => {
36 if (mergedAppearance) {
37 if (!setTheme(savedTheme.id)) {
38 toastManager.add(
39 stackedThreadToast({
40 type: "error",
41 title: "Could not save your theme",
42 description: "Browser storage is unavailable, so the change was not kept.",
43 }),
44 );
45 return false;
46 }
47 toastManager.add(
48 stackedThreadToast({
49 type: "success",
50 title: `${savedTheme.label} updated`,
51 description: `Its ${mergedAppearance} palette was added.`,
52 }),
53 );
54 return true;
55 }
56 if (!created) {
57 // The edited theme may be showing through the base preference or either
58 // half of the mix; the preference itself is untouched (a setTheme here
59 // would clear the mix), the palette just needs re-applying.
60 const wasActive =
61 getThemeDefinition(theme)?.id === savedTheme.id ||
62 themeHalves?.light === savedTheme.id ||
63 themeHalves?.dark === savedTheme.id;
64 if (wasActive) refreshTheme();
65 toastManager.add(
66 stackedThreadToast({
67 type: "success",
68 title: `${savedTheme.label} saved`,
69 description: wasActive ? "Your changes are now active." : "Your changes are saved.",
70 }),
71 );
72 return true;
73 }
74
75 if (!setTheme(savedTheme.id)) {
76 toastManager.add(
77 stackedThreadToast({
78 type: "error",
79 title: "Could not save your theme",
80 description: "Browser storage is unavailable, so the change was not kept.",
81 }),
82 );
83 return false;
84 }
85 toastManager.add(
86 stackedThreadToast({
87 type: "success",
88 title: `${savedTheme.label} created`,
89 description: "It’s now active.",
90 }),
91 );
92 return true;
93 };
94
95 if (!session) return null;
96
97 return (
98 <Suspense fallback={null}>
99 <ThemeEditorPanel
100 editingTheme={editingTheme}
101 initialAppearance={session.initialAppearance}
102 key={session.id}
103 onOpenChange={(open) => {
104 if (!open) closeThemeEditor();
105 }}
106 onSaved={handleSaved}
107 open
108 restoreTheme={refreshTheme}
109 seedName={session.seedName ?? undefined}
110 seedTheme={seedTheme}
111 />
112 </Suspense>
113 );
114}