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

hardwareKeyboardCommands.ts

93 lines3.1 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 const handlersInRegistrationOrder = Array.from(commandHandlers);
71 for (let index = handlersInRegistrationOrder.length - 1; index >= 0; index -= 1) {
72 const handler = handlersInRegistrationOrder[index];
73 if (!handler) continue;
74 if (handler(command) !== false) return true;
75 }
76 return false;
78​
79export function parseActiveThreadPath(pathname: string): {
80 readonly environmentId: EnvironmentId;
81 readonly threadId: ThreadId;
82} | null {
83 const match = /^\/threads\/([^/]+)\/([^/]+)(?:\/|$)/.exec(pathname);
84 if (!match?.[1] || !match[2]) return null;
85 try {
86 return {
87 environmentId: EnvironmentId.make(decodeURIComponent(match[1])),
88 threadId: ThreadId.make(decodeURIComponent(match[2])),
89 };
90 } catch {
91 return null;
92 }