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

useChatShortcuts.ts

112 lines4.5 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 /** Buffers input while a kept view is yielding to the route target. */
30 readonly interceptKey?: (
31 event: KeyboardEvent,
32 context: ShortcutMatchContext & { readonly modelPickerOpen: boolean },
33 ) => boolean;
34 readonly closeThreadFind: () => void;
35 readonly commands: Partial<Record<KeybindingCommand, ChatShortcutHandler>>;
36 /** Handles commands not in `commands`, such as project scripts. */
37 readonly fallback: (command: KeybindingCommand) => ChatShortcutHandler | undefined;
39​
40/**
41 * The chat's window keyboard shortcuts, plus Escape closing thread find.
42 * Listeners attach once and read the latest input on each event, so handlers
43 * act on the current render without re-registering on every change.
44 */
45export function useChatShortcuts(input: ChatShortcutsInput) {
46 const latest = useRef(input);
47 useLayoutEffect(() => {
48 latest.current = input;
49 });
50 useEffect(() => {
51 const handler = (event: KeyboardEvent) => {
52 const current = latest.current;
53 const { keybindings } = current;
54 if (preventRepeatedTerminalCloseShortcut(event, keybindings)) {
55 event.stopPropagation();
56 return;
57 }
58 if (current.threadFindActive && event.key === "Escape") return;
59 if (isTerminalCloseConfirmPending() && preventTerminalCloseShortcut(event, keybindings)) {
60 event.stopPropagation();
61 return;
62 }
63 if (!current.enabled || isCommandPaletteOpen()) return;
64 const terminalFocusOwner = getTerminalFocusOwner();
65 if (event.defaultPrevented && terminalFocusOwner === null) return;
66 const shortcutContext = current.getShortcutContext(event.target);
67 if (current.interceptKey?.(event, shortcutContext)) return;
68​
69 if (
70 !shortcutContext.terminalFocus &&
71 !shortcutContext.modelPickerOpen &&
72 shouldTypeToFocusComposer(event) &&
73 current.typeIntoComposer(event.key)
74 ) {
75 event.preventDefault();
76 event.stopPropagation();
77 return;
78 }
79​
80 const command = resolveChatShortcutCommand(event, keybindings, current.scriptKeybindings, {
81 context: shortcutContext,
82 });
83 if (!command) return;
84 const run = current.commands[command] ?? current.fallback(command);
85 if (!run || run({ event, terminalFocusOwner }) === false) return;
86 event.preventDefault();
87 event.stopPropagation();
88 };
89 const dismissFind = (event: KeyboardEvent) => {
90 const current = latest.current;
91 if (
92 event.key !== "Escape" ||
93 !current.threadFindActive ||
94 event.defaultPrevented ||
95 event.isComposing ||
96 event.keyCode === 229 ||
97 isCommandPaletteOpen()
98 )
99 return;
100 const context = current.getShortcutContext(event.target);
101 if (context.terminalFocus || context.previewFocus || context.modelPickerOpen) return;
102 event.preventDefault();
103 current.closeThreadFind();
104 };
105 window.addEventListener("keydown", handler, true);
106 window.addEventListener("keydown", dismissFind);
107 return () => {
108 window.removeEventListener("keydown", handler, true);
109 window.removeEventListener("keydown", dismissFind);
110 };
111 }, []);