The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { msg } from "gt-react"; |
| 2 | import { lazy, Suspense, useSyncExternalStore } from "react"; |
| 3 | |
| 4 | import { useTheme } from "../../hooks/useTheme"; |
| 5 | import { m } from "../../i18n/messages"; |
| 6 | import { |
| 7 | getThemeDefinition, |
| 8 | subscribeToCustomThemes, |
| 9 | type ThemeAppearance, |
| 10 | type ThemeDefinition, |
| 11 | } from "../../themePalette"; |
| 12 | import { stackedThreadToast, toastManager } from "../ui/toast"; |
| 13 | import { useThemeEditorStore } from "./themeEditorStore"; |
| 14 | |
| 15 | const ThemeEditorPanel = lazy(() => |
| 16 | import("./ThemeEditorPanel").then((module) => ({ default: module.ThemeEditorPanel })), |
| 17 | ); |
| 18 | |
| 19 | function useThemeDefinition(id: string | null | undefined) { |
| 20 | return useSyncExternalStore( |
| 21 | subscribeToCustomThemes, |
| 22 | () => (id ? (getThemeDefinition(id) ?? null) : null), |
| 23 | () => null, |
| 24 | ); |
| 25 | } |
| 26 | |
| 27 | export function ThemeEditorHost() { |
| 28 | const session = useThemeEditorStore((store) => store.session); |
| 29 | const closeThemeEditor = useThemeEditorStore((store) => store.closeThemeEditor); |
| 30 | const { theme, setTheme, themeHalves, refreshTheme } = useTheme(); |
| 31 | const editingTheme = useThemeDefinition(session?.editingThemeId); |
| 32 | const seedTheme = useThemeDefinition(session?.seedThemeId); |
| 33 | |
| 34 | const handleSaved = ( |
| 35 | savedTheme: ThemeDefinition, |
| 36 | { created, mergedAppearance }: { created: boolean; mergedAppearance?: ThemeAppearance }, |
| 37 | ) => { |
| 38 | if (mergedAppearance) { |
| 39 | if (!setTheme(savedTheme.id)) { |
| 40 | toastManager.add( |
| 41 | stackedThreadToast({ |
| 42 | type: "error", |
| 43 | title: m(msg("Could not save your theme")), |
| 44 | description: m(msg("Browser storage is unavailable, so the change was not kept.")), |
| 45 | }), |
| 46 | ); |
| 47 | return false; |
| 48 | } |
| 49 | return true; |
| 50 | } |
| 51 | if (!created) { |
| 52 | // Reapply the palette without setTheme, which would clear a mix containing the edited theme. |
| 53 | const wasActive = |
| 54 | getThemeDefinition(theme)?.id === savedTheme.id || |
| 55 | themeHalves?.light === savedTheme.id || |
| 56 | themeHalves?.dark === savedTheme.id; |
| 57 | if (wasActive) refreshTheme(); |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | if (!setTheme(savedTheme.id)) { |
| 62 | toastManager.add( |
| 63 | stackedThreadToast({ |
| 64 | type: "error", |
| 65 | title: m(msg("Could not save your theme")), |
| 66 | description: m(msg("Browser storage is unavailable, so the change was not kept.")), |
| 67 | }), |
| 68 | ); |
| 69 | return false; |
| 70 | } |
| 71 | return true; |
| 72 | }; |
| 73 | |
| 74 | if (!session) return null; |
| 75 | |
| 76 | return ( |
| 77 | <Suspense fallback={null}> |
| 78 | <ThemeEditorPanel |
| 79 | editingTheme={editingTheme} |
| 80 | initialAppearance={session.initialAppearance} |
| 81 | key={session.id} |
| 82 | onOpenChange={(open) => { |
| 83 | if (!open) closeThemeEditor(); |
| 84 | }} |
| 85 | onSaved={handleSaved} |
| 86 | open |
| 87 | restoreTheme={refreshTheme} |
| 88 | seedName={session.seedName ?? undefined} |
| 89 | seedTheme={seedTheme} |
| 90 | /> |
| 91 | </Suspense> |
| 92 | ); |
| 93 | } |