Skip to content

Commit 49e00ee

Browse files
Sinan53xSinandevin-ai-integration[bot]
authored
fix(web): thread drops land in the visible thread's draft (pingdotgg#20024)
Co-authored-by: Sinan <[email protected]> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 34b0a9e commit 49e00ee

3 files changed

Lines changed: 81 additions & 4 deletions

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5779,8 +5779,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
57795779
},
57805780
});
57815781

5782-
const attachDroppedThreadFile = useEffectEvent((file: File) => addComposerAttachments([file]));
5783-
const importDroppedThread = useEffectEvent(
5782+
// Stable callbacks, not useEffectEvent: ChatComposer is memoized, and React
5783+
// 19.2 never refreshes an effect event's impl on memo fibers — stale closures
5784+
// here would import the dropped thread into the first mounted thread's draft.
5785+
const attachDroppedThreadFile = useStableCallback((file: File) => addComposerAttachments([file]));
5786+
const importDroppedThread = useStableCallback(
57845787
async (ref: ScopedThreadRef, isActive: () => boolean) => {
57855788
const targetKey = attachmentTargetKey;
57865789
pendingDraftWork.begin(targetKey);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// @vitest-environment jsdom
2+
3+
import { act, memo, useRef } from "react";
4+
import { createRoot, type Root } from "react-dom/client";
5+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
6+
import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";
7+
8+
import { THREAD_CONTEXT_DROP_EVENT, useThreadContextDrop } from "./threadContextDrag";
9+
10+
const ENVIRONMENT_ID = "environment-local" as EnvironmentId;
11+
12+
let root: Root;
13+
let container: HTMLDivElement;
14+
15+
beforeEach(() => {
16+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
17+
container = document.createElement("div");
18+
document.body.append(container);
19+
root = createRoot(container);
20+
});
21+
22+
afterEach(async () => {
23+
await act(() => root.unmount());
24+
container.remove();
25+
vi.unstubAllGlobals();
26+
});
27+
28+
// ChatComposer is memoized and reused across thread navigation; each render's
29+
// drop callback must write into the draft of the thread it last rendered.
30+
const ComposerDropTarget = memo(function ComposerDropTarget({
31+
draftKey,
32+
drafts,
33+
}: {
34+
draftKey: string;
35+
drafts: Map<string, Array<ThreadId>>;
36+
}) {
37+
const targetRef = useRef<HTMLDivElement | null>(null);
38+
useThreadContextDrop(targetRef, ENVIRONMENT_ID, draftKey, (refs, isActive) => {
39+
if (!isActive()) return;
40+
drafts.get(draftKey)?.push(...refs.map((ref) => ref.threadId));
41+
});
42+
return <div data-drop-target ref={targetRef} />;
43+
});
44+
45+
describe("useThreadContextDrop", () => {
46+
it("drops into the last rendered thread's draft when the memoized target is reused", async () => {
47+
const drafts = new Map<string, Array<ThreadId>>([
48+
["thread-a", []],
49+
["thread-b", []],
50+
]);
51+
52+
await act(() => root.render(<ComposerDropTarget draftKey="thread-a" drafts={drafts} />));
53+
await act(() => root.render(<ComposerDropTarget draftKey="thread-b" drafts={drafts} />));
54+
55+
const dropped: ScopedThreadRef = {
56+
environmentId: ENVIRONMENT_ID,
57+
threadId: "thread-dropped" as ThreadId,
58+
};
59+
const target = container.querySelector("[data-drop-target]");
60+
expect(target).not.toBeNull();
61+
act(() => {
62+
target!.dispatchEvent(new CustomEvent(THREAD_CONTEXT_DROP_EVENT, { detail: [dropped] }));
63+
});
64+
65+
expect(drafts.get("thread-b")).toEqual(["thread-dropped"]);
66+
expect(drafts.get("thread-a")).toEqual([]);
67+
});
68+
});

‎apps/web/src/components/chat/threadContextDrag.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { EnvironmentId, ScopedThreadRef } from "@t3tools/contracts";
2-
import { type RefObject, useEffect, useEffectEvent, useSyncExternalStore } from "react";
2+
import { type RefObject, useEffect, useSyncExternalStore } from "react";
3+
4+
import { useStableCallback } from "../../hooks/useStableCallback";
35

46
export const THREAD_CONTEXT_DROP_EVENT = "t3-thread-context-drop";
57

@@ -9,7 +11,10 @@ export function useThreadContextDrop(
911
targetKey: string,
1012
onDrop: (refs: ReadonlyArray<ScopedThreadRef>, isActive: () => boolean) => void,
1113
) {
12-
const handleDrop = useEffectEvent(onDrop);
14+
// useStableCallback, not useEffectEvent: callers are memoized (ChatComposer),
15+
// and React 19.2 never refreshes an effect event's impl on memo fibers, which
16+
// would leave this drop writing into the first mounted thread's draft.
17+
const handleDrop = useStableCallback(onDrop);
1318
useEffect(() => {
1419
const target = targetRef.current;
1520
if (!target) return;
@@ -26,6 +31,7 @@ export function useThreadContextDrop(
2631
environmentId,
2732
targetKey,
2833
targetRef,
34+
handleDrop,
2935
]);
3036
}
3137

0 commit comments

Comments
 (0)