import React, { useState, useEffect } from "react"; import { motion, AnimatePresence } from "framer-motion"; import { Upload, FileText, Image, FileSpreadsheet, X, Sparkles, AlertCircle } from "lucide-react"; import { cn } from "@/lib/utils"; import { Input } from "@/components/ui/input"; // Allowed file types const ALLOWED_TYPES = [ "application/pdf", "image/png", "image/jpeg", "image/jpg", "image/tiff", "image/tif" ]; // Allowed file extensions (for fallback validation) const ALLOWED_EXTENSIONS = [".pdf", ".png", ".jpg", ".jpeg", ".tiff", ".tif"]; // Maximum file size: 4 MB const MAX_FILE_SIZE = 4 * 1024 * 1024; // 4 MB in bytes export default function UploadZone({ onFileSelect, selectedFile, onClear, keyFields = "", onKeyFieldsChange = () => {} }) { const [isDragging, setIsDragging] = useState(false); const [error, setError] = useState(null); const validateFile = (file) => { // Reset error setError(null); // Check file type const fileExtension = "." + file.name.split(".").pop().toLowerCase(); const isValidType = ALLOWED_TYPES.includes(file.type) || ALLOWED_EXTENSIONS.includes(fileExtension); if (!isValidType) { setError("Only PDF, PNG, JPG, and TIFF files are allowed."); return false; } // Check file size if (file.size > MAX_FILE_SIZE) { const fileSizeMB = (file.size / 1024 / 1024).toFixed(2); setError(`File size exceeds 4 MB limit. Your file is ${fileSizeMB} MB.`); return false; } return true; }; const handleFileSelect = (file) => { if (validateFile(file)) { setError(null); onFileSelect(file); } }; const handleDragOver = (e) => { e.preventDefault(); setIsDragging(true); }; const handleDragLeave = () => { setIsDragging(false); }; const handleDrop = (e) => { e.preventDefault(); setIsDragging(false); const file = e.dataTransfer.files[0]; if (file) { handleFileSelect(file); } }; const getFileIcon = (type) => { if (type?.includes("image")) return Image; if (type?.includes("spreadsheet") || type?.includes("excel")) return FileSpreadsheet; return FileText; }; const FileIcon = selectedFile ? getFileIcon(selectedFile.type) : FileText; // Clear error when file is cleared useEffect(() => { if (!selectedFile) { setError(null); } }, [selectedFile]); return (
{selectedFile.name}
{error}