Skip to content

Commit 26ccad5

Browse files
feat(web): queue repeat sidebar drops per thread with per-drop identity
The pending store held a single drop, so a second drop before the thread opened replaced the first, and a stale navigation cleared by thread key alone could wipe a newer drop for the same thread. Queue each drop with its own id: ChatView consumes every queued drop for the open thread in order, and navigation cleanup removes only the drop that started it.
1 parent c5bee00 commit 26ccad5

5 files changed

Lines changed: 135 additions & 88 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7581,41 +7581,38 @@ export default function ChatView(props: ChatViewProps) {
75817581
// actually open, then take the exact same path as a workspace drop:
75827582
// validate, compress, focus the composer, never send. Kept above the
75837583
// no-active-thread early return so hook order never changes.
7584-
const pendingSidebarFileDrop = useSidebarPendingFileDropStore((state) => state.pending);
7584+
const pendingSidebarFileDrops = useSidebarPendingFileDropStore((state) => state.pending);
75857585
const consumePendingFileDrop = useSidebarPendingFileDropStore(
75867586
(state) => state.consumePendingFileDrop,
75877587
);
75887588
useEffect(() => {
7589-
if (pendingSidebarFileDrop === null) return;
7589+
if (pendingSidebarFileDrops.length === 0) return;
75907590
// A promoting draft can mount this view with the server thread id while
75917591
// its composer is still draft-keyed; finalization would discard what we
75927592
// attach there. Only the canonical thread target may consume a drop.
75937593
if (
75947594
typeof composerDraftTarget === "string" ||
7595-
!isSameSidebarThreadRef(composerDraftTarget, pendingSidebarFileDrop.threadRef)
7595+
!pendingSidebarFileDrops.some((drop) =>
7596+
isSameSidebarThreadRef(composerDraftTarget, drop.threadRef),
7597+
)
75967598
) {
75977599
return;
75987600
}
75997601
if (!activeThread) return;
76007602
if (!composerRef.current) {
76017603
const raf = window.requestAnimationFrame(() => {
76027604
if (!composerRef.current) return;
7603-
const latestPending = useSidebarPendingFileDropStore.getState().pending;
7604-
if (latestPending === null) return;
7605-
if (
7606-
typeof composerDraftTarget === "string" ||
7607-
!isSameSidebarThreadRef(composerDraftTarget, latestPending.threadRef)
7608-
) {
7609-
return;
7610-
}
7611-
const files = consumePendingFileDrop(latestPending.threadRef);
7605+
if (typeof composerDraftTarget === "string") return;
7606+
// Consume matches by target, so a newer drop that arrived meanwhile
7607+
// is collected too rather than orphaned.
7608+
const files = consumePendingFileDrop(composerDraftTarget);
76127609
if (files !== null) {
76137610
composerRef.current?.addDroppedFiles(files);
76147611
}
76157612
});
76167613
return () => window.cancelAnimationFrame(raf);
76177614
}
7618-
const files = consumePendingFileDrop(pendingSidebarFileDrop.threadRef);
7615+
const files = consumePendingFileDrop(composerDraftTarget);
76197616
if (files !== null) {
76207617
composerRef.current.addDroppedFiles(files);
76217618
}
@@ -7624,7 +7621,7 @@ export default function ChatView(props: ChatViewProps) {
76247621
composerDraftTarget,
76257622
composerRef,
76267623
consumePendingFileDrop,
7627-
pendingSidebarFileDrop,
7624+
pendingSidebarFileDrops,
76287625
]);
76297626

76307627
// Empty state: no active thread

‎apps/web/src/components/Sidebar.tsx‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,11 +2529,13 @@ export default function Sidebar() {
25292529
// not the open thread we stash the files and let ChatView hand them over
25302530
// once the navigation actually lands; if the route bounced (thread gone),
25312531
// nothing will consume them, so clear instead of surprising the user later.
2532-
const setPendingFileDrop = useSidebarPendingFileDropStore((s) => s.setPendingFileDrop);
2532+
const queuePendingFileDrop = useSidebarPendingFileDropStore((s) => s.queuePendingFileDrop);
25332533
const clearPendingFileDrop = useSidebarPendingFileDropStore((s) => s.clearPendingFileDrop);
25342534
const handleThreadFileDrop = useCallback(
25352535
async (threadRef: ScopedThreadRef, files: File[]) => {
2536-
setPendingFileDrop({ threadRef, files });
2536+
// Queued, not replaced: a second drop before the thread opens keeps
2537+
// both files, and the id lets cleanup below touch only this drop.
2538+
const dropId = queuePendingFileDrop({ threadRef, files });
25372539
// Key match alone is not "already there": during draft promotion the
25382540
// resolved route key is the server thread while the URL is still the
25392541
// draft route, and its composer would swallow the drop then discard it.
@@ -2545,26 +2547,23 @@ export default function Sidebar() {
25452547
if (landedBefore) return;
25462548
try {
25472549
await navigateToThread(threadRef);
2548-
// A newer drop may have replaced ours while the navigation was in
2549-
// flight; only clear the stash if it still belongs to this drop.
2550+
// A newer drop may have arrived while the navigation was in flight;
2551+
// clearing by id leaves those files untouched.
25502552
const landed =
25512553
router.buildLocation({
25522554
to: "/$environmentId/$threadId",
25532555
params: buildThreadRouteParams(threadRef),
25542556
}).pathname === router.state.location.pathname;
2555-
const pending = useSidebarPendingFileDropStore.getState().pending;
2556-
if (!landed && pending !== null && isSameSidebarThreadRef(pending.threadRef, threadRef)) {
2557-
clearPendingFileDrop();
2557+
if (!landed) {
2558+
clearPendingFileDrop(dropId);
25582559
}
25592560
} catch {
2560-
// Navigation failed outright; nothing will consume this drop.
2561-
const pending = useSidebarPendingFileDropStore.getState().pending;
2562-
if (pending !== null && isSameSidebarThreadRef(pending.threadRef, threadRef)) {
2563-
clearPendingFileDrop();
2564-
}
2561+
// Navigation failed outright; nothing will consume this drop, but a
2562+
// newer drop for the same thread may still be deliverable.
2563+
clearPendingFileDrop(dropId);
25652564
}
25662565
},
2567-
[clearPendingFileDrop, navigateToThread, router, setPendingFileDrop],
2566+
[clearPendingFileDrop, navigateToThread, queuePendingFileDrop, router],
25682567
);
25692568

25702569
const navigateToDraft = useCallback(

‎apps/web/src/routes/_chat.$environmentId.$threadId.tsx‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ import { threadHasStarted } from "../components/ChatView.logic";
66
import { finalizePromotedDraftThreadByRef, useComposerDraftStore } from "../composerDraftStore";
77
import { resolveThreadRouteRef, resolveThreadRouteRenderState } from "../threadRoutes";
88
import { resolveThreadSyncPhase } from "../threadSync";
9-
import {
10-
isSameSidebarThreadRef,
11-
useSidebarPendingFileDropStore,
12-
} from "../sidebarPendingFileDropStore";
9+
import { useSidebarPendingFileDropStore } from "../sidebarPendingFileDropStore";
1310
import { SidebarInset } from "~/components/ui/sidebar";
1411
import {
1512
useEnvironmentThreadRefs,
@@ -70,10 +67,8 @@ function ChatThreadRouteView() {
7067
// passed its landing check; once the thread reads as missing it can
7168
// never be attached, release it even when there is nowhere to redirect.
7269
if (renderState === "missing") {
73-
const { pending, clearPendingFileDrop } = useSidebarPendingFileDropStore.getState();
74-
if (pending && isSameSidebarThreadRef(pending.threadRef, threadRef)) {
75-
clearPendingFileDrop();
76-
}
70+
const { clearPendingFileDropsForThread } = useSidebarPendingFileDropStore.getState();
71+
clearPendingFileDropsForThread(threadRef);
7772
if (environmentHasAnyThreads) {
7873
void navigate({ to: "/", replace: true });
7974
}

‎apps/web/src/sidebarPendingFileDropStore.test.ts‎

Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,76 +4,113 @@ import { scopeThreadRef } from "@t3tools/client-runtime/environment";
44
import { type EnvironmentId, ThreadId } from "@t3tools/contracts";
55

66
import {
7+
isSameSidebarThreadRef,
78
useSidebarPendingFileDropStore,
89
type SidebarPendingFileDrop,
910
} from "./sidebarPendingFileDropStore";
1011

11-
function makeFiles(count: number): File[] {
12-
return Array.from({ length: count }, (_, index) => new File(["x"], `f${index}.png`));
12+
function makeFiles(...names: string[]): File[] {
13+
return names.map((name) => new File(["x"], name));
1314
}
1415

15-
function makeEntry(environmentId: string, threadId: string, files: File[]): SidebarPendingFileDrop {
16+
function makeEntry(
17+
environmentId: string,
18+
threadId: string,
19+
files: File[],
20+
): Omit<SidebarPendingFileDrop, "id"> {
1621
return {
1722
threadRef: scopeThreadRef(environmentId as EnvironmentId, ThreadId.make(threadId)),
1823
files,
1924
};
2025
}
2126

27+
function refOf(environmentId: string, threadId: string) {
28+
return makeEntry(environmentId, threadId, []).threadRef;
29+
}
30+
2231
beforeEach(() => {
23-
useSidebarPendingFileDropStore.setState({ pending: null });
32+
useSidebarPendingFileDropStore.setState({ pending: [] });
2433
});
2534

2635
describe("sidebarPendingFileDropStore", () => {
2736
it("starts empty", () => {
28-
expect(useSidebarPendingFileDropStore.getState().pending).toBeNull();
37+
expect(useSidebarPendingFileDropStore.getState().pending).toEqual([]);
2938
});
3039

3140
it("stashes and consumes a drop for the matching thread", () => {
32-
const files = makeFiles(2);
33-
const entry = makeEntry("env-1", "thread-1", files);
34-
useSidebarPendingFileDropStore.getState().setPendingFileDrop(entry);
41+
const files = makeFiles("a.png", "b.png");
42+
const store = useSidebarPendingFileDropStore.getState();
43+
store.queuePendingFileDrop(makeEntry("env-1", "thread-1", files));
3544

3645
expect(
37-
useSidebarPendingFileDropStore.getState().consumePendingFileDrop(entry.threadRef),
46+
useSidebarPendingFileDropStore.getState().consumePendingFileDrop(refOf("env-1", "thread-1")),
3847
).toEqual(files);
39-
expect(useSidebarPendingFileDropStore.getState().pending).toBeNull();
48+
expect(useSidebarPendingFileDropStore.getState().pending).toEqual([]);
4049
});
4150

42-
it("refuses to consume for a different thread without clearing the stash", () => {
43-
const entry = makeEntry("env-1", "thread-1", makeFiles(1));
44-
useSidebarPendingFileDropStore.getState().setPendingFileDrop(entry);
51+
it("accumulates repeat drops onto the same thread instead of replacing", () => {
52+
const store = useSidebarPendingFileDropStore.getState();
53+
store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("a.png")));
54+
store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("b.png")));
4555

46-
const otherThread = makeEntry("env-1", "thread-2", []).threadRef;
4756
expect(
48-
useSidebarPendingFileDropStore.getState().consumePendingFileDrop(otherThread),
49-
).toBeNull();
50-
expect(useSidebarPendingFileDropStore.getState().pending).toEqual(entry);
57+
useSidebarPendingFileDropStore.getState().consumePendingFileDrop(refOf("env-1", "thread-1")),
58+
).toEqual(makeFiles("a.png", "b.png"));
59+
expect(useSidebarPendingFileDropStore.getState().pending).toEqual([]);
60+
});
5161

52-
useSidebarPendingFileDropStore.getState().clearPendingFileDrop();
53-
expect(useSidebarPendingFileDropStore.getState().pending).toBeNull();
62+
it("keeps drops for other threads when consuming one thread", () => {
63+
const store = useSidebarPendingFileDropStore.getState();
64+
store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("a.png")));
65+
store.queuePendingFileDrop(makeEntry("env-1", "thread-2", makeFiles("b.png")));
66+
67+
expect(
68+
useSidebarPendingFileDropStore.getState().consumePendingFileDrop(refOf("env-1", "thread-2")),
69+
).toEqual(makeFiles("b.png"));
70+
expect(useSidebarPendingFileDropStore.getState().pending).toHaveLength(1);
5471
});
5572

56-
it("replaces an earlier pending drop with a newer one", () => {
57-
const first = makeEntry("env-1", "thread-1", makeFiles(1));
58-
const second = makeEntry("env-2", "thread-2", makeFiles(3));
59-
useSidebarPendingFileDropStore.getState().setPendingFileDrop(first);
60-
useSidebarPendingFileDropStore.getState().setPendingFileDrop(second);
73+
it("clears only the drop matching a stale navigation's id", () => {
74+
const store = useSidebarPendingFileDropStore.getState();
75+
const firstId = store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("a.png")));
76+
store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("b.png")));
6177

62-
expect(useSidebarPendingFileDropStore.getState().pending).toEqual(second);
78+
useSidebarPendingFileDropStore.getState().clearPendingFileDrop(firstId);
6379
expect(
64-
useSidebarPendingFileDropStore.getState().consumePendingFileDrop(first.threadRef),
65-
).toBeNull();
80+
useSidebarPendingFileDropStore.getState().consumePendingFileDrop(refOf("env-1", "thread-1")),
81+
).toEqual(makeFiles("b.png"));
82+
});
83+
84+
it("clears every drop for a missing thread", () => {
85+
const store = useSidebarPendingFileDropStore.getState();
86+
store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("a.png")));
87+
store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("b.png")));
88+
store.queuePendingFileDrop(makeEntry("env-1", "thread-2", makeFiles("c.png")));
89+
90+
useSidebarPendingFileDropStore
91+
.getState()
92+
.clearPendingFileDropsForThread(refOf("env-1", "thread-1"));
6693
expect(
67-
useSidebarPendingFileDropStore.getState().consumePendingFileDrop(second.threadRef),
68-
).not.toBeNull();
94+
useSidebarPendingFileDropStore.getState().consumePendingFileDrop(refOf("env-1", "thread-2")),
95+
).toEqual(makeFiles("c.png"));
96+
expect(useSidebarPendingFileDropStore.getState().pending).toEqual([]);
6997
});
7098

7199
it("does not confuse refs whose joined keys collide on colons", () => {
72-
const entry = makeEntry("a", "b:c", makeFiles(1));
73-
useSidebarPendingFileDropStore.getState().setPendingFileDrop(entry);
100+
const store = useSidebarPendingFileDropStore.getState();
101+
store.queuePendingFileDrop(makeEntry("a", "b:c", makeFiles("a.png")));
102+
103+
expect(
104+
useSidebarPendingFileDropStore.getState().consumePendingFileDrop(refOf("a:b", "c")),
105+
).toBeNull();
106+
expect(useSidebarPendingFileDropStore.getState().pending).toHaveLength(1);
107+
});
108+
});
74109

75-
const colliding = makeEntry("a:b", "c", []).threadRef;
76-
expect(useSidebarPendingFileDropStore.getState().consumePendingFileDrop(colliding)).toBeNull();
77-
expect(useSidebarPendingFileDropStore.getState().pending).toEqual(entry);
110+
describe("isSameSidebarThreadRef", () => {
111+
it("compares fields, not joined keys", () => {
112+
expect(isSameSidebarThreadRef(refOf("a", "b:c"), refOf("a", "b:c"))).toBe(true);
113+
expect(isSameSidebarThreadRef(refOf("a", "b:c"), refOf("a:b", "c"))).toBe(false);
114+
expect(isSameSidebarThreadRef(refOf("a", "b"), refOf("a", "c"))).toBe(false);
78115
});
79116
});

‎apps/web/src/sidebarPendingFileDropStore.ts‎

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,63 @@ export function isSameSidebarThreadRef(a: ScopedThreadRef, b: ScopedThreadRef):
1212
}
1313

1414
/**
15-
* Files dropped onto a sidebar thread row while another thread was open.
16-
* The row hands these off and navigates; ChatView attaches them through the
17-
* composer's normal drop path once the dropped-on thread actually becomes
18-
* active, so they can never land on the wrong thread's draft.
15+
* One sidebar row drop. Drops queue up instead of replacing each other, so a
16+
* second drop onto the same thread before it opens keeps both files; each
17+
* entry carries its own id so a stale navigation can only ever clear the drop
18+
* that started it.
1919
*/
2020
export interface SidebarPendingFileDrop {
21+
id: string;
2122
threadRef: ScopedThreadRef;
2223
files: File[];
2324
}
2425

2526
interface SidebarPendingFileDropStoreState {
26-
pending: SidebarPendingFileDrop | null;
27-
/** Replaces any earlier pending drop: only the latest handoff matters. */
28-
setPendingFileDrop: (entry: SidebarPendingFileDrop) => void;
29-
clearPendingFileDrop: () => void;
27+
pending: SidebarPendingFileDrop[];
3028
/**
31-
* Returns the stashed files when `threadRef` matches the drop target and
32-
* clears the entry; returns null (leaving state untouched) otherwise.
29+
* Appends a drop to the queue and returns its id, for later
30+
* identity-checked cleanup.
31+
*/
32+
queuePendingFileDrop: (entry: Omit<SidebarPendingFileDrop, "id">) => string;
33+
/** Removes the single drop with this id, leaving newer drops untouched. */
34+
clearPendingFileDrop: (id: string) => void;
35+
/** Removes every queued drop aimed at this thread (e.g. it went missing). */
36+
clearPendingFileDropsForThread: (threadRef: ScopedThreadRef) => void;
37+
/**
38+
* Returns every queued drop's files for `threadRef`, oldest first, and
39+
* removes them; returns null (leaving state untouched) when none match.
3340
*/
3441
consumePendingFileDrop: (threadRef: ScopedThreadRef) => File[] | null;
3542
}
3643

44+
let nextPendingFileDropId = 0;
45+
3746
export const useSidebarPendingFileDropStore = create<SidebarPendingFileDropStoreState>()(
3847
(set, get) => ({
39-
pending: null,
40-
setPendingFileDrop: (entry) => {
41-
set({ pending: entry });
48+
pending: [],
49+
queuePendingFileDrop: (entry) => {
50+
const id = `sidebar-file-drop-${(nextPendingFileDropId += 1)}`;
51+
set((state) => ({ pending: [...state.pending, { ...entry, id }] }));
52+
return id;
53+
},
54+
clearPendingFileDrop: (id) => {
55+
set((state) => ({ pending: state.pending.filter((drop) => drop.id !== id) }));
4256
},
43-
clearPendingFileDrop: () => {
44-
set({ pending: null });
57+
clearPendingFileDropsForThread: (threadRef) => {
58+
set((state) => ({
59+
pending: state.pending.filter((drop) => !isSameSidebarThreadRef(drop.threadRef, threadRef)),
60+
}));
4561
},
4662
consumePendingFileDrop: (threadRef) => {
47-
const pending = get().pending;
48-
if (pending === null || !isSameSidebarThreadRef(pending.threadRef, threadRef)) {
63+
const matches = get().pending.filter((drop) =>
64+
isSameSidebarThreadRef(drop.threadRef, threadRef),
65+
);
66+
if (matches.length === 0) {
4967
return null;
5068
}
51-
set({ pending: null });
52-
return pending.files;
69+
const matchedIds = new Set(matches.map((drop) => drop.id));
70+
set((state) => ({ pending: state.pending.filter((drop) => !matchedIds.has(drop.id)) }));
71+
return matches.flatMap((drop) => drop.files);
5372
},
5473
}),
5574
);

0 commit comments

Comments
 (0)