ConceptPortal-public/rsconcept/frontend/src/components/ui/PDFViewer/PDFViewer.tsx

41 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-12-28 14:04:44 +03:00
'use client';
2024-08-17 23:02:21 +03:00
import { useMemo } from 'react';
2023-12-28 14:04:44 +03:00
2024-08-17 23:02:21 +03:00
import { useConceptOptions } from '@/context/ConceptOptionsContext';
2023-12-28 14:04:44 +03:00
import useWindowSize from '@/hooks/useWindowSize';
/** Maximum width of the viewer. */
2024-08-17 23:02:21 +03:00
const MAXIMUM_WIDTH = 1600;
/** Minimum width of the viewer. */
2024-06-07 15:05:08 +03:00
const MINIMUM_WIDTH = 300;
2023-12-28 14:04:44 +03:00
interface PDFViewerProps {
/** PDF file to display. */
2024-08-17 23:02:21 +03:00
file?: string;
/** Offset from the left side of the window. */
2024-06-07 15:05:08 +03:00
offsetXpx?: number;
/** Minimum width of the viewer. */
2024-06-07 15:05:08 +03:00
minWidth?: number;
2023-12-28 14:04:44 +03:00
}
/**
* Displays a PDF file using an embedded viewer.
*/
2024-06-07 15:05:08 +03:00
function PDFViewer({ file, offsetXpx, minWidth = MINIMUM_WIDTH }: PDFViewerProps) {
2023-12-28 14:04:44 +03:00
const windowSize = useWindowSize();
2024-08-17 23:02:21 +03:00
const { calculateHeight } = useConceptOptions();
2023-12-28 14:04:44 +03:00
const pageWidth = useMemo(() => {
2024-06-07 15:05:08 +03:00
return Math.max(minWidth, Math.min((windowSize?.width ?? 0) - (offsetXpx ?? 0) - 10, MAXIMUM_WIDTH));
}, [windowSize, offsetXpx, minWidth]);
2024-08-17 23:02:21 +03:00
const pageHeight = useMemo(() => calculateHeight('1rem'), [calculateHeight]);
2023-12-28 14:04:44 +03:00
2024-08-17 23:02:21 +03:00
return <embed src={`${file}#toolbar=0`} className='p-3' style={{ width: pageWidth, height: pageHeight }} />;
2023-12-28 14:04:44 +03:00
}
export default PDFViewer;