'use client'; import { useState } from 'react'; import { formatBytes } from '@/lib/format'; import { documentToasts } from '@/lib/toast'; import { InlineLoading } from '@/components/shared/LoadingSpinner'; import { DocumentStatusBadge } from '@/components/shared/DocumentStatusBadge'; import type { Document } from '@/types/document'; import { DOCUMENT_STATUS } from '@/lib/constants'; interface DocumentListPanelProps { documents: Document[]; loading: boolean; selectedDocId: string | null; onSelectDoc: (id: string | null) => void; onDocumentsChange: (updater: (prev: Document[]) => Document[]) => void; onOpenAddToBot?: (doc: Document) => void; } export const DocumentListPanel = ({ documents, loading, selectedDocId, onSelectDoc, onDocumentsChange, onOpenAddToBot, }: DocumentListPanelProps) => { const [processing, setProcessing] = useState(null); const handleProcess = async (documentId: string) => { setProcessing(documentId); try { const response = await fetch('/api/documents/process', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ documentId }), }); const data = await response.json(); if (data.success) { onDocumentsChange((prev) => prev.map((doc) => (doc.id === documentId ? data.document : doc)), ); documentToasts.processSuccess(data.chunks_created); } else { documentToasts.processError(data.error); // Reload documents to get error state const listResponse = await fetch('/api/documents'); const listData = await listResponse.json(); if (listData.success) { onDocumentsChange(() => listData.documents); } } } catch { documentToasts.processError(); } finally { setProcessing(null); } }; const handleDelete = async (documentId: string) => { if (!confirm('Are you sure you want to delete this document?')) return; try { const response = await fetch(`/api/documents?id=${documentId}`, { method: 'DELETE', }); const data = await response.json(); if (data.success) { onDocumentsChange((prev) => prev.filter((doc) => doc.id !== documentId)); documentToasts.deleteSuccess(); } else { documentToasts.deleteError(data.error); } } catch { documentToasts.deleteError(); } }; return (

Documents

{loading ? ( ) : documents.length === 0 ? (

No documents yet

Upload a PDF, TXT, or MD file to get started

) : (
{documents.map((doc) => (

{doc.name}

{formatBytes(doc.size_bytes || 0)} {doc.chunk_count ? ` • ${doc.chunk_count} chunks` : ''}

{doc.error_message && (

{doc.error_message}

)}
{doc.status === DOCUMENT_STATUS.PENDING && ( )} {doc.status === DOCUMENT_STATUS.READY && ( <> {onOpenAddToBot && ( )} )}
))}
)}
); };