"use client" import { useState } from "react" import { Button } from "@/components/ui/button" import { Card, CardContent } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { X, ChevronLeft, ChevronRight, Download, ZoomIn, ZoomOut, RotateCw, Maximize2 } from "lucide-react" import { formatFileSize, type ImageFile } from "@/lib/image-utils" interface ImageGalleryProps { images: ImageFile[] className?: string } interface ImageModalProps { images: ImageFile[] currentIndex: number onClose: () => void onNavigate: (index: number) => void } function ImageModal({ images, currentIndex, onClose, onNavigate }: ImageModalProps) { const [zoom, setZoom] = useState(1) const [rotation, setRotation] = useState(0) const currentImage = images[currentIndex] const handlePrevious = () => { const newIndex = currentIndex > 0 ? currentIndex - 1 : images.length - 1 onNavigate(newIndex) setZoom(1) setRotation(0) } const handleNext = () => { const newIndex = currentIndex < images.length - 1 ? currentIndex + 1 : 0 onNavigate(newIndex) setZoom(1) setRotation(0) } const handleZoomIn = () => setZoom((prev) => Math.min(prev + 0.25, 3)) const handleZoomOut = () => setZoom((prev) => Math.max(prev - 0.25, 0.25)) const handleRotate = () => setRotation((prev) => (prev + 90) % 360) const handleDownload = () => { // 創建下載連結 const link = document.createElement("a") link.href = currentImage.url link.download = currentImage.name document.body.appendChild(link) link.click() document.body.removeChild(link) } return (
{/* 關閉按鈕 */} {/* 工具列 */}
{currentIndex + 1} / {images.length}
{Math.round(zoom * 100)}%
{/* 導航按鈕 */} {images.length > 1 && ( <> )} {/* 圖片容器 */}
{currentImage.name} { // 如果圖片載入失敗,顯示錯誤訊息 const target = e.target as HTMLImageElement target.src = "/placeholder.svg?height=400&width=400&text=圖片載入失敗" }} />
{/* 圖片資訊 */}
{currentImage.name}
{formatFileSize(currentImage.size)} • {currentImage.type}
) } export default function ImageGallery({ images, className = "" }: ImageGalleryProps) { const [modalOpen, setModalOpen] = useState(false) const [currentImageIndex, setCurrentImageIndex] = useState(0) if (images.length === 0) return null const openModal = (index: number) => { setCurrentImageIndex(index) setModalOpen(true) } const closeModal = () => { setModalOpen(false) } return ( <>
📷 相關圖片 ({images.length})
{images.map((image, index) => (
openModal(index)}>
{image.name} { // 如果圖片載入失敗,顯示預設圖片 const target = e.target as HTMLImageElement target.src = "/placeholder.svg?height=200&width=200&text=圖片載入失敗" }} /> {/* 懸停覆蓋層 */}
點擊放大
{/* 檔案名稱 */}
{image.name}
))}
{/* 圖片模態框 */} {modalOpen && ( )} ) }