GeezerVison is showing github.com — refactor(web): the upload queue alone records finished file uploads o… · pingdotgg/t3code@7b4c4b4
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/chat/ChatComposer.tsx -32Lines changed: 0 additions & 32 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -811,7 +811,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) | 811 | 811 | const removeComposerDraftImage = useComposerDraftStore((store) => store.removeImage); | 812 | 812 | const addComposerDraftFiles = useComposerDraftStore((store) => store.addFiles); | 813 | 813 | const removeComposerDraftFile = useComposerDraftStore((store) => store.removeFile); | 814 | | - const setComposerDraftFileUpload = useComposerDraftStore((store) => store.setFileUpload); | 815 | 814 | const insertComposerDraftTerminalContext = useComposerDraftStore( | 816 | 815 | (store) => store.insertTerminalContext, | 817 | 816 | ); | @@ -880,37 +879,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) | 880 | 879 | supportsAttachmentUploads, | 881 | 880 | ]); | 882 | 881 |
| 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 | | - | 914 | 882 | const { | 915 | 883 | providerInstanceEntries, | 916 | 884 | selectedProviderEntry, | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/lib/attachmentUploadQueue.test.ts +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -379,6 +379,32 @@ describe("attachmentUploadQueue", () => { | 379 | 379 | } | 380 | 380 | }); | 381 | 381 |
| | 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 | + | 382 | 408 | it("turns an expired persisted file into a marker that a re-pick can replace", async () => { | 383 | 409 | const draftId = DraftId.make("draft-expired-upload"); | 384 | 410 | const file: ComposerFileAttachment = { | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/lib/attachmentUploadQueue.ts +7-2Lines changed: 7 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -57,6 +57,8 @@ interface UploadJob { | 57 | 57 | stopWatchingConnection: () => void; | 58 | 58 | } | 59 | 59 |
| | 60 | +type DraftFileOwner = Pick<UploadJob, "image" | "environmentId" | "draftTarget">; | | 61 | + | 60 | 62 | const jobsByImageId = new Map<string, UploadJob>(); | 61 | 63 | const queue: UploadJob[] = []; | 62 | 64 | const activeUploadsByEnvironment = new Map<EnvironmentId, number>(); | @@ -82,7 +84,7 @@ export function readAttachmentUpload(imageId: string): AttachmentUploadState | u | 82 | 84 | return useAttachmentUploadStore.getState().uploadsByImageId[imageId]; | 83 | 85 | } | 84 | 86 |
| 85 | | -function resolveCurrentFileDraftTarget(job: UploadJob): ComposerThreadTarget | undefined { | | 87 | +function resolveCurrentFileDraftTarget(job: DraftFileOwner): ComposerThreadTarget | undefined { | 86 | 88 | if (job.draftTarget === undefined || job.image.type !== "file") { | 87 | 89 | return undefined; | 88 | 90 | } | @@ -110,7 +112,7 @@ function resolveCurrentFileDraftTarget(job: UploadJob): ComposerThreadTarget | u | 110 | 112 | } | 111 | 113 |
| 112 | 114 | /** 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 { | 114 | 116 | const draftTarget = resolveCurrentFileDraftTarget(job); | 115 | 117 | if (draftTarget === undefined) { | 116 | 118 | return; | @@ -402,6 +404,9 @@ export function startAttachmentUpload(input: { | 402 | 404 |
| 403 | 405 | const existing = readAttachmentUpload(input.image.id); | 404 | 406 | 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); | 405 | 410 | return; | 406 | 411 | } | 407 | 412 | if (existing?.status === "failed" && existing.environmentId === input.environmentId) { | |
|