import React, { useState, useRef } from 'react'; import { Upload, AlertCircle, FileText, X, Check } from 'lucide-react'; import { cn } from "@/lib/utils"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/Components/ui/card"; import { Button } from "@/Components/ui/button"; import { Alert, AlertDescription } from "@/Components/ui/alert"; export default function FileUpload({ onFileUpload, maxSize = 5 }: { onFileUpload: (file: File) => void; maxSize?: number; }) { const [dragActive, setDragActive] = useState(false); const [error, setError] = useState(null); const [selectedFile, setSelectedFile] = useState(null); const inputRef = useRef(null); const handleDrag = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); if (e.type === "dragenter" || e.type === "dragover") { setDragActive(true); } else if (e.type === "dragleave") { setDragActive(false); } }; const validateFile = (file: File): boolean => { setError(null); // Check file type if (file.type !== 'text/plain' && !file.name.endsWith('.txt')) { setError('Only .txt files are allowed'); return false; } // Check file size if (file.size > maxSize * 1024 * 1024) { setError(`File size must be less than ${maxSize}MB`); return false; } return true; }; const handleDrop = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setDragActive(false); const file = e.dataTransfer.files?.[0]; if (file && validateFile(file)) { setSelectedFile(file); onFileUpload(file); } }; const handleChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file && validateFile(file)) { setSelectedFile(file); onFileUpload(file); } }; const handleClick = () => { inputRef.current?.click(); }; const handleRemove = () => { setSelectedFile(null); setError(null); if (inputRef.current) { inputRef.current.value = ''; } }; return ( Upload Text File Drag and drop a .txt file or click to browse
{selectedFile ? (

{selectedFile.name}

{(selectedFile.size / 1024).toFixed(1)} KB

) : (

or drag and drop it here

Only .txt files up to {maxSize}MB are supported

)}
{error && ( {error} )}
); }