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