TypeScript
fix(web): reload saved colors when reopening the theme editor (#9847)
Julius Marmingecaf4981e32f4
diff --git a/apps/web/src/components/settings/ThemeEditorHost.test.tsx b/apps/web/src/components/settings/ThemeEditorHost.test.tsxnew file mode 100644index 00000000000..eaf0c1b06b2--- /dev/null+++ b/apps/web/src/components/settings/ThemeEditorHost.test.tsx@@ -0,0 +1,191 @@+import type { ReactElement } from "react";+import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";++import { reactHookHarness as hooks } from "../../test/reactHookHarness";+import {+ getThemeDefinition,+ installCustomTheme,+ invalidateCustomThemes,+ parseThemeFile,+ removeCustomTheme,+ THEME_FILE_VERSION,+ themeColorToHex,+ updateCustomTheme,+ type ThemeDefinition,+} from "../../themePalette";+import type { ThemeEditorSession } from "./themeEditorStore";++const state = vi.hoisted(() => ({+ session: null as ThemeEditorSession | null,+ closeThemeEditor: vi.fn(),+ onStoreChange: vi.fn(),+ subscriptions: new Set<() => void>(),+ theme: {+ theme: "system",+ themeHalves: null,+ setTheme: vi.fn(),+ refreshTheme: vi.fn(),+ },+}));++vi.mock("react", async (importOriginal) => {+ const actual = await importOriginal<typeof import("react")>();+ const { reactHookHarness } = await import("../../test/reactHookHarness");+ return {+ ...actual,+ useCallback: reactHookHarness.useCallback,+ useSyncExternalStore: (+ subscribe: (listener: () => void) => () => void,+ getSnapshot: () => unknown,+ ) => {+ const subscription = reactHookHarness.useRef<(() => void) | null>(null);+ if (!subscription.current) {+ subscription.current = subscribe(() => state.onStoreChange());+ state.subscriptions.add(subscription.current);+ }+ return getSnapshot();+ },+ };+});++vi.mock("react/compiler-runtime", async () => {+ const { reactHookHarness } = await import("../../test/reactHookHarness");+ return { c: reactHookHarness.useMemoCache };+});++vi.mock("../../hooks/useTheme", () => ({ useTheme: () => state.theme }));+vi.mock("./themeEditorStore", () => ({+ useThemeEditorStore: (select: (store: typeof state) => unknown) => select(state),+}));+vi.mock("../ui/toast", () => ({+ toastManager: { add: vi.fn() },+ stackedThreadToast: (value: unknown) => value,+}));++import { ThemeEditorHost } from "./ThemeEditorHost";++function renderEditor() {+ hooks.beginRender();+ const host = ThemeEditorHost() as ReactElement<{+ children: ReactElement<{+ editingTheme: ThemeDefinition | null;+ seedTheme: ThemeDefinition | null;+ }>;+ }> | null;+ return host?.props.children.props ?? null;+}++describe("ThemeEditorHost", () => {+ beforeEach(() => {+ hooks.reset();+ state.session = null;+ state.onStoreChange.mockReset();+ const storage = new Map<string, string>();+ vi.stubGlobal("window", {+ localStorage: {+ getItem: (key: string) => storage.get(key) ?? null,+ setItem: (key: string, value: string) => storage.set(key, value),+ },+ addEventListener: vi.fn(),+ removeEventListener: vi.fn(),+ });+ invalidateCustomThemes();+ });++ afterEach(() => {+ for (const unsubscribe of state.subscriptions) unsubscribe();+ state.subscriptions.clear();+ vi.unstubAllGlobals();+ invalidateCustomThemes();+ });++ it.each(["editingTheme", "seedTheme"] as const)(+ "reopens the same %s with its saved colors",+ (field) => {+ const theme = installCustomTheme(+ parseThemeFile({+ version: THEME_FILE_VERSION,+ id: "saved-colors",+ name: "Saved colors",+ appearance: "dark",+ colors: { accent: "#1f6e4a" },+ }),+ );+ const session = {+ id: 1,+ editingThemeId: field === "editingTheme" ? theme.id : null,+ seedThemeId: field === "seedTheme" ? theme.id : null,+ seedName: null,+ initialAppearance: "dark" as const,+ };+ state.session = session;+ expect(themeColorToHex(renderEditor()?.[field]?.colors.accent ?? "")).toBe("#1f6e4a");++ updateCustomTheme({ ...theme, colors: { ...theme.colors, accent: "#7241b8" } });+ expect(themeColorToHex(getThemeDefinition(theme.id)?.colors.accent ?? "")).toBe("#7241b8");+ state.session = null;+ expect(renderEditor()).toBeNull();+ state.session = { ...session, id: 2 };++ expect(themeColorToHex(renderEditor()?.[field]?.colors.accent ?? "")).toBe("#7241b8");+ },+ );++ it.each(["editingTheme", "seedTheme"] as const)(+ "refreshes an open %s when the library changes",+ (field) => {+ const theme = installCustomTheme(+ parseThemeFile({+ version: THEME_FILE_VERSION,+ id: "updated-theme",+ name: "Updated theme",+ appearance: "dark",+ colors: { accent: "#1f6e4a" },+ }),+ );+ state.session = {+ id: 1,+ editingThemeId: field === "editingTheme" ? theme.id : null,+ seedThemeId: field === "seedTheme" ? theme.id : null,+ seedName: null,+ initialAppearance: "dark",+ };+ let editor = renderEditor();+ state.onStoreChange.mockImplementation(() => {+ editor = renderEditor();+ });++ updateCustomTheme({ ...theme, colors: { ...theme.colors, accent: "#7241b8" } });++ expect(themeColorToHex(editor?.[field]?.colors.accent ?? "")).toBe("#7241b8");+ },+ );++ it("does not keep editing a theme removed from the library", () => {+ const theme = installCustomTheme(+ parseThemeFile({+ version: THEME_FILE_VERSION,+ id: "removed-theme",+ name: "Removed theme",+ appearance: "dark",+ colors: { accent: "#1f6e4a" },+ }),+ );+ state.session = {+ id: 1,+ editingThemeId: theme.id,+ seedThemeId: null,+ seedName: null,+ initialAppearance: "dark",+ };+ let editor = renderEditor();+ expect(editor?.editingTheme?.id).toBe(theme.id);+ state.onStoreChange.mockImplementation(() => {+ editor = renderEditor();+ });++ removeCustomTheme(theme.id);++ expect(editor?.editingTheme).toBeNull();+ });+});diff --git a/apps/web/src/components/settings/ThemeEditorHost.tsx b/apps/web/src/components/settings/ThemeEditorHost.tsxindex 7eca6a2de52..1ffc48fef62 100644--- a/apps/web/src/components/settings/ThemeEditorHost.tsx+++ b/apps/web/src/components/settings/ThemeEditorHost.tsx@@ -1,7 +1,12 @@-import { lazy, Suspense, useCallback } from "react";+import { lazy, Suspense, useCallback, useSyncExternalStore } from "react";import { useTheme } from "../../hooks/useTheme";-import { getThemeDefinition, type ThemeAppearance, type ThemeDefinition } from "../../themePalette";+import {+ getThemeDefinition,+ subscribeToCustomThemes,+ type ThemeAppearance,+ type ThemeDefinition,+} from "../../themePalette";import { stackedThreadToast, toastManager } from "../ui/toast";import { useThemeEditorStore } from "./themeEditorStore";@@ -12,6 +17,14 @@ const ThemeEditorPanel = lazy(() =>import("./ThemeEditorPanel").then((module) => ({ default: module.ThemeEditorPanel })),);+function useThemeDefinition(id: string | null | undefined) {+ return useSyncExternalStore(+ subscribeToCustomThemes,+ () => (id ? (getThemeDefinition(id) ?? null) : null),+ () => null,+ );+}+/*** Renders the theme editor above the router. The editor paints its draft on* the live app, so it has to outlive the settings route: the point is to walk@@ -21,6 +34,9 @@ export function ThemeEditorHost() {const session = useThemeEditorStore((store) => store.session);const closeThemeEditor = useThemeEditorStore((store) => store.closeThemeEditor);const { theme, setTheme, themeHalves, refreshTheme } = useTheme();+ // A saved definition can change without its id changing between sessions.+ const editingTheme = useThemeDefinition(session?.editingThemeId);+ const seedTheme = useThemeDefinition(session?.seedThemeId);// The panel reports which path it actually took: a theme removed while its// editor is open resolves to null there, so the save becomes a create even@@ -95,13 +111,6 @@ export function ThemeEditorHost() {if (!session) return null;- // Resolve on every render: an edit or import can change the stored- // definitions while a session is open.- const editingTheme = session.editingThemeId- ? (getThemeDefinition(session.editingThemeId) ?? null)- : null;- const seedTheme = session.seedThemeId ? (getThemeDefinition(session.seedThemeId) ?? null) : null;-return (<Suspense fallback={null}><ThemeEditorPanel