Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useChatShortcuts.ts

106 lines4.2 KB

1import type { KeybindingCommand, ResolvedKeybindingsConfig } from "@t3tools/contracts";
2import { useEffect, useLayoutEffect, useRef } 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 { shouldTypeToFocusComposer } from "./composerFocusRedirect";
12​
13/** Runs a command. Returning false lets the key event through untouched. */
14export type ChatShortcutHandler = (context: {
15 readonly event: KeyboardEvent;
16 readonly terminalFocusOwner: TerminalFocusOwner | null;
17}) => boolean | void;
18​
19export interface ChatShortcutsInput {
20 readonly enabled: boolean;
21 readonly keybindings: ResolvedKeybindingsConfig;
22 readonly scriptKeybindings: ResolvedKeybindingsConfig;
23 readonly getShortcutContext: (target: EventTarget | null) => ShortcutMatchContext & {
24 readonly modelPickerOpen: boolean;
25 };
26 readonly threadFindActive: boolean;
27 /** Types a printable key into the composer; false when it could not take it. */
28 readonly typeIntoComposer: (key: string) => boolean;
29 readonly closeThreadFind: () => void;
30 readonly commands: Partial<Record<KeybindingCommand, ChatShortcutHandler>>;
31 /** Handles commands not in `commands`, such as project scripts. */
32 readonly fallback: (command: KeybindingCommand) => ChatShortcutHandler | undefined;
34​
35/**
36 * The chat's window keyboard shortcuts, plus Escape closing thread find.
37 * Listeners attach once and read the latest input on each event, so handlers
38 * act on the current render without re-registering on every change.
39 */
40export function useChatShortcuts(input: ChatShortcutsInput) {
41 const latest = useRef(input);
42 useLayoutEffect(() => {
43 latest.current = input;
44 });
45 useEffect(() => {
46 const handler = (event: KeyboardEvent) => {
47 const current = latest.current;
48 const { keybindings } = current;
49 if (preventRepeatedTerminalCloseShortcut(event, keybindings)) {
50 event.stopPropagation();
51 return;
52 }
53 if (current.threadFindActive && event.key === "Escape") return;
54 if (isTerminalCloseConfirmPending() && preventTerminalCloseShortcut(event, keybindings)) {
55 event.stopPropagation();
56 return;
57 }
58 if (!current.enabled || isCommandPaletteOpen()) return;
59 const terminalFocusOwner = getTerminalFocusOwner();
60 if (event.defaultPrevented && terminalFocusOwner === null) return;
61 const shortcutContext = current.getShortcutContext(event.target);
62​
63 if (
64 !shortcutContext.terminalFocus &&
65 !shortcutContext.modelPickerOpen &&
66 shouldTypeToFocusComposer(event) &&
67 current.typeIntoComposer(event.key)
68 ) {
69 event.preventDefault();
70 event.stopPropagation();
71 return;
72 }
73​
74 const command = resolveChatShortcutCommand(event, keybindings, current.scriptKeybindings, {
75 context: shortcutContext,
76 });
77 if (!command) return;
78 const run = current.commands[command] ?? current.fallback(command);
79 if (!run || run({ event, terminalFocusOwner }) === false) return;
80 event.preventDefault();
81 event.stopPropagation();
82 };
83 const dismissFind = (event: KeyboardEvent) => {
84 const current = latest.current;
85 if (
86 event.key !== "Escape" ||
87 !current.threadFindActive ||
88 event.defaultPrevented ||
89 event.isComposing ||
90 event.keyCode === 229 ||
91 isCommandPaletteOpen()
92 )
93 return;
94 const context = current.getShortcutContext(event.target);
95 if (context.terminalFocus || context.previewFocus || context.modelPickerOpen) return;
96 event.preventDefault();
97 current.closeThreadFind();
98 };
99 window.addEventListener("keydown", handler, true);
100 window.addEventListener("keydown", dismissFind);
101 return () => {
102 window.removeEventListener("keydown", handler, true);
103 window.removeEventListener("keydown", dismissFind);
104 };
105 }, []);