fix: guard chat file preview rendering when mime type is missing (#35355)

Co-authored-by: Stephen Zhou <hi@hyoban.cc>
This commit is contained in:
hyl64
2026-04-17 16:34:04 +08:00
committed by GitHub
parent bd25240123
commit f56ce9d3b1
2 changed files with 19 additions and 3 deletions

View File

@@ -222,6 +222,21 @@ describe('FileItem (chat-input)', () => {
expect(document.querySelector('audio')).not.toBeInTheDocument()
})
it('should not throw when file type is missing', () => {
expect(() => {
render(
<FileItem
file={createFile({
name: 'generated.png',
type: undefined as unknown as string,
supportFileType: 'document',
})}
canPreview
/>,
)
}).not.toThrow()
})
it('should close video preview', () => {
render(
<FileItem

View File

@@ -36,6 +36,7 @@ const FileItem = ({
const [previewUrl, setPreviewUrl] = useState('')
const ext = getFileExtension(name, type, isRemote)
const uploadError = progress === -1
const [typeCategory = '', typeSubtype = ''] = type?.split('/') ?? []
let tmp_preview_url = url || base64Url
if (!tmp_preview_url && file?.originalFile)
@@ -121,7 +122,7 @@ const FileItem = ({
</div>
</div>
{
type.split('/')[0] === 'audio' && canPreview && previewUrl && (
typeCategory === 'audio' && canPreview && previewUrl && (
<AudioPreview
title={name}
url={previewUrl}
@@ -130,7 +131,7 @@ const FileItem = ({
)
}
{
type.split('/')[0] === 'video' && canPreview && previewUrl && (
typeCategory === 'video' && canPreview && previewUrl && (
<VideoPreview
title={name}
url={previewUrl}
@@ -139,7 +140,7 @@ const FileItem = ({
)
}
{
type.split('/')[1] === 'pdf' && canPreview && previewUrl && (
typeSubtype === 'pdf' && canPreview && previewUrl && (
<PdfPreview url={previewUrl} onCancel={() => { setPreviewUrl('') }} />
)
}