"use client";
import { useState, useEffect } from 'react';
export default function PdfViewer({ pdfUrl, pageNumber, searchTerm }) {
const [loading, setLoading] = useState(true);
const [error, setError] = useState(false);
// Reset loading state when URL or page changes
useEffect(() => {
setLoading(true);
setError(false);
}, [pdfUrl, pageNumber]);
if (!pdfUrl) {
return (
No PDF available for this document.
);
}
// PDF pages in our data are 0-indexed; PDF.js viewer expects 1-indexed pages
const viewerPage = (pageNumber ?? 0) + 1;
// Proxy/Route the PDF through our local PDF.js viewer to support highlighting and bypass CORS.
const isLocal = pdfUrl.startsWith('/') || pdfUrl.includes('localhost') || pdfUrl.includes('127.0.0.1');
const viewerPdfUrl = isLocal ? pdfUrl : `/api/pdf-proxy?url=${encodeURIComponent(pdfUrl)}`;
const proxyUrl = `/pdfjs/web/viewer.html?file=${encodeURIComponent(viewerPdfUrl)}#page=${viewerPage}${searchTerm ? `&search=${encodeURIComponent(searchTerm)}&phrase=true` : ''}`;
return (
{loading && !error && (
Loading PDF...
This may take a moment for large documents.
)}
{error && (
)}
);
}