Skip to content

Commit d294836

Browse files
fix(mobile): share import keeps the new-task composer locked until it finishes
The new-task screen read whether a share import was running from the phase of the target it rendered. When that target went null mid-import (server config dropped on disconnect, or the selected project stopped matching the route's project), Start unlocked while the import kept merging into the draft. The share imports now track running attempts per draft route, abandoned ones included, and the screen locks on that. The machine tests await each attempt's run promise or the Deferred that marks the step, instead of polling with vi.waitFor. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 88f2836 commit d294836

3 files changed

Lines changed: 88 additions & 23 deletions

File tree

‎apps/mobile/src/features/threads/NewTaskDraftScreen.tsx‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,7 @@ import {
8888
resolveNewTaskWorkspaceLabel,
8989
} from "./new-task-context-presentation";
9090
import { incomingShareImports, useIncomingShare } from "../sharing/IncomingShareProvider";
91-
import {
92-
idleImportedShareAtom,
93-
idleShareImportPhaseAtom,
94-
isShareImportInFlight,
95-
} from "./incomingShareImport";
91+
import { idleShareFlagAtom, idleShareImportPhaseAtom } from "./incomingShareImport";
9692
import { newTaskFlowKey } from "./new-task-flow-state";
9793
import { appAtomRegistry } from "../../state/atom-registry";
9894

@@ -282,7 +278,7 @@ export function NewTaskDraftScreen(props: {
282278
const routeImportedShare = useAtomValue(
283279
flowKey !== undefined && props.incomingShareId
284280
? incomingShareImports.importedAtom(flowKey, props.incomingShareId)
285-
: idleImportedShareAtom,
281+
: idleShareFlagAtom,
286282
);
287283
const hasImportedIncomingShare = Boolean(
288284
props.incomingShareId &&
@@ -325,7 +321,11 @@ export function NewTaskDraftScreen(props: {
325321
? idleShareImportPhaseAtom
326322
: incomingShareImports.phaseAtom(shareImportTarget),
327323
);
328-
const isImportingShare = isShareImportInFlight(shareImportPhase);
324+
// Locks on the route, not the target: the target turns null when server config
325+
// drops or the project stops matching, while the import keeps writing the draft.
326+
const isImportingShare = useAtomValue(
327+
flowKey === undefined ? idleShareFlagAtom : incomingShareImports.inFlightAtom(flowKey),
328+
);
329329
const isIncomingShareTransferPending = Boolean(
330330
incomingShare && shareImportPhase !== "cancelled" && !isIncomingShareAwaitingServerConfig,
331331
);

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

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,16 @@ describe("incoming share imports", () => {
114114
it("reserves, attaches and consumes once, releasing files the draft did not keep", async () => {
115115
const kept = fileAttachment("kept");
116116
const skipped = fileAttachment("skipped");
117-
const { deps, calls, drafts, inbox, watch } = setup({
117+
const { deps, calls, drafts, inbox, imports, watch } = setup({
118118
selectAttachments: () => ({ status: "ready", attachments: [kept], warnings: ["too big"] }),
119119
});
120120
inbox.set("share-1", share([kept, skipped]));
121-
const watched = watch(importTarget());
121+
const target = importTarget();
122+
const watched = watch(target);
122123

123124
expect(watched.phase()).toBe("reserving");
124-
await vi.waitFor(() => expect(watched.phase()).toBe("done"));
125+
await imports.settled(target);
126+
expect(watched.phase()).toBe("done");
125127

126128
expect(calls).toEqual(["reserve", "merge", "consume"]);
127129
expect(drafts.get("draft-a")?.attachments).toEqual([kept]);
@@ -144,7 +146,8 @@ describe("incoming share imports", () => {
144146
tasks.splice(0).forEach((task) => task());
145147
expect(registry.getNodes().size).toBe(0);
146148
const unmount = registry.mount(atom);
147-
await vi.waitFor(() => expect(registry.get(atom)).toBe("done"));
149+
await imports.settled(importTarget());
150+
expect(registry.get(atom)).toBe("done");
148151
unmount();
149152

150153
expect(deps.reserve).toHaveBeenCalledTimes(1);
@@ -159,7 +162,8 @@ describe("incoming share imports", () => {
159162
});
160163
const target = importTarget();
161164
const watched = watch(target);
162-
await vi.waitFor(() => expect(watched.phase()).toBe("failed"));
165+
await imports.settled(target);
166+
expect(watched.phase()).toBe("failed");
163167
expect(deps.onImportFailure).toHaveBeenCalledWith(target, new Error("disk full"));
164168

165169
await imports.cancel(target);
@@ -185,7 +189,8 @@ describe("incoming share imports", () => {
185189
});
186190
const target = importTarget();
187191
const watched = watch(target);
188-
await vi.waitFor(() => expect(watched.phase()).toBe("failed"));
192+
await imports.settled(target);
193+
expect(watched.phase()).toBe("failed");
189194

190195
await imports.cancel(target);
191196
expect(watched.phase()).toBe("failed");
@@ -200,10 +205,17 @@ describe("incoming share imports", () => {
200205
});
201206

202207
it("stops before consuming once its route leaves the stack", async () => {
208+
const mergeStarted = Promise.withResolvers<void>();
203209
const merged = Promise.withResolvers<{ readonly skippedAttachmentCount: number }>();
204-
const { deps, imports, watch } = setup({ mergeIntoDraft: () => merged.promise });
210+
const { deps, imports, watch } = setup({
211+
mergeIntoDraft: () => {
212+
mergeStarted.resolve();
213+
return merged.promise;
214+
},
215+
});
205216
const watched = watch(importTarget());
206-
await vi.waitFor(() => expect(watched.phase()).toBe("attaching"));
217+
await mergeStarted.promise;
218+
expect(watched.phase()).toBe("attaching");
207219

208220
const pruned = imports.prune(new Set(["flow-b"]));
209221
merged.resolve({ skippedAttachmentCount: 0 });
@@ -225,7 +237,8 @@ describe("incoming share imports", () => {
225237
});
226238
const target = importTarget();
227239
const watched = watch(target);
228-
await vi.waitFor(() => expect(watched.phase()).toBe("failed"));
240+
await imports.settled(target);
241+
expect(watched.phase()).toBe("failed");
229242
watched.unmount();
230243
await imports.prune(new Set());
231244

@@ -238,15 +251,41 @@ describe("incoming share imports", () => {
238251

239252
it("a route switching drafts abandons the in-flight import", async () => {
240253
const reserved = Promise.withResolvers<void>();
241-
const { deps, watch } = setup({
254+
const { deps, imports, watch } = setup({
242255
reserve: vi.fn().mockReturnValueOnce(reserved.promise).mockResolvedValue(undefined),
243256
});
244257
watch(importTarget()).unmount();
245-
const second = watch(importTarget({ draftKey: "draft-b" }));
258+
const secondTarget = importTarget({ draftKey: "draft-b" });
259+
const second = watch(secondTarget);
246260
reserved.resolve();
247261

248-
await vi.waitFor(() => expect(second.phase()).toBe("done"));
262+
await imports.settled(secondTarget);
263+
expect(second.phase()).toBe("done");
249264
expect(deps.mergeIntoDraft).toHaveBeenCalledTimes(1);
250265
expect(deps.mergeIntoDraft).toHaveBeenCalledWith("draft-b", expect.anything());
251266
});
267+
268+
it("stays in flight for its route after the screen stops rendering its target", async () => {
269+
const mergeStarted = Promise.withResolvers<void>();
270+
const merged = Promise.withResolvers<{ readonly skippedAttachmentCount: number }>();
271+
const { imports, registry, watch } = setup({
272+
mergeIntoDraft: () => {
273+
mergeStarted.resolve();
274+
return merged.promise;
275+
},
276+
});
277+
const inFlight = imports.inFlightAtom("flow-a");
278+
const unmountInFlight = registry.mount(inFlight);
279+
const target = importTarget();
280+
const watched = watch(target);
281+
await mergeStarted.promise;
282+
283+
watched.unmount();
284+
expect(registry.get(inFlight)).toBe(true);
285+
286+
merged.resolve({ skippedAttachmentCount: 0 });
287+
await imports.settled(target);
288+
expect(registry.get(inFlight)).toBe(false);
289+
unmountInFlight();
290+
});
252291
});

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function transitionShareImport(
5050
return TRANSITIONS[phase][event] ?? phase;
5151
}
5252

53-
export function isShareImportInFlight(phase: ShareImportPhase): boolean {
53+
function isShareImportInFlight(phase: ShareImportPhase): boolean {
5454
return phase === "reserving" || phase === "attaching" || phase === "consuming";
5555
}
5656

@@ -153,6 +153,17 @@ export function makeIncomingShareImports(deps: ShareImportDeps) {
153153
),
154154
).pipe(Atom.withLabel("mobile:incoming-share-imported:route")),
155155
);
156+
// One flow key per running attempt, abandoned ones included: an abandoned
157+
// attempt can still be inside a draft write when the screen's target goes away.
158+
const runningAtom = Atom.make<ReadonlyArray<string>>([]).pipe(
159+
Atom.keepAlive,
160+
Atom.withLabel("mobile:incoming-share-import-running"),
161+
);
162+
const isRunningAtom = Atom.family((flowKey: string) =>
163+
Atom.make((get) => get(runningAtom).includes(flowKey)).pipe(
164+
Atom.withLabel("mobile:incoming-share-import-running:route"),
165+
),
166+
);
156167

157168
function notify(entry: Entry): void {
158169
// Listeners rebuild their node, which re-registers them; iterate a copy.
@@ -238,7 +249,13 @@ export function makeIncomingShareImports(deps: ShareImportDeps) {
238249
function begin(key: string, entry: Entry): void {
239250
entry.backup ??= deps.readDraft(entry.target.draftKey);
240251
advance(entry, "start");
241-
entry.run = runImport(key, entry);
252+
const { flowKey } = entry.target;
253+
deps.registry.set(runningAtom, [...deps.registry.get(runningAtom), flowKey]);
254+
entry.run = runImport(key, entry).finally(() => {
255+
const running = [...deps.registry.get(runningAtom)];
256+
running.splice(running.indexOf(flowKey), 1);
257+
deps.registry.set(runningAtom, running);
258+
});
242259
}
243260

244261
function abandon(key: string, entry: Entry): void {
@@ -317,6 +334,15 @@ export function makeIncomingShareImports(deps: ShareImportDeps) {
317334
return entry.phase;
318335
}).pipe(Atom.withLabel("mobile:incoming-share-import")),
319336
),
337+
/**
338+
* Whether an import for this route is still running, including one whose
339+
* target the screen no longer renders.
340+
*/
341+
inFlightAtom: isRunningAtom,
342+
/** Resolves once the target's current attempt stops. */
343+
settled(target: ShareImportTarget): Promise<void> {
344+
return entries.get(keyOf(target))?.run ?? Promise.resolve();
345+
},
320346
/** Whether this route already moved the share into one of its drafts. */
321347
importedAtom(flowKey: string, shareId: string) {
322348
return isImportedAtom(JSON.stringify([flowKey, shareId]));
@@ -356,6 +382,6 @@ export const idleShareImportPhaseAtom = Atom.make<ShareImportPhase>("idle").pipe
356382
Atom.withLabel("mobile:incoming-share-import:idle"),
357383
);
358384

359-
export const idleImportedShareAtom = Atom.make(false).pipe(
360-
Atom.withLabel("mobile:incoming-share-imported:idle"),
385+
export const idleShareFlagAtom = Atom.make(false).pipe(
386+
Atom.withLabel("mobile:incoming-share-flag:idle"),
361387
);

0 commit comments

Comments
 (0)