Open History to see the last commit
1import type { KeybindingCommand, ResolvedKeybindingsConfig } from "@t3tools/contracts";
2import { useEffect, useEffectEvent } from "react";
3import { isCommandPaletteOpen } from "../../commandPaletteBus";
4import { resolveChatShortcutCommand, type ShortcutMatchContext } from "../../keybindings";
5import { isTerminalCloseConfirmPending } from "../../lib/terminalCloseConfirm";
6import {
7 preventRepeatedTerminalCloseShortcut,
8 preventTerminalCloseShortcut,
9} from "../../lib/terminalCloseShortcut";
10import { getTerminalFocusOwner, type TerminalFocusOwner } from "../../lib/terminalFocus";
11import type { ComposerHandleRef } from "../../composerHandleContext";
12import type { BranchToolbarHandle } from "../BranchToolbar";
13import { shouldTypeToFocusComposer } from "./composerFocusRedirect";
14import { composerShortcutCommands } from "./composerShortcuts";
15
16/** Returning false lets the key event through untouched. */
17export type ChatShortcutHandler = (context: {
18 readonly event: KeyboardEvent;
19 readonly terminalFocusOwner: TerminalFocusOwner | null;
20}) => boolean | void;
21
22export type ChatShortcutCommands = Partial<Record<KeybindingCommand, ChatShortcutHandler>>;
23
24export interface ChatShortcutsInput {
25 readonly enabled: boolean;
26 readonly keybindings: ResolvedKeybindingsConfig;
27 readonly scriptKeybindings: ResolvedKeybindingsConfig;
28 readonly getShortcutContext: (target: EventTarget | null) => ShortcutMatchContext & {
29 readonly modelPickerOpen: boolean;
30 };
31 readonly threadFindActive: boolean;
32 /** Takes type-to-focus keys and the composer and branch toolbar chords. */
33 readonly composerRef: ComposerHandleRef;
34 readonly branchToolbarRef: { readonly current: BranchToolbarHandle | null };
35 readonly closeThreadFind: () => void;
36 readonly commands: ChatShortcutCommands;
37 /** Handles commands not in `commands`, such as project scripts. */
38 readonly fallback: (command: KeybindingCommand) => ChatShortcutHandler | undefined;
39}
40
41/**
42 * The chat's window keyboard shortcuts, plus Escape closing thread find.
43 * Listeners attach once; the effect events read the latest input, so handlers
44 * act on the current render without re-registering on every change.
45 */
46export function useChatShortcuts(input: ChatShortcutsInput) {
47 const handleShortcut = useEffectEvent((event: KeyboardEvent) => {
48 const { keybindings } = input;
49 if (preventRepeatedTerminalCloseShortcut(event, keybindings)) {
50 event.stopPropagation();
51 return;
52 }
53 if (input.threadFindActive && event.key === "Escape") return;
54 if (isTerminalCloseConfirmPending() && preventTerminalCloseShortcut(event, keybindings)) {
55 event.stopPropagation();
56 return;
57 }
58 if (!input.enabled || isCommandPaletteOpen()) return;
59 const terminalFocusOwner = getTerminalFocusOwner();
60 if (event.defaultPrevented && terminalFocusOwner === null) return;
61 const shortcutContext = input.getShortcutContext(event.target);
62
63 if (
64 !shortcutContext.terminalFocus &&
65 !shortcutContext.modelPickerOpen &&
66 shouldTypeToFocusComposer(event) &&
67 input.composerRef.current?.insertTextAtEnd(event.key) === true
68 ) {
69 event.preventDefault();
70 event.stopPropagation();
71 return;
72 }
73
74 const command = resolveChatShortcutCommand(event, keybindings, input.scriptKeybindings, {
75 context: shortcutContext,
76 });
77 if (!command) return;
78 const run =
79 input.commands[command] ??
80 composerShortcutCommands(input.composerRef, input.branchToolbarRef)[command] ??
81 input.fallback(command);
82 if (!run || run({ event, terminalFocusOwner }) === false) return;
83 event.preventDefault();
84 event.stopPropagation();
85 });
86 const dismissFind = useEffectEvent((event: KeyboardEvent) => {
87 if (
88 event.key !== "Escape" ||
89 !input.threadFindActive ||
90 event.defaultPrevented ||
91 event.isComposing ||
92 event.keyCode === 229 ||
93 isCommandPaletteOpen()
94 )
95 return;
96 const context = input.getShortcutContext(event.target);
97 if (context.terminalFocus || context.previewFocus || context.modelPickerOpen) return;
98 event.preventDefault();
99 input.closeThreadFind();
100 });
101 useEffect(() => {
102 const onKeyDownCapture = (event: KeyboardEvent) => handleShortcut(event);
103 const onKeyDown = (event: KeyboardEvent) => dismissFind(event);
104 window.addEventListener("keydown", onKeyDownCapture, true);
105 window.addEventListener("keydown", onKeyDown);
106 return () => {
107 window.removeEventListener("keydown", onKeyDownCapture, true);
108 window.removeEventListener("keydown", onKeyDown);
109 };
110 }, []);
111}