Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/mobile/lib/attachments/settle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 },
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, AttachmentUpload>;
}
Expand Down Expand Up @@ -76,12 +79,6 @@ export const useComposerDraftsStore = create<ComposerDraftsStore>()((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) =>
Expand All @@ -101,13 +98,17 @@ export const useComposerDraftsStore = create<ComposerDraftsStore>()((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) =>
Expand Down
Loading