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 type { AnyRouter } from "@tanstack/react-router"; |
| 2 | import { msg } from "gt-react"; |
| 3 | |
| 4 | import { |
| 5 | dispatchCommandPalette, |
| 6 | isCommandPaletteMounted, |
| 7 | useCommandPaletteStore, |
| 8 | } from "../commandPaletteStore"; |
| 9 | import { overlayModeForCommand } from "../components/CommandPalette.logic"; |
| 10 | import { APPEARANCE_OPTIONS, notifyThemeSaveFailure } from "../components/CommandPaletteThemeItems"; |
| 11 | import { toggleThemeEditorForTheme } from "../components/settings/themeEditorStore"; |
| 12 | import { toastManager } from "../components/ui/toast"; |
| 13 | import { getThemeSnapshot, setAppearanceMode } from "../hooks/useTheme"; |
| 14 | import { m } from "../i18n/messages"; |
| 15 | import { selectActiveRightPanel, useRightPanelStore } from "../rightPanelStore"; |
| 16 | import { type CommandHandler, type CommandScopeOptions, registerCommands } from "./dispatcher"; |
| 17 | import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore"; |
| 18 | import { resolveThreadRouteTarget } from "../threadRoutes"; |
| 19 | |
| 20 | function consume(event: KeyboardEvent): void { |
| 21 | event.preventDefault(); |
| 22 | event.stopPropagation(); |
| 23 | } |
| 24 | |
| 25 | const toggleOverlay = |
| 26 | (command: Parameters<typeof overlayModeForCommand>[0]): CommandHandler => |
| 27 | (event) => { |
| 28 | const mode = overlayModeForCommand(command); |
| 29 | if (mode === null) return false; |
| 30 | consume(event); |
| 31 | dispatchCommandPalette({ _tag: "ToggleMode", mode }); |
| 32 | }; |
| 33 | |
| 34 | /** |
| 35 | * Palette, theme and usage shortcuts. They read the route, panels and theme |
| 36 | * when the key is pressed, and are live only while the palette is mounted. |
| 37 | */ |
| 38 | export function installCommandPaletteCommands( |
| 39 | router: AnyRouter, |
| 40 | options: Pick<CommandScopeOptions, "keybindings"> = {}, |
| 41 | ): () => void { |
| 42 | const readRouteThreadRef = () => { |
| 43 | const target = resolveThreadRouteTarget(router.state.matches.at(-1)?.params ?? {}); |
| 44 | return target?.kind === "server" ? target.threadRef : null; |
| 45 | }; |
| 46 | |
| 47 | // A child search (files, contents) goes back to the palette on Escape before |
| 48 | // its own widgets or the dialog can treat the key as a close. |
| 49 | const disposeEscape = registerCommands( |
| 50 | {}, |
| 51 | { |
| 52 | capture: true, |
| 53 | when: isCommandPaletteMounted, |
| 54 | onKeyDown: (event) => { |
| 55 | if (event.isComposing || event.keyCode === 229 || event.key !== "Escape") return false; |
| 56 | const { open, mode } = useCommandPaletteStore.getState(); |
| 57 | if (!open || mode === "command") return false; |
| 58 | consume(event); |
| 59 | dispatchCommandPalette({ _tag: "BackToCommand" }); |
| 60 | }, |
| 61 | }, |
| 62 | ); |
| 63 | |
| 64 | const disposeCommands = registerCommands( |
| 65 | { |
| 66 | "appearance.cycle": (event) => { |
| 67 | consume(event); |
| 68 | if (event.repeat) return; |
| 69 | const { appearanceMode } = getThemeSnapshot(); |
| 70 | const index = APPEARANCE_OPTIONS.findIndex((option) => option.mode === appearanceMode); |
| 71 | const nextMode = APPEARANCE_OPTIONS[(index + 1) % APPEARANCE_OPTIONS.length]!.mode; |
| 72 | if (!setAppearanceMode(nextMode)) { |
| 73 | notifyThemeSaveFailure(); |
| 74 | return; |
| 75 | } |
| 76 | toastManager.add({ |
| 77 | id: "appearance-cycle", |
| 78 | title: m( |
| 79 | msg( |
| 80 | "{mode, select, system {Appearance: System} light {Appearance: Light} other {Appearance: Dark}}", |
| 81 | ), |
| 82 | { mode: nextMode }, |
| 83 | ), |
| 84 | timeout: 1500, |
| 85 | }); |
| 86 | }, |
| 87 | "theme.select": (event) => { |
| 88 | consume(event); |
| 89 | if (event.repeat) return; |
| 90 | dispatchCommandPalette({ _tag: "OpenChangeTheme" }); |
| 91 | }, |
| 92 | "themeEditor.toggle": (event) => { |
| 93 | consume(event); |
| 94 | const { theme, themeHalves, resolvedTheme } = getThemeSnapshot(); |
| 95 | toggleThemeEditorForTheme({ theme, themeHalves, initialAppearance: resolvedTheme }); |
| 96 | }, |
| 97 | "usage.open": (event) => { |
| 98 | consume(event); |
| 99 | dispatchCommandPalette({ _tag: "SetOpen", open: false }); |
| 100 | void router.navigate({ to: "/usage" }); |
| 101 | }, |
| 102 | "commandPalette.toggle": toggleOverlay("commandPalette.toggle"), |
| 103 | "filePicker.toggle": toggleOverlay("filePicker.toggle"), |
| 104 | "projectSearch.toggle": toggleOverlay("projectSearch.toggle"), |
| 105 | }, |
| 106 | { |
| 107 | ...options, |
| 108 | when: isCommandPaletteMounted, |
| 109 | context: () => { |
| 110 | const threadRef = readRouteThreadRef(); |
| 111 | return { |
| 112 | terminalOpen: threadRef |
| 113 | ? selectThreadTerminalUiState( |
| 114 | useTerminalUiStateStore.getState().terminalUiStateByThreadKey, |
| 115 | threadRef, |
| 116 | ).terminalOpen |
| 117 | : false, |
| 118 | previewOpen: threadRef |
| 119 | ? selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, threadRef) === |
| 120 | "preview" |
| 121 | : false, |
| 122 | usagePageOpen: router.state.location.pathname === "/usage", |
| 123 | }; |
| 124 | }, |
| 125 | }, |
| 126 | ); |
| 127 | |
| 128 | return () => { |
| 129 | disposeEscape(); |
| 130 | disposeCommands(); |
| 131 | }; |
| 132 | } |