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

hardwareKeyboardCommands.ts

106 lines3.7 KB

1import { EnvironmentId, ThreadId, type ThreadJumpKeybindingCommand } from "@t3tools/contracts";
2import { use, useEffect } from "react";
3​
4import { NativeWorkspaceTabActivityContext } from "../../native/v5-workspace-context";
5​
6export type HardwareKeyboardCommand =
7 | ThreadJumpKeybindingCommand
8 | "commandPalette"
9 | "paletteNext"
10 | "palettePrevious"
11 | "paletteDismiss"
12 | "newTask"
13 | "focusSearch"
14 | "back"
15 | "files"
16 | "terminal"
17 | "review"
18 | "copyThreadReference"
19 | "toggleSidebar"
20 | "cycleHost";
21​
22type CommandHandler = (command: HardwareKeyboardCommand) => boolean | void;
23​
24const handlers = new Map<HardwareKeyboardCommand, Set<CommandHandler>>();
25const registrationListeners = new Set<() => void>();
26let registrationVersion = 0;
27​
28export function useHardwareKeyboardCommand(
29 command: HardwareKeyboardCommand | ReadonlyArray<HardwareKeyboardCommand>,
30 handler: CommandHandler,
31): void {
32 // A retained tab stays mounted while another is shown; the newest handler wins, so a hidden tab
33 // must not register.
34 const tabActive = use(NativeWorkspaceTabActivityContext)?.active ?? true;
35 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- registers the handler in the module-level hardware keyboard command registry
36 useEffect(() => {
37 if (!tabActive) return;
38 const commands = typeof command === "string" ? [command] : command;
39 for (const command of commands) {
40 const commandHandlers = handlers.get(command) ?? new Set<CommandHandler>();
41 commandHandlers.add(handler);
42 handlers.set(command, commandHandlers);
43 }
44 registrationVersion += 1;
45 registrationListeners.forEach((listener) => listener());
46 return () => {
47 for (const command of commands) {
48 const commandHandlers = handlers.get(command);
49 commandHandlers?.delete(handler);
50 if (commandHandlers?.size === 0) handlers.delete(command);
51 }
52 registrationVersion += 1;
53 registrationListeners.forEach((listener) => listener());
54 };
55 }, [command, handler, tabActive]);
57​
58export function getRegisteredHardwareKeyboardCommands(): ReadonlySet<HardwareKeyboardCommand> {
59 return new Set(handlers.keys());
61​
62export function getHardwareKeyboardCommandRegistrationVersion(): number {
63 return registrationVersion;
65​
66export function subscribeToHardwareKeyboardCommandRegistrations(listener: () => void): () => void {
67 registrationListeners.add(listener);
68 return () => registrationListeners.delete(listener);
70​
71export function dispatchHardwareKeyboardCommand(command: HardwareKeyboardCommand): boolean {
72 const commandHandlers = handlers.get(command);
73 if (!commandHandlers) return false;
74 const handlersInRegistrationOrder = Array.from(commandHandlers);
75 for (let index = handlersInRegistrationOrder.length - 1; index >= 0; index -= 1) {
76 const handler = handlersInRegistrationOrder[index];
77 if (!handler) continue;
78 if (handler(command) !== false) return true;
79 }
80 return false;
82​
83export function parseActiveThreadPath(pathname: string): {
84 readonly environmentId: EnvironmentId;
85 readonly threadId: ThreadId;
86} | null {
87 const match = /^\/threads\/([^/]+)\/([^/]+)(?:\/|$)/.exec(pathname);
88 if (!match?.[1] || !match[2]) return null;
89 try {
90 return {
91 environmentId: EnvironmentId.make(decodeURIComponent(match[1])),
92 threadId: ThreadId.make(decodeURIComponent(match[2])),
93 };
94 } catch {
95 return null;
96 }
98​
99export function nextEnvironmentId<T extends { readonly environmentId: string }>(
100 environments: ReadonlyArray<T>,
101 currentId: string | null,
102): T["environmentId"] | null {
103 if (environments.length < 2) return null;
104 const index = environments.findIndex((environment) => environment.environmentId === currentId);
105 return environments[(index + 1) % environments.length]?.environmentId ?? null;