1import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
2import { THREAD_JUMP_KEYBINDING_COMMANDS } from "@t3tools/contracts";
3
4import type { ThreadListV2ListItem } from "../threads/threadListV2";
5import {
6 useHardwareKeyboardCommand,
7 type HardwareKeyboardCommand,
8} from "./hardwareKeyboardCommands";
9
10type ThreadShortcutListItem = ThreadListV2ListItem | { readonly type: "v2-show-more" };
11
12export function threadJumpIndex(command: HardwareKeyboardCommand) {
13 return THREAD_JUMP_KEYBINDING_COMMANDS.findIndex((candidate) => candidate === command);
14}
15
16export function threadJumpTarget(
17 items: ReadonlyArray<ThreadShortcutListItem>,
18 command: HardwareKeyboardCommand,
19) {
20 let index = threadJumpIndex(command);
21 if (index < 0) return null;
22 for (const item of items) {
23 const thread = item.type === "v2-thread" ? item.item.thread : null;
24 if (thread !== null && index-- === 0) return thread;
25 }
26 return null;
27}
28
29export function useThreadJumpShortcuts(
30 items: ReadonlyArray<ThreadShortcutListItem>,
31 onSelectThread: (thread: EnvironmentThreadShell) => void,
32) {
33 const jumpToThread = (command: HardwareKeyboardCommand) => {
34 const thread = threadJumpTarget(items, command);
35 if (thread !== null) onSelectThread(thread);
36 return true;
37 };
38 useHardwareKeyboardCommand(THREAD_JUMP_KEYBINDING_COMMANDS, jumpToThread);
39}