import { useId, useRef, useState } from 'react'; const MAX_FILES = 20; const MAX_SIZE_MB = 250; const MAX_SIZE_BYTES = MAX_SIZE_MB * 1024 * 1024; const formatSize = (bytes) => { if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB'; return (bytes / (1024 * 1024)).toFixed(1) + ' MB'; }; export default function FileAttachment({ files, onChange }) { const inputId = useId(); const [errors, setErrors] = useState([]); const [dragging, setDragging] = useState(false); const dragCounter = useRef(0); const processFiles = (incoming) => { const combined = [...files, ...incoming]; const errs = []; if (combined.length > MAX_FILES) errs.push(`Maximum ${MAX_FILES} files allowed.`); incoming.filter(f => f.size > MAX_SIZE_BYTES).forEach(f => errs.push(`"${f.name}" exceeds ${MAX_SIZE_MB} MB limit.`)); if (errs.length > 0) { setErrors(errs); return; } setErrors([]); onChange(combined); }; const handleChange = (e) => { processFiles(Array.from(e.target.files)); e.target.value = ''; }; const handleDragEnter = (e) => { e.preventDefault(); dragCounter.current++; setDragging(true); }; const handleDragLeave = (e) => { e.preventDefault(); dragCounter.current--; if (dragCounter.current === 0) setDragging(false); }; const handleDragOver = (e) => { e.preventDefault(); }; const handleDrop = (e) => { e.preventDefault(); dragCounter.current = 0; setDragging(false); const dropped = Array.from(e.dataTransfer.files); if (dropped.length > 0) processFiles(dropped); }; const remove = (index) => { setErrors([]); onChange(files.filter((_, i) => i !== index)); }; return (