Open History to see the last commit
1import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
2import { THREAD_JUMP_KEYBINDING_COMMANDS } from "@t3tools/contracts";
3import { useCallback } from "react";
4
5import type { HomeListItem } from "../home/homeListItems";
6import type { ThreadListV2ListItem } from "../threads/threadListV2";
7import {
8 useHardwareKeyboardCommand,
9 type HardwareKeyboardCommand,
10} from "./hardwareKeyboardCommands";
11
12type ThreadShortcutListItem =
13 | HomeListItem
14 | ThreadListV2ListItem
15 | { readonly type: "v2-show-more" };
16
17export function threadJumpIndex(command: HardwareKeyboardCommand) {
18 return THREAD_JUMP_KEYBINDING_COMMANDS.findIndex((candidate) => candidate === command);
19}
20
21/** Uses the rendered list so filters, collapsed groups and shelves keep their order. */
22export function threadJumpTarget(
23 items: ReadonlyArray<ThreadShortcutListItem>,
24 command: HardwareKeyboardCommand,
25) {
26 let index = threadJumpIndex(command);
27 if (index < 0) return null;
28 for (const item of items) {
29 const thread =
30 item.type === "thread" ? item.thread : item.type === "v2-thread" ? item.item.thread : null;
31 if (thread !== null && index-- === 0) return thread;
32 }
33 return null;
34}
35
36export function useThreadJumpShortcuts(
37 items: ReadonlyArray<ThreadShortcutListItem>,
38 onSelectThread: (thread: EnvironmentThreadShell) => void,
39) {
40 const jumpToThread = useCallback(
41 (command: HardwareKeyboardCommand) => {
42 const thread = threadJumpTarget(items, command);
43 if (thread !== null) onSelectThread(thread);
44 return true;
45 },
46 [items, onSelectThread],
47 );
48 useHardwareKeyboardCommand(THREAD_JUMP_KEYBINDING_COMMANDS, jumpToThread);
49}