import React, { useState, useEffect, useCallback } from 'react' import axios from 'axios' import { useDropzone } from 'react-dropzone' import { Upload, Trash2, Eye, RefreshCw, FileText, X } from 'lucide-react' const STAGES = ['input', 'parsed', 'generated', 'curated', 'final'] function formatBytes(bytes) { if (bytes < 1024) return `${bytes} B` if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB` return `${(bytes / (1024 * 1024)).toFixed(1)} MB` } export default function DocumentManager({ connected }) { const [stage, setStage] = useState('input') const [files, setFiles] = useState([]) const [loading, setLoading] = useState(false) const [uploading, setUploading] = useState(false) const [preview, setPreview] = useState(null) const [error, setError] = useState('') const fetchFiles = useCallback(async () => { if (!connected) return setLoading(true); setError('') try { const { data } = await axios.get(`/api/files/${stage}`) setFiles(data.files) } catch (err) { setError(err.response?.data?.detail || err.message) } finally { setLoading(false) } }, [stage, connected]) useEffect(() => { fetchFiles() }, [fetchFiles]) const onDrop = useCallback(async accepted => { if (!accepted.length || !connected) return setUploading(true) const formData = new FormData() formData.append('file', accepted[0]) try { await axios.post('/api/upload', formData) fetchFiles() } catch (err) { setError(err.response?.data?.detail || err.message) } finally { setUploading(false) } }, [connected, fetchFiles]) const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop, disabled: !connected || stage !== 'input', multiple: false, }) const deleteFile = async name => { if (!window.confirm(`Delete "${name}"?`)) return await axios.delete(`/api/files/${stage}/${name}`) fetchFiles() } const previewFile = async name => { const { data } = await axios.get(`/api/files/${stage}/${name}/preview`) setPreview({ name, content: data.content }) } return (
Uploading…
:{isDragActive ? 'Drop file here' : 'Drag & drop or click to upload to /input'}
}{error}
)} {loading ? (Loading…
) : files.length === 0 ? (No files in /{stage}
) : (| Name | Size | Modified | |
|---|---|---|---|
|
|
{formatBytes(f.size)} | {f.modified} |
|
{preview.content}