Elffuss BraidHub

kikocis/utopia public

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