import { useEffect, useRef, useState, useCallback } from "react"; import { Document, Page, pdfjs } from "react-pdf"; import "react-pdf/dist/esm/Page/AnnotationLayer.css"; import "react-pdf/dist/esm/Page/TextLayer.css"; import ArrowCircleLeftIcon from "@mui/icons-material/ArrowCircleLeft"; import ArrowCircleRightIcon from "@mui/icons-material/ArrowCircleRight"; import { Box, IconButton } from "@mui/material"; pdfjs.GlobalWorkerOptions.workerSrc = new URL( "pdfjs-dist/build/pdf.worker.min.mjs", import.meta.url ).toString(); interface PDFViewerProps { pitchBookId: string; currentPage?: number; } export default function PDFViewer({ pitchBookId, currentPage }: PDFViewerProps) { const [numPages, setNumPages] = useState(null); const [pageNumber, setPageNumber] = useState(currentPage || 1); const [containerWidth, setContainerWidth] = useState(null); const containerRef = useRef(null); const [highlightLabels, setHighlightLabels] = useState([]); const onDocumentLoadSuccess = ({ numPages }: { numPages: number }) => { setNumPages(numPages); }; // Aktuelle Containergröße berechnen useEffect(() => { const updateWidth = () => { if (containerRef.current) { setContainerWidth(containerRef.current.offsetWidth); } }; updateWidth(); window.addEventListener("resize", updateWidth); return () => window.removeEventListener("resize", updateWidth); }, []); // Seite ändern, wenn prop sich ändert useEffect(() => { if (currentPage && currentPage !== pageNumber) { setPageNumber(currentPage); } }, [currentPage]); // Highlight-Logik useEffect(() => { setHighlightLabels(["LTV", "Fondsmanager", "Risikoprofil"]); }, []); function highlightPattern(text: string, patterns: string[]) { for (const word of patterns) { const regex = new RegExp(`(${word})`, "gi"); text = text.replace(regex, "$1"); } return text; } const textRenderer = useCallback( (textItem: { str: string }) => highlightPattern(textItem.str, highlightLabels), [highlightLabels] ); return ( console.error("Es gab ein Fehler beim Laden des PDFs:", error) } onSourceError={(error) => console.error("Ungültige PDF:", error)} > {containerWidth && ( )} setPageNumber((p) => p - 1)} > {pageNumber} / {numPages} = (numPages || 1)} onClick={() => setPageNumber((p) => p + 1)} > ); }