import { useState, useEffect, useCallback } from 'react'; import { api, DocTreeNode, DocContent } from './api'; import { DocTree } from './components/DocTree'; import { Editor } from './components/Editor'; import { SearchBar } from './components/SearchBar'; import { VersionHistory } from './components/VersionHistory'; import AgentChat from './components/AgentChat'; type View = 'editor' | 'history'; export default function App() { const agentApiBase = window.location.protocol === 'file:' ? 'http://127.0.0.1:3890' : ''; const [tree, setTree] = useState([]); const [currentDoc, setCurrentDoc] = useState(null); const [currentPath, setCurrentPath] = useState(''); const [view, setView] = useState('editor'); const [sidebarOpen, setSidebarOpen] = useState(true); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [agentPanelOpen, setAgentPanelOpen] = useState(true); const refreshTree = useCallback(async () => { try { const t = await api.getTree(); setTree(t); } catch (err: any) { setError(`加载文档树失败: ${err.message}`); } }, []); const openDoc = useCallback(async (docPath: string) => { setLoading(true); setError(null); try { const doc = await api.getDoc(docPath); setCurrentDoc(doc); setCurrentPath(docPath); setView('editor'); } catch (err: any) { setError(`加载文档失败: ${err.message}`); } finally { setLoading(false); } }, []); const saveDoc = useCallback(async (title: string, body: string) => { if (!currentPath) return; setLoading(true); try { const doc = await api.updateDoc(currentPath, title, body); setCurrentDoc(doc); await refreshTree(); } catch (err: any) { setError(`保存失败: ${err.message}`); } finally { setLoading(false); } }, [currentPath, refreshTree]); const createDoc = useCallback(async (parentPath: string) => { const name = prompt('文档文件名(不含 .md):'); if (!name) return; const title = prompt('文档标题:') || name; const docPath = parentPath ? `${parentPath}/${name}.md` : `${name}.md`; try { const doc = await api.createDoc(docPath, title, `# ${title}\n\n在这里开始写作...\n`); setCurrentDoc(doc); setCurrentPath(docPath); await refreshTree(); } catch (err: any) { setError(`创建失败: ${err.message}`); } }, [refreshTree]); const deleteDoc = useCallback(async () => { if (!currentPath) return; if (!confirm(`确认删除 ${currentPath}?`)) return; try { await api.deleteDoc(currentPath); setCurrentDoc(null); setCurrentPath(''); await refreshTree(); } catch (err: any) { setError(`删除失败: ${err.message}`); } }, [currentPath, refreshTree]); useEffect(() => { refreshTree(); }, [refreshTree]); return (
{/* 顶栏 */}

光湖知识库

{currentDoc && ( <> )}
{/* 主体 */}
{/* 侧栏 */} {sidebarOpen && ( )} {/* 内容区 */}
{error && (
{error}
)} {loading &&
加载中...
} {!currentDoc && !loading && (
📖

选择左侧文档开始阅读

或点击「+ 根目录文档」创建新文档

)} {currentDoc && view === 'editor' && ( )} {currentDoc && view === 'history' && ( )}
{/* Agent 面板 */} {agentPanelOpen && ( )}
); }