Open History to see the last commit
1import { useCallback } from "react";
2
3import { useTheme } from "../../hooks/useTheme";
4import { getThemeDefinition, type ThemeAppearance, type ThemeDefinition } from "../../themePalette";
5import { stackedThreadToast, toastManager } from "../ui/toast";
6import { ThemeEditorPanel } from "./ThemeEditorPanel";
7import { useThemeEditorStore } from "./themeEditorStore";
8
9/**
10 * Renders the theme editor above the router. The editor paints its draft on
11 * the live app, so it has to outlive the settings route: the point is to walk
12 * through threads, panels, and pages while the colors are being tuned.
13 */
14export function ThemeEditorHost() {
15 const session = useThemeEditorStore((store) => store.session);
16 const closeThemeEditor = useThemeEditorStore((store) => store.closeThemeEditor);
17 const { theme, setTheme, themeHalves, refreshTheme } = useTheme();
18
19 // The panel reports which path it actually took: a theme removed while its
20 // editor is open resolves to null there, so the save becomes a create even
21 // though the session still names it.
22 const handleSaved = useCallback(
23 (
24 savedTheme: ThemeDefinition,
25 { created, mergedAppearance }: { created: boolean; mergedAppearance?: ThemeAppearance },
26 ) => {
27 // A merge completed an existing theme's light/dark pair; activating the
28 // whole theme shows the new palette right away.
29 if (mergedAppearance) {
30 if (!setTheme(savedTheme.id)) {
31 toastManager.add(
32 stackedThreadToast({
33 type: "error",
34 title: "Could not save your theme",
35 description: "Browser storage is unavailable, so the change was not kept.",
36 }),
37 );
38 return false;
39 }
40 toastManager.add(
41 stackedThreadToast({
42 type: "success",
43 title: `${savedTheme.label} updated`,
44 description: `Its ${mergedAppearance} palette was added.`,
45 }),
46 );
47 return true;
48 }
49 if (!created) {
50 // The edited theme may be showing through the base preference or either
51 // half of the mix; the preference itself is untouched (a setTheme here
52 // would clear the mix), the palette just needs re-applying.
53 const wasActive =
54 getThemeDefinition(theme)?.id === savedTheme.id ||
55 themeHalves?.light === savedTheme.id ||
56 themeHalves?.dark === savedTheme.id;
57 if (wasActive) refreshTheme();
58 toastManager.add(
59 stackedThreadToast({
60 type: "success",
61 title: `${savedTheme.label} saved`,
62 description: wasActive ? "Your changes are now active." : "Your changes are saved.",
63 }),
64 );
65 return true;
66 }
67
68 if (!setTheme(savedTheme.id)) {
69 toastManager.add(
70 stackedThreadToast({
71 type: "error",
72 title: "Could not save your theme",
73 description: "Browser storage is unavailable, so the change was not kept.",
74 }),
75 );
76 return false;
77 }
78 toastManager.add(
79 stackedThreadToast({
80 type: "success",
81 title: `${savedTheme.label} created`,
82 description: "It’s now active.",
83 }),
84 );
85 return true;
86 },
87 [refreshTheme, setTheme, theme, themeHalves],
88 );
89
90 if (!session) return null;
91
92 // Resolve on every render: an edit or import can change the stored
93 // definitions while a session is open.
94 const editingTheme = session.editingThemeId
95 ? (getThemeDefinition(session.editingThemeId) ?? null)
96 : null;
97 const seedTheme = session.seedThemeId ? (getThemeDefinition(session.seedThemeId) ?? null) : null;
98
99 return (
100 <ThemeEditorPanel
101 editingTheme={editingTheme}
102 initialAppearance={session.initialAppearance}
103 key={session.id}
104 onOpenChange={(open) => {
105 if (!open) closeThemeEditor();
106 }}
107 onSaved={handleSaved}
108 open
109 restoreTheme={refreshTheme}
110 seedName={session.seedName ?? undefined}
111 seedTheme={seedTheme}
112 />
113 );
114}