GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(mobile): share-import cancel undoes only what the import merged · pingdotgg/t3code@33c080d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/mobile/src
    • features
      • sharing
        • IncomingShareProvider.tsx
      • threads
        • incomingShareImport.test.ts
        • incomingShareImport.ts
    • state
      • use-composer-drafts.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/mobile/src/features/sharing/IncomingShareProvider.tsx‎

Copy file name to clipboardExpand all lines: apps/mobile/src/features/sharing/IncomingShareProvider.tsx
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ import { serverEnvironment } from "../../state/server";
3535
import {
3636
getComposerDraftSnapshot,
3737
mergeComposerDraftContent,
38-
restoreComposerDraftSnapshot,
3938
scheduleUnusedComposerAttachmentCleanup,
39+
undoComposerDraftMerge,
4040
} from "../../state/use-composer-drafts";
4141
import {
4242
EMPTY_INCOMING_SHARE_PRESENTATION_STATE,
@@ -302,7 +302,7 @@ export const incomingShareImports = makeIncomingShareImports({
302302
release: releaseShareReservation,
303303
consume: consumeShare,
304304
readDraft: getComposerDraftSnapshot,
305-
restoreDraft: restoreComposerDraftSnapshot,
305+
undoMerge: undoComposerDraftMerge,
306306
mergeIntoDraft: mergeComposerDraftContent,
307307
selectAttachments: (share, environmentId) =>
308308
selectIncomingShareAttachmentsForServer({
Collapse file

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

Copy file name to clipboardExpand all lines: apps/mobile/src/features/threads/incomingShareImport.test.ts
+115-28Lines changed: 115 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { describe, expect, it, vi } from "vite-plus/test";
33

44
import type { DraftComposerAttachment } from "../../lib/composerImages";
55
import type { IncomingShareDraft } from "../sharing/incoming-share-model";
6-
import type { ComposerDraft } from "../../state/use-composer-drafts";
6+
import { undoComposerDraftMergeState, type ComposerDraft } from "../../state/use-composer-drafts";
77
import {
88
makeIncomingShareImports,
99
type ShareImportDeps,
@@ -13,7 +13,34 @@ import {
1313
} from "./incomingShareImport";
1414

1515
const destination = { environmentId: "env-1", projectId: "project-1" };
16-
const edited: ComposerDraft = { text: "typed after the restore", attachments: [] };
16+
const edited: ComposerDraft = { text: "typed after the undo", attachments: [] };
17+
18+
/** The draft store's own undo, applied to the test's draft map. */
19+
function undoInto(
20+
drafts: Map<string, ComposerDraft>,
21+
draftKey: string,
22+
snapshot: ComposerDraft,
23+
merged: ComposerDraft,
24+
): void {
25+
const next = undoComposerDraftMergeState(Object.fromEntries(drafts), draftKey, snapshot, merged);
26+
drafts.clear();
27+
for (const [key, draft] of Object.entries(next)) drafts.set(key, draft);
28+
}
29+
30+
function mergeInto(
31+
drafts: Map<string, ComposerDraft>,
32+
draftKey: string,
33+
content: {
34+
readonly text: string;
35+
readonly attachments: ReadonlyArray<DraftComposerAttachment>;
36+
},
37+
): void {
38+
const existing = drafts.get(draftKey) ?? { text: "", attachments: [] };
39+
drafts.set(draftKey, {
40+
text: `${existing.text}\n${content.text}`,
41+
attachments: [...existing.attachments, ...content.attachments],
42+
});
43+
}
1744

1845
function fileAttachment(id: string): DraftComposerAttachment {
1946
return {
@@ -66,17 +93,13 @@ function setup(overrides: Partial<ShareImportDeps> = {}) {
6693
calls.push("consume");
6794
}),
6895
readDraft: (draftKey) => drafts.get(draftKey) ?? { text: "", attachments: [] },
69-
restoreDraft: vi.fn(async (draftKey, snapshot) => {
70-
calls.push("restore");
71-
drafts.set(draftKey, snapshot);
96+
undoMerge: vi.fn(async (draftKey, snapshot, merged) => {
97+
calls.push("undo");
98+
undoInto(drafts, draftKey, snapshot, merged);
7299
}),
73100
mergeIntoDraft: vi.fn(async (draftKey, content) => {
74101
calls.push("merge");
75-
const existing = drafts.get(draftKey) ?? { text: "", attachments: [] };
76-
drafts.set(draftKey, {
77-
text: `${existing.text}\n${content.text}`,
78-
attachments: [...existing.attachments, ...content.attachments],
79-
});
102+
mergeInto(drafts, draftKey, content);
80103
return { skippedAttachmentCount: 0 };
81104
}),
82105
selectAttachments: (incoming) => ({
@@ -170,7 +193,7 @@ describe("incoming share imports", () => {
170193
await imports.cancel(target);
171194

172195
expect(watched.phase()).toBe("cancelled");
173-
expect(calls).toEqual(["reserve", "merge", "restore", "release"]);
196+
expect(calls).toEqual(["reserve", "merge", "undo", "release"]);
174197
expect(drafts.get("draft-a")).toEqual({ text: "mine", attachments: [] });
175198
// Unconsumed: the inbox still owns the files.
176199
expect(deps.releaseUnusedAttachments).not.toHaveBeenCalled();
@@ -200,7 +223,7 @@ describe("incoming share imports", () => {
200223

201224
await imports.cancel(target);
202225
expect(watched.phase()).toBe("cancelled");
203-
expect(deps.restoreDraft).toHaveBeenCalledTimes(1);
226+
expect(deps.undoMerge).toHaveBeenCalledTimes(1);
204227
expect(drafts.get("draft-a")).toEqual({ text: "mine", attachments: [] });
205228
expect(deps.leave).toHaveBeenCalledTimes(1);
206229
});
@@ -227,23 +250,27 @@ describe("incoming share imports", () => {
227250
watched.unmount();
228251
});
229252

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;
253+
const userChanges = [
254+
["edited", (drafts: Map<string, ComposerDraft>) => drafts.set("draft-a", edited), edited],
255+
["deleted", (drafts: Map<string, ComposerDraft>) => drafts.delete("draft-a"), undefined],
256+
] as const;
257+
258+
it.each(userChanges)(
259+
"a retried cancel keeps a draft the user %s after the undo was shown",
260+
async (_change, change, expected) => {
261+
let undoAttempts = 0;
237262
const { deps, drafts, watch, imports } = setup({
238263
consume: vi.fn(async () => {
239264
throw new Error("disk full");
240265
}),
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-
}),
266+
// Like undoComposerDraftMerge: publishes, then fails to persist.
267+
undoMerge: vi.fn(
268+
async (draftKey: string, snapshot: ComposerDraft, merged: ComposerDraft) => {
269+
undoAttempts += 1;
270+
undoInto(drafts, draftKey, snapshot, merged);
271+
if (undoAttempts === 1) throw new Error("write failed");
272+
},
273+
),
247274
});
248275
const target = importTarget();
249276
const watched = watch(target);
@@ -252,15 +279,75 @@ describe("incoming share imports", () => {
252279
expect(deps.onCancelFailure).toHaveBeenCalledWith(target, new Error("write failed"));
253280

254281
change(drafts);
255-
const expected = drafts.get("draft-a");
256282
await imports.cancel(target);
257283

258284
expect(watched.phase()).toBe("cancelled");
259-
expect(drafts.get("draft-a")).toBe(expected);
285+
expect(drafts.get("draft-a")).toEqual(expected);
260286
expect(deps.release).toHaveBeenCalledTimes(1);
261287
},
262288
);
263289

290+
it("cancel undoes the import after an upload finished on its attachment", async () => {
291+
const file = fileAttachment("file");
292+
const { drafts, inbox, watch, imports } = setup({
293+
consume: vi.fn(async () => {
294+
throw new Error("disk full");
295+
}),
296+
});
297+
inbox.set("share-1", share([file]));
298+
const target = importTarget();
299+
watch(target);
300+
await imports.settled(target);
301+
302+
const merged = drafts.get("draft-a");
303+
drafts.set("draft-a", {
304+
text: merged?.text ?? "",
305+
attachments: [{ ...file, uploadedAttachmentId: "uploaded-1" }],
306+
});
307+
await imports.cancel(target);
308+
309+
expect(drafts.get("draft-a")).toEqual({ text: "mine", attachments: [] });
310+
});
311+
312+
it.each(userChanges)(
313+
"cancel keeps a draft the user %s after a merge that published but failed to persist",
314+
async (_change, change, expected) => {
315+
const { drafts, watch, imports } = setup({
316+
// Like mergeComposerDraftContent: publishes, then fails to persist.
317+
mergeIntoDraft: vi.fn(async (draftKey, content) => {
318+
mergeInto(drafts, draftKey, content);
319+
throw new Error("write failed");
320+
}),
321+
});
322+
const target = importTarget();
323+
const watched = watch(target);
324+
await imports.settled(target);
325+
expect(watched.phase()).toBe("failed");
326+
327+
change(drafts);
328+
await imports.cancel(target);
329+
330+
expect(watched.phase()).toBe("cancelled");
331+
expect(drafts.get("draft-a")).toEqual(expected);
332+
},
333+
);
334+
335+
it("cancel after a merge that published but failed to persist removes the merged share", async () => {
336+
const { drafts, watch, imports } = setup({
337+
mergeIntoDraft: vi.fn(async (draftKey, content) => {
338+
mergeInto(drafts, draftKey, content);
339+
throw new Error("write failed");
340+
}),
341+
});
342+
const target = importTarget();
343+
watch(target);
344+
await imports.settled(target);
345+
346+
await imports.cancel(target);
347+
348+
expect(drafts.get("draft-a")).toEqual({ text: "mine", attachments: [] });
349+
});
350+
264351
it("a failed import keeps its cancel after its route left, and reports a failed undo", async () => {
265352
const { calls, deps, imports, watch } = setup({
266353
consume: vi.fn(async () => {
@@ -279,7 +366,7 @@ describe("incoming share imports", () => {
279366

280367
await imports.cancel(target);
281368

282-
expect(calls).toEqual(["reserve", "merge", "restore"]);
369+
expect(calls).toEqual(["reserve", "merge", "undo"]);
283370
expect(deps.onCancelFailure).toHaveBeenCalledWith(target, new Error("locked"));
284371
expect(deps.leave).not.toHaveBeenCalled();
285372
});
Collapse file

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

Copy file name to clipboardExpand all lines: apps/mobile/src/features/threads/incomingShareImport.ts
+31-29Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,15 @@ export interface ShareImportDeps {
7575
readonly release: (shareId: string, destination: IncomingShareDestination) => Promise<void>;
7676
readonly consume: (shareId: string) => Promise<void>;
7777
readonly readDraft: (draftKey: string) => ComposerDraft;
78-
readonly restoreDraft: (draftKey: string, snapshot: ComposerDraft) => Promise<void>;
78+
/**
79+
* Restores `snapshot` if the draft still equals `merged`; otherwise removes only
80+
* what the merge added, keeping later edits.
81+
*/
82+
readonly undoMerge: (
83+
draftKey: string,
84+
snapshot: ComposerDraft,
85+
merged: ComposerDraft,
86+
) => Promise<void>;
7987
readonly mergeIntoDraft: (
8088
draftKey: string,
8189
content: {
@@ -109,22 +117,14 @@ interface Entry {
109117
run: Promise<void>;
110118
/** The draft before the first attempt merged into it; cancel restores it. */
111119
backup: ComposerDraft | null;
112-
/** The draft as the last merge left it; cancel only restores over this. */
120+
/** The draft as the last merge published it; cancel undoes against it. */
113121
merged: ComposerDraft | null;
114122
reservedFor: IncomingShareDestination | null;
115123
consumed: boolean;
116124
needsRestore: boolean;
117125
readonly listeners: Set<() => void>;
118126
}
119127

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-
128128
function keyOf(target: ShareImportTarget): string {
129129
return JSON.stringify([
130130
target.flowKey,
@@ -216,13 +216,18 @@ export function makeIncomingShareImports(deps: ShareImportDeps) {
216216
throw new Error("Server attachment support is still loading.");
217217
}
218218
entry.needsRestore = true;
219-
entry.merged = null;
220-
const { skippedAttachmentCount } = await deps.mergeIntoDraft(draftKey, {
221-
text: share.text,
222-
attachments: selected.attachments,
223-
sourceShareId: shareId,
224-
});
225-
entry.merged = deps.readDraft(draftKey);
219+
let skippedAttachmentCount: number;
220+
try {
221+
({ skippedAttachmentCount } = await deps.mergeIntoDraft(draftKey, {
222+
text: share.text,
223+
attachments: selected.attachments,
224+
sourceShareId: shareId,
225+
}));
226+
} finally {
227+
// The merge publishes before it persists, so read it back even when
228+
// persisting rejects.
229+
entry.merged = deps.readDraft(draftKey);
230+
}
226231
if (stopped()) return;
227232
advance(entry, "attached");
228233
await deps.consume(shareId);
@@ -316,19 +321,16 @@ export function makeIncomingShareImports(deps: ShareImportDeps) {
316321
// durable writes this import made.
317322
try {
318323
if (entry.needsRestore && 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-
}
324+
// The draft can change while a failed import waits for an answer, by the
325+
// user or an upload finishing, and a retried undo runs after its own
326+
// publish. Undoing against the merged draft keeps those changes.
327+
const merged = entry.merged ?? entry.backup;
328+
await deps.undoMerge(draftKey, entry.backup, merged);
331329
entry.needsRestore = false;
330+
// Sweep the share's merged-in files only once the inbox entry was
331+
// consumed: before that, the inbox still owns them for a later import.
332+
// The sweep skips files a draft still holds.
333+
if (entry.consumed) deps.releaseUnusedAttachments(merged.attachments);
332334
}
333335
if (entry.reservedFor) {
334336
await deps.release(shareId, entry.reservedFor);
Collapse file

‎apps/mobile/src/state/use-composer-drafts.ts‎

Copy file name to clipboardExpand all lines: apps/mobile/src/state/use-composer-drafts.ts
-23Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,29 +1716,6 @@ export async function mergeComposerDraftContent(
17161716
return { skippedAttachmentCount };
17171717
}
17181718

1719-
export async function restoreComposerDraftSnapshot(
1720-
draftKey: string,
1721-
snapshot: ComposerDraft,
1722-
): Promise<void> {
1723-
ensureComposerDraftsLoaded();
1724-
if (loadPromise !== null) {
1725-
await loadPromise;
1726-
}
1727-
if (persistTimer !== null) {
1728-
clearTimeout(persistTimer);
1729-
persistTimer = null;
1730-
}
1731-
const next = restoreComposerDraftSnapshotState(
1732-
appAtomRegistry.get(composerDraftsAtom),
1733-
draftKey,
1734-
snapshot,
1735-
);
1736-
appAtomRegistry.set(composerDraftsAtom, next);
1737-
await persistenceQueue.run(() =>
1738-
writePersistedComposerState(next, appAtomRegistry.get(stickyComposerModelSelectionAtom)),
1739-
);
1740-
}
1741-
17421719
export function sameComposerDraftState(a: ComposerDraft, b: ComposerDraft): boolean {
17431720
return (
17441721
a.text === b.text &&

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.