tahamajs/NS / frontend /src /components /PDFViewer.tsx
tahamajs's picture
download
raw
4 kB
import { useState } from 'react';
import { ExternalLink, Maximize2, Minimize2, Download, AlertCircle, Loader2 } from 'lucide-react';
interface PDFViewerProps {
title: string;
pdfUrl: string;
onClose?: () => void;
}
export default function PDFViewer({ title, pdfUrl, onClose }: PDFViewerProps) {
const [isFullscreen, setIsFullscreen] = useState(false);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(false);
return (
<div className={`flex flex-col bg-slate-900 border border-slate-800 rounded-3xl overflow-hidden shadow-2xl transition-all ${
isFullscreen ? 'fixed inset-4 z-50 rounded-2xl' : 'w-full h-[600px]'
}`}>
{/* Viewer Header */}
<div className="bg-slate-950 px-5 py-3 border-b border-slate-800 flex items-center justify-between gap-3 shrink-0">
<div className="flex items-center gap-2 min-w-0">
<span className="p-1.5 bg-violet-600/20 text-violet-400 rounded-lg text-xs font-black shrink-0">PDF</span>
<h3 className="font-bold text-white text-xs sm:text-sm truncate">{title}</h3>
</div>
<div className="flex items-center gap-2 shrink-0">
<button
onClick={() => setIsFullscreen(v => !v)}
className="p-1.5 bg-slate-800 hover:bg-slate-700 text-slate-300 rounded-lg text-xs transition-colors flex items-center gap-1"
title={isFullscreen ? "Exit Fullscreen" : "Fullscreen"}
>
{isFullscreen ? <Minimize2 size={14} /> : <Maximize2 size={14} />}
<span className="hidden sm:inline text-xs">{isFullscreen ? "Exit" : "Expand"}</span>
</button>
<a
href={pdfUrl}
download
className="p-1.5 bg-slate-800 hover:bg-slate-700 text-slate-300 rounded-lg text-xs transition-colors flex items-center gap-1"
title="Download PDF"
>
<Download size={14} />
<span className="hidden sm:inline text-xs">Download</span>
</a>
<a
href={pdfUrl}
target="_blank"
rel="noreferrer"
className="p-1.5 bg-slate-800 hover:bg-slate-700 text-slate-300 rounded-lg text-xs transition-colors"
title="Open in new tab"
>
<ExternalLink size={14} />
</a>
{onClose && (
<button
onClick={onClose}
className="p-1.5 bg-rose-600/20 text-rose-300 hover:bg-rose-600 hover:text-white rounded-lg text-xs font-bold transition-all ml-1"
>
✕
</button>
)}
</div>
</div>
{/* Viewer Body */}
<div className="flex-1 relative bg-slate-950">
{loading && (
<div className="absolute inset-0 flex flex-col items-center justify-center gap-2 bg-slate-950 text-slate-400 text-xs">
<Loader2 size={24} className="animate-spin text-violet-400" />
Loading Document PDF…
</div>
)}
{error ? (
<div className="absolute inset-0 flex flex-col items-center justify-center gap-3 p-6 text-center bg-slate-950 text-slate-400">
<AlertCircle size={32} className="text-rose-400" />
<p className="text-xs">Unable to load inline PDF preview.</p>
<a
href={pdfUrl}
target="_blank"
rel="noreferrer"
className="px-4 py-2 bg-violet-600 hover:bg-violet-500 text-white font-bold rounded-xl text-xs flex items-center gap-1.5 transition-all shadow-md shadow-violet-600/20"
>
Open PDF Directly <ExternalLink size={12} />
</a>
</div>
) : (
<iframe
src={pdfUrl}
className="w-full h-full border-0"
title={title}
onLoad={() => setLoading(false)}
onError={() => { setLoading(false); setError(true); }}
/>
)}
</div>
</div>
);
}

Xet Storage Details

Size:
4 kB
·
Xet hash:
ff531112d54a0e92494d68cfca8879659bf147cbf88ee8bc89c7e86a671ff5c7

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.