Elffuss BraidHub

kikocis/utopia public

b7cca7fa81d9 · 5.8 KiBRawPermalink
1import {
2 createRootRoute,
3 createRoute,
4 createRouter,
5 redirect,
6} from "@tanstack/react-router";
7import { Account } from "./pages/Account";
8import { AccountShell } from "./pages/AccountShell";
9import { Chat } from "./pages/Chat";
10import { DocViewer } from "./pages/DocViewer";
11import { DocsPage } from "./pages/Docs";
12import { Graph } from "./pages/Graph";
13import { Library } from "./pages/Library";
14import { Login } from "./pages/Login";
15import { Privacy, Terms } from "./pages/Legal";
16import { KbSettings } from "./pages/KbSettings";
17import { MyKbs } from "./pages/MyKbs";
18import { NotFound } from "./pages/ServerDown";
19import { Ontology } from "./pages/Ontology";
20import { Mappings } from "./pages/Mappings";
21import { Review } from "./pages/Review";
22import { Search } from "./pages/Search";
23import { Settings } from "./pages/Settings";
24import { Shell } from "./pages/Shell";
25
26const rootRoute = createRootRoute();
27
28const loginRoute = createRoute({
29 getParentRoute: () => rootRoute,
30 path: "/login",
31 component: Login,
32});
33
34// 公共法务页:登录前可达
35const privacyRoute = createRoute({
36 getParentRoute: () => rootRoute,
37 path: "/privacy",
38 component: Privacy,
39});
40
41const termsRoute = createRoute({
42 getParentRoute: () => rootRoute,
43 path: "/terms",
44 component: Terms,
45});
46
47const appRoute = createRoute({
48 getParentRoute: () => rootRoute,
49 id: "app",
50 component: Shell,
51});
52
53const indexRoute = createRoute({
54 getParentRoute: () => appRoute,
55 path: "/",
56 beforeLoad: () => {
57 // 首页 = 图谱:产品的差异化门面
58 throw redirect({ to: "/graph" });
59 },
60});
61
62const chatRoute = createRoute({
63 getParentRoute: () => appRoute,
64 path: "/chat",
65 component: Chat,
66});
67
68// 会话即路由:/chat/$conversationId 只承载 URL(刷新/分享回到同一会话),
69// 渲染仍由父级 Chat 负责——父级在 /chat ↔ /chat/$id 间保持挂载,流式不断
70const chatConversationRoute = createRoute({
71 getParentRoute: () => chatRoute,
72 path: "$conversationId",
73 component: () => null,
74});
75
76const searchRoute = createRoute({
77 getParentRoute: () => appRoute,
78 path: "/search",
79 component: Search,
80});
81
82const graphRoute = createRoute({
83 getParentRoute: () => appRoute,
84 path: "/graph",
85 validateSearch: (search: Record<string, unknown>): { entity?: string } => ({
86 entity: typeof search.entity === "string" ? search.entity : undefined,
87 }),
88 component: Graph,
89});
90
91const docRoute = createRoute({
92 getParentRoute: () => appRoute,
93 path: "/doc/$docId",
94 validateSearch: (search: Record<string, unknown>): { chunk?: string } => ({
95 chunk: typeof search.chunk === "string" ? search.chunk : undefined,
96 }),
97 component: DocViewer,
98});
99
100const libraryRoute = createRoute({
101 getParentRoute: () => appRoute,
102 path: "/library",
103 validateSearch: (search: Record<string, unknown>): { src?: string } => ({
104 src: typeof search.src === "string" ? search.src : undefined,
105 }),
106 component: Library,
107});
108
109const ontologyRoute = createRoute({
110 getParentRoute: () => appRoute,
111 path: "/ontology",
112 component: Ontology,
113});
114
115const mappingsRoute = createRoute({
116 getParentRoute: () => appRoute,
117 path: "/mappings",
118 component: Mappings,
119});
120
121const reviewRoute = createRoute({
122 getParentRoute: () => appRoute,
123 path: "/review",
124 component: Review,
125});
126
127// 内置文档:公开路由(登录前可读;私有部署离线可用)
128const docsIndexRoute = createRoute({
129 getParentRoute: () => rootRoute,
130 path: "/docs",
131 beforeLoad: () => {
132 throw redirect({ to: "/docs/$slug", params: { slug: "ingest" } });
133 },
134});
135
136const docsRoute = createRoute({
137 getParentRoute: () => rootRoute,
138 path: "/docs/$slug",
139 component: DocsPage,
140});
141
142// 账户层(Profile / Administration):与 KB 无关,用无 tab 导航的独立壳
143const accountShellRoute = createRoute({
144 getParentRoute: () => rootRoute,
145 id: "account",
146 component: AccountShell,
147});
148
149const accountRoute = createRoute({
150 getParentRoute: () => accountShellRoute,
151 path: "/account",
152 component: Account,
153});
154
155const myKbsRoute = createRoute({
156 getParentRoute: () => accountShellRoute,
157 path: "/account/kbs",
158 component: MyKbs,
159});
160
161const adminRoute = createRoute({
162 getParentRoute: () => accountShellRoute,
163 path: "/admin",
164 // 深链指定页签(如 KB 数据节的"注册新连接"直达 Data sources)
165 validateSearch: (
166 search: Record<string, unknown>,
167 ): { tab?: "models" | "members" | "kbs" | "datasources" | "deployment" } => ({
168 tab:
169 search.tab === "models" ||
170 search.tab === "members" ||
171 search.tab === "kbs" ||
172 search.tab === "datasources" ||
173 search.tab === "deployment"
174 ? search.tab
175 : undefined,
176 }),
177 component: Settings,
178});
179
180// 旧路径兼容:/settings → /admin
181const settingsRoute = createRoute({
182 getParentRoute: () => rootRoute,
183 path: "/settings",
184 beforeLoad: () => {
185 throw redirect({ to: "/admin" });
186 },
187});
188
189const kbSettingsRoute = createRoute({
190 getParentRoute: () => appRoute,
191 path: "/kb-settings",
192 validateSearch: (search: Record<string, unknown>): { kb?: string } => ({
193 kb: typeof search.kb === "string" ? search.kb : undefined,
194 }),
195 component: KbSettings,
196});
197
198const routeTree = rootRoute.addChildren([
199 loginRoute,
200 privacyRoute,
201 termsRoute,
202 settingsRoute,
203 docsIndexRoute,
204 docsRoute,
205 accountShellRoute.addChildren([accountRoute, myKbsRoute, adminRoute]),
206 appRoute.addChildren([
207 indexRoute,
208 chatRoute.addChildren([chatConversationRoute]),
209 searchRoute,
210 graphRoute,
211 docRoute,
212 libraryRoute,
213 reviewRoute,
214 ontologyRoute,
215 mappingsRoute,
216 kbSettingsRoute,
217 ]),
218]);
219
220export const router = createRouter({
221 routeTree,
222 // 迷失之城:未知路径的惩戒页
223 defaultNotFoundComponent: NotFound,
224});
225
226declare module "@tanstack/react-router" {
227 interface Register {
228 router: typeof router;
229 }
230}