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

hardwareKeyboardCommands.ts

99 lines3.3 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 | "cycleHost";
19​
20type CommandHandler = (command: HardwareKeyboardCommand) => boolean | void;
21​
22const handlers = new Map<HardwareKeyboardCommand, Set<CommandHandler>>();
23const registrationListeners = new Set<() => void>();
24let registrationVersion = 0;
25​
26export function useHardwareKeyboardCommand(
27 command: HardwareKeyboardCommand | ReadonlyArray<HardwareKeyboardCommand>,
28 handler: CommandHandler,
29): void {
30 useEffect(() => {
31 const commands = typeof command === "string" ? [command] : command;
32 for (const command of commands) {
33 const commandHandlers = handlers.get(command) ?? new Set<CommandHandler>();
34 commandHandlers.add(handler);
35 handlers.set(command, commandHandlers);
36 }
37 registrationVersion += 1;
38 registrationListeners.forEach((listener) => listener());
39 return () => {
40 for (const command of commands) {
41 const commandHandlers = handlers.get(command);
42 commandHandlers?.delete(handler);
43 if (commandHandlers?.size === 0) handlers.delete(command);
44 }
45 registrationVersion += 1;
46 registrationListeners.forEach((listener) => listener());
47 };
48 }, [command, handler]);
50​
51export function getRegisteredHardwareKeyboardCommands(): ReadonlySet<HardwareKeyboardCommand> {
52 return new Set(handlers.keys());
54​
55export function getHardwareKeyboardCommandRegistrationVersion(): number {
56 return registrationVersion;
58​
59export function subscribeToHardwareKeyboardCommandRegistrations(listener: () => void): () => void {
60 registrationListeners.add(listener);
61 return () => registrationListeners.delete(listener);
63​
64export function dispatchHardwareKeyboardCommand(command: HardwareKeyboardCommand): boolean {
65 const commandHandlers = handlers.get(command);
66 if (!commandHandlers) return false;
67 const handlersInRegistrationOrder = Array.from(commandHandlers);
68 for (let index = handlersInRegistrationOrder.length - 1; index >= 0; index -= 1) {
69 const handler = handlersInRegistrationOrder[index];
70 if (!handler) continue;
71 if (handler(command) !== false) return true;
72 }
73 return false;
75​
76export function parseActiveThreadPath(pathname: string): {
77 readonly environmentId: EnvironmentId;
78 readonly threadId: ThreadId;
79} | null {
80 const match = /^\/threads\/([^/]+)\/([^/]+)(?:\/|$)/.exec(pathname);
81 if (!match?.[1] || !match[2]) return null;
82 try {
83 return {
84 environmentId: EnvironmentId.make(decodeURIComponent(match[1])),
85 threadId: ThreadId.make(decodeURIComponent(match[2])),
86 };
87 } catch {
88 return null;
89 }
91​
92export function nextEnvironmentId<T extends { readonly environmentId: string }>(
93 environments: ReadonlyArray<T>,
94 currentId: string | null,
95): T["environmentId"] | null {
96 if (environments.length < 2) return null;
97 const index = environments.findIndex((environment) => environment.environmentId === currentId);
98 return environments[(index + 1) % environments.length]?.environmentId ?? null;