Elffuss BraidHub

kikocis/utopia public

dev · 5.9 KiBRawPermalink
1/* 来源栏("来源即文件夹"):Library 与 DocViewer 共用的左侧导航。 */
2import type { SourceKind } from "../sourceKinds";
3import { useQuery } from "@tanstack/react-query";
4import {
5 Archive,
6 BookOpen,
7 Brain,
8 Briefcase,
9 CircleDot,
10 Building2,
11 Cloud,
12 Database,
13 FileText,
14 FlaskConical,
15 FolderOpen,
16 Globe,
17 HardDrive,
18 Inbox,
19 Library as LibraryIcon,
20 Newspaper,
21 Notebook,
22 Plus,
23 Puzzle,
24 Rocket,
25 Rss,
26 Server,
27 SquareKanban,
28 Upload,
29 Users,
30 Webhook,
31 type LucideIcon,
32} from "lucide-react";
33import { api, type SourceView } from "../api";
34import { S } from "../i18n";
35import { RAIL_CLS } from "../ui";
36
37/** 左栏选择:全部 / 手动上传 / 某个来源 id */
38export type LibrarySelection = "all" | "uploads" | string;
39
40/** 可选的来源图标(lucide 图标名 → 组件)。 */
41export const SOURCE_ICONS: Record<string, LucideIcon> = {
42 "folder-open": FolderOpen,
43 globe: Globe,
44 rss: Rss,
45 webhook: Webhook,
46 "book-open": BookOpen,
47 newspaper: Newspaper,
48 "file-text": FileText,
49 database: Database,
50 cloud: Cloud,
51 "hard-drive": HardDrive,
52 inbox: Inbox,
53 archive: Archive,
54 briefcase: Briefcase,
55 "building-2": Building2,
56 "flask-conical": FlaskConical,
57 notebook: Notebook,
58 rocket: Rocket,
59 server: Server,
60 users: Users,
61};
62
63// 按 SourceKind 键全:加一种来源没配图标,tsc 就红
64export const KIND_ICON: Record<SourceKind, LucideIcon> = {
65 folder: FolderOpen,
66 url: Globe,
67 rss: Rss,
68 api: Webhook,
69 custom: Puzzle,
70 github_issues: CircleDot,
71 jira_issues: SquareKanban,
72 s3: HardDrive,
73 azure_blob: Cloud,
74 gcs: Cloud,
75 webdav: FolderOpen,
76 notion: Notebook,
77 memory: Brain,
78 upload: Upload,
79};
80
81/** 有拉取/同步语义的来源类型(folder/api 无同步概念) */
82export const SYNCING_KINDS = new Set([
83 "url",
84 "rss",
85 "custom",
86 "github_issues",
87 "jira_issues",
88 "s3",
89 "azure_blob",
90 "gcs",
91 "webdav",
92 "notion",
93]);
94
95export const SYNC_DOT: Record<SourceView["last_sync_status"], string> = {
96 never: "bg-neutral-600",
97 queued: "bg-[var(--u-warn)]",
98 running: "bg-[var(--u-warn)] animate-pulse",
99 ok: "bg-[var(--u-ok)]",
100 failed: "bg-[var(--u-danger)]",
101};
102
103export function sourceIcon(s: SourceView): LucideIcon {
104 // 内置类型图标固定,只有 custom 尊重用户自选图标
105 if (s.kind === "custom" && s.icon && SOURCE_ICONS[s.icon]) return SOURCE_ICONS[s.icon];
106 return KIND_ICON[s.kind] || Globe;
107}
108
109function RailItem({
110 active,
111 onClick,
112 icon,
113 label,
114 count,
115 dot,
116}: {
117 active: boolean;
118 onClick: () => void;
119 icon: React.ReactNode;
120 label: string;
121 count: number;
122 dot?: string;
123}) {
124 return (
125 <button
126 onClick={onClick}
127 className={`w-full flex items-center gap-2 rounded-lg px-2.5 py-1.5 text-[13px] transition-colors ${
128 active ? "u-nav-active" : "text-neutral-400 hover:bg-white/[0.05] hover:text-neutral-200"
129 }`}
130 >
131 <span className="shrink-0 text-neutral-500">{icon}</span>
132 <span className="truncate">{label}</span>
133 {dot && <span className={`h-1.5 w-1.5 rounded-full shrink-0 ${dot}`} />}
134 <span className="ml-auto shrink-0 u-num text-[10.5px] text-neutral-600">{count}</span>
135 </button>
136 );
137}
138
139export function SourcesRail({
140 kbId,
141 active,
142 onSelect,
143 onAdd,
144}: {
145 kbId: string;
146 active: LibrarySelection | null;
147 onSelect: (sel: LibrarySelection) => void;
148 /** 缺省时隐藏 "+"(如文档查看页) */
149 onAdd?: () => void;
150}) {
151 // 左栏只要两个数:整库多少篇、没有来源的多少篇。**各取一页零条**——
152 // 统计随响应回来,不必把文档拉下来数
153 const docs = useQuery({
154 queryKey: ["docCount", kbId],
155 queryFn: () => api.documents(kbId, { limit: 1, offset: 0 }),
156 });
157 const uploads = useQuery({
158 queryKey: ["docCount", kbId, "uploads"],
159 queryFn: () => api.documents(kbId, { source: "none", limit: 1, offset: 0 }),
160 });
161 const sources = useQuery({
162 queryKey: ["sources", kbId],
163 queryFn: () => api.sources(kbId),
164 });
165
166 const sourceList = sources.data?.sources ?? [];
167 const uploadsCount = uploads.data?.total ?? 0;
168
169 return (
170 <aside className={`${RAIL_CLS} flex flex-col`}>
171 {/* 全部文档置顶为一级入口;SOURCES 小节(含 +)居其下 */}
172 <div className="px-2 pt-3">
173 <RailItem
174 active={active === "all"}
175 onClick={() => onSelect("all")}
176 icon={<LibraryIcon size={14} />}
177 label={S.library.allDocs}
178 count={docs.data?.total ?? 0}
179 />
180 </div>
181 <div className="flex items-center justify-between px-4 pt-3 pb-1.5">
182 <span className="text-[10px] font-medium uppercase tracking-[0.08em] text-neutral-500">
183 {S.library.sources}
184 </span>
185 {onAdd && (
186 <button
187 onClick={onAdd}
188 title={S.library.addSource}
189 className="text-neutral-500 hover:text-neutral-200"
190 >
191 <Plus size={14} />
192 </button>
193 )}
194 </div>
195 <div className="u-scroll flex-1 overflow-y-auto px-2 pb-3 space-y-0.5">
196 {/* Uploads:常驻默认来源(上传的默认去处,不可删除) */}
197 <RailItem
198 active={active === "uploads"}
199 onClick={() => onSelect("uploads")}
200 icon={<Upload size={14} />}
201 label={S.library.uploads}
202 count={uploadsCount}
203 />
204 {sourceList.map((s) => {
205 const Icon = sourceIcon(s);
206 return (
207 <RailItem
208 key={s.id}
209 active={active === s.id}
210 onClick={() => onSelect(s.id)}
211 icon={<Icon size={14} />}
212 label={s.name}
213 count={s.doc_count}
214 dot={
215 SYNCING_KINDS.has(s.kind) || s.kind === "api"
216 ? SYNC_DOT[s.last_sync_status]
217 : undefined
218 }
219 />
220 );
221 })}
222 </div>
223 </aside>
224 );
225}