import { useState } from 'react' import api from '@/services/api' import type { KnowledgeBaseTree as Tree, KnowledgeTopicNode, KnowledgeCategoryNode, KnowledgeFact } from '@/types' type Reload = () => void | Promise function errMsg(err: any, fallback: string): string { return err?.response?.data?.detail || fallback } /** A toggle button that reveals a single-line input with save/cancel. */ function InlineForm({ label, placeholder, onSubmit }: { label: string placeholder: string onSubmit: (value: string) => Promise }) { const [open, setOpen] = useState(false) const [value, setValue] = useState('') const [busy, setBusy] = useState(false) if (!open) { return } const save = async () => { if (!value.trim()) return setBusy(true) try { await onSubmit(value.trim()); setOpen(false) } finally { setBusy(false) } } return ( setValue(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') save(); if (e.key === 'Escape') setOpen(false) }} /> ) } /** Inline editor for a node's name + description (used by topics & categories). */ function NodeEditForm({ initialName, initialDesc, namePlaceholder, onSave, onCancel }: { initialName: string initialDesc: string namePlaceholder: string onSave: (name: string, description: string) => Promise onCancel: () => void }) { const [name, setName] = useState(initialName) const [desc, setDesc] = useState(initialDesc) const [busy, setBusy] = useState(false) const save = async () => { if (!name.trim()) return setBusy(true) try { await onSave(name.trim(), desc) } finally { setBusy(false) } } return (
setName(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') save(); if (e.key === 'Escape') onCancel() }} />