'use client'; import { useState, useRef, useCallback } from 'react'; import Image from 'next/image'; interface PhotoUploadModalProps { isOpen: boolean; onClose: () => void; onUpload: (file: File) => Promise; currentPhoto?: string | null; } export function PhotoUploadModal({ isOpen, onClose, onUpload, currentPhoto }: PhotoUploadModalProps) { const [selectedFile, setSelectedFile] = useState(null); const [previewUrl, setPreviewUrl] = useState(null); const [isDragging, setIsDragging] = useState(false); const [isUploading, setIsUploading] = useState(false); const [error, setError] = useState(null); const fileInputRef = useRef(null); const validateFile = (file: File): string | null => { const allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp']; const maxSize = 5 * 1024 * 1024; // 5MB if (!allowedTypes.includes(file.type)) { return 'Only JPG, PNG, GIF, and WebP images are allowed'; } if (file.size > maxSize) { return 'File size must be less than 5MB'; } return null; }; const handleFileSelect = (file: File) => { const validationError = validateFile(file); if (validationError) { setError(validationError); return; } setError(null); setSelectedFile(file); // Create preview URL const reader = new FileReader(); reader.onload = (e) => { setPreviewUrl(e.target?.result as string); }; reader.readAsDataURL(file); }; const handleInputChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { handleFileSelect(file); } }; const handleDragOver = useCallback((e: React.DragEvent) => { e.preventDefault(); setIsDragging(true); }, []); const handleDragLeave = useCallback((e: React.DragEvent) => { e.preventDefault(); setIsDragging(false); }, []); const handleDrop = useCallback((e: React.DragEvent) => { e.preventDefault(); setIsDragging(false); const file = e.dataTransfer.files?.[0]; if (file) { handleFileSelect(file); } }, []); const handleUpload = async () => { if (!selectedFile) return; try { setIsUploading(true); setError(null); await onUpload(selectedFile); handleClose(); } catch (err) { console.error('Upload failed:', err); setError('Failed to upload photo. Please try again.'); } finally { setIsUploading(false); } }; const handleClose = () => { setSelectedFile(null); setPreviewUrl(null); setError(null); setIsDragging(false); onClose(); }; const handleBrowseClick = () => { fileInputRef.current?.click(); }; if (!isOpen) return null; return (
{/* Backdrop */}
{/* Modal */}
{/* Header */}

Upload Profile Photo

{/* Content */}
{/* Error Message */} {error && (

{error}

)} {/* Preview or Upload Area */} {previewUrl ? (
Preview
) : (
{/* Current Photo Preview */} {currentPhoto && (
Current photo
)}

Drag and drop your photo here

or{' '}

JPG, PNG, GIF, WebP • Max 5MB

)}
{/* Footer */}
); }