import React, { useEffect, useState } from 'react' function statusLabel(job) { if (!job) return null const type = job.type === 'prepare' ? 'prepare' : job.type const progress = typeof job.progress === 'number' ? `${job.progress.toFixed(0)}%` : null const detail = job.detail ? ` ${job.detail}` : '' return `${type} · ${job.status}${progress ? ` · ${progress}` : ''}${detail}` } export default function LibraryManager({ apiBase, library, jobs, chatLibrarySlug, pendingChatLibrarySlug, onRefresh, onToggleChatLibrary, onDeleted }) { const [busy, setBusy] = useState(false) const [confirmDelete, setConfirmDelete] = useState(false) const [errorMessage, setErrorMessage] = useState('') useEffect(() => { setConfirmDelete(false) setErrorMessage('') }, [library?.slug, library?.name]) async function expectOk(response) { if (response.ok) return response const detail = await response.text() throw new Error(detail || `HTTP ${response.status}`) } async function runAction(fn) { setBusy(true) try { setErrorMessage('') await fn() setConfirmDelete(false) } finally { setBusy(false) await onRefresh() } } async function addPaths() { if (!library) return const paths = await window.electronAPI?.pickPaths?.() if (!Array.isArray(paths) || paths.length === 0) return try { await runAction(async () => { const response = await fetch(`${apiBase}/libraries/${library.slug}/files/register`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ paths }) }) await expectOk(response) }) } catch (error) { setErrorMessage(String(error?.message || error)) } } async function removeFile(rel) { if (!library) return try { await runAction(async () => { const response = await fetch(`${apiBase}/libraries/${library.slug}/files`, { method: 'DELETE', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ rel }) }) await expectOk(response) }) } catch (error) { setErrorMessage(String(error?.message || error)) } } async function deleteLibrary() { if (!library) return await runAction(async () => { const response = await fetch(`${apiBase}/libraries/${library.slug}`, { method: 'DELETE' }) await expectOk(response) }) onDeleted?.(library.slug) } if (!library) { return (

Create a database, add files, then add it to chat. Heimgeist will prepare retrieval automatically.

) } const activeJobs = (jobs || []).filter(job => job.slug === library.slug && (job.status === 'queued' || job.status === 'running')) const usingInChat = chatLibrarySlug === library.slug const isPreparingForChat = pendingChatLibrarySlug === library.slug const isReadyForChat = !!library.states?.is_indexed return (
{confirmDelete && (
Delete "{library.name}"? This removes the registered files and local retrieval data for this database.
)} {errorMessage &&
{errorMessage}
}
Files: {library.files?.length || 0}
{isPreparingForChat ? 'Preparing' : isReadyForChat ? 'Ready for chat' : library.files?.length ? 'Needs preparation' : 'No data yet'}
{usingInChat ? 'In chat' : isPreparingForChat ? 'Adding to chat' : 'Not in chat'}
{usingInChat && (
This database is attached to chat. Heimgeist retrieves relevant snippets before each message and appends them to the prompt.
)} {isPreparingForChat && (
Preparing this database for chat. Heimgeist is reading files, enriching content, and building search indexes automatically.
)} {!library.files?.length && !usingInChat && !isPreparingForChat && (
Add files to make this database available in chat.
)} {library.files?.length > 0 && !isReadyForChat && !usingInChat && !isPreparingForChat && (
Add To Chat will prepare this database automatically before it is used.
)} {activeJobs.length > 0 && (
{activeJobs.map(job => (
{statusLabel(job)}
))}
)}

Files

{library.files?.length ? (
{library.files.map(file => (
{file.name || file.path}
{file.path}
))}
) : (

No files registered yet.

)}
) }