The best way to control Claude Code, Codex and more from any computer or phone
fix(web): thread drops land in the visible thread's draft (#20024)
Co-authored-by: Sinan <sinan@Mac-mini-von-Sinan.local> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Sinan Turna committed
Oct 18, 2026 at 21:52 UTC
49e00ee30ca0b7c6d607f697a0624e363e8a98b7
3 files changed
+81
-4
apps/web/src/components/chat/ChatComposer.tsx
+5
-2
@@ -5779,8 +5779,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
5779
},
5780
});
5781
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(
5787
async (ref: ScopedThreadRef, isActive: () => boolean) => {
5788
const targetKey = attachmentTargetKey;
5789
pendingDraftWork.begin(targetKey);
apps/web/src/components/chat/threadContextDrag.test.tsx
new
+68
@@ -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
+8
-2
@@ -1,5 +1,7 @@
1
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";
5
6
export const THREAD_CONTEXT_DROP_EVENT = "t3-thread-context-drop";
7
@@ -9,7 +11,10 @@ export function useThreadContextDrop(
11
targetKey: string,
12
onDrop: (refs: ReadonlyArray<ScopedThreadRef>, isActive: () => boolean) => void,
13
) {
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);
18
useEffect(() => {
19
const target = targetRef.current;
20
if (!target) return;
@@ -26,6 +31,7 @@ export function useThreadContextDrop(
31
environmentId,
32
targetKey,
33
targetRef,
34
+ handleDrop,
35
]);
36
}
37