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

main
ts 106 lines 3.29 KB
Raw
1 import type { EnvironmentId } from "@t3tools/contracts";
2 import { type RefObject, useEffect, useEffectEvent, useSyncExternalStore } from "react";
3
4 import type { ThreadContextDropRef } from "./threadContextDrop.logic";
5
6 export const THREAD_CONTEXT_DROP_EVENT = "t3-thread-context-drop";
7
8 export function useThreadContextDrop(
9 targetRef: RefObject<EventTarget | null>,
10 environmentId: EnvironmentId,
11 targetKey: string,
12 onDrop: (refs: ReadonlyArray<ThreadContextDropRef>, isActive: () => boolean) => void,
13 ) {
14 const handleDrop = useEffectEvent(onDrop);
15 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes the target element to thread-context drop events and marks old drops inactive
16 useEffect(() => {
17 const target = targetRef.current;
18 if (!target) return;
19 let active = true;
20 const listener = (event: Event) =>
21 handleDrop((event as CustomEvent<ReadonlyArray<ThreadContextDropRef>>).detail, () => active);
22 target.addEventListener(THREAD_CONTEXT_DROP_EVENT, listener);
23 return () => {
24 active = false;
25 target.removeEventListener(THREAD_CONTEXT_DROP_EVENT, listener);
26 };
27 }, [
28 // oxlint-disable-next-line react/exhaustive-effect-dependencies -- A new environment or target ends the previous listener, so isActive reports false to drops still in flight for it.
29 environmentId,
30 targetKey,
31 targetRef,
32 ]);
33 }
34
35 const DROP_TARGET_ATTRIBUTE = "data-thread-context-drop";
36 const DROP_OVER_ATTRIBUTE = "data-thread-context-over";
37
38 export interface ThreadContextDragGhost {
39 readonly x: number;
40 readonly y: number;
41 readonly title: string;
42 readonly count: number;
43 }
44
45 let ghost: ThreadContextDragGhost | null = null;
46 const listeners = new Set<() => void>();
47
48 function setGhost(next: ThreadContextDragGhost | null) {
49 if (ghost === next) return;
50 ghost = next;
51 for (const listener of listeners) listener();
52 }
53
54 export function useThreadContextDragGhost(): ThreadContextDragGhost | null {
55 return useSyncExternalStore(
56 (listener) => {
57 listeners.add(listener);
58 return () => listeners.delete(listener);
59 },
60 () => ghost,
61 () => null,
62 );
63 }
64
65 let overTarget: HTMLElement | null = null;
66
67 function findDropTarget(point: { x: number; y: number }): HTMLElement | null {
68 return (
69 document
70 .elementFromPoint(point.x, point.y)
71 ?.closest<HTMLElement>(`[${DROP_TARGET_ATTRIBUTE}]`) ?? null
72 );
73 }
74
75 export function moveThreadContextDrag(
76 point: { x: number; y: number },
77 label: { title: string; count: number },
78 ) {
79 const target = findDropTarget(point);
80 if (target !== overTarget) {
81 overTarget?.removeAttribute(DROP_OVER_ATTRIBUTE);
82 target?.setAttribute(DROP_OVER_ATTRIBUTE, "true");
83 overTarget = target;
84 }
85 setGhost({ x: point.x, y: point.y, ...label });
86 }
87
88 export function endThreadContextDrag() {
89 overTarget?.removeAttribute(DROP_OVER_ATTRIBUTE);
90 overTarget = null;
91 setGhost(null);
92 }
93
94 export function dropThreadContext(
95 point: { x: number; y: number },
96 threads: ReadonlyArray<ThreadContextDropRef>,
97 ): boolean {
98 const target = findDropTarget(point);
99 if (!target || threads.length === 0) return false;
100 target.dispatchEvent(new CustomEvent(THREAD_CONTEXT_DROP_EVENT, { detail: threads }));
101 return true;
102 }
103
104 export function threadContextDropTargetProps() {
105 return { [DROP_TARGET_ATTRIBUTE]: "true" } as const;
106 }