App.tsx
12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import {
Boxes, ChevronLeft, ClipboardCheck, Database, FileText, FlaskConical, Home, Inbox, Library, ListChecks, Menu,
MessageSquareText, Monitor, Moon, Network, ScrollText, Settings, Sun, Users, Wrench,
} from "lucide-react";
import { useCallback, useEffect, useState } from "react";
import { Badge, Toaster } from "@/components/ui";
import { AskPage } from "@/features/ask/AskPage";
import { AuditPage } from "@/features/audit/AuditPage";
import { CapabilityPage } from "@/features/capability/CapabilityPage";
import { DocumentsPage } from "@/features/documents/DocumentsPage";
import { EvalPage } from "@/features/eval/EvalPage";
import { GraphPage } from "@/features/graph/GraphPage";
import { HomePage } from "@/features/home/HomePage";
import { InboxPage } from "@/features/inbox/InboxPage";
import { KernelPage } from "@/features/kernel/KernelPage";
import { MembersPage } from "@/features/members/MembersPage";
import { ObjectsPage } from "@/features/objects/ObjectsPage";
import { SettingsPage } from "@/features/settings/SettingsPage";
import { SopPage } from "@/features/sop/SopPage";
import { SqlPage } from "@/features/sql/SqlPage";
import { DialogHost } from "@/lib/dialog";
import { currentUserId, onUserChange, setCurrentUserId } from "@/lib/identity";
import { go, parseHash } from "@/lib/nav";
import { onto, type InboxCounts, type Me, type UserRec } from "@/lib/onto";
import { cn } from "@/lib/utils";
import { applyTheme, useAppStore, type Theme } from "@/store/useAppStore";
type PageKey = "home" | "ask" | "inbox" | "objects" | "kernel" | "capability" | "sop" | "documents" | "graph"
| "data" | "eval" | "audit" | "members" | "settings";
interface NavItem { key: PageKey; label: string; icon: React.ReactNode; hint: string }
const NAV: { group: string; items: NavItem[] }[] = [
{ group: "工作台", items: [
{ key: "home", label: "首页", icon: <Home className="h-4 w-4" />, hint: "上手向导与待办概况" },
{ key: "ask", label: "提问", icon: <MessageSquareText className="h-4 w-4" />, hint: "问知识、问数据、问怎么办,一个入口" },
{ key: "inbox", label: "收件箱", icon: <Inbox className="h-4 w-4" />, hint: "待我审批、我的待办、审核队列、通知" },
{ key: "objects", label: "对象", icon: <Boxes className="h-4 w-4" />, hint: "任一业务对象的 360 视图" },
] },
{ group: "建设台", items: [
{ key: "kernel", label: "本体目录", icon: <Library className="h-4 w-4" />, hint: "对象、指标、规则、周期、角色、流程:只定义一次" },
{ key: "capability", label: "能力库", icon: <Wrench className="h-4 w-4" />, hint: "函数、工具、技能、动作:AI 与流程能做什么" },
{ key: "sop", label: "SOP 流程", icon: <ListChecks className="h-4 w-4" />, hint: "可执行的标准流程:人执行、AI 执行、问答引用" },
{ key: "documents", label: "文档", icon: <FileText className="h-4 w-4" />, hint: "上传、抽取、重抽与删除" },
{ key: "graph", label: "知识图谱", icon: <Network className="h-4 w-4" />, hint: "文档里抽出的实体与关系(证据层)" },
{ key: "data", label: "数据连接", icon: <Database className="h-4 w-4" />, hint: "数据源、物理建模、SQL 调试" },
] },
{ group: "治理", items: [
{ key: "eval", label: "评测中心", icon: <FlaskConical className="h-4 w-4" />, hint: "问答与本体评测,发布前回归" },
{ key: "audit", label: "审计中心", icon: <ScrollText className="h-4 w-4" />, hint: "谁、何时、做了什么;模型用量" },
{ key: "members", label: "成员与角色", icon: <Users className="h-4 w-4" />, hint: "系统角色、业务角色、数据范围" },
{ key: "settings", label: "设置", icon: <Settings className="h-4 w-4" />, hint: "模型、抽取、检索与治理参数" },
] },
];
const ALL = NAV.flatMap((g) => g.items);
const THEME_ICON: Record<Theme, React.ReactNode> = {
light: <Sun className="h-4 w-4" />, dark: <Moon className="h-4 w-4" />, system: <Monitor className="h-4 w-4" />,
};
function pageFromHash(): PageKey {
const p = parseHash().page;
return (ALL.find((n) => n.key === p)?.key ?? "home") as PageKey;
}
export default function App() {
const [page, setPage] = useState<PageKey>(pageFromHash);
const [me, setMe] = useState<Me | null>(null);
const [users, setUsers] = useState<UserRec[]>([]);
const [counts, setCounts] = useState<InboxCounts | null>(null);
const [online, setOnline] = useState(true);
const [mobileNavOpen, setMobileNavOpen] = useState(false);
const [uid, setUid] = useState(currentUserId());
const { theme, setTheme, sidebarCollapsed, toggleSidebar, graphFocus } = useAppStore();
const refresh = useCallback(() => {
void onto.me().then((m) => { setMe(m); setOnline(true); }).catch(() => setOnline(false));
void onto.inbox().then(setCounts).catch(() => undefined);
}, []);
useEffect(() => { applyTheme(theme); }, [theme]);
useEffect(() => { refresh(); }, [refresh, page, uid]);
useEffect(() => { void onto.users().then((r) => setUsers(r.users)).catch(() => undefined); }, [uid]);
useEffect(() => onUserChange(setUid), []);
useEffect(() => {
const t = window.setInterval(() => void onto.inbox().then(setCounts).catch(() => undefined), 30000);
return () => window.clearInterval(t);
}, []);
useEffect(() => { if (graphFocus) go("graph"); }, [graphFocus?.seq]); // eslint-disable-line react-hooks/exhaustive-deps
// hash 路由:#/页面?参数
useEffect(() => {
if (!window.location.hash) go(page);
const on = () => setPage(pageFromHash());
window.addEventListener("hashchange", on);
return () => window.removeEventListener("hashchange", on);
}, []); // eslint-disable-line react-hooks/exhaustive-deps
const nav = (k: PageKey) => { go(k); setMobileNavOpen(false); };
const current = ALL.find((n) => n.key === page)!;
const inboxTotal = (counts?.approvals || 0) + (counts?.tasks || 0) + (counts?.reviews || 0);
return (
<div className="flex h-full">
{mobileNavOpen && <div className="fixed inset-0 z-40 bg-black/40 md:hidden" onClick={() => setMobileNavOpen(false)} aria-hidden />}
<aside className={cn(
"flex shrink-0 flex-col border-r border-line bg-surface transition-[width,transform]",
sidebarCollapsed ? "w-[60px]" : "w-[216px]",
"max-md:fixed max-md:inset-y-0 max-md:left-0 max-md:z-50 max-md:w-[216px]",
mobileNavOpen ? "max-md:translate-x-0" : "max-md:-translate-x-full")}>
<div className="flex h-14 items-center gap-2.5 border-b border-line px-3">
<span className="grid h-8 w-8 shrink-0 place-items-center rounded-lg bg-brand text-white"><Network className="h-4 w-4" /></span>
{!sidebarCollapsed && (
<div className="min-w-0">
<p className="truncate text-[13px] font-semibold leading-tight">Ontology 本体系统</p>
<p className="truncate text-[10.5px] text-muted">知识 · 数据 · 流程 · 行动</p>
</div>
)}
</div>
<nav className="flex-1 space-y-3 overflow-auto p-2">
{NAV.map((g) => (
<div key={g.group}>
{!sidebarCollapsed && <p className="px-2.5 pb-1 text-[10.5px] font-medium uppercase tracking-wide text-muted/70">{g.group}</p>}
<div className="space-y-0.5">
{g.items.map((n) => (
<button key={n.key} title={sidebarCollapsed ? n.label : n.hint} onClick={() => nav(n.key)}
className={cn("flex w-full items-center gap-2.5 rounded-lg px-2.5 py-1.5 text-[13px] transition-colors",
page === n.key ? "bg-brand/10 font-medium text-brand" : "text-muted hover:bg-ink/5 hover:text-ink",
sidebarCollapsed && "justify-center px-0")}>
<span className="relative">{n.icon}
{n.key === "inbox" && inboxTotal > 0 && sidebarCollapsed && <i className="absolute -right-1 -top-1 h-2 w-2 rounded-full bg-bad" />}
</span>
{!sidebarCollapsed && <span className="flex-1 truncate text-left">{n.label}</span>}
{!sidebarCollapsed && n.key === "inbox" && inboxTotal > 0 && <Badge tone="bg-bad/10 text-bad">{inboxTotal}</Badge>}
</button>
))}
</div>
</div>
))}
</nav>
<div className="space-y-1 border-t border-line p-2">
<button className={cn("flex w-full items-center gap-2.5 rounded-lg px-2.5 py-2 text-[13px] text-muted hover:bg-ink/5", sidebarCollapsed && "justify-center px-0")}
onClick={() => setTheme(theme === "light" ? "dark" : theme === "dark" ? "system" : "light")}>
{THEME_ICON[theme]}
{!sidebarCollapsed && <span>{theme === "light" ? "浅色" : theme === "dark" ? "深色" : "跟随系统"}</span>}
</button>
<button className={cn("flex w-full items-center gap-2.5 rounded-lg px-2.5 py-2 text-[13px] text-muted hover:bg-ink/5", sidebarCollapsed && "justify-center px-0")}
onClick={() => { if (window.matchMedia("(max-width: 767px)").matches) setMobileNavOpen(false); else toggleSidebar(); }}>
<ChevronLeft className={cn("h-4 w-4 transition-transform", sidebarCollapsed && "rotate-180")} />
{!sidebarCollapsed && <span>收起</span>}
</button>
</div>
</aside>
<div className="flex min-w-0 flex-1 flex-col">
<header className="flex h-14 shrink-0 items-center gap-3 border-b border-line bg-surface px-4">
<button className="btn btn-ghost btn-sm -ml-1 shrink-0 md:hidden" onClick={() => setMobileNavOpen(true)} aria-label="打开导航">
<Menu className="h-4 w-4" />
</button>
<div className="min-w-0">
<h1 className="text-[15px] font-semibold leading-tight">{current.label}</h1>
<p className="truncate text-[11px] text-muted">{current.hint}</p>
</div>
<div className="ml-auto flex items-center gap-2">
{!online && <Badge tone="bg-bad/10 text-bad">服务未就绪</Badge>}
{counts && counts.approvals > 0 && (
<button onClick={() => go("inbox", { tab: "approvals" })}>
<Badge tone="bg-warn/10 text-warn"><ClipboardCheck className="h-3 w-3" />待审批 {counts.approvals}</Badge>
</button>
)}
<label className="flex items-center gap-1.5 text-[12px] text-muted" title="以谁的身份操作(审计、审批、数据范围都按它)">
身份
<select className="input h-8 w-auto py-0 text-[12.5px]" value={uid} onChange={(e) => setCurrentUserId(e.target.value)}>
{!users.some((u) => u.id === uid) && <option value={uid}>{uid}</option>}
{users.map((u) => <option key={u.id} value={u.id}>{u.name}</option>)}
</select>
</label>
</div>
</header>
<main className={cn("min-h-0 flex-1 p-4", page === "ask" ? "overflow-hidden" : "overflow-auto")}>
{/* 切换身份时整页重挂,所有数据按新身份重新加载 */}
<div key={uid} className="h-full">
{page === "home" && <HomePage me={me} />}
{page === "ask" && <AskPage />}
{page === "inbox" && <InboxPage onChanged={refresh} />}
{page === "objects" && <ObjectsPage me={me} />}
{page === "kernel" && <KernelPage me={me} />}
{page === "capability" && <CapabilityPage me={me} />}
{page === "sop" && <SopPage me={me} />}
{page === "documents" && <DocumentsPage onExtracted={refresh} />}
{page === "graph" && <GraphPage />}
{page === "data" && <SqlPage />}
{page === "eval" && <EvalPage me={me} />}
{page === "audit" && <AuditPage />}
{page === "members" && <MembersPage me={me} />}
{page === "settings" && <SettingsPage onChanged={refresh} />}
</div>
</main>
</div>
<Toaster />
<DialogHost />
</div>
);
}