import React, { useRef, useState } from 'react'; import { Upload, FileText, CheckCircle, X, Image as ImageIcon } from 'lucide-react'; import { FileData } from '../types'; interface FileUploadProps { label: string; description: string; accept: string; onFileSelect: (data: FileData | null) => void; selectedFile: FileData | null; } export const FileUpload: React.FC = ({ label, description, accept, onFileSelect, selectedFile }) => { const inputRef = useRef(null); const [isDragging, setIsDragging] = useState(false); const processFile = (file: File) => { // Create a robust Blob URL for previewing (works better than Base64 for PDFs) const objectUrl = URL.createObjectURL(file); const reader = new FileReader(); reader.onload = () => { const base64String = reader.result as string; // Remove data URL prefix for API usage const base64Content = base64String.split(',')[1]; onFileSelect({ file, previewUrl: objectUrl, base64: base64Content, type: file.type as any }); }; reader.readAsDataURL(file); }; const handleDrop = (e: React.DragEvent) => { e.preventDefault(); setIsDragging(false); if (e.dataTransfer.files && e.dataTransfer.files[0]) { processFile(e.dataTransfer.files[0]); } }; const handleChange = (e: React.ChangeEvent) => { if (e.target.files && e.target.files[0]) { processFile(e.target.files[0]); } }; const clearFile = (e: React.MouseEvent) => { e.stopPropagation(); onFileSelect(null); if (inputRef.current) inputRef.current.value = ''; }; return (
{!selectedFile ? (
inputRef.current?.click()} onDragOver={(e) => { e.preventDefault(); setIsDragging(true); }} onDragLeave={() => setIsDragging(false)} onDrop={handleDrop} className={` relative border-2 border-dashed rounded-xl p-8 text-center cursor-pointer transition-all duration-200 ${isDragging ? 'border-indigo-500 bg-indigo-50' : 'border-slate-300 hover:border-slate-400 hover:bg-slate-50'} `} >

Click to upload or drag and drop

{description}

) : (
{selectedFile.type === 'application/pdf' ? ( ) : ( Preview )}

{selectedFile.file.name}

{(selectedFile.file.size / 1024 / 1024).toFixed(2)} MB

)}
); };