overleaf/services/web/frontend/js/features/pdf-preview/components/pdf-preview-provider.tsx
Alf Eaton 04900349e6 Add PdfPreviewProvider and preserve PDF viewer loading error (#14970)
GitOrigin-RevId: ac305633c7c0bc13f5f670b71890cfd30a3cbffd
2023-09-29 08:04:10 +00:00

34 lines
792 B
TypeScript

import { createContext, FC, useContext, useMemo, useState } from 'react'
const PdfPreviewContext = createContext<
| {
loadingError: boolean
setLoadingError: (value: boolean) => void
}
| undefined
>(undefined)
export const usePdfPreviewContext = () => {
const context = useContext(PdfPreviewContext)
if (!context) {
throw new Error(
'usePdfPreviewContext is only avalable inside PdfPreviewProvider'
)
}
return context
}
export const PdfPreviewProvider: FC = ({ children }) => {
const [loadingError, setLoadingError] = useState(false)
const value = useMemo(
() => ({ loadingError, setLoadingError }),
[loadingError]
)
return (
<PdfPreviewContext.Provider value={value}>
{children}
</PdfPreviewContext.Provider>
)
}