2024-06-07 20:17:03 +03:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
import useWindowSize from '@/hooks/useWindowSize';
|
2025-01-14 21:57:32 +03:00
|
|
|
import { useFitHeight } from '@/stores/appLayout';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
2024-11-21 15:09:31 +03:00
|
|
|
/** Maximum width of the viewer. */
|
2024-08-17 23:01:55 +03:00
|
|
|
const MAXIMUM_WIDTH = 1600;
|
2024-11-21 15:09:31 +03:00
|
|
|
|
|
|
|
/** Minimum width of the viewer. */
|
2024-06-07 20:17:03 +03:00
|
|
|
const MINIMUM_WIDTH = 300;
|
|
|
|
|
|
|
|
interface PDFViewerProps {
|
2024-11-21 15:09:31 +03:00
|
|
|
/** PDF file to display. */
|
2024-08-17 23:01:55 +03:00
|
|
|
file?: string;
|
2024-11-21 15:09:31 +03:00
|
|
|
|
|
|
|
/** Offset from the left side of the window. */
|
2024-06-07 20:17:03 +03:00
|
|
|
offsetXpx?: number;
|
2024-11-21 15:09:31 +03:00
|
|
|
|
|
|
|
/** Minimum width of the viewer. */
|
2024-06-07 20:17:03 +03:00
|
|
|
minWidth?: number;
|
|
|
|
}
|
|
|
|
|
2024-11-21 15:09:31 +03:00
|
|
|
/**
|
|
|
|
* Displays a PDF file using an embedded viewer.
|
|
|
|
*/
|
2024-06-07 20:17:03 +03:00
|
|
|
function PDFViewer({ file, offsetXpx, minWidth = MINIMUM_WIDTH }: PDFViewerProps) {
|
|
|
|
const windowSize = useWindowSize();
|
|
|
|
|
2024-12-13 21:30:49 +03:00
|
|
|
const pageWidth = Math.max(minWidth, Math.min((windowSize?.width ?? 0) - (offsetXpx ?? 0) - 10, MAXIMUM_WIDTH));
|
2025-01-14 21:57:32 +03:00
|
|
|
const pageHeight = useFitHeight('1rem');
|
2024-06-07 20:17:03 +03:00
|
|
|
|
2024-08-17 23:01:55 +03:00
|
|
|
return <embed src={`${file}#toolbar=0`} className='p-3' style={{ width: pageWidth, height: pageHeight }} />;
|
2024-06-07 20:17:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default PDFViewer;
|