| 1 | /* 账户层"我的知识库":可访问的库 + 我的角色 + 加入信息 + 概览统计。 |
| 2 | 只读全景——建库是管理动作,入口在 System settings › Knowledge bases。 */ |
| 3 | import { useQuery } from "@tanstack/react-query"; |
| 4 | import { useNavigate } from "@tanstack/react-router"; |
| 5 | import { Lock } from "lucide-react"; |
| 6 | import { api, type MyKb } from "../api"; |
| 7 | import { S } from "../i18n"; |
| 8 | import { useKb } from "../kb"; |
| 9 | import { Chip, Loading } from "../ui"; |
| 10 | |
| 11 | const ymd = (iso: string) => iso.slice(0, 10); |
| 12 | |
| 13 | function joinInfo(row: MyKb): string { |
| 14 | if (row.my_role === "owner") return S.account.deploymentAdmin; |
| 15 | if (row.joined_at) { |
| 16 | return row.added_by_name |
| 17 | ? S.account.addedBy(row.added_by_name, ymd(row.joined_at)) |
| 18 | : S.account.joinedOn(ymd(row.joined_at)); |
| 19 | } |
| 20 | return S.account.openToEveryone; |
| 21 | } |
| 22 | |
| 23 | export function MyKbs() { |
| 24 | const navigate = useNavigate(); |
| 25 | const { workspace, setKb } = useKb(); |
| 26 | |
| 27 | const mine = useQuery({ |
| 28 | queryKey: ["myKbs", workspace?.id], |
| 29 | queryFn: () => api.myKbs(workspace!.id), |
| 30 | enabled: !!workspace, |
| 31 | }); |
| 32 | |
| 33 | if (!workspace || mine.isPending) return <Loading>{S.nav.loading}</Loading>; |
| 34 | |
| 35 | const rows = mine.data?.kbs ?? []; |
| 36 | const openKb = (id: string) => { |
| 37 | setKb(id); |
| 38 | navigate({ to: "/kb/$kbId/graph", params: { kbId: id } }); |
| 39 | }; |
| 40 | |
| 41 | return ( |
| 42 | <div className="max-w-2xl p-8"> |
| 43 | <div className="mb-6"> |
| 44 | <h1 className="u-title text-lg">{S.account.kbsTitle}</h1> |
| 45 | </div> |
| 46 | |
| 47 | <div className="space-y-3"> |
| 48 | {rows.map((row) => { |
| 49 | const canManage = row.my_role === "admin" || row.my_role === "owner"; |
| 50 | return ( |
| 51 | <div key={row.kb.id} className="glass glass-hover rounded-2xl px-5 py-4"> |
| 52 | <div className="flex items-start gap-3"> |
| 53 | <div className="min-w-0 flex-1"> |
| 54 | <div className="flex items-center gap-2"> |
| 55 | <span className="text-[15px] font-medium text-neutral-100 truncate"> |
| 56 | {row.kb.name} |
| 57 | </span> |
| 58 | {row.kb.visibility === "restricted" ? ( |
| 59 | <span className="flex items-center gap-1 text-[10.5px] text-neutral-500"> |
| 60 | <Lock size={10} /> |
| 61 | {S.account.kbRestricted} |
| 62 | </span> |
| 63 | ) : ( |
| 64 | <span className="text-[10.5px] text-neutral-600">{S.account.kbOpen}</span> |
| 65 | )} |
| 66 | </div> |
| 67 | {row.kb.description && ( |
| 68 | <p className="mt-0.5 text-xs text-neutral-500 truncate"> |
| 69 | {row.kb.description} |
| 70 | </p> |
| 71 | )} |
| 72 | <p className="mt-1.5 text-[11px] text-neutral-600"> |
| 73 | <span className="u-num">{S.account.kbStats(row.doc_count, row.member_count)}</span> |
| 74 | <span className="mx-1.5 text-neutral-700">·</span> |
| 75 | {joinInfo(row)} |
| 76 | </p> |
| 77 | </div> |
| 78 | <div className="shrink-0 flex items-center gap-2"> |
| 79 | {row.my_role && ( |
| 80 | <Chip tone={canManage ? "info" : "neutral"}> |
| 81 | {S.account.roleNames[row.my_role] ?? row.my_role} |
| 82 | </Chip> |
| 83 | )} |
| 84 | <button |
| 85 | onClick={() => openKb(row.kb.id)} |
| 86 | className="u-btn u-btn-ghost px-2.5 py-1 text-xs" |
| 87 | > |
| 88 | {S.account.openKb} |
| 89 | </button> |
| 90 | {canManage && ( |
| 91 | <button |
| 92 | onClick={() => { |
| 93 | setKb(row.kb.id); |
| 94 | navigate({ to: "/kb/$kbId/settings", params: { kbId: row.kb.id } }); |
| 95 | }} |
| 96 | className="u-btn u-btn-ghost px-2.5 py-1 text-xs" |
| 97 | > |
| 98 | {S.account.kbSettingsBtn} |
| 99 | </button> |
| 100 | )} |
| 101 | </div> |
| 102 | </div> |
| 103 | </div> |
| 104 | ); |
| 105 | })} |
| 106 | </div> |
| 107 | |
| 108 | </div> |
| 109 | ); |
| 110 | } |