import React, { useState, useRef } from 'react'; import { UploadCloud, Link as LinkIcon, AlertCircle, CheckCircle } from 'lucide-react'; import { processSourceText } from '../../lib/extractionPipeline'; import Card from '../ui/Card'; import Button from '../ui/Button'; import Input from '../ui/Input'; const UploadZone = ({ onUploadComplete }) => { const [isDragging, setIsDragging] = useState(false); const [url, setUrl] = useState(''); const [isProcessing, setIsProcessing] = useState(false); const [status, setStatus] = useState(null); // { type: 'error' | 'success', msg: string } const fileInputRef = useRef(null); const handleDragOver = (e) => { e.preventDefault(); setIsDragging(true); }; const handleDragLeave = () => setIsDragging(false); const processFile = async (file) => { setIsProcessing(true); setStatus(null); try { const text = await file.text(); await processSourceText(text, file.name); setStatus({ type: 'success', msg: `Successfully processed: ${file.name}` }); if (onUploadComplete) onUploadComplete(); } catch (error) { setStatus({ type: 'error', msg: `Error processing file: ${error.message}` }); } finally { setIsProcessing(false); } }; const handleDrop = (e) => { e.preventDefault(); setIsDragging(false); if (e.dataTransfer.files?.length > 0) { const file = e.dataTransfer.files[0]; if (file.type === 'text/plain' || file.name.endsWith('.md')) { processFile(file); } else { setStatus({ type: 'error', msg: 'Only .txt and .md files are currently supported.' }); } } }; const handleFileSelect = (e) => { if (e.target.files?.length > 0) processFile(e.target.files[0]); }; const handleUrlSubmit = async (e) => { e.preventDefault(); setStatus({ type: 'error', msg: 'URL import is disabled in the browser due to CORS restrictions.' }); }; return (
fileInputRef.current?.click()} >

Drag files here

Supports .txt and .md (max 5MB)

OR
setUrl(e.target.value)} disabled={isProcessing} />
{status && (
{status.type === 'error' ? : }

{status.type === 'error' ? 'Error' : 'Success'}

{status.msg}

)}
); }; export default UploadZone;