Elffuss BraidHub

kikocis/utopia public

95c87338cdc8 · 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 memory: Brain,
74 upload: Upload,
75} as const;
76
77/** 有拉取/同步语义的来源类型(folder/api 无同步概念) */
78export const SYNCING_KINDS = new Set([
79 "url",
80 "rss",
81 "custom",
82 "github_issues",
83 "jira_issues",
84 "s3",
85 "azure_blob",
86 "gcs",
87]);
88
89export const SYNC_DOT: Record<SourceView["last_sync_status"], string> = {
90 never: "bg-neutral-600",
91 queued: "bg-[var(--u-warn)]",
92 running: "bg-[var(--u-warn)] animate-pulse",
93 ok: "bg-[var(--u-ok)]",
94 failed: "bg-[var(--u-danger)]",
95};
96
97export function sourceIcon(s: SourceView): LucideIcon {
98 // 内置类型图标固定,只有 custom 尊重用户自选图标
99 if (s.kind === "custom" && s.icon && SOURCE_ICONS[s.icon]) return SOURCE_ICONS[s.icon];
100 return KIND_ICON[s.kind] || Globe;
101}
102
103function RailItem({
104 active,
105 onClick,
106 icon,
107 label,
108 count,
109 dot,
110}: {
111 active: boolean;
112 onClick: () => void;
113 icon: React.ReactNode;
114 label: string;
115 count: number;
116 dot?: string;
117}) {
118 return (
119 <button
120 onClick={onClick}
121 className={`w-full flex items-center gap-2 rounded-lg px-2.5 py-1.5 text-[13px] transition-colors ${
122 active ? "u-nav-active" : "text-neutral-400 hover:bg-white/[0.05] hover:text-neutral-200"
123 }`}
124 >
125 <span className="shrink-0 text-neutral-500">{icon}</span>
126 <span className="truncate">{label}</span>
127 {dot && <span className={`h-1.5 w-1.5 rounded-full shrink-0 ${dot}`} />}
128 <span className="ml-auto shrink-0 u-num text-[10.5px] text-neutral-600">{count}</span>
129 </button>
130 );
131}
132
133export function SourcesRail({
134 kbId,
135 active,
136 onSelect,
137 onAdd,
138}: {
139 kbId: string;
140 active: LibrarySelection | null;
141 onSelect: (sel: LibrarySelection) => void;
142 /** 缺省时隐藏 "+"(如文档查看页) */
143 onAdd?: () => void;
144}) {
145 // 左栏只要两个数:整库多少篇、没有来源的多少篇。**各取一页零条**——
146 // 统计随响应回来,不必把文档拉下来数
147 const docs = useQuery({
148 queryKey: ["docCount", kbId],
149 queryFn: () => api.documents(kbId, { limit: 1, offset: 0 }),
150 });
151 const uploads = useQuery({
152 queryKey: ["docCount", kbId, "uploads"],
153 queryFn: () => api.documents(kbId, { source: "none", limit: 1, offset: 0 }),
154 });
155 const sources = useQuery({
156 queryKey: ["sources", kbId],
157 queryFn: () => api.sources(kbId),
158 });
159
160 const sourceList = sources.data?.sources ?? [];
161 const uploadsCount = uploads.data?.total ?? 0;
162
163 return (
164 <aside className={`${RAIL_CLS} flex flex-col`}>
165 {/* 全部文档置顶为一级入口;SOURCES 小节(含 +)居其下 */}
166 <div className="px-2 pt-3">
167 <RailItem
168 active={active === "all"}
169 onClick={() => onSelect("all")}
170 icon={<LibraryIcon size={14} />}
171 label={S.library.allDocs}
172 count={docs.data?.total ?? 0}
173 />
174 </div>
175 <div className="flex items-center justify-between px-4 pt-3 pb-1.5">
176 <span className="text-[10px] font-medium uppercase tracking-[0.08em] text-neutral-500">
177 {S.library.sources}
178 </span>
179 {onAdd && (
180 <button
181 onClick={onAdd}
182 title={S.library.addSource}
183 className="text-neutral-500 hover:text-neutral-200"
184 >
185 <Plus size={14} />
186 </button>
187 )}
188 </div>
189 <div className="u-scroll flex-1 overflow-y-auto px-2 pb-3 space-y-0.5">
190 {/* Uploads:常驻默认来源(上传的默认去处,不可删除) */}
191 <RailItem
192 active={active === "uploads"}
193 onClick={() => onSelect("uploads")}
194 icon={<Upload size={14} />}
195 label={S.library.uploads}
196 count={uploadsCount}
197 />
198 {sourceList.map((s) => {
199 const Icon = sourceIcon(s);
200 return (
201 <RailItem
202 key={s.id}
203 active={active === s.id}
204 onClick={() => onSelect(s.id)}
205 icon={<Icon size={14} />}
206 label={s.name}
207 count={s.doc_count}
208 dot={
209 SYNCING_KINDS.has(s.kind) || s.kind === "api"
210 ? SYNC_DOT[s.last_sync_status]
211 : undefined
212 }
213 />
214 );
215 })}
216 </div>
217 </aside>
218 );
219}