'use client'; import * as React from 'react'; import { Upload, X } from 'lucide-react'; import { cn } from '@/lib/utils/cn'; import { formatFileSize } from '@/lib/utils/fileUtils'; import { Button } from '@/components/ui/Button'; export interface FileUploadProps { onFileSelect: (file: File) => void; onFileRemove: () => void; selectedFile?: File; accept?: string; maxSizeMB?: number; disabled?: boolean; } export function FileUpload({ onFileSelect, onFileRemove, selectedFile, accept, maxSizeMB = 500, disabled = false, }: FileUploadProps) { const [isDragging, setIsDragging] = React.useState(false); const fileInputRef = React.useRef(null); const handleDragEnter = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); if (!disabled) { setIsDragging(true); } }; const handleDragLeave = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setIsDragging(false); }; const handleDragOver = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); }; const handleDrop = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setIsDragging(false); if (disabled) return; const files = Array.from(e.dataTransfer.files); if (files.length > 0) { handleFile(files[0]); } }; const handleFileInput = (e: React.ChangeEvent) => { const files = Array.from(e.target.files || []); if (files.length > 0) { handleFile(files[0]); } }; const handleFile = (file: File) => { // Check file size const maxBytes = maxSizeMB * 1024 * 1024; if (file.size > maxBytes) { alert(`File size exceeds ${maxSizeMB}MB limit. Please choose a smaller file.`); return; } onFileSelect(file); }; const handleClick = () => { if (!disabled) { fileInputRef.current?.click(); } }; const handleRemove = (e: React.MouseEvent) => { e.stopPropagation(); onFileRemove(); if (fileInputRef.current) { fileInputRef.current.value = ''; } }; return (
{selectedFile ? (

{selectedFile.name}

{formatFileSize(selectedFile.size)}

) : (

Drop your file here or click to browse

Maximum file size: {maxSizeMB}MB

)}
); }