Open History to see the last commit
1import type { AnyRouter } from "@tanstack/react-router";
2import { msg } from "gt-react";
3
4import {
5 dispatchCommandPalette,
6 isCommandPaletteMounted,
7 useCommandPaletteStore,
8} from "../commandPaletteStore";
9import { overlayModeForCommand } from "../components/CommandPalette.logic";
10import { APPEARANCE_OPTIONS, notifyThemeSaveFailure } from "../components/CommandPaletteThemeItems";
11import { toggleThemeEditorForTheme } from "../components/settings/themeEditorStore";
12import { toastManager } from "../components/ui/toast";
13import { getThemeSnapshot, setAppearanceMode } from "../hooks/useTheme";
14import { m } from "../i18n/messages";
15import { selectActiveRightPanel, useRightPanelStore } from "../rightPanelStore";
16import { type CommandHandler, type CommandScopeOptions, registerCommands } from "./dispatcher";
17import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore";
18import { resolveThreadRouteTarget } from "../threadRoutes";
19
20function consume(event: KeyboardEvent): void {
21 event.preventDefault();
22 event.stopPropagation();
23}
24
25const 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 */
38export 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 if (event.repeat) return;
60 dispatchCommandPalette({ _tag: "BackToCommand" });
61 },
62 },
63 );
64
65 const disposeCommands = registerCommands(
66 {
67 "appearance.cycle": (event) => {
68 consume(event);
69 if (event.repeat) return;
70 const { appearanceMode } = getThemeSnapshot();
71 const index = APPEARANCE_OPTIONS.findIndex((option) => option.mode === appearanceMode);
72 const nextMode = APPEARANCE_OPTIONS[(index + 1) % APPEARANCE_OPTIONS.length]!.mode;
73 if (!setAppearanceMode(nextMode)) {
74 notifyThemeSaveFailure();
75 return;
76 }
77 toastManager.add({
78 id: "appearance-cycle",
79 title: m(
80 msg(
81 "{mode, select, system {Appearance: System} light {Appearance: Light} other {Appearance: Dark}}",
82 ),
83 { mode: nextMode },
84 ),
85 timeout: 1500,
86 });
87 },
88 "theme.select": (event) => {
89 consume(event);
90 if (event.repeat) return;
91 dispatchCommandPalette({ _tag: "OpenChangeTheme" });
92 },
93 "themeEditor.toggle": (event) => {
94 consume(event);
95 const { theme, themeHalves, resolvedTheme } = getThemeSnapshot();
96 toggleThemeEditorForTheme({ theme, themeHalves, initialAppearance: resolvedTheme });
97 },
98 "usage.open": (event) => {
99 consume(event);
100 dispatchCommandPalette({ _tag: "SetOpen", open: false });
101 void router.navigate({ to: "/usage" });
102 },
103 "commandPalette.toggle": toggleOverlay("commandPalette.toggle"),
104 "filePicker.toggle": toggleOverlay("filePicker.toggle"),
105 "projectSearch.toggle": toggleOverlay("projectSearch.toggle"),
106 },
107 {
108 ...options,
109 when: isCommandPaletteMounted,
110 context: () => {
111 const threadRef = readRouteThreadRef();
112 return {
113 terminalOpen: threadRef
114 ? selectThreadTerminalUiState(
115 useTerminalUiStateStore.getState().terminalUiStateByThreadKey,
116 threadRef,
117 ).terminalOpen
118 : false,
119 previewOpen: threadRef
120 ? selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, threadRef) ===
121 "preview"
122 : false,
123 usagePageOpen: router.state.location.pathname === "/usage",
124 };
125 },
126 },
127 );
128
129 return () => {
130 disposeEscape();
131 disposeCommands();
132 };
133}