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