"use client"; import { useEffect, useState } from "react"; import { motion } from "framer-motion"; import { Download, CheckCircle2, ZoomIn, FileText } from "lucide-react"; import { useTheme } from "@/app/lib/ThemeContext"; import ImageLightbox, { downloadGalleryImage, isPdfSrc } from "./ImageLightbox"; import PdfReader, { PdfPages } from "./PdfReader"; import { loadPdf, type PdfDocument } from "./pdfDocument"; export interface GalleryImage { id: string; title: string; subtitle?: string; /** gradient used as placeholder when no real src */ gradient: string; /** optional real image URL */ src?: string; } interface Props { images: GalleryImage[]; } const stagger = { hidden: {}, visible: { transition: { staggerChildren: 0.08 } } }; const cardAnim = { hidden: { opacity: 0, y: 20, scale: 0.96 }, visible: { opacity: 1, y: 0, scale: 1, transition: { type: "spring" as const, stiffness: 320, damping: 26 } }, }; export default function ImageGallery({ images }: Props) { const { isDark } = useTheme(); const [downloaded, setDownloaded] = useState>(new Set()); const [preview, setPreview] = useState(null); const [reading, setReading] = useState<{ image: GalleryImage; doc: PdfDocument } | null>(null); /** Loaded PDF per entry id; `null` means it failed to load (show the placeholder). */ const [pdfs, setPdfs] = useState>({}); useEffect(() => { let live = true; for (const img of images) { if (!img.src || !isPdfSrc(img.src)) continue; loadPdf(img.src).then( (doc) => live && setPdfs((prev) => ({ ...prev, [img.id]: doc })), () => live && setPdfs((prev) => ({ ...prev, [img.id]: null })), ); } return () => { live = false; }; }, [images]); const openEntry = (img: GalleryImage) => { if (!isPdfSrc(img.src)) return setPreview(img); const doc = pdfs[img.id]; if (doc) setReading({ image: img, doc }); else if (doc === null) window.open(img.src, "_blank", "noopener,noreferrer"); }; const t = { text: isDark ? "#FFFFFF" : "#1a1a1a", textSec: isDark ? "rgba(255,255,255,0.8)" : "#5C5C5C", textMuted: isDark ? "rgba(255,255,255,0.6)" : "#6B6B6B", card: isDark ? "rgba(255,255,255,0.06)" : "#FFFFFF", border: isDark ? "rgba(255,255,255,0.08)" : "rgba(0,0,0,0.07)", shadow: isDark ? "0 4px 24px rgba(0,0,0,0.35), inset 0 1px 0 rgba(255,255,255,0.06)" : "0 2px 16px rgba(0,0,0,0.07), 0 1px 3px rgba(0,0,0,0.04)", }; const handleDownload = (img: GalleryImage) => { downloadGalleryImage(img); setDownloaded((prev) => new Set(prev).add(img.id)); setTimeout(() => { setDownloaded((prev) => { const next = new Set(prev); next.delete(img.id); return next; }); }, 2200); }; return ( <> {images.map((img) => { const isDone = downloaded.has(img.id); const isPdf = isPdfSrc(img.src); const pdfDoc = pdfs[img.id]; return ( {/* Image area */} openEntry(img)} whileTap={{ scale: 0.98 }} className="relative w-full block" > {isPdf && pdfDoc ? ( /* PDF upload — preview page 1; tapping opens the in-app reader */
) : isPdf ? ( /* PDF loading, or failed to load (then tapping opens it in a new tab) */

{img.title}

{pdfDoc === null && ( PDF )}
) : img.src ? ( /* Real poster — show the entire image at its natural ratio (no crop) */ // eslint-disable-next-line @next/next/no-img-element -- arbitrary remote image URLs; next/image would require per-host remotePatterns config and fixed dimensions. {img.title} ) : ( /* Decorative placeholder with pattern — needs a fixed ratio (no intrinsic size) */

{img.title}

)} {/* Zoom hint */}
{/* Info + download */}

{img.title}

{img.subtitle && (

{img.subtitle}

)}
handleDownload(img)} whileTap={{ scale: 0.9 }} transition={{ type: "spring", stiffness: 420, damping: 24 }} className="flex items-center gap-1.5 px-3.5 py-2 rounded-xl text-xs font-bold flex-shrink-0 ml-3" style={{ background: isDone ? "rgba(34,197,94,0.12)" : "rgba(255,107,0,0.12)", border: `1px solid ${isDone ? "rgba(34,197,94,0.3)" : "rgba(255,107,0,0.3)"}`, color: isDone ? "#22c55e" : "#FF6B00", }} > {isDone ? ( <> Saved ) : ( <> View )}
); })} {/* Fullscreen preview overlay */} setPreview(null)} /> setReading(null)} /> ); }