Skip to content

Commit 7b4c4b4

Browse files
refactor(web): the upload queue alone records finished file uploads on drafts (pingdotgg#26050)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 03186e0 commit 7b4c4b4

3 files changed

Lines changed: 33 additions & 34 deletions

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
811811
const removeComposerDraftImage = useComposerDraftStore((store) => store.removeImage);
812812
const addComposerDraftFiles = useComposerDraftStore((store) => store.addFiles);
813813
const removeComposerDraftFile = useComposerDraftStore((store) => store.removeFile);
814-
const setComposerDraftFileUpload = useComposerDraftStore((store) => store.setFileUpload);
815814
const insertComposerDraftTerminalContext = useComposerDraftStore(
816815
(store) => store.insertTerminalContext,
817816
);
@@ -880,37 +879,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
880879
supportsAttachmentUploads,
881880
]);
882881

883-
useEffect(() => {
884-
for (const file of composerFiles) {
885-
if (
886-
!attachmentUploadsCapabilityKnown ||
887-
!supportsAttachmentUploads ||
888-
maxFileAttachmentBytes === null ||
889-
file.sizeBytes > maxFileAttachmentBytes
890-
) {
891-
continue;
892-
}
893-
const upload = uploadsByImageId[file.id];
894-
if (upload?.status === "ready" && upload.environmentId === environmentId) {
895-
setComposerDraftFileUpload(
896-
attachmentDraftTarget,
897-
file.id,
898-
environmentId,
899-
upload.attachmentId,
900-
);
901-
}
902-
}
903-
}, [
904-
attachmentUploadsCapabilityKnown,
905-
attachmentDraftTarget,
906-
composerFiles,
907-
environmentId,
908-
maxFileAttachmentBytes,
909-
setComposerDraftFileUpload,
910-
supportsAttachmentUploads,
911-
uploadsByImageId,
912-
]);
913-
914882
const {
915883
providerInstanceEntries,
916884
selectedProviderEntry,

‎apps/web/src/lib/attachmentUploadQueue.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,32 @@ describe("attachmentUploadQueue", () => {
379379
}
380380
});
381381

382+
it("stamps a file that lands in a draft after its upload finished", async () => {
383+
const draftId = DraftId.make("draft-restored-upload");
384+
const file = makeFile("restored");
385+
try {
386+
startAttachmentUpload({ environmentId: firstEnvironment, image: file, draftTarget: draftId });
387+
await Promise.resolve();
388+
const settled = awaitAttachmentUploads([file.id]);
389+
TestXmlHttpRequest.requests[0]!.complete();
390+
await settled;
391+
392+
useComposerDraftStore.getState().addFiles(draftId, [file]);
393+
startAttachmentUpload({ environmentId: firstEnvironment, image: file, draftTarget: draftId });
394+
395+
expect(TestXmlHttpRequest.requests).toHaveLength(1);
396+
expect(useComposerDraftStore.getState().getComposerDraft(draftId)?.files).toMatchObject([
397+
{
398+
id: file.id,
399+
uploadedAttachmentId: "pending-environment-1-restored.pdf",
400+
uploadEnvironmentId: firstEnvironment,
401+
},
402+
]);
403+
} finally {
404+
useComposerDraftStore.getState().clearComposerContent(draftId);
405+
}
406+
});
407+
382408
it("turns an expired persisted file into a marker that a re-pick can replace", async () => {
383409
const draftId = DraftId.make("draft-expired-upload");
384410
const file: ComposerFileAttachment = {

‎apps/web/src/lib/attachmentUploadQueue.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ interface UploadJob {
5757
stopWatchingConnection: () => void;
5858
}
5959

60+
type DraftFileOwner = Pick<UploadJob, "image" | "environmentId" | "draftTarget">;
61+
6062
const jobsByImageId = new Map<string, UploadJob>();
6163
const queue: UploadJob[] = [];
6264
const activeUploadsByEnvironment = new Map<EnvironmentId, number>();
@@ -82,7 +84,7 @@ export function readAttachmentUpload(imageId: string): AttachmentUploadState | u
8284
return useAttachmentUploadStore.getState().uploadsByImageId[imageId];
8385
}
8486

85-
function resolveCurrentFileDraftTarget(job: UploadJob): ComposerThreadTarget | undefined {
87+
function resolveCurrentFileDraftTarget(job: DraftFileOwner): ComposerThreadTarget | undefined {
8688
if (job.draftTarget === undefined || job.image.type !== "file") {
8789
return undefined;
8890
}
@@ -110,7 +112,7 @@ function resolveCurrentFileDraftTarget(job: UploadJob): ComposerThreadTarget | u
110112
}
111113

112114
/** Persist background completions even when no composer is mounted. */
113-
function stampDraftFileUpload(job: UploadJob, attachmentId: string): void {
115+
function stampDraftFileUpload(job: DraftFileOwner, attachmentId: string): void {
114116
const draftTarget = resolveCurrentFileDraftTarget(job);
115117
if (draftTarget === undefined) {
116118
return;
@@ -402,6 +404,9 @@ export function startAttachmentUpload(input: {
402404

403405
const existing = readAttachmentUpload(input.image.id);
404406
if (existing?.status === "ready" && existing.environmentId === input.environmentId) {
407+
// A file restored into a draft after its upload finished (stash, failed send) missed the
408+
// completion stamp; stamping here keeps it from re-uploading after a reload.
409+
stampDraftFileUpload(input, existing.attachmentId);
405410
return;
406411
}
407412
if (existing?.status === "failed" && existing.environmentId === input.environmentId) {

0 commit comments

Comments
 (0)