'use client' import { useCallback, useState } from 'react' import { useDropzone } from 'react-dropzone' import { motion, AnimatePresence } from 'framer-motion' import { Upload, Image, Video, FileText, X, AlertCircle } from 'lucide-react' import { cn } from '@/lib/utils' import { Button } from './ui/button' import toast from 'react-hot-toast' interface UploadZoneProps { onUpload?: (files: File[]) => void onDragChange?: (isDragging: boolean) => void accept?: Record maxSize?: number maxFiles?: number className?: string } export function UploadZone({ onUpload, onDragChange, accept = { 'image/*': ['.png', '.jpg', '.jpeg', '.gif', '.webp'], 'video/*': ['.mp4', '.webm', '.mov'], }, maxSize = 50 * 1024 * 1024, // 50MB maxFiles = 10, className, }: UploadZoneProps) { const [files, setFiles] = useState([]) const [isProcessing, setIsProcessing] = useState(false) const onDrop = useCallback( async (acceptedFiles: File[], rejectedFiles: any[]) => { if (rejectedFiles.length > 0) { const errors = rejectedFiles.map((file) => { if (file.errors[0]?.code === 'file-too-large') { return `${file.file.name} is too large (max ${maxSize / 1024 / 1024}MB)` } return `${file.file.name} is not supported` }) toast.error(errors.join(', ')) return } setFiles(acceptedFiles) if (onUpload) { setIsProcessing(true) try { await onUpload(acceptedFiles) } catch (error) { toast.error('Failed to upload files') } finally { setIsProcessing(false) } } }, [onUpload, maxSize] ) const { getRootProps, getInputProps, isDragActive, isDragReject } = useDropzone({ onDrop, accept, maxSize, maxFiles, onDragEnter: () => onDragChange?.(true), onDragLeave: () => onDragChange?.(false), onDropAccepted: () => onDragChange?.(false), onDropRejected: () => onDragChange?.(false), }) const getFileIcon = (file: File) => { if (file.type.startsWith('image/')) return if (file.type.startsWith('video/')) return