import { useState, useRef } from 'react'; import { uploadDocument } from '../services/api'; export default function FileUpload({ onUploadSuccess, documentCount }) { const [isDragging, setIsDragging] = useState(false); const [isUploading, setIsUploading] = useState(false); const [progress, setProgress] = useState(''); const [error, setError] = useState(''); const fileInputRef = useRef(null); const isLimitReached = typeof documentCount === 'number' && documentCount >= 3; const handleDragOver = (e) => { e.preventDefault(); if (!isLimitReached) setIsDragging(true); }; const handleDragLeave = (e) => { e.preventDefault(); setIsDragging(false); }; const handleDrop = (e) => { e.preventDefault(); setIsDragging(false); if (!isLimitReached) { const files = e.dataTransfer.files; if (files.length > 0) handleFile(files[0]); } }; const handleFileSelect = (e) => { if (!isLimitReached && e.target.files.length > 0) handleFile(e.target.files[0]); }; const handleFile = async (file) => { if (isLimitReached) { setError('Maximum limit of 3 documents reached.'); return; } if (!file.name.toLowerCase().endsWith('.pdf')) { setError('Only PDF files are supported'); return; } if (file.size > 50 * 1024 * 1024) { setError('File size must be under 50MB'); return; } setError(''); setIsUploading(true); setProgress('Uploading and processing...'); try { const result = await uploadDocument(file); setProgress(`✅ ${result.message}`); onUploadSuccess?.(result.document); setTimeout(() => setProgress(''), 3000); } catch (err) { setError(err.message); } finally { setIsUploading(false); if (fileInputRef.current) fileInputRef.current.value = ''; } }; return (
{progress}
> ) : ( <>{isLimitReached ? ( Limit reached (3 max). Delete a file to upload. ) : ( <>Drop your PDF here or browse> )}
Supports PDF up to 50MB
> )}