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

hardwareKeyboardCommands.ts

91 lines3.0 KB

1import { EnvironmentId, ThreadId, type ThreadJumpKeybindingCommand } from "@t3tools/contracts";
2import { useEffect } from "react";
3​
4export type HardwareKeyboardCommand =
5 | ThreadJumpKeybindingCommand
6 | "commandPalette"
7 | "paletteNext"
8 | "palettePrevious"
9 | "paletteDismiss"
10 | "newTask"
11 | "focusSearch"
12 | "back"
13 | "files"
14 | "terminal"
15 | "review"
16 | "copyThreadReference"
17 | "toggleSidebar";
18​
19type CommandHandler = (command: HardwareKeyboardCommand) => boolean | void;
20​
21const handlers = new Map<HardwareKeyboardCommand, Set<CommandHandler>>();
22const registrationListeners = new Set<() => void>();
23let registrationVersion = 0;
24​
25/**
26 * Registers a context-specific hardware-keyboard action. The most recently mounted handler gets
27 * the first chance to consume the command, allowing focused screens to override app defaults.
28 */
29export function useHardwareKeyboardCommand(
30 command: HardwareKeyboardCommand | ReadonlyArray<HardwareKeyboardCommand>,
31 handler: CommandHandler,
32): void {
33 useEffect(() => {
34 const commands = typeof command === "string" ? [command] : command;
35 for (const command of commands) {
36 const commandHandlers = handlers.get(command) ?? new Set<CommandHandler>();
37 commandHandlers.add(handler);
38 handlers.set(command, commandHandlers);
39 }
40 registrationVersion += 1;
41 registrationListeners.forEach((listener) => listener());
42 return () => {
43 for (const command of commands) {
44 const commandHandlers = handlers.get(command);
45 commandHandlers?.delete(handler);
46 if (commandHandlers?.size === 0) handlers.delete(command);
47 }
48 registrationVersion += 1;
49 registrationListeners.forEach((listener) => listener());
50 };
51 }, [command, handler]);
53​
54export function getRegisteredHardwareKeyboardCommands(): ReadonlySet<HardwareKeyboardCommand> {
55 return new Set(handlers.keys());
57​
58export function getHardwareKeyboardCommandRegistrationVersion(): number {
59 return registrationVersion;
61​
62export function subscribeToHardwareKeyboardCommandRegistrations(listener: () => void): () => void {
63 registrationListeners.add(listener);
64 return () => registrationListeners.delete(listener);
66​
67export function dispatchHardwareKeyboardCommand(command: HardwareKeyboardCommand): boolean {
68 const commandHandlers = handlers.get(command);
69 if (!commandHandlers) return false;
70 // `.reverse()` on a copy, not `.toReversed()`: Hermes has no ES2023 array methods.
71 for (const handler of [...commandHandlers].reverse()) {
72 if (handler(command) !== false) return true;
73 }
74 return false;
76​
77export function parseActiveThreadPath(pathname: string): {
78 readonly environmentId: EnvironmentId;
79 readonly threadId: ThreadId;
80} | null {
81 const match = /^\/threads\/([^/]+)\/([^/]+)(?:\/|$)/.exec(pathname);
82 if (!match?.[1] || !match[2]) return null;
83 try {
84 return {
85 environmentId: EnvironmentId.make(decodeURIComponent(match[1])),
86 threadId: ThreadId.make(decodeURIComponent(match[2])),
87 };
88 } catch {
89 return null;
90 }