Skip to content

Commit 6292d3b

Browse files
fix(mobile): a retried share-import cancel keeps later draft edits
restoreDraft shows the backup before it persists it. When persisting failed, the import still needed a restore, so retrying the cancel put the backup back over whatever the draft held by then: an edit made after the restore appeared was lost, and a deleted draft came back. A cancel now restores only over the draft the import's merge left, or re-persists a backup that is already showing. Any other draft is left as the user left it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d294836 commit 6292d3b

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

‎apps/mobile/src/features/threads/incomingShareImport.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from "./incomingShareImport";
1414

1515
const destination = { environmentId: "env-1", projectId: "project-1" };
16+
const edited: ComposerDraft = { text: "typed after the restore", attachments: [] };
1617

1718
function fileAttachment(id: string): DraftComposerAttachment {
1819
return {
@@ -226,6 +227,40 @@ describe("incoming share imports", () => {
226227
watched.unmount();
227228
});
228229

230+
it.each([
231+
["edited", (drafts: Map<string, ComposerDraft>) => drafts.set("draft-a", edited)],
232+
["deleted", (drafts: Map<string, ComposerDraft>) => drafts.delete("draft-a")],
233+
] as const)(
234+
"a retried cancel keeps a draft the user %s after the restore was shown",
235+
async (_change, change) => {
236+
let restoreAttempts = 0;
237+
const { deps, drafts, watch, imports } = setup({
238+
consume: vi.fn(async () => {
239+
throw new Error("disk full");
240+
}),
241+
// Like restoreComposerDraftSnapshot: publishes, then fails to persist.
242+
restoreDraft: vi.fn(async (draftKey: string, snapshot: ComposerDraft) => {
243+
restoreAttempts += 1;
244+
drafts.set(draftKey, snapshot);
245+
if (restoreAttempts === 1) throw new Error("write failed");
246+
}),
247+
});
248+
const target = importTarget();
249+
const watched = watch(target);
250+
await imports.settled(target);
251+
await imports.cancel(target);
252+
expect(deps.onCancelFailure).toHaveBeenCalledWith(target, new Error("write failed"));
253+
254+
change(drafts);
255+
const expected = drafts.get("draft-a");
256+
await imports.cancel(target);
257+
258+
expect(watched.phase()).toBe("cancelled");
259+
expect(drafts.get("draft-a")).toBe(expected);
260+
expect(deps.release).toHaveBeenCalledTimes(1);
261+
},
262+
);
263+
229264
it("a failed import keeps its cancel after its route left, and reports a failed undo", async () => {
230265
const { calls, deps, imports, watch } = setup({
231266
consume: vi.fn(async () => {

‎apps/mobile/src/features/threads/incomingShareImport.ts‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,22 @@ interface Entry {
109109
run: Promise<void>;
110110
/** The draft before the first attempt merged into it; cancel restores it. */
111111
backup: ComposerDraft | null;
112+
/** The draft as the last merge left it; cancel only restores over this. */
113+
merged: ComposerDraft | null;
112114
reservedFor: IncomingShareDestination | null;
113115
consumed: boolean;
114116
needsRestore: boolean;
115117
readonly listeners: Set<() => void>;
116118
}
117119

120+
/** Drafts are replaced, never mutated, so field identity means unchanged. */
121+
function sameDraft(a: ComposerDraft, b: ComposerDraft): boolean {
122+
const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
123+
return Array.from(keys).every(
124+
(key) => a[key as keyof ComposerDraft] === b[key as keyof ComposerDraft],
125+
);
126+
}
127+
118128
function keyOf(target: ShareImportTarget): string {
119129
return JSON.stringify([
120130
target.flowKey,
@@ -206,11 +216,13 @@ export function makeIncomingShareImports(deps: ShareImportDeps) {
206216
throw new Error("Server attachment support is still loading.");
207217
}
208218
entry.needsRestore = true;
219+
entry.merged = null;
209220
const { skippedAttachmentCount } = await deps.mergeIntoDraft(draftKey, {
210221
text: share.text,
211222
attachments: selected.attachments,
212223
sourceShareId: shareId,
213224
});
225+
entry.merged = deps.readDraft(draftKey);
214226
if (stopped()) return;
215227
advance(entry, "attached");
216228
await deps.consume(shareId);
@@ -283,6 +295,7 @@ export function makeIncomingShareImports(deps: ShareImportDeps) {
283295
detached: false,
284296
run: Promise.resolve(),
285297
backup: null,
298+
merged: null,
286299
reservedFor: null,
287300
consumed: false,
288301
needsRestore: false,
@@ -303,13 +316,19 @@ export function makeIncomingShareImports(deps: ShareImportDeps) {
303316
// durable writes this import made.
304317
try {
305318
if (entry.needsRestore && entry.backup) {
306-
// The restore drops the share's merged-in attachments from the draft.
307-
// Sweep them only when the inbox entry was consumed: before that, the
308-
// inbox still references these files and keeps them for a later import.
309-
const mergedAttachments = deps.readDraft(draftKey).attachments;
310-
await deps.restoreDraft(draftKey, entry.backup);
319+
const current = deps.readDraft(draftKey);
320+
const merged = entry.merged ?? current;
321+
// restoreDraft shows the backup before persisting it, so a retry after a
322+
// failed restore may find the backup already shown: persist it again.
323+
// Anything else is an edit or deletion made since; leave it.
324+
if (sameDraft(current, merged) || sameDraft(current, entry.backup)) {
325+
await deps.restoreDraft(draftKey, entry.backup);
326+
// The restore drops the share's merged-in attachments from the draft.
327+
// Sweep them only when the inbox entry was consumed: before that, the
328+
// inbox still references these files and keeps them for a later import.
329+
if (entry.consumed) deps.releaseUnusedAttachments(merged.attachments);
330+
}
311331
entry.needsRestore = false;
312-
if (entry.consumed) deps.releaseUnusedAttachments(mergedAttachments);
313332
}
314333
if (entry.reservedFor) {
315334
await deps.release(shareId, entry.reservedFor);

0 commit comments

Comments
 (0)