Skip to content

Commit 05914c0

Browse files
fix(mobile): an attachment picked while drafts load stays in the visible draft
A picker or import still writing to the fresh draft now counts as the user working in it, so the unsent-draft resume no longer moves the composer off that draft and strands the attachment in a hidden one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6d368a4 commit 05914c0

4 files changed

Lines changed: 35 additions & 8 deletions

File tree

‎apps/mobile/src/features/threads/new-task-flow-provider.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import * as Arr from "effect/Array";
2222
import { pipe } from "effect/Function";
2323

2424
import { useProjects, useServerConfigs, useThreadShells } from "../../state/entities";
25-
import { composerDraftHasUserContent } from "../../state/pending-new-tasks-model";
2625
import type { TurnCommandMetadata } from "../../lib/commandMetadata";
2726
import type { DraftComposerAttachment } from "../../lib/composerImages";
2827
import type { ModelOption, ProviderGroup } from "../../lib/modelOptions";
@@ -49,6 +48,7 @@ import {
4948
createNewTaskDraft,
5049
findResumableNewTaskDraftKey,
5150
getComposerDraftSnapshot,
51+
isComposerDraftInUse,
5252
isNewTaskDraftKey,
5353
removeComposerDraftAttachment,
5454
replaceComposerDraftAttachments,
@@ -414,8 +414,8 @@ export function NewTaskFlowProvider(
414414
return;
415415
}
416416
if (!resumeUnsentDraft) return;
417-
// Text the user typed before drafts loaded wins over an older unsent draft.
418-
const unsentDraftKey = composerDraftHasUserContent(getComposerDraftSnapshot(activeDraftKey))
417+
// Work the user started before drafts loaded wins over an older unsent draft.
418+
const unsentDraftKey = isComposerDraftInUse(activeDraftKey)
419419
? null
420420
: findResumableNewTaskDraftKey(
421421
appAtomRegistry.get(composerDraftsAtom),

‎apps/mobile/src/state/composerDraftOperations.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export function startComposerDraftOperation(draftKey: string): {
2424
};
2525
}
2626

27+
export function hasComposerDraftOperations(draftKey: string): boolean {
28+
return operationsByDraftKey.has(draftKey);
29+
}
30+
2731
export function abortComposerDraftOperations(draftKey: string): void {
2832
const operations = operationsByDraftKey.get(draftKey);
2933
if (!operations) return;

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ import {
194194
type ComposerDraft,
195195
findNewTaskDraftKeys,
196196
findResumableNewTaskDraftKey,
197+
isComposerDraftInUse,
197198
flushComposerDrafts,
198199
getComposerDraftSnapshot,
199200
mergeComposerDraftContentState,
@@ -1993,6 +1994,22 @@ describe("mobile composer drafts", () => {
19931994
expect(findResumableNewTaskDraftKey(withoutFree, project, exclude)).toBeNull();
19941995
});
19951996

1997+
it("keeps a fresh draft the user is attaching to, so a resume cannot strand the attachment", () => {
1998+
const key = createNewTaskDraft({
1999+
environmentId: EnvironmentId.make("environment-1"),
2000+
projectId: ProjectId.make("project-1"),
2001+
});
2002+
expect(isComposerDraftInUse(key)).toBe(false);
2003+
2004+
const picker = startComposerDraftOperation(key);
2005+
expect(isComposerDraftInUse(key)).toBe(true);
2006+
picker.finish();
2007+
expect(isComposerDraftInUse(key)).toBe(false);
2008+
2009+
setComposerDraftText(key, "typed before drafts loaded");
2010+
expect(isComposerDraftInUse(key)).toBe(true);
2011+
});
2012+
19962013
it("merges persisted option memory without replacing newer choices", async () => {
19972014
composerDraftFileMocks.setDocument({
19982015
schemaVersion: 1,

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import { appAtomRegistry } from "./atom-registry";
5757
import {
5858
abortComposerDraftOperations,
5959
abortComposerDraftOperationsWhere,
60+
hasComposerDraftOperations,
6061
} from "./composerDraftOperations";
6162
import {
6263
isNewTaskDraftKey,
@@ -1931,6 +1932,10 @@ export function findNewTaskDraftKeys(
19311932
.map(([key]) => key);
19321933
}
19331934

1935+
function hasUserContent(draft: ComposerDraft): boolean {
1936+
return draft.text.trim().length > 0 || draft.attachments.length > 0;
1937+
}
1938+
19341939
export function findResumableNewTaskDraftKey(
19351940
drafts: Readonly<Record<string, ComposerDraft>>,
19361941
project: { readonly environmentId: EnvironmentId; readonly projectId: ProjectId },
@@ -1939,15 +1944,16 @@ export function findResumableNewTaskDraftKey(
19391944
return (
19401945
findNewTaskDraftKeys(drafts, project).find((key) => {
19411946
const draft = drafts[key];
1942-
return (
1943-
!exclude.has(key) &&
1944-
draft !== undefined &&
1945-
(draft.text.trim().length > 0 || draft.attachments.length > 0)
1946-
);
1947+
return !exclude.has(key) && draft !== undefined && hasUserContent(draft);
19471948
}) ?? null
19481949
);
19491950
}
19501951

1952+
/** Whether the user is already working in a draft: content, or a picker or import still writing to it. */
1953+
export function isComposerDraftInUse(draftKey: string): boolean {
1954+
return hasUserContent(getComposerDraftSnapshot(draftKey)) || hasComposerDraftOperations(draftKey);
1955+
}
1956+
19511957
export async function clearComposerDraftsEnvironment(environmentId: EnvironmentId): Promise<void> {
19521958
ensureComposerDraftsLoaded();
19531959
if (loadPromise !== null) {

0 commit comments

Comments
 (0)