Files
co-lite/solvway-customer-portal/app/components/ui/ImageGallery.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

216 lines
8.5 KiB
TypeScript

"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<Set<string>>(new Set());
const [preview, setPreview] = useState<GalleryImage | null>(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<Record<string, PdfDocument | null>>({});
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 (
<>
<motion.div
variants={stagger}
initial="hidden"
animate="visible"
className="space-y-4"
>
{images.map((img) => {
const isDone = downloaded.has(img.id);
const isPdf = isPdfSrc(img.src);
const pdfDoc = pdfs[img.id];
return (
<motion.div
key={img.id}
variants={cardAnim}
className="rounded-2xl overflow-hidden"
style={{
background: t.card,
border: `1px solid ${t.border}`,
boxShadow: t.shadow,
}}
>
{/* Image area */}
<motion.button
onClick={() => openEntry(img)}
whileTap={{ scale: 0.98 }}
className="relative w-full block"
>
{isPdf && pdfDoc ? (
/* PDF upload — preview page 1; tapping opens the in-app reader */
<div className="w-full aspect-[16/10] overflow-hidden bg-white">
<PdfPages doc={pdfDoc} title={img.title} maxPages={1} />
</div>
) : isPdf ? (
/* PDF loading, or failed to load (then tapping opens it in a new tab) */
<div
className="relative w-full aspect-[16/10] flex flex-col items-center justify-center gap-2"
style={{ background: img.gradient }}
>
<FileText strokeWidth={1.75} className="w-12 h-12 stroke-white/85" />
<p className="text-xl font-extrabold text-white/80 tracking-tight">{img.title}</p>
{pdfDoc === null && (
<span className="px-2 py-0.5 rounded-md text-xs font-black tracking-[0.2em] text-white bg-black/30">
PDF
</span>
)}
</div>
) : 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
src={img.src}
alt={img.title}
className="w-full h-auto block"
/>
) : (
/* Decorative placeholder with pattern — needs a fixed ratio (no intrinsic size) */
<div
className="relative w-full aspect-[16/10] flex items-center justify-center"
style={{ background: img.gradient }}
>
<div
className="absolute inset-0 stripe-animate opacity-30 pointer-events-none"
/>
<div
className="absolute inset-0 pointer-events-none"
style={{
background: "radial-gradient(ellipse at 30% 30%, rgba(255,255,255,0.15) 0%, transparent 60%)",
}}
/>
<p
className="relative text-xl font-extrabold text-white/70 tracking-tight"
style={{ textShadow: "0 2px 8px rgba(0,0,0,0.3)" }}
>
{img.title}
</p>
</div>
)}
{/* Zoom hint */}
<div
className="absolute top-3 right-3 w-8 h-8 rounded-xl flex items-center justify-center"
style={{
background: "rgba(0,0,0,0.35)",
backdropFilter: "blur(8px)",
border: "1px solid rgba(255,255,255,0.15)",
}}
>
<ZoomIn strokeWidth={2} className="w-4 h-4 stroke-white/80" />
</div>
</motion.button>
{/* Info + download */}
<div className="flex items-center justify-between px-4 py-3">
<div className="flex-1 min-w-0">
<p className="text-sm font-bold leading-tight" style={{ color: t.text }}>
{img.title}
</p>
{img.subtitle && (
<p className="text-xs mt-0.5" style={{ color: t.textMuted }}>
{img.subtitle}
</p>
)}
</div>
<motion.button
onClick={() => 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 ? (
<><CheckCircle2 strokeWidth={2} className="w-3.5 h-3.5" /> Saved</>
) : (
<><Download strokeWidth={2} className="w-3.5 h-3.5" /> View</>
)}
</motion.button>
</div>
</motion.div>
);
})}
</motion.div>
{/* Fullscreen preview overlay */}
<ImageLightbox image={preview} onClose={() => setPreview(null)} />
<PdfReader pdf={reading} onClose={() => setReading(null)} />
</>
);
}