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
15 changes: 15 additions & 0 deletions frontend/src/components/Chat/ConversationEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,18 @@ describe('ConversationEditor', () => {
expect(onSaved).not.toHaveBeenCalled()
})
})

it('revokes the blob URL when an attachment is removed from the editor', async () => {
const user = userEvent.setup()
const mockObjectUrl = 'blob:http://localhost/attach-1'
global.URL.createObjectURL = jest.fn().mockReturnValue(mockObjectUrl)
global.URL.revokeObjectURL = jest.fn()
setupEditor()

const first = screen.getByRole('region', { name: 'Message 1' })
await user.upload(within(first).getAllByTestId('file-input')[0], new File(['x'], 'photo.png', { type: 'image/png' }))

await user.click(await screen.findByRole('button', { name: 'Remove photo.png' }))

expect(global.URL.revokeObjectURL).toHaveBeenCalledWith(mockObjectUrl)
})
7 changes: 6 additions & 1 deletion frontend/src/components/Chat/ConversationEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,13 @@ function DraftPieceEditor({ piece, disabled, onChange, onAttachments, onConverte
})
}, [media, onChange, piece.draftId])
const changeAttachments = useCallback((attachments: MessageAttachment[]): void => {
if (media) {
const nextUrls = new Set(attachments.map((attachment: MessageAttachment) => attachment.url))
const current = draftAttachment(piece)
if (!nextUrls.has(current.url) && current.url.startsWith('blob:')) URL.revokeObjectURL(current.url)
}
onAttachments(piece.draftId, attachments)
}, [onAttachments, piece.draftId])
}, [media, piece, onAttachments])
const clearConversion = (): void => onChange(piece.draftId, (current: ConversationDraftPiece) => ({
...current, converted_value: undefined, converted_value_data_type: undefined, applied_converter_ids: [],
}))
Expand Down
Loading