Open History to see the last commit
1import { memo, useRef } from "react";
2import { describe, expect, it } from "vite-plus/test";
3import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";
4
5import { act, render } from "../../../test/render";
6import { THREAD_CONTEXT_DROP_EVENT, useThreadContextDrop } from "./threadContextDrag";
7
8const ENVIRONMENT_ID = "environment-local" as EnvironmentId;
9
10// ChatComposer is memoized and reused across thread navigation; each render's
11// drop callback must write into the draft of the thread it last rendered.
12const ComposerDropTarget = memo(function ComposerDropTarget({
13 draftKey,
14 drafts,
15}: {
16 draftKey: string;
17 drafts: Map<string, Array<ThreadId>>;
18}) {
19 const targetRef = useRef<HTMLDivElement | null>(null);
20 useThreadContextDrop(targetRef, ENVIRONMENT_ID, draftKey, (refs, isActive) => {
21 if (!isActive()) return;
22 drafts.get(draftKey)?.push(...refs.map((ref) => ref.threadId));
23 });
24 return <div data-drop-target ref={targetRef} />;
25});
26
27// Same target key, so the listener effect never re-runs: only the effect event
28// itself can hand the drop the latest props (React 19.2 kept the first render's).
29const StableKeyDropTarget = memo(function StableKeyDropTarget({
30 received,
31}: {
32 received: Array<ThreadId>;
33}) {
34 const targetRef = useRef<HTMLDivElement | null>(null);
35 useThreadContextDrop(targetRef, ENVIRONMENT_ID, "thread-a", (refs) => {
36 received.push(...refs.map((ref) => ref.threadId));
37 });
38 return <div data-drop-target ref={targetRef} />;
39});
40
41describe("useThreadContextDrop", () => {
42 it("drops into the last rendered thread's draft when the memoized target is reused", async () => {
43 const drafts = new Map<string, Array<ThreadId>>([
44 ["thread-a", []],
45 ["thread-b", []],
46 ]);
47
48 await using screen = await render(<ComposerDropTarget draftKey="thread-a" drafts={drafts} />);
49 await screen.rerender(<ComposerDropTarget draftKey="thread-b" drafts={drafts} />);
50
51 const dropped: ScopedThreadRef = {
52 environmentId: ENVIRONMENT_ID,
53 threadId: "thread-dropped" as ThreadId,
54 };
55 const target = screen.container.querySelector("[data-drop-target]");
56 expect(target).not.toBeNull();
57 await act(() => {
58 target!.dispatchEvent(new CustomEvent(THREAD_CONTEXT_DROP_EVENT, { detail: [dropped] }));
59 });
60
61 expect(drafts.get("thread-b")).toEqual(["thread-dropped"]);
62 expect(drafts.get("thread-a")).toEqual([]);
63 });
64
65 it("reads the latest props in a memoized target whose listener stays attached", async () => {
66 const first: Array<ThreadId> = [];
67 const latest: Array<ThreadId> = [];
68
69 await using screen = await render(<StableKeyDropTarget received={first} />);
70 await screen.rerender(<StableKeyDropTarget received={latest} />);
71
72 const target = screen.container.querySelector("[data-drop-target]");
73 expect(target).not.toBeNull();
74 await act(() => {
75 target!.dispatchEvent(
76 new CustomEvent(THREAD_CONTEXT_DROP_EVENT, {
77 detail: [{ environmentId: ENVIRONMENT_ID, threadId: "thread-dropped" as ThreadId }],
78 }),
79 );
80 });
81
82 expect(latest).toEqual(["thread-dropped"]);
83 expect(first).toEqual([]);
84 });
85});