Repository navigation
fix(site): persist workspace upload drafts - #32221
Conversation
Flatten failure handling and replace field-by-field comparisons with the existing equality helper. Compare reference counts so failed removals cannot restore duplicate chips while failed additions preserve saved references.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
| const workspaceUploadSchema: Yup.ObjectSchema<UploadChatWorkspaceFileResponse> = | ||
| Yup.object({ | ||
| path: Yup.string().required(), | ||
| name: Yup.string().required(), | ||
| size: Yup.number().min(0).max(Number.MAX_VALUE).required(), | ||
| media_type: Yup.string().defined(), | ||
| workspace_id: Yup.string().defined(), | ||
| }).required(); | ||
|
|
||
| const readWorkspaceUploadDraft = ( | ||
| storageKey: string | undefined, | ||
| ): readonly UploadChatWorkspaceFileResponse[] | undefined => { | ||
| if (!storageKey) { | ||
| return []; | ||
| } | ||
| try { | ||
| const stored: unknown = JSON.parse( | ||
| localStorage.getItem(storageKey) ?? "[]", | ||
| ); | ||
| if (!Array.isArray(stored)) { | ||
| return undefined; | ||
| } | ||
| return stored.filter((value: unknown) => | ||
| workspaceUploadSchema.isValidSync(value, { strict: true }), | ||
| ); | ||
| } catch { | ||
| // Unavailable or invalid storage must not prevent composing a message. | ||
| return undefined; | ||
| } | ||
| }; | ||
|
|
||
| const restoreWorkspaceUploadDraft = ( | ||
| storageKey: string | undefined, | ||
| workspaceId: string | undefined, | ||
| ): readonly WorkspaceFileUpload[] => | ||
| (readWorkspaceUploadDraft(storageKey) ?? []) | ||
| .filter((response) => response.workspace_id === workspaceId) | ||
| .map((response) => ({ | ||
| id: createUploadId(), | ||
| file: new File([], response.name, { type: response.media_type }), | ||
| status: "uploaded", | ||
| response, | ||
| })); | ||
|
|
||
| const persistWorkspaceUploadDraft = ( | ||
| storageKey: string | undefined, | ||
| uploads: readonly WorkspaceFileUpload[], | ||
| ) => { | ||
| if (!storageKey) { | ||
| return; | ||
| } | ||
| const responses = uploads.flatMap((upload) => | ||
| upload.status === "uploaded" && upload.response ? [upload.response] : [], | ||
| ); | ||
| try { | ||
| if (responses.length === 0) { | ||
| localStorage.removeItem(storageKey); | ||
| } else { | ||
| localStorage.setItem(storageKey, JSON.stringify(responses)); | ||
| } | ||
| return; | ||
| } catch { | ||
| toast.warning( | ||
| "Could not save workspace upload changes. Your draft may be outdated when you return to this chat.", | ||
| { id: storageKey }, | ||
| ); | ||
| } | ||
|
|
||
| // A failed save can leave removed uploads in storage. | ||
| const savedResponses = readWorkspaceUploadDraft(storageKey); | ||
| if ( | ||
| savedResponses?.every( | ||
| (saved) => | ||
| responses.filter((response) => isEqual(response, saved)).length >= | ||
| savedResponses.filter((response) => isEqual(response, saved)).length, | ||
| ) | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| localStorage.removeItem(storageKey); | ||
| } catch { | ||
| // The in-memory draft remains usable if storage is unavailable. | ||
| } | ||
| }; |
There was a problem hiding this comment.
I'm still pained by our lack of an actual abstraction for this localStorage stuff. I might tackle this myself using a proper slow burn approach instead of trying to migrate everything at once and introduce a 10k LoC PR stack
There was a problem hiding this comment.
Zustand's developer just decided this was dead to him. 🥲 I hate it with a burning passion too, but now I don't know what I want to use.
There was a problem hiding this comment.
Damn, I've got some ideas that don't require their projects anyways
Completed but unsent workspace-upload chips disappeared when leaving a chat or when an optimistic workspace rebind rolled back, even though the files remained in the workspace. Persist compose-draft metadata by chat and workspace, restore completed references when returning, and clear them on successful send or explicit removal.
PNGs use the normal image-attachment path, which already persists drafts, so they do not reproduce this issue. To reproduce the original bug, upload a
.zipor.tarin an existing chat with a running workspace, wait for completion, do not send the message, then switch chats and return. Check whether the unsent composer chip survives, not whether the file remains in the workspace.Refs CODAGT-1172 and #27080.
Implementation decisions