Elffuss BraidHub

kikocis/utopia public

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