"use client" import { useState, useEffect } from "react" import Image from "next/image" import { X, ZoomIn, Loader2, RefreshCw, AlertCircle } from "lucide-react" import { Button } from "@/components/ui/button" interface ImagePreviewProps { src: string alt: string className?: string } export function ImagePreview({ src, alt, className = "" }: ImagePreviewProps) { const [isOpen, setIsOpen] = useState(false) const [imageError, setImageError] = useState(false) const [isLoading, setIsLoading] = useState(true) const [lightboxLoading, setLightboxLoading] = useState(true) const [lightboxError, setLightboxError] = useState(false) const [retryKey, setRetryKey] = useState(0) // Handle ESC key to close modal useEffect(() => { if (!isOpen) return const handleEscape = (e: KeyboardEvent) => { if (e.key === "Escape") { setIsOpen(false) } } // Prevent body scroll when modal is open document.body.style.overflow = "hidden" window.addEventListener("keydown", handleEscape) return () => { window.removeEventListener("keydown", handleEscape) document.body.style.overflow = "unset" } }, [isOpen]) const handleImageLoad = () => { setIsLoading(false) } const handleImageError = () => { setImageError(true) setIsLoading(false) } const handleLightboxImageLoad = () => { setLightboxLoading(false) } const handleLightboxImageError = () => { setLightboxError(true) setLightboxLoading(false) } const handleRetry = () => { setImageError(false) setIsLoading(true) setLightboxError(false) setLightboxLoading(true) // Force reload by updating retry key which will change the src setRetryKey(prev => prev + 1) } // Add cache-busting parameter for retry const imageSrc = retryKey > 0 ? (src.includes("?") ? `${src}&retry=${retryKey}` : `${src}?retry=${retryKey}`) : src const handleOpen = () => { setIsOpen(true) setLightboxLoading(true) setLightboxError(false) } const handleClose = () => { setIsOpen(false) } // Error state for thumbnail if (imageError) { return (
Failed to load image
Failed to load image