import React, { useCallback, useState } from 'react'; import { useDropzone } from 'react-dropzone'; import { Upload, X, FileText } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { cn } from '@/lib/utils'; interface FileUploadProps { onFileSelect: (file: File) => void; onFileRemove: () => void; selectedFile?: File; currentFileUrl?: string; accept?: string; maxSize?: number; className?: string; } export function FileUpload({ onFileSelect, onFileRemove, selectedFile, currentFileUrl, accept = '.pdf,.jpg,.jpeg,.png', maxSize = 5 * 1024 * 1024, // 5MB className }: FileUploadProps) { const [error, setError] = useState(''); const onDrop = useCallback((acceptedFiles: File[], rejectedFiles: any[]) => { setError(''); if (rejectedFiles.length > 0) { const rejection = rejectedFiles[0]; if (rejection.errors[0]?.code === 'file-too-large') { setError('File is too large. Maximum size is 5MB.'); } else if (rejection.errors[0]?.code === 'file-invalid-type') { setError('Invalid file type. Please upload PDF, JPG, or PNG files.'); } else { setError('File upload failed. Please try again.'); } return; } if (acceptedFiles.length > 0) { onFileSelect(acceptedFiles[0]); } }, [onFileSelect]); const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop, accept: { 'application/pdf': ['.pdf'], 'image/jpeg': ['.jpg', '.jpeg'], 'image/png': ['.png'] }, maxSize, multiple: false }); const hasFile = selectedFile || currentFileUrl; return (
{!hasFile ? (

{isDragActive ? ( 'Drop the file here...' ) : ( <> Drag & drop an invoice file here, or{' '} click to browse )}

PDF, JPG, PNG up to 5MB

) : (
{selectedFile ? selectedFile.name : 'Current invoice file'}
)} {error && (

{error}

)}
); }