"use client"; import { useState, useEffect, useCallback } from "react"; import { X, Download, ZoomIn, ZoomOut, RotateCw } from "lucide-react"; import { Button } from "@/components/ui/button"; import { useTranslations } from "next-intl"; interface ImagePreviewModalProps { name: string; onClose: () => void; onDownload: (name: string) => Promise; getImageUrl: (name: string) => Promise; } export function ImagePreviewModal({ name, onClose, onDownload, getImageUrl }: ImagePreviewModalProps) { const t = useTranslations("files"); const [imageUrl, setImageUrl] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(false); const [zoom, setZoom] = useState(1); const [rotation, setRotation] = useState(0); useEffect(() => { let revoke: string | null = null; setLoading(true); setError(false); getImageUrl(name) .then((url) => { revoke = url; setImageUrl(url); setLoading(false); }) .catch(() => { setError(true); setLoading(false); }); return () => { if (revoke) URL.revokeObjectURL(revoke); }; }, [name, getImageUrl]); const handleKeyDown = useCallback((e: KeyboardEvent) => { if (e.key === "Escape") onClose(); if (e.key === "+" || e.key === "=") setZoom((z) => Math.min(z + 0.25, 5)); if (e.key === "-") setZoom((z) => Math.max(z - 0.25, 0.25)); if (e.key === "r") setRotation((r) => r + 90); }, [onClose]); useEffect(() => { window.addEventListener("keydown", handleKeyDown); return () => window.removeEventListener("keydown", handleKeyDown); }, [handleKeyDown]); return (
{/* Header */}
{name}
{/* Image */}
e.stopPropagation()}> {loading && (
)} {error && (

{t("preview_error")}

)} {imageUrl && !error && ( {name} setLoading(false)} draggable={false} /> )}
); }