import { useCallback, useState } from 'react' import { deleteDocument, resetDocuments } from '../api/client.ts' import { useTheme } from '../context/ThemeContext.tsx' import { useToast } from './shared/Toast.tsx' import type { StatusDetails } from '../api/types.ts' import type { ViewId } from '../App.tsx' import './sidebar.css' interface NavEntry { id: ViewId icon: string label: string } const NAV_ITEMS: NavEntry[] = [ { id: 'upload', icon: 'πŸ“', label: 'Upload Files' }, { id: 'web', icon: '🌍', label: 'Web Import' }, { id: 'chat', icon: 'πŸ’­', label: 'Q&A Chat' }, { id: 'summary', icon: 'πŸ“Š', label: 'Summarize' }, ] interface SidebarProps { activeView: ViewId onViewChange: (view: ViewId) => void status: StatusDetails | null statusError: boolean onStatusRefresh: () => void } export default function Sidebar({ activeView, onViewChange, status, statusError, onStatusRefresh, }: SidebarProps) { const { theme, toggleTheme } = useTheme() const { showToast } = useToast() const [deletingDoc, setDeletingDoc] = useState(null) const [resetting, setResetting] = useState(false) const docCount = status?.document_count ?? 0 const hasDocuments = status?.has_documents ?? false const documents = status?.indexed_documents ?? [] const pulseColor = statusError ? 'var(--danger)' : hasDocuments ? 'var(--success)' : 'var(--text-light)' const statusLabel = statusError ? 'Connection Error' : hasDocuments ? 'Ready' : 'No Docs' const handleDelete = useCallback( async (docName: string) => { setDeletingDoc(docName) try { const res = await deleteDocument(docName) showToast(res.message, 'success') onStatusRefresh() } catch (err) { showToast(err instanceof Error ? err.message : 'Failed to delete document', 'error') } finally { setDeletingDoc(null) } }, [showToast, onStatusRefresh], ) const handleReset = useCallback(async () => { if (!window.confirm('Reset all documents? This cannot be undone.')) return setResetting(true) try { const res = await resetDocuments() showToast(res.message, 'success') onStatusRefresh() } catch (err) { showToast(err instanceof Error ? err.message : 'Reset failed', 'error') } finally { setResetting(false) } }, [showToast, onStatusRefresh]) return ( ) }