| 1 | /* 知识库设置:左栏分节(General / Members / Danger zone),为未来设置项立骨架 |
| 2 | (抽取设置、保留策略、库级令牌…)。访问控制由 API 端执行(库 admin 起步)。 |
| 3 | 分节互斥渲染也根治了下拉弹层被后续玻璃卡(backdrop-filter 自成 stacking |
| 4 | context)遮蔽的层级 bug。 */ |
| 5 | import { useEffect, useState } from "react"; |
| 6 | import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; |
| 7 | import { useParams, useNavigate } from "@tanstack/react-router"; |
| 8 | import { |
| 9 | History as HistoryIcon, |
| 10 | Lock, |
| 11 | Settings2, |
| 12 | TriangleAlert, |
| 13 | Users, |
| 14 | } from "lucide-react"; |
| 15 | import { api, type AuditEvent } from "../api"; |
| 16 | import { LANG_NAMES, S } from "../i18n"; |
| 17 | import { |
| 18 | DangerConfirm, |
| 19 | Dropdown, |
| 20 | Loading, |
| 21 | Pager, |
| 22 | RAIL_CLS, |
| 23 | SearchSelect, |
| 24 | } from "../ui"; |
| 25 | |
| 26 | const KB_ROLES = [ |
| 27 | { value: "viewer", label: S.kbset.roles.viewer }, |
| 28 | { value: "editor", label: S.kbset.roles.editor }, |
| 29 | { value: "admin", label: S.kbset.roles.admin }, |
| 30 | ]; |
| 31 | |
| 32 | /** |
| 33 | * 这个库能授予哪些角色。 |
| 34 | * |
| 35 | * **open 库没有 viewer 可授**:`access::kb_role` 对 open 库直接给部署内每个人 |
| 36 | * Viewer,所以写一行 `role=viewer` 什么都没多给——一条空操作的记录, |
| 37 | * 还占着成员名单一行让人以为它起了作用。列在这里的意义只剩"给写权限"。 |
| 38 | * |
| 39 | * 历史数据里可能存着 open 库的 viewer 行,但那些行在名单里已经不显示了 |
| 40 | *(见 `listed`),所以这里不必为"当前值不在选项里"兜底。 |
| 41 | */ |
| 42 | function rolesFor(isOpen: boolean) { |
| 43 | return isOpen ? KB_ROLES.filter((r) => r.value !== "viewer") : KB_ROLES; |
| 44 | } |
| 45 | |
| 46 | type Section = "general" | "members" | "activity" | "danger"; |
| 47 | |
| 48 | export function KbSettings() { |
| 49 | const navigate = useNavigate(); |
| 50 | const queryClient = useQueryClient(); |
| 51 | /* 库 id 来自路径。**从前是 `?kb=`**——那是这套路由改造之前唯一 |
| 52 | 带着库走的地方,现在整片都在 /kb/$kbId 之下,它就不必自成一格了 */ |
| 53 | const { kbId } = useParams({ from: "/app/kb/$kbId/settings" }); |
| 54 | |
| 55 | const kb = useQuery({ |
| 56 | queryKey: ["kbOne", kbId], |
| 57 | queryFn: () => api.kbDetail(kbId!), |
| 58 | enabled: !!kbId, |
| 59 | }); |
| 60 | |
| 61 | const [section, setSection] = useState<Section>("general"); |
| 62 | const [name, setName] = useState(""); |
| 63 | const [desc, setDesc] = useState(""); |
| 64 | const [visibility, setVisibility] = useState<"open" | "restricted">("open"); |
| 65 | const [autoExtend, setAutoExtend] = useState(true); |
| 66 | // **默认关**,与上面那个相反:推理往账本里写事实,而声明可能是错的 |
| 67 | const [materialize, setMaterialize] = useState(false); |
| 68 | const [inferMins, setInferMins] = useState(60); |
| 69 | const [ontoLang, setOntoLang] = useState<"en" | "zh">("en"); |
| 70 | const [confirmingDelete, setConfirmingDelete] = useState(false); |
| 71 | const [error, setError] = useState<string | null>(null); |
| 72 | |
| 73 | useEffect(() => { |
| 74 | if (kb.data) { |
| 75 | setName(kb.data.name); |
| 76 | setDesc(kb.data.description ?? ""); |
| 77 | setVisibility(kb.data.visibility); |
| 78 | setAutoExtend(kb.data.auto_extend_ontology); |
| 79 | setMaterialize(kb.data.materialize_inferences); |
| 80 | setInferMins(kb.data.inference_interval_minutes); |
| 81 | setOntoLang(kb.data.ontology_lang); |
| 82 | } |
| 83 | }, [kb.data]); |
| 84 | |
| 85 | const invalidate = () => { |
| 86 | queryClient.invalidateQueries({ queryKey: ["kbOne", kbId] }); |
| 87 | queryClient.invalidateQueries({ queryKey: ["kbs"] }); |
| 88 | }; |
| 89 | |
| 90 | const save = useMutation({ |
| 91 | mutationFn: () => |
| 92 | api.updateKb(kbId!, { |
| 93 | name: name.trim(), |
| 94 | description: desc.trim() || null, |
| 95 | visibility, |
| 96 | auto_extend_ontology: autoExtend, |
| 97 | materialize_inferences: materialize, |
| 98 | inference_interval_minutes: inferMins, |
| 99 | ontology_lang: ontoLang, |
| 100 | }), |
| 101 | onSuccess: () => { |
| 102 | setError(null); |
| 103 | invalidate(); |
| 104 | }, |
| 105 | onError: (e) => setError((e as Error).message), |
| 106 | }); |
| 107 | |
| 108 | const removeKb = useMutation({ |
| 109 | mutationFn: () => api.deleteKb(kbId!), |
| 110 | onSuccess: () => { |
| 111 | queryClient.invalidateQueries({ queryKey: ["kbs"] }); |
| 112 | navigate({ to: "/kb/$kbId/library", params: { kbId } }); |
| 113 | }, |
| 114 | onError: (e) => setError((e as Error).message), |
| 115 | }); |
| 116 | |
| 117 | if (!kbId || kb.isPending) return <Loading>{S.nav.loading}</Loading>; |
| 118 | if (kb.isError) |
| 119 | return ( |
| 120 | <div className="p-8 text-sm text-rose-400"> |
| 121 | {(kb.error as Error).message} |
| 122 | </div> |
| 123 | ); |
| 124 | |
| 125 | const lbl = "block text-xs font-medium text-neutral-500 mb-1"; |
| 126 | const rail = |
| 127 | "w-full flex items-center gap-2.5 rounded-lg px-3 py-2 text-[13px] text-left transition-colors"; |
| 128 | |
| 129 | const isDefault = kb.data.is_default; |
| 130 | const sections: { |
| 131 | key: Section; |
| 132 | label: string; |
| 133 | Icon: typeof Settings2; |
| 134 | danger?: boolean; |
| 135 | }[] = [ |
| 136 | { key: "general", label: S.kbset.general, Icon: Settings2 }, |
| 137 | { key: "members", label: S.kbset.members, Icon: Users }, |
| 138 | { key: "activity", label: S.kbset.activity, Icon: HistoryIcon }, |
| 139 | // 默认库不可删除:danger 节整个不出现 |
| 140 | ...(isDefault |
| 141 | ? [] |
| 142 | : [ |
| 143 | { |
| 144 | key: "danger" as Section, |
| 145 | label: S.kbset.danger, |
| 146 | Icon: TriangleAlert, |
| 147 | danger: true, |
| 148 | }, |
| 149 | ]), |
| 150 | ]; |
| 151 | |
| 152 | return ( |
| 153 | <div className="h-full flex"> |
| 154 | {/* 分节导航:未来的抽取设置/保留策略/令牌等在此扩展 */} |
| 155 | <aside className={`${RAIL_CLS} p-3 space-y-0.5`}> |
| 156 | {sections.map(({ key, label, Icon, danger }) => ( |
| 157 | <button |
| 158 | key={key} |
| 159 | onClick={() => setSection(key)} |
| 160 | className={`${rail} ${ |
| 161 | section === key |
| 162 | ? "u-nav-active" |
| 163 | : danger |
| 164 | ? "text-neutral-500 hover:bg-white/[0.05] hover:text-[var(--u-danger)]" |
| 165 | : "text-neutral-400 hover:bg-white/[0.05] hover:text-neutral-200" |
| 166 | }`} |
| 167 | > |
| 168 | <Icon size={14} /> |
| 169 | {label} |
| 170 | </button> |
| 171 | ))} |
| 172 | </aside> |
| 173 | |
| 174 | <main className="flex-1 min-w-0 overflow-y-auto u-scroll px-8 py-6"> |
| 175 | <div className="max-w-xl space-y-5"> |
| 176 | {/* 不缀库名:顶栏切换器已标明当前库 */} |
| 177 | <h2 className="u-title text-lg">{S.kbset.title}</h2> |
| 178 | |
| 179 | {section === "general" && ( |
| 180 | <div className="glass rounded-xl p-4 space-y-3"> |
| 181 | <div className="grid grid-cols-2 gap-2"> |
| 182 | <div> |
| 183 | <label className={lbl}>{S.settings.kbs.name}</label> |
| 184 | <input |
| 185 | className="input-dark w-full px-3 py-2 text-sm" |
| 186 | value={name} |
| 187 | onChange={(e) => setName(e.target.value)} |
| 188 | /> |
| 189 | </div> |
| 190 | <div> |
| 191 | <label className={lbl}>{S.settings.kbs.visibility}</label> |
| 192 | {isDefault ? ( |
| 193 | /* 默认库锁 open:说明常驻可见(藏在 hover 里等于没解释),详情见 grid 下方整行 */ |
| 194 | <div className="flex items-center gap-1.5 rounded-lg border border-white/10 px-2.5 py-2 text-[11px] text-neutral-500 cursor-not-allowed"> |
| 195 | <Lock size={11} className="shrink-0 text-neutral-600" /> |
| 196 | {S.kbset.defaultOpenLabel} |
| 197 | </div> |
| 198 | ) : ( |
| 199 | <div className="flex rounded-lg overflow-hidden border border-white/10"> |
| 200 | {( |
| 201 | [ |
| 202 | ["open", "Open"], |
| 203 | ["restricted", S.settings.kbs.visRestricted], |
| 204 | ] as const |
| 205 | ).map(([v, label]) => ( |
| 206 | <button |
| 207 | key={v} |
| 208 | onClick={() => setVisibility(v)} |
| 209 | title={label} |
| 210 | className={`flex-1 px-2 py-2 text-[11px] truncate transition-colors ${ |
| 211 | visibility === v |
| 212 | ? "bg-white/[0.12] text-white" |
| 213 | : "text-neutral-500 hover:bg-white/[0.05] hover:text-neutral-300" |
| 214 | }`} |
| 215 | > |
| 216 | {label} |
| 217 | </button> |
| 218 | ))} |
| 219 | </div> |
| 220 | )} |
| 221 | </div> |
| 222 | </div> |
| 223 | {isDefault && ( |
| 224 | <p className="text-xs leading-relaxed text-neutral-500"> |
| 225 | {S.kbset.defaultOpenNote} |
| 226 | </p> |
| 227 | )} |
| 228 | <div> |
| 229 | <label className={lbl}>{S.settings.kbs.description}</label> |
| 230 | <input |
| 231 | className="input-dark w-full px-3 py-2 text-sm" |
| 232 | value={desc} |
| 233 | onChange={(e) => setDesc(e.target.value)} |
| 234 | /> |
| 235 | </div> |
| 236 | {/* 自动扩本体:默认开,因为新库的十个默认关系不是任何人选的。 |
| 237 | 说明里要讲清关掉之后失去的**只是**代劳,不是留意 */} |
| 238 | <label className="flex items-start gap-2.5 pt-1 cursor-pointer"> |
| 239 | <input |
| 240 | type="checkbox" |
| 241 | className="mt-0.5 accent-[var(--u-accent)]" |
| 242 | checked={autoExtend} |
| 243 | onChange={(e) => setAutoExtend(e.target.checked)} |
| 244 | /> |
| 245 | <span className="min-w-0"> |
| 246 | <span className="block text-sm text-neutral-200"> |
| 247 | {S.kbset.autoExtend} |
| 248 | </span> |
| 249 | <span className="block text-xs leading-relaxed text-neutral-500"> |
| 250 | {S.kbset.autoExtendNote} |
| 251 | </span> |
| 252 | </span> |
| 253 | </label> |
| 254 | {/* 物化推理:**默认关**,与上面那个相反。自动扩本体动的是词表, |
| 255 | 这个动的是账本——它按公理往图里写事实,而声明可能是错的 */} |
| 256 | <label className="flex items-start gap-2.5 pt-1 cursor-pointer"> |
| 257 | <input |
| 258 | type="checkbox" |
| 259 | className="mt-0.5 accent-[var(--u-accent)]" |
| 260 | checked={materialize} |
| 261 | onChange={(e) => setMaterialize(e.target.checked)} |
| 262 | /> |
| 263 | <span className="min-w-0"> |
| 264 | <span className="block text-sm text-neutral-200"> |
| 265 | {S.kbset.materialize} |
| 266 | </span> |
| 267 | <span className="block text-xs leading-relaxed text-neutral-500"> |
| 268 | {S.kbset.materializeNote} |
| 269 | </span> |
| 270 | </span> |
| 271 | </label> |
| 272 | {/* 重推间隔。**只在开着的时候露出来**——关着时它不影响任何事, |
| 273 | 摆在那里只会让人以为设了就会推 */} |
| 274 | {materialize && ( |
| 275 | <div className="pl-6 flex items-center gap-2"> |
| 276 | <label className="text-xs text-neutral-500"> |
| 277 | {S.kbset.inferEvery} |
| 278 | </label> |
| 279 | <input |
| 280 | type="number" |
| 281 | min={5} |
| 282 | max={10080} |
| 283 | className="input-dark w-24 px-2 py-1 text-xs u-num" |
| 284 | value={inferMins} |
| 285 | onChange={(e) => setInferMins(Number(e.target.value))} |
| 286 | /> |
| 287 | <span className="text-xs text-neutral-500"> |
| 288 | {S.kbset.minutes} |
| 289 | </span> |
| 290 | {kb.data.last_inference_at && ( |
| 291 | <span className="text-[11px] text-neutral-600"> |
| 292 | {S.kbset.lastInference( |
| 293 | new Date(kb.data.last_inference_at).toLocaleString(), |
| 294 | )} |
| 295 | </span> |
| 296 | )} |
| 297 | </div> |
| 298 | )} |
| 299 | {/* 语料语言。**不是界面语言**——类描述逐字进抽取提示词, |
| 300 | 读者是正在读这些文档的模型,所以它跟文档走不跟读者走 */} |
| 301 | <div className="pt-1"> |
| 302 | <span className="block text-sm text-neutral-200"> |
| 303 | {S.kbset.ontologyLang} |
| 304 | </span> |
| 305 | <span className="mt-0.5 block text-xs leading-relaxed text-neutral-500"> |
| 306 | {S.kbset.ontologyLangNote} |
| 307 | </span> |
| 308 | <div className="mt-2 flex gap-1 rounded-lg bg-white/5 p-1 w-fit"> |
| 309 | {(["en", "zh"] as const).map((l) => ( |
| 310 | <button |
| 311 | key={l} |
| 312 | onClick={() => setOntoLang(l)} |
| 313 | className={`rounded-md px-3 py-1 text-[12px] font-medium transition-colors ${ |
| 314 | ontoLang === l |
| 315 | ? "bg-white/10 text-neutral-100" |
| 316 | : "text-neutral-500 hover:text-neutral-300" |
| 317 | }`} |
| 318 | > |
| 319 | {LANG_NAMES[l]} |
| 320 | </button> |
| 321 | ))} |
| 322 | </div> |
| 323 | </div> |
| 324 | <div className="flex items-center gap-3"> |
| 325 | <button |
| 326 | className="u-btn u-btn-primary px-3.5 py-1.5 text-xs" |
| 327 | disabled={!name.trim() || save.isPending} |
| 328 | onClick={() => save.mutate()} |
| 329 | > |
| 330 | {S.kbset.save} |
| 331 | </button> |
| 332 | {save.isSuccess && ( |
| 333 | <span className="text-xs text-neutral-400"> |
| 334 | {S.kbset.saved} |
| 335 | </span> |
| 336 | )} |
| 337 | </div> |
| 338 | </div> |
| 339 | )} |
| 340 | |
| 341 | {section === "members" && ( |
| 342 | <KbMembers kbId={kbId} isOpen={kb.data.visibility === "open"} /> |
| 343 | )} |
| 344 | |
| 345 | {section === "activity" && <KbActivity kbId={kbId} />} |
| 346 | |
| 347 | {section === "danger" && ( |
| 348 | <div className="glass rounded-2xl px-5 py-4 flex items-center justify-between gap-4"> |
| 349 | <div className="min-w-0"> |
| 350 | <div className="text-sm font-medium text-neutral-200"> |
| 351 | {S.kbset.deleteRowTitle} |
| 352 | </div> |
| 353 | <div className="mt-0.5 text-xs text-neutral-500"> |
| 354 | {S.kbset.deleteRowHint} |
| 355 | </div> |
| 356 | </div> |
| 357 | <button |
| 358 | className="u-btn px-3.5 py-1.5 text-xs font-semibold shrink-0" |
| 359 | style={{ |
| 360 | background: "var(--u-danger-solid)", |
| 361 | color: "#ffffff", |
| 362 | }} |
| 363 | onClick={() => setConfirmingDelete(true)} |
| 364 | > |
| 365 | {S.kbset.deleteRowBtn} |
| 366 | </button> |
| 367 | </div> |
| 368 | )} |
| 369 | |
| 370 | {error && <p className="text-sm text-rose-400">{error}</p>} |
| 371 | |
| 372 | {confirmingDelete && ( |
| 373 | <DangerConfirm |
| 374 | title={S.kbset.deleteKb} |
| 375 | hint={S.kbset.deleteHint(kb.data.name)} |
| 376 | requireText={kb.data.name} |
| 377 | confirmLabel={S.kbset.deleteBtn} |
| 378 | cancelLabel={S.library.cancel} |
| 379 | busy={removeKb.isPending} |
| 380 | onConfirm={() => removeKb.mutate()} |
| 381 | onCancel={() => setConfirmingDelete(false)} |
| 382 | /> |
| 383 | )} |
| 384 | </div> |
| 385 | </main> |
| 386 | </div> |
| 387 | ); |
| 388 | } |
| 389 | |
| 390 | /** detail 里挑一个人类可读的名字(按 action 语义各异,逐键兜底) */ |
| 391 | function auditDetailName(e: AuditEvent): string { |
| 392 | const d = e.detail; |
| 393 | const cand = [d.label, d.name, d.filename, d.key, d.role]; |
| 394 | const hit = cand.find((v) => typeof v === "string" && v); |
| 395 | return typeof hit === "string" ? hit : ""; |
| 396 | } |
| 397 | |
| 398 | const AUDIT_PAGE = 50; |
| 399 | |
| 400 | function KbActivity({ kbId }: { kbId: string }) { |
| 401 | // 筛选按真实查法来:查一类动作、查一个人、查一段时间。 |
| 402 | // 动作前缀匹配——`entity.` 就能把 retyped / renamed 一族一起捞出来 |
| 403 | const [action, setAction] = useState(""); |
| 404 | const [since, setSince] = useState(""); |
| 405 | const [until, setUntil] = useState(""); |
| 406 | const [page, setPage] = useState(0); |
| 407 | const audit = useQuery({ |
| 408 | queryKey: ["kbAudit", kbId, action, since, until, page], |
| 409 | queryFn: () => |
| 410 | api.kbAudit(kbId, { |
| 411 | action: action || undefined, |
| 412 | since: since || undefined, |
| 413 | until: until || undefined, |
| 414 | limit: AUDIT_PAGE, |
| 415 | offset: page * AUDIT_PAGE, |
| 416 | }), |
| 417 | placeholderData: (prev) => prev, |
| 418 | }); |
| 419 | const events = audit.data?.events ?? []; |
| 420 | const total = audit.data?.total ?? 0; |
| 421 | // 下拉按这个库实际发生过的动作填,不是硬编码清单 |
| 422 | const actions = audit.data?.actions ?? []; |
| 423 | const filtered = !!(action || since || until); |
| 424 | |
| 425 | const reset = (fn: () => void) => { |
| 426 | fn(); |
| 427 | setPage(0); |
| 428 | }; |
| 429 | |
| 430 | return ( |
| 431 | <div className="glass rounded-xl p-4"> |
| 432 | <p className="text-xs text-neutral-500 mb-3">{S.kbset.activityHint}</p> |
| 433 | <div className="mb-3 flex flex-wrap items-center gap-2"> |
| 434 | <select |
| 435 | className="input-dark px-2 py-1 text-xs" |
| 436 | value={action} |
| 437 | onChange={(e) => reset(() => setAction(e.target.value))} |
| 438 | > |
| 439 | <option value="">{S.kbset.auditAllActions}</option> |
| 440 | {actions.map((a) => ( |
| 441 | <option key={a} value={a}> |
| 442 | {a} |
| 443 | </option> |
| 444 | ))} |
| 445 | </select> |
| 446 | <input |
| 447 | type="date" |
| 448 | className="input-dark px-2 py-1 text-xs u-num" |
| 449 | value={since} |
| 450 | title={S.kbset.auditSince} |
| 451 | onChange={(e) => reset(() => setSince(e.target.value))} |
| 452 | /> |
| 453 | <span className="text-xs text-neutral-600">→</span> |
| 454 | <input |
| 455 | type="date" |
| 456 | className="input-dark px-2 py-1 text-xs u-num" |
| 457 | value={until} |
| 458 | title={S.kbset.auditUntil} |
| 459 | onChange={(e) => reset(() => setUntil(e.target.value))} |
| 460 | /> |
| 461 | {filtered && ( |
| 462 | <button |
| 463 | className="u-btn u-btn-ghost px-2 py-1 text-xs" |
| 464 | onClick={() => |
| 465 | reset(() => { |
| 466 | setAction(""); |
| 467 | setSince(""); |
| 468 | setUntil(""); |
| 469 | }) |
| 470 | } |
| 471 | > |
| 472 | {S.kbset.auditClear} |
| 473 | </button> |
| 474 | )} |
| 475 | <span className="ml-auto u-num text-[11px] text-neutral-500"> |
| 476 | {S.kbset.auditTotal(total)} |
| 477 | </span> |
| 478 | </div> |
| 479 | {audit.isPending ? ( |
| 480 | <p className="text-xs text-neutral-600">{S.nav.loading}</p> |
| 481 | ) : events.length === 0 ? ( |
| 482 | <p className="text-xs text-neutral-600">{S.kbset.activityEmpty}</p> |
| 483 | ) : ( |
| 484 | <div className="space-y-0.5"> |
| 485 | {events.map((e) => ( |
| 486 | <div |
| 487 | key={e.id} |
| 488 | className="flex items-baseline gap-3 py-1.5 text-[13px]" |
| 489 | > |
| 490 | <span className="u-num shrink-0 text-[11px] text-neutral-600"> |
| 491 | {e.created_at.slice(0, 16).replace("T", " ")} |
| 492 | </span> |
| 493 | <span className="min-w-0 truncate"> |
| 494 | <span className="text-neutral-200"> |
| 495 | {e.actor_name ?? |
| 496 | (e.actor_id |
| 497 | ? S.kbset.deletedUser |
| 498 | : e.action.startsWith("review.") |
| 499 | ? S.kbset.adjudicator |
| 500 | : S.kbset.engine)} |
| 501 | </span>{" "} |
| 502 | <span className="text-neutral-500"> |
| 503 | {S.kbset.auditActions[e.action] ?? e.action} |
| 504 | </span> |
| 505 | {auditDetailName(e) && ( |
| 506 | <span className="text-neutral-300"> |
| 507 | {" "} |
| 508 | “{auditDetailName(e)}” |
| 509 | </span> |
| 510 | )} |
| 511 | </span> |
| 512 | </div> |
| 513 | ))} |
| 514 | </div> |
| 515 | )} |
| 516 | <Pager total={total} pageSize={AUDIT_PAGE} page={page} onPage={setPage} /> |
| 517 | </div> |
| 518 | ); |
| 519 | } |
| 520 | |
| 521 | function KbMembers({ kbId, isOpen }: { kbId: string; isOpen: boolean }) { |
| 522 | const queryClient = useQueryClient(); |
| 523 | const members = useQuery({ |
| 524 | queryKey: ["kbMembers", kbId], |
| 525 | queryFn: () => api.kbMembers(kbId), |
| 526 | }); |
| 527 | const orgUsers = useQuery({ queryKey: ["orgUsers"], queryFn: api.orgUsers }); |
| 528 | const [addUserId, setAddUserId] = useState(""); |
| 529 | // open 库连 viewer 这个选项都没有,默认值得跟着走 |
| 530 | const [addRole, setAddRole] = useState(isOpen ? "editor" : "viewer"); |
| 531 | |
| 532 | const invalidate = () => |
| 533 | queryClient.invalidateQueries({ queryKey: ["kbMembers", kbId] }); |
| 534 | |
| 535 | const setMember = useMutation({ |
| 536 | mutationFn: ({ userId, role }: { userId: string; role: string }) => |
| 537 | api.setKbMember(kbId, userId, role), |
| 538 | onSuccess: () => { |
| 539 | setAddUserId(""); |
| 540 | invalidate(); |
| 541 | }, |
| 542 | }); |
| 543 | const remove = useMutation({ |
| 544 | mutationFn: (userId: string) => api.removeKbMember(kbId, userId), |
| 545 | onSuccess: invalidate, |
| 546 | }); |
| 547 | |
| 548 | // open 库里 `role=viewer` 的一行**等价于没有这一行**:读权限本来人人都有, |
| 549 | // 那条记录什么都没授予。所以名单里只留真正拿到写权限的人。 |
| 550 | // |
| 551 | // **不算进 memberIds 是配套的一半**,不能只藏不放:留在里面的话, |
| 552 | // 那个人会从添加选择器里消失,于是再也授不了 editor—— |
| 553 | // 一条本该无意义的记录反而把人锁住了 |
| 554 | const listed = (members.data?.members ?? []).filter( |
| 555 | (m) => !isOpen || m.role !== "viewer", |
| 556 | ); |
| 557 | const memberIds = new Set(listed.map((m) => m.user_id)); |
| 558 | const addable = orgUsers.data?.filter((u) => !memberIds.has(u.id)) ?? []; |
| 559 | |
| 560 | if (members.isError) return null; |
| 561 | |
| 562 | return ( |
| 563 | <div className="glass rounded-xl p-4"> |
| 564 | <p className="text-xs text-neutral-500 mb-3"> |
| 565 | {isOpen ? S.kbset.membersHintOpen : S.kbset.membersHintRestricted} |
| 566 | </p> |
| 567 | |
| 568 | {members.data && listed.length === 0 && ( |
| 569 | <p className="text-xs text-neutral-600 mb-3"> |
| 570 | {isOpen ? S.kbset.noWriters : S.kbset.noMembers} |
| 571 | </p> |
| 572 | )} |
| 573 | {listed.map((m) => ( |
| 574 | <div key={m.user_id} className="flex items-center gap-3 py-1.5"> |
| 575 | <div className="min-w-0 flex-1"> |
| 576 | <span className="text-sm text-neutral-200">{m.display_name}</span> |
| 577 | <span className="ml-2 text-xs text-neutral-500">{m.email}</span> |
| 578 | </div> |
| 579 | <Dropdown |
| 580 | size="sm" |
| 581 | className="w-24" |
| 582 | value={m.role} |
| 583 | onChange={(role) => setMember.mutate({ userId: m.user_id, role })} |
| 584 | options={rolesFor(isOpen)} |
| 585 | /> |
| 586 | <button |
| 587 | onClick={() => remove.mutate(m.user_id)} |
| 588 | className="text-xs text-neutral-500 hover:text-rose-400" |
| 589 | > |
| 590 | {S.kbset.remove} |
| 591 | </button> |
| 592 | </div> |
| 593 | ))} |
| 594 | |
| 595 | {/* **picker 常驻**,不按"有没有人可加"来显示或隐藏。 |
| 596 | 一个时有时无的控件比一个空着的控件更让人困惑——不见了的第一反应是 |
| 597 | 功能坏了,而不是"没人可加"。空列表由 SearchSelect 自己说 |
| 598 | (它有 noMatches 空态),这里不必再加一句话 */} |
| 599 | <div className="mt-3 flex gap-2 items-center border-t border-white/5 pt-3"> |
| 600 | <SearchSelect |
| 601 | className="flex-1" |
| 602 | value={addUserId} |
| 603 | onChange={setAddUserId} |
| 604 | placeholder={S.kbset.addMember} |
| 605 | options={addable.map((u) => ({ |
| 606 | value: u.id, |
| 607 | label: u.display_name, |
| 608 | hint: u.email, |
| 609 | }))} |
| 610 | /> |
| 611 | <Dropdown |
| 612 | className="w-24" |
| 613 | value={addRole} |
| 614 | onChange={setAddRole} |
| 615 | options={rolesFor(isOpen)} |
| 616 | /> |
| 617 | <button |
| 618 | className="u-btn u-btn-primary px-3 py-1.5 text-xs" |
| 619 | disabled={!addUserId || setMember.isPending} |
| 620 | onClick={() => setMember.mutate({ userId: addUserId, role: addRole })} |
| 621 | > |
| 622 | {S.members.add} |
| 623 | </button> |
| 624 | </div> |
| 625 | </div> |
| 626 | ); |
| 627 | } |