PDF error handling
This commit is contained in:
@ -1,19 +1,37 @@
|
|||||||
|
import { useState } from 'react'
|
||||||
|
|
||||||
|
import { mdiAlertOutline } from '@mdi/js'
|
||||||
|
import Icon from '@mdi/react'
|
||||||
|
|
||||||
interface PDFProps {
|
interface PDFProps {
|
||||||
url: string
|
url: string
|
||||||
form?: boolean
|
form?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
const PDF = ({ url, form = false }: PDFProps) => (
|
const PDF = ({ url, form = false }: PDFProps) => {
|
||||||
<embed
|
const [error, setError] = useState<boolean>(false)
|
||||||
src={url}
|
|
||||||
title="PDF"
|
return error ? (
|
||||||
type="application/pdf"
|
<div role="alert" className="alert alert-warning">
|
||||||
style={{
|
<Icon path={mdiAlertOutline} className="size-7" />
|
||||||
width: 'calc(100vw - 1rem)',
|
<span>
|
||||||
height: `calc(100vh - ${form ? '8.5rem' : '6rem'})`,
|
Could not load PDF, this is common in in-app browsers, try opening this
|
||||||
}}
|
page in a regular browser
|
||||||
className="rounded-xl"
|
</span>
|
||||||
/>
|
</div>
|
||||||
)
|
) : (
|
||||||
|
<iframe
|
||||||
|
src={url}
|
||||||
|
title="PDF"
|
||||||
|
style={{
|
||||||
|
width: 'calc(100vw - 1rem)',
|
||||||
|
height: `calc(100vh - ${form ? '8.5rem' : '6rem'})`,
|
||||||
|
}}
|
||||||
|
className="rounded-xl"
|
||||||
|
onError={() => setError(true)}
|
||||||
|
onLoad={() => setError(false)}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export default PDF
|
export default PDF
|
||||||
|
Reference in New Issue
Block a user