import { useEffect, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import i18n from '../i18n/index.js'; import { DISABLED, INPUT_CLS } from '../lib/styles.js'; import { type KnowledgeDocument, useKbStore } from '../stores/knowledge-base.js'; import { EmptyState } from './EmptyState.js'; import { ChevronDown, Trash } from './Icons.js'; import { LoadingDots } from './LoadingDots.js'; import { LoadMore } from './LoadMore.js'; import { PanelForm } from './PanelForm.js'; import { PanelHeader } from './PanelHeader.js'; function statusBadge(status: string | null) { const s = status ?? 'processing'; const colors: Record = { ready: 'bg-green-900/30 text-green-400 border-green-800/30', processing: 'bg-yellow-900/30 text-yellow-400 border-yellow-800/30', failed: 'bg-red-900/30 text-red-400 border-red-800/30', }; const labels: Record = { ready: i18n.t('knowledge.statusReady'), processing: i18n.t('knowledge.statusProcessing'), failed: i18n.t('knowledge.statusFailed'), outdated: i18n.t('features.excluded'), }; return ( {labels[s] ?? s} ); } function DocRow({ doc, onDelete, onRetry, }: { doc: KnowledgeDocument; onDelete: () => void; onRetry?: () => void; }) { const { t } = useTranslation(); return (
{doc.filename} {statusBadge(doc.status)} {doc.chunk_count != null && doc.status === 'ready' && ( {t('knowledge.chunkCount', { count: doc.chunk_count })} )}
{(doc.status === 'failed' || doc.status === 'outdated') && onRetry && ( )}
); } function KbCard({ kbId }: { kbId: string }) { const { t } = useTranslation(); const kbs = useKbStore((s) => s.kbs); const documents = useKbStore((s) => s.documents); const documentsTotal = useKbStore((s) => s.documentsTotal); const loadingMoreDocs = useKbStore((s) => s.loadingMoreDocs); const fetchDocuments = useKbStore((s) => s.fetchDocuments); const loadMoreDocuments = useKbStore((s) => s.loadMoreDocuments); const uploadDocument = useKbStore((s) => s.uploadDocument); const deleteDocument = useKbStore((s) => s.deleteDocument); const deleteKb = useKbStore((s) => s.deleteKb); const setKbShared = useKbStore((s) => s.setKbShared); const retryDocument = useKbStore((s) => s.retryDocument); const uploading = useKbStore((s) => s.uploading); const kb = kbs.find((k) => k.id === kbId); const [expanded, setExpanded] = useState(false); const [shareError, setShareError] = useState(false); const fileRef = useRef(null); if (!kb) return null; const docs = documents[kbId]; const handleExpand = async () => { if (!expanded && !docs) await fetchDocuments(kbId); setExpanded((v) => !v); }; // The store reverts its optimistic flip and rethrows. Something has to catch // that: an async onClick handler that rejects is an unhandled rejection, and // the chip would silently snap back with no explanation — the same shape that // once left MemoryPage's form failing in silence. const handleToggleShared = async () => { setShareError(false); try { await setKbShared(kbId, !kb.shared_with_peers); } catch { setShareError(true); } }; const handleUpload = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; await uploadDocument(kbId, file); e.target.value = ''; if (!expanded) setExpanded(true); }; return (
{/* Reads as a state chip rather than a switch, because "off" is the normal condition and should look unremarkable — it is being ON that is worth noticing across a list of bases. */}
{expanded && (
{!docs ? (

{t('common.loading')}

) : docs.length === 0 ? (

{t('knowledge.docsEmpty')}

) : ( <> {docs.map((doc) => ( deleteDocument(kbId, doc.id)} onRetry={() => retryDocument(kbId, doc.id)} /> ))} loadMoreDocuments(kbId)} /> )}
)}
); } export function KnowledgePage() { const { t } = useTranslation(); const kbs = useKbStore((s) => s.kbs); const loading = useKbStore((s) => s.loading); const fetchKbs = useKbStore((s) => s.fetchKbs); const createKb = useKbStore((s) => s.createKb); const [showForm, setShowForm] = useState(false); const [name, setName] = useState(''); const [description, setDescription] = useState(''); const [saving, setSaving] = useState(false); useEffect(() => { fetchKbs(); }, [fetchKbs]); const handleCreate = async () => { if (!name.trim()) return; setSaving(true); try { await createKb(name.trim(), description.trim() || undefined); setName(''); setDescription(''); setShowForm(false); } finally { setSaving(false); } }; return (
setShowForm((v) => !v)} /> {/* New KB form */} {showForm && ( setShowForm(false)} onSubmit={handleCreate} submitDisabled={!name.trim() || saving} submitLabel={saving ? t('common.creating') : t('common.create')} > setName(e.target.value)} className={INPUT_CLS} /> setDescription(e.target.value)} className={INPUT_CLS} /> )} {/* KB list */}
{loading ? (
) : kbs.length === 0 ? ( ) : ( kbs.map((kb) => ) )}
); }