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