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

30 lines
894 B
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';
2024-08-17 23:02:21 +03:00
const MAXIMUM_WIDTH = 1600;
2024-06-07 15:05:08 +03:00
const MINIMUM_WIDTH = 300;
2023-12-28 14:04:44 +03:00
interface PDFViewerProps {
2024-08-17 23:02:21 +03:00
file?: string;
2024-06-07 15:05:08 +03:00
offsetXpx?: number;
minWidth?: number;
2023-12-28 14:04:44 +03:00
}
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;