diff --git a/apps/mobile/lib/attachments/settle.test.ts b/apps/mobile/lib/attachments/settle.test.ts index 36a6301922e..2f7b0915e9c 100644 --- a/apps/mobile/lib/attachments/settle.test.ts +++ b/apps/mobile/lib/attachments/settle.test.ts @@ -5,10 +5,12 @@ import { waitForSettledUploads } from "./settle"; const KEY = "workspace:test"; const attach = (...ids: string[]) => { - useComposerDraftsStore.getState().addAttachments( + const store = useComposerDraftsStore.getState(); + store.addAttachments( KEY, ids.map((id) => ({ id, type: "file" as const, uri: `file:///${id}` })), ); + for (const id of ids) store.beginUpload(KEY, id); }; beforeEach(() => { diff --git a/apps/mobile/screens/(authenticated)/stores/composerDraftsStore/composerDraftsStore.test.ts b/apps/mobile/screens/(authenticated)/stores/composerDraftsStore/composerDraftsStore.test.ts new file mode 100644 index 00000000000..e5ff0ae9835 --- /dev/null +++ b/apps/mobile/screens/(authenticated)/stores/composerDraftsStore/composerDraftsStore.test.ts @@ -0,0 +1,35 @@ +import { beforeEach, describe, expect, test } from "bun:test"; +import { useComposerDraftsStore } from "./composerDraftsStore"; + +const KEY = "workspace:test"; + +const uploads = () => + useComposerDraftsStore.getState().draftsByKey[KEY]?.uploads; + +beforeEach(() => { + useComposerDraftsStore.setState({ draftsByKey: {} }); +}); + +describe("composerDraftsStore uploads", () => { + // SUPER-3442: a host workspace never uploads at attach time, so a seeded + // entry stayed at 0% and the composer refused to send. + test("adding an attachment records no upload until one begins", () => { + useComposerDraftsStore + .getState() + .addAttachments(KEY, [{ id: "a", type: "image", uri: "file:///a.jpg" }]); + + expect(uploads()).toEqual({}); + }); + + test("beginUpload starts tracking an attachment the draft holds", () => { + const store = useComposerDraftsStore.getState(); + store.addAttachments(KEY, [ + { id: "a", type: "image", uri: "file:///a.jpg" }, + ]); + store.beginUpload(KEY, "a"); + + expect(uploads()).toEqual({ + a: { progress: 0, fileId: undefined, error: undefined }, + }); + }); +}); diff --git a/apps/mobile/screens/(authenticated)/stores/composerDraftsStore/composerDraftsStore.ts b/apps/mobile/screens/(authenticated)/stores/composerDraftsStore/composerDraftsStore.ts index fd5c71e03c0..3495eb9c72b 100644 --- a/apps/mobile/screens/(authenticated)/stores/composerDraftsStore/composerDraftsStore.ts +++ b/apps/mobile/screens/(authenticated)/stores/composerDraftsStore/composerDraftsStore.ts @@ -24,6 +24,9 @@ export interface ComposerDraft { * so a progress tick leaves the `attachments` array identity alone — it * crosses into SwiftUI as a prop, and re-creating it sixty times a second * would rebuild the whole tray. + * + * Only `beginUpload` adds an entry. A host draft uploads nothing, and an + * entry that nothing settles keeps the composer's Send disabled forever. */ uploads: Record; } @@ -76,12 +79,6 @@ export const useComposerDraftsStore = create()((set) => ({ update(state, key, (draft) => ({ ...draft, attachments: [...draft.attachments, ...items], - uploads: { - ...draft.uploads, - ...Object.fromEntries( - items.map((item) => [item.id, { progress: 0 }]), - ), - }, })), ), removeAttachment: (key, id) => @@ -101,13 +98,17 @@ export const useComposerDraftsStore = create()((set) => ({ return { draftsByKey: rest }; }), beginUpload: (key, id) => - set((state) => - patchUpload(state, key, id, { - progress: 0, - fileId: undefined, - error: undefined, - }), - ), + set((state) => { + const draft = state.draftsByKey[key]; + if (!draft?.attachments.some((item) => item.id === id)) return state; + return update(state, key, (current) => ({ + ...current, + uploads: { + ...current.uploads, + [id]: { progress: 0, fileId: undefined, error: undefined }, + }, + })); + }), setUploadProgress: (key, id, progress) => set((state) => patchUpload(state, key, id, { progress })), finishUpload: (key, id, fileId) =>