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

threadKeyboardShortcuts.ts

43 lines1.4 KB

1import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
2import { THREAD_JUMP_KEYBINDING_COMMANDS } from "@t3tools/contracts";
3import { useCallback } from "react";
4​
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);
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 const thread = item.type === "v2-thread" ? item.item.thread : null;
25 if (thread !== null && index-- === 0) return thread;
26 }
27 return null;
29​
30export function useThreadJumpShortcuts(
31 items: ReadonlyArray<ThreadShortcutListItem>,
32 onSelectThread: (thread: EnvironmentThreadShell) => void,
33) {
34 const jumpToThread = useCallback(
35 (command: HardwareKeyboardCommand) => {
36 const thread = threadJumpTarget(items, command);
37 if (thread !== null) onSelectThread(thread);
38 return true;
39 },
40 [items, onSelectThread],
41 );
42 useHardwareKeyboardCommand(THREAD_JUMP_KEYBINDING_COMMANDS, jumpToThread);