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