The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { BUILT_IN_THEMES } from "@t3tools/shared/themePalettes"; |
| 2 | import { msg, T, useGT, useMessages } from "gt-react"; |
| 3 | import { MonitorIcon, MoonIcon, PaletteIcon, SunIcon } from "lucide-react"; |
| 4 | |
| 5 | import { m } from "../i18n/messages"; |
| 6 | import { useCustomThemes } from "../hooks/useCustomThemes"; |
| 7 | import { useEnvironmentThemeDefinitions } from "../hooks/useEnvironmentTheme"; |
| 8 | import { useTheme } from "../hooks/useTheme"; |
| 9 | import { getThemeDefinition } from "../themePalette"; |
| 10 | import { |
| 11 | ADDON_ICON_CLASS, |
| 12 | ITEM_ICON_CLASS, |
| 13 | type CommandPaletteActionItem, |
| 14 | type CommandPaletteSubmenuItem, |
| 15 | } from "./CommandPalette.logic"; |
| 16 | import { toggleThemeEditorForTheme } from "./settings/themeEditorStore"; |
| 17 | import { |
| 18 | STANDARD_THEME_CARDS, |
| 19 | getThemeCardDefinition, |
| 20 | ThemePreviewCircle, |
| 21 | } from "./settings/ThemePreviewCircles"; |
| 22 | import { stackedThreadToast, toastManager } from "./ui/toast"; |
| 23 | |
| 24 | export const APPEARANCE_OPTIONS = [ |
| 25 | { mode: "system", label: msg("System"), icon: MonitorIcon }, |
| 26 | { mode: "light", label: msg("Light"), icon: SunIcon }, |
| 27 | { mode: "dark", label: msg("Dark"), icon: MoonIcon }, |
| 28 | ] as const; |
| 29 | |
| 30 | export function notifyThemeSaveFailure(): void { |
| 31 | toastManager.add( |
| 32 | stackedThreadToast({ |
| 33 | type: "error", |
| 34 | title: m(msg("Couldn't save theme selection")), |
| 35 | description: m(msg("Try again.")), |
| 36 | }), |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | // Reads its own source hooks so the compiler caches the cards on them. Taking the |
| 41 | // sources as arguments would leave it hook-free, and the compiler would cache nothing. |
| 42 | function useThemeCards() { |
| 43 | const customThemes = useCustomThemes(); |
| 44 | const environmentThemes = useEnvironmentThemeDefinitions(); |
| 45 | const seen = new Set<string>(); |
| 46 | return [ |
| 47 | ...STANDARD_THEME_CARDS.map((card) => ({ ...card, id: null })), |
| 48 | ...[...BUILT_IN_THEMES, ...customThemes, ...environmentThemes] |
| 49 | .filter((definition) => { |
| 50 | if (seen.has(definition.id)) return false; |
| 51 | seen.add(definition.id); |
| 52 | return true; |
| 53 | }) |
| 54 | .map(getThemeCardDefinition), |
| 55 | ]; |
| 56 | } |
| 57 | |
| 58 | export function useCommandPaletteThemeItems() { |
| 59 | const { |
| 60 | theme, |
| 61 | themeHalves, |
| 62 | resolvedTheme, |
| 63 | appearanceMode, |
| 64 | setAppearanceMode, |
| 65 | setTheme, |
| 66 | setThemeHalf, |
| 67 | } = useTheme(); |
| 68 | const themeCards = useThemeCards(); |
| 69 | const gt = useGT(); |
| 70 | const messages = useMessages(); |
| 71 | |
| 72 | const changeThemeItem: CommandPaletteSubmenuItem = { |
| 73 | kind: "submenu", |
| 74 | value: "action:change-theme", |
| 75 | searchTerms: ["change theme", "appearance", "colors", "palette"], |
| 76 | title: gt("Change theme"), |
| 77 | icon: <PaletteIcon className={ITEM_ICON_CLASS} />, |
| 78 | addonIcon: <PaletteIcon className={ADDON_ICON_CLASS} />, |
| 79 | shortcutCommand: "theme.select", |
| 80 | groups: [ |
| 81 | { |
| 82 | value: "themes", |
| 83 | label: gt("Change theme"), |
| 84 | items: themeCards.map(({ id, label, previews }) => ({ |
| 85 | kind: "action", |
| 86 | value: id === null ? "theme:standard" : `theme:palette:${id}`, |
| 87 | title: label, |
| 88 | description: |
| 89 | previews.length === 1 |
| 90 | ? previews[0]!.mode === "light" |
| 91 | ? gt("For light mode") |
| 92 | : gt("For dark mode") |
| 93 | : undefined, |
| 94 | searchTerms: [label, "theme", "appearance"], |
| 95 | icon: <PaletteIcon className={ITEM_ICON_CLASS} />, |
| 96 | titleTrailingContent: ( |
| 97 | <span className="flex shrink-0 items-center gap-2"> |
| 98 | {(themeHalves?.[resolvedTheme] ?? getThemeDefinition(theme)?.id ?? null) === id ? ( |
| 99 | <span className="text-xs text-muted-foreground/70"> |
| 100 | <T context="the theme or mode in use now">Current</T> |
| 101 | </span> |
| 102 | ) : null} |
| 103 | <span className="flex items-center gap-1" aria-hidden> |
| 104 | {previews.map((preview) => ( |
| 105 | <ThemePreviewCircle |
| 106 | key={preview.mode} |
| 107 | colors={preview.colors} |
| 108 | mode={preview.mode} |
| 109 | className="size-3 border-0" |
| 110 | /> |
| 111 | ))} |
| 112 | </span> |
| 113 | </span> |
| 114 | ), |
| 115 | run: async () => { |
| 116 | const saved = |
| 117 | previews.length === 1 && id !== null |
| 118 | ? setThemeHalf(previews[0]!.mode, id) |
| 119 | : setTheme(id ?? appearanceMode); |
| 120 | if (!saved) notifyThemeSaveFailure(); |
| 121 | }, |
| 122 | })), |
| 123 | }, |
| 124 | ], |
| 125 | }; |
| 126 | |
| 127 | const changeAppearanceItem: CommandPaletteSubmenuItem = { |
| 128 | kind: "submenu", |
| 129 | value: "action:change-appearance", |
| 130 | searchTerms: ["change appearance", "light", "dark", "system", "mode", "toggle"], |
| 131 | title: gt("Change appearance"), |
| 132 | icon: <MonitorIcon className={ITEM_ICON_CLASS} />, |
| 133 | addonIcon: <MonitorIcon className={ADDON_ICON_CLASS} />, |
| 134 | shortcutCommand: "appearance.cycle", |
| 135 | groups: [ |
| 136 | { |
| 137 | value: "appearance", |
| 138 | label: gt("Change appearance"), |
| 139 | items: APPEARANCE_OPTIONS.map(({ mode, label, icon: Icon }) => ({ |
| 140 | kind: "action", |
| 141 | value: `appearance:${mode}`, |
| 142 | title: messages(label), |
| 143 | searchTerms: [messages(label), label, "appearance", "mode"], |
| 144 | icon: <Icon className={ITEM_ICON_CLASS} />, |
| 145 | titleTrailingContent: |
| 146 | appearanceMode === mode ? ( |
| 147 | <span className="text-xs text-muted-foreground/70"> |
| 148 | <T context="the theme or mode in use now">Current</T> |
| 149 | </span> |
| 150 | ) : undefined, |
| 151 | run: async () => { |
| 152 | if (!setAppearanceMode(mode)) notifyThemeSaveFailure(); |
| 153 | }, |
| 154 | })), |
| 155 | }, |
| 156 | ], |
| 157 | }; |
| 158 | |
| 159 | const themeEditorItem: CommandPaletteActionItem = { |
| 160 | kind: "action", |
| 161 | value: "action:theme-editor", |
| 162 | searchTerms: ["theme", "appearance", "colors", "palette", "customize"], |
| 163 | title: gt("Toggle theme editor"), |
| 164 | icon: <PaletteIcon className={ITEM_ICON_CLASS} />, |
| 165 | shortcutCommand: "themeEditor.toggle", |
| 166 | run: async () => { |
| 167 | toggleThemeEditorForTheme({ |
| 168 | theme, |
| 169 | themeHalves, |
| 170 | initialAppearance: resolvedTheme, |
| 171 | }); |
| 172 | }, |
| 173 | }; |
| 174 | |
| 175 | return { changeThemeItem, changeAppearanceItem, themeEditorItem }; |
| 176 | } |