2026-04-15 21:42:50 +05:30
|
|
|
"use client";
|
|
|
|
|
|
2026-04-16 11:23:13 +05:30
|
|
|
import { ZoomInIcon, ZoomOutIcon } from "lucide-react";
|
|
|
|
|
import type { PDFDocumentProxy, RenderTask } from "pdfjs-dist";
|
2026-04-17 16:45:14 +05:30
|
|
|
import * as pdfjsLib from "pdfjs-dist";
|
|
|
|
|
import { useCallback, useEffect, useRef, useState } from "react";
|
2026-04-15 21:42:50 +05:30
|
|
|
import { Button } from "@/components/ui/button";
|
2026-04-15 21:56:18 +05:30
|
|
|
import { Spinner } from "@/components/ui/spinner";
|
2026-04-15 21:42:50 +05:30
|
|
|
import { getAuthHeaders } from "@/lib/auth-utils";
|
|
|
|
|
|
2026-04-16 11:23:13 +05:30
|
|
|
pdfjsLib.GlobalWorkerOptions.workerSrc = new URL(
|
2026-04-15 21:42:50 +05:30
|
|
|
"pdfjs-dist/build/pdf.worker.min.mjs",
|
|
|
|
|
import.meta.url
|
|
|
|
|
).toString();
|
|
|
|
|
|
|
|
|
|
interface PdfViewerProps {
|
|
|
|
|
pdfUrl: string;
|
2026-04-16 22:53:17 +05:30
|
|
|
isPublic?: boolean;
|
2026-04-15 21:42:50 +05:30
|
|
|
}
|
|
|
|
|
|
2026-04-16 23:44:15 +05:30
|
|
|
interface PageDimensions {
|
|
|
|
|
width: number;
|
|
|
|
|
height: number;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 21:42:50 +05:30
|
|
|
const ZOOM_STEP = 0.15;
|
|
|
|
|
const MIN_ZOOM = 0.5;
|
|
|
|
|
const MAX_ZOOM = 3;
|
2026-04-16 11:23:13 +05:30
|
|
|
const PAGE_GAP = 12;
|
2026-04-16 23:44:15 +05:30
|
|
|
const SCROLL_DEBOUNCE_MS = 30;
|
|
|
|
|
const BUFFER_PAGES = 1;
|
2026-04-15 21:42:50 +05:30
|
|
|
|
2026-04-16 22:53:17 +05:30
|
|
|
export function PdfViewer({ pdfUrl, isPublic = false }: PdfViewerProps) {
|
2026-04-16 11:23:13 +05:30
|
|
|
const [numPages, setNumPages] = useState(0);
|
2026-04-15 21:42:50 +05:30
|
|
|
const [scale, setScale] = useState(1);
|
2026-04-16 11:23:13 +05:30
|
|
|
const [loading, setLoading] = useState(true);
|
2026-04-15 21:42:50 +05:30
|
|
|
const [loadError, setLoadError] = useState<string | null>(null);
|
|
|
|
|
|
2026-04-16 11:23:13 +05:30
|
|
|
const scrollContainerRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const pdfDocRef = useRef<PDFDocumentProxy | null>(null);
|
|
|
|
|
const canvasRefs = useRef<Map<number, HTMLCanvasElement>>(new Map());
|
|
|
|
|
const renderTasksRef = useRef<Map<number, RenderTask>>(new Map());
|
|
|
|
|
const renderedScalesRef = useRef<Map<number, number>>(new Map());
|
2026-04-16 23:44:15 +05:30
|
|
|
const pageDimsRef = useRef<PageDimensions[]>([]);
|
|
|
|
|
const visiblePagesRef = useRef<Set<number>>(new Set());
|
|
|
|
|
const scrollTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
|
|
|
|
|
|
|
|
const getScaledHeight = useCallback(
|
|
|
|
|
(pageIndex: number) => {
|
|
|
|
|
const dims = pageDimsRef.current[pageIndex];
|
|
|
|
|
return dims ? Math.floor(dims.height * scale) : 0;
|
|
|
|
|
},
|
2026-04-17 16:45:14 +05:30
|
|
|
[scale]
|
2026-04-16 23:44:15 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const getVisibleRange = useCallback(() => {
|
|
|
|
|
const container = scrollContainerRef.current;
|
|
|
|
|
if (!container || pageDimsRef.current.length === 0) return { first: 1, last: 1 };
|
|
|
|
|
|
|
|
|
|
const scrollTop = container.scrollTop;
|
|
|
|
|
const viewportHeight = container.clientHeight;
|
|
|
|
|
const scrollBottom = scrollTop + viewportHeight;
|
|
|
|
|
|
|
|
|
|
let cumTop = 16;
|
|
|
|
|
let first = 1;
|
|
|
|
|
let last = pageDimsRef.current.length;
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < pageDimsRef.current.length; i++) {
|
|
|
|
|
const pageHeight = getScaledHeight(i);
|
|
|
|
|
const pageBottom = cumTop + pageHeight;
|
|
|
|
|
|
|
|
|
|
if (pageBottom >= scrollTop && first === 1) {
|
|
|
|
|
first = i + 1;
|
|
|
|
|
}
|
|
|
|
|
if (cumTop > scrollBottom) {
|
|
|
|
|
last = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cumTop = pageBottom + PAGE_GAP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
first = Math.max(1, first - BUFFER_PAGES);
|
|
|
|
|
last = Math.min(pageDimsRef.current.length, last + BUFFER_PAGES);
|
|
|
|
|
|
|
|
|
|
return { first, last };
|
|
|
|
|
}, [getScaledHeight]);
|
2026-04-15 21:42:50 +05:30
|
|
|
|
2026-04-16 11:23:13 +05:30
|
|
|
const renderPage = useCallback(async (pageNum: number, currentScale: number) => {
|
|
|
|
|
const pdf = pdfDocRef.current;
|
|
|
|
|
const canvas = canvasRefs.current.get(pageNum);
|
|
|
|
|
if (!pdf || !canvas) return;
|
|
|
|
|
|
|
|
|
|
if (renderedScalesRef.current.get(pageNum) === currentScale) return;
|
|
|
|
|
|
|
|
|
|
const existing = renderTasksRef.current.get(pageNum);
|
|
|
|
|
if (existing) {
|
|
|
|
|
existing.cancel();
|
|
|
|
|
renderTasksRef.current.delete(pageNum);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const page = await pdf.getPage(pageNum);
|
|
|
|
|
const viewport = page.getViewport({ scale: currentScale });
|
|
|
|
|
const dpr = window.devicePixelRatio || 1;
|
|
|
|
|
|
|
|
|
|
canvas.width = Math.floor(viewport.width * dpr);
|
|
|
|
|
canvas.height = Math.floor(viewport.height * dpr);
|
|
|
|
|
canvas.style.width = `${Math.floor(viewport.width)}px`;
|
|
|
|
|
canvas.style.height = `${Math.floor(viewport.height)}px`;
|
|
|
|
|
|
|
|
|
|
const renderTask = page.render({
|
|
|
|
|
canvas,
|
|
|
|
|
viewport,
|
|
|
|
|
transform: dpr !== 1 ? [dpr, 0, 0, dpr, 0, 0] : undefined,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
renderTasksRef.current.set(pageNum, renderTask);
|
2026-04-15 21:42:50 +05:30
|
|
|
|
2026-04-16 11:23:13 +05:30
|
|
|
await renderTask.promise;
|
|
|
|
|
renderTasksRef.current.delete(pageNum);
|
|
|
|
|
renderedScalesRef.current.set(pageNum, currentScale);
|
2026-04-16 23:44:15 +05:30
|
|
|
page.cleanup();
|
2026-04-16 11:23:13 +05:30
|
|
|
} catch (err: unknown) {
|
|
|
|
|
if (err instanceof Error && err.message?.includes("cancelled")) return;
|
|
|
|
|
console.error(`Failed to render page ${pageNum}:`, err);
|
|
|
|
|
}
|
2026-04-15 21:42:50 +05:30
|
|
|
}, []);
|
|
|
|
|
|
2026-04-16 23:44:15 +05:30
|
|
|
const cleanupPage = useCallback((pageNum: number) => {
|
|
|
|
|
const existing = renderTasksRef.current.get(pageNum);
|
|
|
|
|
if (existing) {
|
|
|
|
|
existing.cancel();
|
|
|
|
|
renderTasksRef.current.delete(pageNum);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const canvas = canvasRefs.current.get(pageNum);
|
|
|
|
|
if (canvas) {
|
|
|
|
|
const ctx = canvas.getContext("2d");
|
|
|
|
|
if (ctx) ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
|
canvas.width = 0;
|
|
|
|
|
canvas.height = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderedScalesRef.current.delete(pageNum);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const renderVisiblePages = useCallback(() => {
|
|
|
|
|
if (!pdfDocRef.current || pageDimsRef.current.length === 0) return;
|
|
|
|
|
|
|
|
|
|
const { first, last } = getVisibleRange();
|
|
|
|
|
const newVisible = new Set<number>();
|
|
|
|
|
|
|
|
|
|
for (let i = first; i <= last; i++) {
|
|
|
|
|
newVisible.add(i);
|
|
|
|
|
renderPage(i, scale);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const pageNum of visiblePagesRef.current) {
|
|
|
|
|
if (!newVisible.has(pageNum)) {
|
|
|
|
|
cleanupPage(pageNum);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
visiblePagesRef.current = newVisible;
|
|
|
|
|
}, [getVisibleRange, renderPage, cleanupPage, scale]);
|
|
|
|
|
|
2026-04-16 11:23:13 +05:30
|
|
|
useEffect(() => {
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
|
|
|
|
|
const loadDocument = async () => {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
setLoadError(null);
|
|
|
|
|
setNumPages(0);
|
2026-04-16 23:44:15 +05:30
|
|
|
pageDimsRef.current = [];
|
2026-04-16 11:23:13 +05:30
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const loadingTask = pdfjsLib.getDocument({
|
|
|
|
|
url: pdfUrl,
|
|
|
|
|
httpHeaders: getAuthHeaders(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const pdf = await loadingTask.promise;
|
|
|
|
|
if (cancelled) {
|
|
|
|
|
pdf.destroy();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-16 23:44:15 +05:30
|
|
|
const dims: PageDimensions[] = [];
|
|
|
|
|
for (let i = 1; i <= pdf.numPages; i++) {
|
|
|
|
|
const page = await pdf.getPage(i);
|
|
|
|
|
const viewport = page.getViewport({ scale: 1 });
|
|
|
|
|
dims.push({ width: viewport.width, height: viewport.height });
|
|
|
|
|
page.cleanup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cancelled) {
|
|
|
|
|
pdf.destroy();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-16 11:23:13 +05:30
|
|
|
pdfDocRef.current = pdf;
|
2026-04-16 23:44:15 +05:30
|
|
|
pageDimsRef.current = dims;
|
2026-04-16 11:23:13 +05:30
|
|
|
setNumPages(pdf.numPages);
|
|
|
|
|
setLoading(false);
|
|
|
|
|
} catch (err: unknown) {
|
|
|
|
|
if (cancelled) return;
|
|
|
|
|
const message = err instanceof Error ? err.message : "Failed to load PDF";
|
|
|
|
|
setLoadError(message);
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
loadDocument();
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
for (const task of renderTasksRef.current.values()) {
|
|
|
|
|
task.cancel();
|
|
|
|
|
}
|
|
|
|
|
renderTasksRef.current.clear();
|
|
|
|
|
renderedScalesRef.current.clear();
|
2026-04-16 23:44:15 +05:30
|
|
|
visiblePagesRef.current.clear();
|
2026-04-16 11:23:13 +05:30
|
|
|
pdfDocRef.current?.destroy();
|
|
|
|
|
pdfDocRef.current = null;
|
|
|
|
|
};
|
|
|
|
|
}, [pdfUrl]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-04-16 23:44:15 +05:30
|
|
|
if (numPages === 0) return;
|
2026-04-16 11:23:13 +05:30
|
|
|
|
|
|
|
|
renderedScalesRef.current.clear();
|
2026-04-16 23:44:15 +05:30
|
|
|
visiblePagesRef.current.clear();
|
2026-04-16 11:23:13 +05:30
|
|
|
|
2026-04-16 23:44:15 +05:30
|
|
|
const frame = requestAnimationFrame(() => {
|
|
|
|
|
renderVisiblePages();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return () => cancelAnimationFrame(frame);
|
|
|
|
|
}, [numPages, renderVisiblePages]);
|
2026-04-16 11:23:13 +05:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const container = scrollContainerRef.current;
|
2026-04-16 23:44:15 +05:30
|
|
|
if (!container || numPages === 0) return;
|
2026-04-16 11:23:13 +05:30
|
|
|
|
|
|
|
|
const handleScroll = () => {
|
2026-04-16 23:44:15 +05:30
|
|
|
if (scrollTimerRef.current) clearTimeout(scrollTimerRef.current);
|
|
|
|
|
scrollTimerRef.current = setTimeout(() => {
|
|
|
|
|
renderVisiblePages();
|
|
|
|
|
}, SCROLL_DEBOUNCE_MS);
|
2026-04-16 11:23:13 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
container.addEventListener("scroll", handleScroll, { passive: true });
|
2026-04-16 23:44:15 +05:30
|
|
|
return () => {
|
|
|
|
|
container.removeEventListener("scroll", handleScroll);
|
|
|
|
|
if (scrollTimerRef.current) clearTimeout(scrollTimerRef.current);
|
|
|
|
|
};
|
|
|
|
|
}, [numPages, renderVisiblePages]);
|
2026-04-15 21:42:50 +05:30
|
|
|
|
2026-04-16 11:23:13 +05:30
|
|
|
const setCanvasRef = useCallback((pageNum: number, el: HTMLCanvasElement | null) => {
|
|
|
|
|
if (el) {
|
|
|
|
|
canvasRefs.current.set(pageNum, el);
|
|
|
|
|
} else {
|
|
|
|
|
canvasRefs.current.delete(pageNum);
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
2026-04-15 21:42:50 +05:30
|
|
|
const zoomIn = useCallback(() => {
|
2026-04-16 11:23:13 +05:30
|
|
|
setScale((prev) => Math.min(MAX_ZOOM, +(prev + ZOOM_STEP).toFixed(2)));
|
2026-04-15 21:42:50 +05:30
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const zoomOut = useCallback(() => {
|
2026-04-16 11:23:13 +05:30
|
|
|
setScale((prev) => Math.max(MIN_ZOOM, +(prev - ZOOM_STEP).toFixed(2)));
|
2026-04-15 21:42:50 +05:30
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
if (loadError) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex flex-col items-center justify-center h-full gap-3 p-6 text-center">
|
2026-04-16 23:44:15 +05:30
|
|
|
<p className="font-medium text-foreground">Failed to load PDF</p>
|
2026-04-15 21:42:50 +05:30
|
|
|
<p className="text-sm text-muted-foreground">{loadError}</p>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex flex-col h-full">
|
|
|
|
|
{numPages > 0 && (
|
2026-04-17 16:45:14 +05:30
|
|
|
<div
|
|
|
|
|
className={`flex items-center justify-center gap-2 px-4 py-2 border-b shrink-0 select-none ${isPublic ? "bg-main-panel" : "bg-sidebar"}`}
|
|
|
|
|
>
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
|
|
|
|
onClick={zoomOut}
|
|
|
|
|
disabled={scale <= MIN_ZOOM}
|
|
|
|
|
className="size-7"
|
|
|
|
|
>
|
2026-04-15 21:42:50 +05:30
|
|
|
<ZoomOutIcon className="size-4" />
|
|
|
|
|
</Button>
|
|
|
|
|
<span className="text-xs text-muted-foreground tabular-nums min-w-[40px] text-center">
|
|
|
|
|
{Math.round(scale * 100)}%
|
|
|
|
|
</span>
|
2026-04-17 16:45:14 +05:30
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
|
|
|
|
onClick={zoomIn}
|
|
|
|
|
disabled={scale >= MAX_ZOOM}
|
|
|
|
|
className="size-7"
|
|
|
|
|
>
|
2026-04-15 21:42:50 +05:30
|
|
|
<ZoomInIcon className="size-4" />
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
2026-04-16 11:23:13 +05:30
|
|
|
<div
|
|
|
|
|
ref={scrollContainerRef}
|
2026-04-16 22:53:17 +05:30
|
|
|
className={`relative flex-1 overflow-auto ${isPublic ? "bg-main-panel" : "bg-sidebar"}`}
|
2026-04-16 11:23:13 +05:30
|
|
|
>
|
|
|
|
|
{loading ? (
|
2026-04-17 16:45:14 +05:30
|
|
|
<div
|
|
|
|
|
className={`absolute inset-0 flex items-center justify-center ${isPublic ? "text-foreground" : "text-sidebar-foreground"}`}
|
|
|
|
|
>
|
2026-04-16 11:23:13 +05:30
|
|
|
<Spinner size="md" />
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
2026-04-16 23:44:15 +05:30
|
|
|
<div className="flex flex-col items-center py-4" style={{ gap: `${PAGE_GAP}px` }}>
|
|
|
|
|
{pageDimsRef.current.map((dims, i) => {
|
|
|
|
|
const pageNum = i + 1;
|
|
|
|
|
const scaledWidth = Math.floor(dims.width * scale);
|
|
|
|
|
const scaledHeight = Math.floor(dims.height * scale);
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
key={pageNum}
|
|
|
|
|
className="relative shrink-0"
|
|
|
|
|
style={{ width: scaledWidth, height: scaledHeight }}
|
|
|
|
|
>
|
|
|
|
|
<canvas
|
|
|
|
|
ref={(el) => setCanvasRef(pageNum, el)}
|
|
|
|
|
className="shadow-lg absolute inset-0"
|
|
|
|
|
/>
|
|
|
|
|
{numPages > 1 && (
|
|
|
|
|
<span className="absolute bottom-2 right-3 text-[10px] tabular-nums text-white/80 bg-black/50 px-1.5 py-0.5 rounded pointer-events-none">
|
|
|
|
|
Page {pageNum}/{numPages}
|
|
|
|
|
</span>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2026-04-16 11:23:13 +05:30
|
|
|
</div>
|
|
|
|
|
)}
|
2026-04-15 21:42:50 +05:30
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|