overleaf/services/web/frontend/js/features/pdf-preview/components/pdf-viewer.js
Alf Eaton 845b2fbc04 Move useScopeValue hook to shared hooks folder and wrap setValue in a function (#5386)
GitOrigin-RevId: b1a6a4e871af3b52fb3d100a83b479c834cb75ca
2021-10-11 08:03:29 +00:00

38 lines
870 B
JavaScript

import useScopeValue from '../../../shared/hooks/use-scope-value'
import { usePdfPreviewContext } from '../contexts/pdf-preview-context'
import { lazy, memo, useEffect } from 'react'
const PdfJsViewer = lazy(() =>
import(/* webpackChunkName: "pdf-js-viewer" */ './pdf-js-viewer')
)
const params = new URLSearchParams(window.location.search)
function PdfViewer() {
const [pdfViewer, setPdfViewer] = useScopeValue('settings.pdfViewer')
useEffect(() => {
const viewer = params.get('viewer')
if (viewer) {
setPdfViewer(viewer)
}
}, [setPdfViewer])
const { pdfUrl } = usePdfPreviewContext()
if (!pdfUrl) {
return null
}
switch (pdfViewer) {
case 'native':
return <iframe title="PDF Preview" src={pdfUrl} />
case 'pdfjs':
default:
return <PdfJsViewer url={pdfUrl} />
}
}
export default memo(PdfViewer)