import { useCallback, useRef, useState } from 'react'; import { Upload, X, FileText, AlertCircle, CheckCircle2 } from 'lucide-react'; interface FileUploadProps { label: string; accept: string; icon?: React.ReactNode; file: File | null; onFile: (file: File | null) => void; description?: string; id: string; } function formatSize(bytes: number): string { if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; } export function FileUploadPanel({ label, accept, icon, file, onFile, description, id }: FileUploadProps) { const [isDragging, setIsDragging] = useState(false); const inputRef = useRef(null); const handleDrop = useCallback( (e: React.DragEvent) => { e.preventDefault(); setIsDragging(false); const dropped = e.dataTransfer.files[0]; if (dropped) onFile(dropped); }, [onFile] ); const handleDragOver = useCallback((e: React.DragEvent) => { e.preventDefault(); setIsDragging(true); }, []); const handleDragLeave = useCallback(() => setIsDragging(false), []); const handleChange = useCallback( (e: React.ChangeEvent) => { const selected = e.target.files?.[0]; if (selected) onFile(selected); }, [onFile] ); return (
{file ? (

{file.name}

{formatSize(file.size)}

) : (
inputRef.current?.click()} role="button" tabIndex={0} id={id} onKeyDown={(e) => e.key === 'Enter' && inputRef.current?.click()} >
{icon || }

{isDragging ? 'Drop it here' : 'Drop file here'}

or browse

{description &&

{description}

}
)}
); }