/** * ImagePreviewModal - Modal for previewing images in SFTP */ import { Loader2, Maximize2, Minus, Plus, RotateCcw } from 'lucide-react'; import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { useI18n } from '../application/i18n/I18nProvider'; import { getImageMimeType } from '../lib/sftpFileUtils'; import { Button } from './ui/button'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from './ui/dialog'; interface ImagePreviewModalProps { open: boolean; onClose: () => void; fileName: string; imageData: ArrayBuffer | null; loading?: boolean; } export const ImagePreviewModal: React.FC = ({ open, onClose, fileName, imageData, loading = false, }) => { const { t } = useI18n(); const containerRef = useRef(null); const imageRef = useRef(null); const [zoom, setZoom] = useState(1); const [position, setPosition] = useState({ x: 0, y: 0 }); const [isDragging, setIsDragging] = useState(false); const [dragStart, setDragStart] = useState({ x: 0, y: 0 }); // Create blob URL for the image const imageUrl = useMemo(() => { if (!imageData) return null; const mimeType = getImageMimeType(fileName); const blob = new Blob([imageData], { type: mimeType }); return URL.createObjectURL(blob); }, [imageData, fileName]); // Cleanup blob URL on unmount useEffect(() => { return () => { if (imageUrl) { URL.revokeObjectURL(imageUrl); } }; }, [imageUrl]); // Reset zoom and position when image changes useEffect(() => { setZoom(1); setPosition({ x: 0, y: 0 }); }, [imageData]); const handleZoomIn = useCallback(() => { setZoom(prev => Math.min(prev * 1.25, 5)); }, []); const handleZoomOut = useCallback(() => { setZoom(prev => Math.max(prev / 1.25, 0.1)); }, []); const handleResetZoom = useCallback(() => { setZoom(1); setPosition({ x: 0, y: 0 }); }, []); const handleFitToWindow = useCallback(() => { if (!imageRef.current || !containerRef.current) return; const container = containerRef.current.getBoundingClientRect(); const img = imageRef.current; const naturalWidth = img.naturalWidth || img.width; const naturalHeight = img.naturalHeight || img.height; if (naturalWidth && naturalHeight) { const scaleX = (container.width - 40) / naturalWidth; const scaleY = (container.height - 40) / naturalHeight; const fitZoom = Math.min(scaleX, scaleY, 1); setZoom(fitZoom); setPosition({ x: 0, y: 0 }); } }, []); // Mouse wheel zoom const handleWheel = useCallback((e: React.WheelEvent) => { e.preventDefault(); const delta = e.deltaY > 0 ? 0.9 : 1.1; setZoom(prev => Math.max(0.1, Math.min(5, prev * delta))); }, []); // Drag to pan const handleMouseDown = useCallback((e: React.MouseEvent) => { if (e.button !== 0) return; setIsDragging(true); setDragStart({ x: e.clientX - position.x, y: e.clientY - position.y }); }, [position]); const handleMouseMove = useCallback((e: React.MouseEvent) => { if (!isDragging) return; setPosition({ x: e.clientX - dragStart.x, y: e.clientY - dragStart.y, }); }, [isDragging, dragStart]); const handleMouseUp = useCallback(() => { setIsDragging(false); }, []); // Keyboard shortcuts const handleKeyDown = useCallback((e: React.KeyboardEvent) => { if (e.key === '+' || e.key === '=') { e.preventDefault(); handleZoomIn(); } else if (e.key === '-') { e.preventDefault(); handleZoomOut(); } else if (e.key === '0') { e.preventDefault(); handleResetZoom(); } }, [handleZoomIn, handleZoomOut, handleResetZoom]); return ( !isOpen && onClose()}> {/* Header */}
{fileName}
{Math.round(zoom * 100)}%
{/* Image viewer */}
{loading && (
{t('sftp.status.loading')}
)} {!loading && !imageUrl && (
{t('sftp.error.loadFailed')}
)} {!loading && imageUrl && ( {fileName} )}
); }; export default ImagePreviewModal;