Skip to content

Commit d621d99

Browse files
committed
fix(web): SnapShot question pins are set before the read and in the question's commit
A capture the drain found was pinned only once it was read, so a question opening meanwhile could claim it, and the composer registered the open question in a passive effect, a frame after the question's UI changed.
1 parent a614327 commit d621d99

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2004,7 +2004,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
20042004
activePendingProgress?.activeQuestion?.allowCustomAnswer !== false &&
20052005
!activePendingIsResponding;
20062006
const questionRequest = pendingUserInputs[0];
2007-
useEffect(() => {
2007+
// A layout effect, so the registry changes in the same commit as the question's UI: a capture
2008+
// resolved between the paint and a passive effect would otherwise see the old question state.
2009+
useLayoutEffect(() => {
20082010
if (!questionAttachmentTarget || !questionRequest || !questionTakesSnapShots) return;
20092011
return trackOpenQuestionAttachmentDraft(routeThreadRef, {
20102012
draftId: questionAttachmentTarget,

‎apps/web/src/components/desktop/SnapShotCoordinator.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,28 @@ describe("open question delivery", () => {
374374
expect(acknowledgeSnapShot).toHaveBeenCalledExactlyOnceWith(capture.id);
375375
});
376376

377+
it("keeps a capture pinned before any question off a question that opens during the read", async () => {
378+
vi.stubGlobal("window", { localStorage: storage, dispatchEvent: vi.fn() });
379+
const pins = new Map();
380+
const resolveTarget = () => resolveSnapShotAttachmentTarget(pins, capture.id, threadRef);
381+
// The drain pins a capture it never saw requested before reading it.
382+
expect(resolveTarget()).toEqual(threadRef);
383+
let untrack = () => {};
384+
const { bridge, acknowledgeSnapShot } = makeBridge(async () => {
385+
untrack = trackOpenQuestionAttachmentDraft(threadRef, request);
386+
return capture;
387+
});
388+
try {
389+
await deliverSnapShot(bridge, capture, resolveTarget);
390+
const store = useComposerDraftStore.getState();
391+
expect(store.getComposerDraft(first)?.images ?? []).toHaveLength(0);
392+
expect(store.getComposerDraft(threadRef)?.images.map(({ id }) => id)).toEqual([capture.id]);
393+
expect(acknowledgeSnapShot).toHaveBeenCalledExactlyOnceWith(capture.id);
394+
} finally {
395+
untrack();
396+
}
397+
});
398+
377399
it("refuses a capture once the request's other questions hold the shared limit", async () => {
378400
vi.stubGlobal("window", { localStorage: storage, dispatchEvent: vi.fn() });
379401
useComposerDraftStore.getState().addImages(

‎apps/web/src/components/desktop/SnapShotCoordinator.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@ export function SnapShotCoordinator() {
351351
}
352352

353353
try {
354+
// A capture this coordinator never saw requested (a reload, a blurred window) is
355+
// pinned here, before the read, so a question that opens meanwhile cannot claim it.
356+
resolveSnapShotAttachmentTarget(questionPinsRef.current, item.id, target);
354357
await deliverSnapShot(bridge, item, () =>
355358
resolveSnapShotAttachmentTarget(questionPinsRef.current, item.id, target),
356359
);

0 commit comments

Comments
 (0)