Skip to content

Commit 7dc1e2b

Browse files
only21milclaude
andcommitted
fix(web): a draft thread with unsaved changes survives another tab discarding it
When another tab discarded a draft thread while this tab still held an unwritten edit or an image not yet saved, adoption removed the thread but kept the draft. The route lost its draft thread, the composer unmounted, and the draft was stored without its thread or, with only the image, lost. Adoption now keeps a draft thread whose draft this tab keeps, and drops it from the baseline so the next write puts the thread back. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent afaf6f4 commit 7dc1e2b

2 files changed

Lines changed: 63 additions & 11 deletions

File tree

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ describe("composerDraftStore context persistence", () => {
732732
});
733733
const read = () => storage.getItem(COMPOSER_DRAFT_STORAGE_KEY);
734734
const openTab = async () => {
735-
const page = new EventTarget();
735+
const page = Object.assign(new EventTarget(), { localStorage: storage });
736736
vi.stubGlobal("window", page);
737737
vi.resetModules();
738738
const store = (await import("./composerDraftStore")).useComposerDraftStore;
@@ -906,6 +906,51 @@ describe("composerDraftStore context persistence", () => {
906906
expect((await openTab()).store.getState().getDraftThread(draftId)?.branch).toBe("feature");
907907
expect(tabB.store.getState().getDraftThread(draftId)?.branch).toBe("feature");
908908
});
909+
910+
it("keeps a draft thread with unsaved changes that another tab discards", async () => {
911+
const { openTab } = openTabs();
912+
const tabA = await openTab();
913+
const tabB = await openTab();
914+
const imageDraft = DraftId.make("cross-tab-image-draft");
915+
const promptDraft = DraftId.make("cross-tab-prompt-draft");
916+
for (const draftId of [imageDraft, promptDraft]) {
917+
tabA.store
918+
.getState()
919+
.setProjectDraftThreadId(
920+
scopeProjectRef(TEST_ENVIRONMENT_ID, ProjectId.make(`${draftId}-project`)),
921+
draftId,
922+
{ environmentSelection: "auto", loadBalancedEnvironmentId: TEST_ENVIRONMENT_ID },
923+
);
924+
}
925+
tabA.write();
926+
tabB.receive();
927+
// The image is unsaved until its serialization finishes, the prompt until the next write.
928+
const image = makeImage({ id: "cross-tab-image", previewUrl: "blob:cross-tab" });
929+
tabA.store.getState().addImage(imageDraft, image);
930+
tabA.store.getState().setPrompt(promptDraft, "unwritten");
931+
tabB.store.getState().clearDraftThread(imageDraft);
932+
tabB.store.getState().clearDraftThread(promptDraft);
933+
tabB.write();
934+
tabA.receive();
935+
expect(tabA.store.getState().getDraftSession(imageDraft)).not.toBeNull();
936+
expect(tabA.store.getState().getDraftSession(promptDraft)).not.toBeNull();
937+
938+
await tabA.store.getState().syncPersistedAttachments(imageDraft, [
939+
{
940+
id: image.id,
941+
name: image.name,
942+
mimeType: image.mimeType,
943+
sizeBytes: image.sizeBytes,
944+
dataUrl: "data:image/png;base64,AQEBAQ==",
945+
},
946+
]);
947+
tabA.write();
948+
const reopened = (await openTab()).store.getState();
949+
expect(reopened.getDraftSession(imageDraft)).not.toBeNull();
950+
expect(reopened.getComposerDraft(imageDraft)?.images.map(({ id }) => id)).toEqual([image.id]);
951+
expect(reopened.getDraftSession(promptDraft)).not.toBeNull();
952+
expect(reopened.getComposerDraft(promptDraft)?.prompt).toBe("unwritten");
953+
});
909954
});
910955

911956
describe("composerDraftStore thread contexts", () => {

‎apps/web/src/composerDraftStore.ts‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,20 @@ function adoptStoredComposerDrafts(): void {
374374
);
375375
if (!stored || !seen || ids.size === 0) return;
376376
const keep = <T>(stored: T | undefined) => stored;
377+
const draftsByThreadKey = adoptStoredRecord(
378+
ids,
379+
"draftsByThreadKey",
380+
stored.draftsByThreadKey,
381+
state.draftsByThreadKey,
382+
adoptStoredDraft,
383+
);
384+
// A draft this tab keeps (an unwritten edit or an unsaved image) keeps its draft thread, and
385+
// the thread leaves the baseline so the next write puts it back.
386+
const keptThreadIds = Object.keys(draftsByThreadKey)
387+
.filter((key) => stored.draftThreadsByThreadKey[key] === undefined)
388+
.map((key) => entryId("draftThreadsByThreadKey", key))
389+
.filter((id) => ids.has(id));
390+
for (const id of keptThreadIds) ids.delete(id);
377391
composerDraftStore.setState({
378392
draftThreadsByThreadKey: adoptStoredRecord(
379393
ids,
@@ -406,20 +420,13 @@ function adoptStoredComposerDrafts(): void {
406420
...(ids.has(entryId("stickyActiveProvider")) && {
407421
stickyActiveProvider: stored.stickyActiveProvider ?? null,
408422
}),
409-
// Last: adopting a draft revokes the preview URLs it replaces.
410-
draftsByThreadKey: adoptStoredRecord(
411-
ids,
412-
"draftsByThreadKey",
413-
stored.draftsByThreadKey,
414-
state.draftsByThreadKey,
415-
adoptStoredDraft,
416-
),
423+
draftsByThreadKey,
417424
});
418425
const adoptedLocal = entriesOf(partializeComposerDraftStoreState(composerDraftStore.getState()));
419426
sync = {
420427
...sync,
421-
local: withEntries(sync.local, ids, adoptedLocal),
422-
stored: withEntries(sync.stored, ids, seen),
428+
local: withEntries(withEntries(sync.local, ids, adoptedLocal), keptThreadIds, seen),
429+
stored: withEntries(sync.stored, [...ids, ...keptThreadIds], seen),
423430
};
424431
}
425432

0 commit comments

Comments
 (0)