@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 65 lines 2.56 KB
Raw
1 import {
2 type SideThreadOpenIntent,
3 sideThreadOpenIntentOf,
4 } from "@t3tools/client-runtime/side-thread";
5 import { useGT } from "gt-react";
6 import { createContext, use, type KeyboardEvent, type MouseEvent } from "react";
7
8 import { contextMenuSourceOf } from "../contextMenuSource";
9 import { isMacPlatform } from "../lib/utils";
10 import { readLocalApi } from "../localApi";
11
12 /**
13 * False where the threads a view names cannot be opened from it, such as a read-only timeline
14 * with no environment behind it. Nothing that would open one is offered there.
15 */
16 export const SideThreadsOpenContext = createContext(true);
17
18 /**
19 * The handlers every row, card and chip that opens a subagent, a side chat or a linked thread
20 * shares: click and Enter open it where the setting says, the modifier or a middle click in the
21 * other place, and the context menu names both. Pass `panel: false` where no panel can show it.
22 * Where threads cannot be opened (`SideThreadsOpenContext`) it gives no handlers at all.
23 */
24 export function useSideThreadTrigger() {
25 const gt = useGT();
26 const canOpen = use(SideThreadsOpenContext);
27 return (open: (intent: SideThreadOpenIntent) => void, options?: { readonly panel?: boolean }) => {
28 if (!canOpen) return {};
29 const mac = isMacPlatform(navigator.platform);
30 return {
31 onClick: (event: MouseEvent) => open(sideThreadOpenIntentOf(event, mac)),
32 // Middle button: keeps the browser's autoscroll from starting under the click.
33 onMouseDown: (event: MouseEvent) => {
34 if (event.button === 1) event.preventDefault();
35 },
36 onAuxClick: (event: MouseEvent) => {
37 if (event.button !== 1) return;
38 event.preventDefault();
39 open("alternate");
40 },
41 onKeyDown: (event: KeyboardEvent) => {
42 if (event.key !== "Enter" || sideThreadOpenIntentOf(event, mac) !== "alternate") return;
43 event.preventDefault();
44 open("alternate");
45 },
46 onContextMenu: (event: MouseEvent) => {
47 const api = readLocalApi();
48 if (!api) return;
49 event.preventDefault();
50 event.stopPropagation();
51 const items = [
52 ...(options?.panel === false
53 ? []
54 : ([{ id: "panel", label: gt("Open in side panel") }] as const)),
55 { id: "main", label: gt("Open in main view") },
56 ] as const;
57 void api.contextMenu
58 .show(items, { x: event.clientX, y: event.clientY }, contextMenuSourceOf(event))
59 .then((clicked) => {
60 if (clicked === "panel" || clicked === "main") open(clicked);
61 });
62 },
63 };
64 };
65 }