TypeScript
refactor(web): the command palette's theme items live in their own file (#22679)
Julius Marminge9ff8cdca8565
diff --git a/apps/web/src/components/CommandPalette.tsx b/apps/web/src/components/CommandPalette.tsxindex 055c2d76c4f..c44bb2c7785 100644--- a/apps/web/src/components/CommandPalette.tsx+++ b/apps/web/src/components/CommandPalette.tsx@@ -67,13 +67,10 @@ import {MessageSquareDashedIcon,LinkIcon,MessageSquareIcon,- MonitorIcon,- MoonIcon,PaletteIcon,RotateCcwIcon,SettingsIcon,SquarePenIcon,- SunIcon,TextSearchIcon,} from "lucide-react";import { requestThreadFindOpen } from "./chat/threadFindActionBus";@@ -104,15 +101,6 @@ import {import { useClientSettings } from "../hooks/useSettings";import { useStableCallback } from "../hooks/useStableCallback";import { useTheme } from "../hooks/useTheme";-import { useCustomThemes } from "../hooks/useCustomThemes";-import { useEnvironmentThemeDefinitions } from "../hooks/useEnvironmentTheme";-import { BUILT_IN_THEMES } from "@t3tools/shared/themePalettes";-import { getThemeDefinition } from "../themePalette";-import {- STANDARD_THEME_CARDS,- getThemeCardDefinition,- ThemePreviewCircle,-} from "./settings/ThemePreviewCircles";import { readLocalApi } from "../localApi";import { desktopLocalBackendId } from "../connection/desktopLocal";import { filesystemEnvironment, useFilesystemReadAccess } from "../state/filesystem";@@ -203,6 +191,11 @@ import {import { orderItemsByPreferredIds, sortLogicalProjectsForSidebar } from "./Sidebar.logic";import { resolveEnvironmentOptionLabel } from "./BranchToolbar.logic";import { CommandPaletteContent } from "./CommandPaletteContent";+import {+ APPEARANCE_OPTIONS,+ notifyThemeSaveFailure,+ useCommandPaletteThemeItems,+} from "./CommandPaletteThemeItems";import {CommandPaletteTitleIcon,CommandPaletteVirtualizedResults,@@ -357,41 +350,9 @@ function overlayModeForCommand(command: string | null): SearchOverlayMode | null: null;}-const APPEARANCE_OPTIONS = [- { mode: "system", label: "System", icon: MonitorIcon },- { mode: "light", label: "Light", icon: SunIcon },- { mode: "dark", label: "Dark", icon: MoonIcon },-] as const;--function notifyThemeSaveFailure(): void {- toastManager.add(- stackedThreadToast({- type: "error",- title: "Couldn't save theme selection",- description: "Try again.",- }),- );-}-// Each helper reads its own source hook so the compiler caches the derived value on that// source. Taking the source as an argument instead leaves the helper hook-free, and the// compiler then treats it as a plain function and caches nothing.-function useThemeCards() {- const customThemes = useCustomThemes();- const environmentThemes = useEnvironmentThemeDefinitions();- const seen = new Set<string>();- return [- ...STANDARD_THEME_CARDS.map((card) => ({ ...card, id: null })),- ...[...BUILT_IN_THEMES, ...customThemes, ...environmentThemes]- .filter((definition) => {- if (seen.has(definition.id)) return false;- seen.add(definition.id);- return true;- })- .map(getThemeCardDefinition),- ];-}-function useThreadSearchWithContentMatches(environmentIds: ReadonlyArray<EnvironmentId>,query: string,@@ -817,16 +778,7 @@ function OpenCommandPaletteDialog(props: {};const threads = useThreadShells();const keybindings = useAtomValue(primaryServerKeybindingsAtom);- const {- theme,- themeHalves,- resolvedTheme,- appearanceMode,- setAppearanceMode,- setTheme,- setThemeHalf,- } = useTheme();- const themeCards = useThemeCards();+ const { changeThemeItem, changeAppearanceItem, themeEditorItem } = useCommandPaletteThemeItems();const providers = useAtomValue(primaryServerProvidersAtom);const providersByEnvironment = useEnvironmentProviders();const providerEntryByEnvironmentAndInstanceId = new Map<string, ProviderInstanceEntry>(@@ -1863,84 +1815,7 @@ function OpenCommandPaletteDialog(props: {});}- const changeThemeItem: CommandPaletteSubmenuItem = {- kind: "submenu",- value: "action:change-theme",- searchTerms: ["change theme", "appearance", "colors", "palette"],- title: "Change theme",- icon: <PaletteIcon className={ITEM_ICON_CLASS} />,- addonIcon: <PaletteIcon className={ADDON_ICON_CLASS} />,- shortcutCommand: "theme.select",- groups: [- {- value: "themes",- label: "Change theme",- items: themeCards.map(({ id, label, previews }) => ({- kind: "action",- value: id === null ? "theme:standard" : `theme:palette:${id}`,- title: label,- description: previews.length === 1 ? `For ${previews[0]!.mode} mode` : undefined,- searchTerms: [label, "theme", "appearance"],- icon: <PaletteIcon className={ITEM_ICON_CLASS} />,- titleTrailingContent: (- <span className="flex shrink-0 items-center gap-2">- {(themeHalves?.[resolvedTheme] ?? getThemeDefinition(theme)?.id ?? null) === id ? (- <span className="text-xs text-muted-foreground/70">Current</span>- ) : null}- <span className="flex items-center gap-1" aria-hidden>- {previews.map((preview) => (- <ThemePreviewCircle- key={preview.mode}- colors={preview.colors}- mode={preview.mode}- className="size-3 border-0"- />- ))}- </span>- </span>- ),- run: async () => {- const saved =- previews.length === 1 && id !== null- ? setThemeHalf(previews[0]!.mode, id)- : setTheme(id ?? appearanceMode);- if (!saved) notifyThemeSaveFailure();- },- })),- },- ],- };actionItems.push(changeThemeItem);-- const changeAppearanceItem: CommandPaletteSubmenuItem = {- kind: "submenu",- value: "action:change-appearance",- searchTerms: ["change appearance", "light", "dark", "system", "mode", "toggle"],- title: "Change appearance",- icon: <MonitorIcon className={ITEM_ICON_CLASS} />,- addonIcon: <MonitorIcon className={ADDON_ICON_CLASS} />,- shortcutCommand: "appearance.cycle",- groups: [- {- value: "appearance",- label: "Change appearance",- items: APPEARANCE_OPTIONS.map(({ mode, label, icon: Icon }) => ({- kind: "action",- value: `appearance:${mode}`,- title: label,- searchTerms: [label, "appearance", "mode"],- icon: <Icon className={ITEM_ICON_CLASS} />,- titleTrailingContent:- appearanceMode === mode ? (- <span className="text-xs text-muted-foreground/70">Current</span>- ) : undefined,- run: async () => {- if (!setAppearanceMode(mode)) notifyThemeSaveFailure();- },- })),- },- ],- };actionItems.push(changeAppearanceItem);// The parent pushes an intent into this mounted dialog; applying it clears the@@ -1984,21 +1859,7 @@ function OpenCommandPaletteDialog(props: {applyOpenIntent(openIntent);}, [openIntent, projectThreadItemCount]);- actionItems.push({- kind: "action",- value: "action:theme-editor",- searchTerms: ["theme", "appearance", "colors", "palette", "customize"],- title: "Toggle theme editor",- icon: <PaletteIcon className={ITEM_ICON_CLASS} />,- shortcutCommand: "themeEditor.toggle",- run: async () => {- toggleThemeEditorForTheme({- theme,- themeHalves,- initialAppearance: resolvedTheme,- });- },- });+ actionItems.push(themeEditorItem);if (environments.some(diff --git a/apps/web/src/components/CommandPaletteThemeItems.tsx b/apps/web/src/components/CommandPaletteThemeItems.tsxnew file mode 100644index 00000000000..818229d9506--- /dev/null+++ b/apps/web/src/components/CommandPaletteThemeItems.tsx@@ -0,0 +1,163 @@+import { BUILT_IN_THEMES } from "@t3tools/shared/themePalettes";+import { MonitorIcon, MoonIcon, PaletteIcon, SunIcon } from "lucide-react";++import { useCustomThemes } from "../hooks/useCustomThemes";+import { useEnvironmentThemeDefinitions } from "../hooks/useEnvironmentTheme";+import { useTheme } from "../hooks/useTheme";+import { getThemeDefinition } from "../themePalette";+import {+ ADDON_ICON_CLASS,+ ITEM_ICON_CLASS,+ type CommandPaletteActionItem,+ type CommandPaletteSubmenuItem,+} from "./CommandPalette.logic";+import { toggleThemeEditorForTheme } from "./settings/themeEditorStore";+import {+ STANDARD_THEME_CARDS,+ getThemeCardDefinition,+ ThemePreviewCircle,+} from "./settings/ThemePreviewCircles";+import { stackedThreadToast, toastManager } from "./ui/toast";++export const APPEARANCE_OPTIONS = [+ { mode: "system", label: "System", icon: MonitorIcon },+ { mode: "light", label: "Light", icon: SunIcon },+ { mode: "dark", label: "Dark", icon: MoonIcon },+] as const;++export function notifyThemeSaveFailure(): void {+ toastManager.add(+ stackedThreadToast({+ type: "error",+ title: "Couldn't save theme selection",+ description: "Try again.",+ }),+ );+}++// Reads its own source hooks so the compiler caches the cards on them. Taking the+// sources as arguments would leave it hook-free, and the compiler would cache nothing.+function useThemeCards() {+ const customThemes = useCustomThemes();+ const environmentThemes = useEnvironmentThemeDefinitions();+ const seen = new Set<string>();+ return [+ ...STANDARD_THEME_CARDS.map((card) => ({ ...card, id: null })),+ ...[...BUILT_IN_THEMES, ...customThemes, ...environmentThemes]+ .filter((definition) => {+ if (seen.has(definition.id)) return false;+ seen.add(definition.id);+ return true;+ })+ .map(getThemeCardDefinition),+ ];+}++export function useCommandPaletteThemeItems() {+ const {+ theme,+ themeHalves,+ resolvedTheme,+ appearanceMode,+ setAppearanceMode,+ setTheme,+ setThemeHalf,+ } = useTheme();+ const themeCards = useThemeCards();++ const changeThemeItem: CommandPaletteSubmenuItem = {+ kind: "submenu",+ value: "action:change-theme",+ searchTerms: ["change theme", "appearance", "colors", "palette"],+ title: "Change theme",+ icon: <PaletteIcon className={ITEM_ICON_CLASS} />,+ addonIcon: <PaletteIcon className={ADDON_ICON_CLASS} />,+ shortcutCommand: "theme.select",+ groups: [+ {+ value: "themes",+ label: "Change theme",+ items: themeCards.map(({ id, label, previews }) => ({+ kind: "action",+ value: id === null ? "theme:standard" : `theme:palette:${id}`,+ title: label,+ description: previews.length === 1 ? `For ${previews[0]!.mode} mode` : undefined,+ searchTerms: [label, "theme", "appearance"],+ icon: <PaletteIcon className={ITEM_ICON_CLASS} />,+ titleTrailingContent: (+ <span className="flex shrink-0 items-center gap-2">+ {(themeHalves?.[resolvedTheme] ?? getThemeDefinition(theme)?.id ?? null) === id ? (+ <span className="text-xs text-muted-foreground/70">Current</span>+ ) : null}+ <span className="flex items-center gap-1" aria-hidden>+ {previews.map((preview) => (+ <ThemePreviewCircle+ key={preview.mode}+ colors={preview.colors}+ mode={preview.mode}+ className="size-3 border-0"+ />+ ))}+ </span>+ </span>+ ),+ run: async () => {+ const saved =+ previews.length === 1 && id !== null+ ? setThemeHalf(previews[0]!.mode, id)+ : setTheme(id ?? appearanceMode);+ if (!saved) notifyThemeSaveFailure();+ },+ })),+ },+ ],+ };++ const changeAppearanceItem: CommandPaletteSubmenuItem = {+ kind: "submenu",+ value: "action:change-appearance",+ searchTerms: ["change appearance", "light", "dark", "system", "mode", "toggle"],+ title: "Change appearance",+ icon: <MonitorIcon className={ITEM_ICON_CLASS} />,+ addonIcon: <MonitorIcon className={ADDON_ICON_CLASS} />,+ shortcutCommand: "appearance.cycle",+ groups: [+ {+ value: "appearance",+ label: "Change appearance",+ items: APPEARANCE_OPTIONS.map(({ mode, label, icon: Icon }) => ({+ kind: "action",+ value: `appearance:${mode}`,+ title: label,+ searchTerms: [label, "appearance", "mode"],+ icon: <Icon className={ITEM_ICON_CLASS} />,+ titleTrailingContent:+ appearanceMode === mode ? (+ <span className="text-xs text-muted-foreground/70">Current</span>+ ) : undefined,+ run: async () => {+ if (!setAppearanceMode(mode)) notifyThemeSaveFailure();+ },+ })),+ },+ ],+ };++ const themeEditorItem: CommandPaletteActionItem = {+ kind: "action",+ value: "action:theme-editor",+ searchTerms: ["theme", "appearance", "colors", "palette", "customize"],+ title: "Toggle theme editor",+ icon: <PaletteIcon className={ITEM_ICON_CLASS} />,+ shortcutCommand: "themeEditor.toggle",+ run: async () => {+ toggleThemeEditorForTheme({+ theme,+ themeHalves,+ initialAppearance: resolvedTheme,+ });+ },+ };++ return { changeThemeItem, changeAppearanceItem, themeEditorItem };+}