import React, { useState, useEffect, useRef } from 'react'; import { Document } from '../types'; import { getDocuments, uploadDocument, deleteDocument } from '../services/api'; interface DocumentsModalProps { isOpen: boolean; onClose: () => void; } const DocumentsModal: React.FC = ({ isOpen, onClose }) => { const [documents, setDocuments] = useState([]); const [isLoading, setIsLoading] = useState(false); const [isUploading, setIsUploading] = useState(false); const [error, setError] = useState(null); const fileInputRef = useRef(null); useEffect(() => { if (isOpen) { loadDocuments(); } }, [isOpen]); const loadDocuments = async () => { try { setIsLoading(true); setError(null); const docs = await getDocuments(); setDocuments(docs); } catch (err) { setError('Failed to load documents'); } finally { setIsLoading(false); } }; const handleFileSelect = async (e: React.ChangeEvent) => { if (e.target.files && e.target.files[0]) { const file = e.target.files[0]; if (file.type !== 'application/pdf') { setError('Only PDF files are allowed'); return; } try { setIsUploading(true); setError(null); await uploadDocument(file); await loadDocuments(); // Reload list } catch (err) { setError(err instanceof Error ? err.message : 'Upload failed'); } finally { setIsUploading(false); // Reset input if (fileInputRef.current) fileInputRef.current.value = ''; } } }; const handleDelete = async (docId: string) => { try { if (!confirm('Are you sure you want to delete this document?')) return; setIsLoading(true); await deleteDocument(docId); await loadDocuments(); } catch (err) { setError(err instanceof Error ? err.message : 'Delete failed'); } finally { setIsLoading(false); } }; if (!isOpen) return null; return (
{/* Header */}

Manage Documents

Upload PDFs to include in the RAG knowledge base

{/* Content */}
{/* Upload Area */}
{/* Error Message */} {error && (
{error}
)} {/* Document List */}

Uploaded Documents

{isLoading && documents.length === 0 ? (
Loading...
) : documents.length === 0 ? (
No documents uploaded yet.
) : (
{documents.map((doc) => (

{doc.filename}

{new Date(doc.upload_date).toLocaleDateString()} • {doc.status}

))}
)}
{/* Footer */}

Documents are processed locally. Embeddings are stored in ChromaDB vector store.

); }; export default DocumentsModal;