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

threadContextDrag.ts

106 lines3.3 KB

1import type { EnvironmentId } from "@t3tools/contracts";
2import { type RefObject, useEffect, useEffectEvent, useSyncExternalStore } from "react";
3​
4import type { ThreadContextDropRef } from "./threadContextDrop.logic";
5​
6export const THREAD_CONTEXT_DROP_EVENT = "t3-thread-context-drop";
7​
8export 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 ]);
34​
35const DROP_TARGET_ATTRIBUTE = "data-thread-context-drop";
36const DROP_OVER_ATTRIBUTE = "data-thread-context-over";
37​
38export interface ThreadContextDragGhost {
39 readonly x: number;
40 readonly y: number;
41 readonly title: string;
42 readonly count: number;
44​
45let ghost: ThreadContextDragGhost | null = null;
46const listeners = new Set<() => void>();
47​
48function setGhost(next: ThreadContextDragGhost | null) {
49 if (ghost === next) return;
50 ghost = next;
51 for (const listener of listeners) listener();
53​
54export function useThreadContextDragGhost(): ThreadContextDragGhost | null {
55 return useSyncExternalStore(
56 (listener) => {
57 listeners.add(listener);
58 return () => listeners.delete(listener);
59 },
60 () => ghost,
61 () => null,
62 );
64​
65let overTarget: HTMLElement | null = null;
66​
67function 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 );
74​
75export 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 });
87​
88export function endThreadContextDrag() {
89 overTarget?.removeAttribute(DROP_OVER_ATTRIBUTE);
90 overTarget = null;
91 setGhost(null);
93​
94export 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;
103​
104export function threadContextDropTargetProps() {
105 return { [DROP_TARGET_ATTRIBUTE]: "true" } as const;