| 1 | /* 用户菜单:顶栏右侧的头像胶囊 + 弹出面板(个人信息 / 系统管理 / 登出)。 |
| 2 | Shell(KB 工作区)与 AccountShell(账户层)共用。 */ |
| 3 | import { useQueryClient } from "@tanstack/react-query"; |
| 4 | import { useNavigate } from "@tanstack/react-router"; |
| 5 | import { |
| 6 | BookMarked, |
| 7 | Check, |
| 8 | Languages, |
| 9 | LogOut, |
| 10 | ShieldCheck, |
| 11 | UserRound, |
| 12 | } from "lucide-react"; |
| 13 | import { api, type User } from "../api"; |
| 14 | import { usePopoverFlip } from "../ui/popoverFlip"; |
| 15 | import { LANGS, LANG_NAMES, S, lang, setLang } from "../i18n"; |
| 16 | |
| 17 | /** 首字母头像:中性灰底(chrome 零色偏),拉丁取词首两枚,CJK 取前两字。 */ |
| 18 | export function Avatar({ name, size = 24 }: { name: string; size?: number }) { |
| 19 | const trimmed = name.trim(); |
| 20 | const words = trimmed.split(/\s+/).filter(Boolean); |
| 21 | const initials = |
| 22 | words.length >= 2 |
| 23 | ? (words[0][0] + words[1][0]).toUpperCase() |
| 24 | : [...trimmed].slice(0, 2).join("").toUpperCase(); |
| 25 | return ( |
| 26 | <span |
| 27 | className="inline-grid place-items-center rounded-full bg-white/[0.09] border border-white/10 text-neutral-200 select-none shrink-0" |
| 28 | style={{ width: size, height: size, fontSize: Math.round(size * 0.38) }} |
| 29 | > |
| 30 | {initials} |
| 31 | </span> |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | export function UserMenu({ user }: { user: User }) { |
| 36 | // 原地变形(FLIP):胶囊"长成"面板。实现共用,见 ui/popoverFlip—— |
| 37 | // 告警铃铛就在旁边,两处各写一遍迟早会差出一点点 |
| 38 | const { open, setOpen, close, rootRef, anchorRef, panelRef } = |
| 39 | usePopoverFlip<HTMLButtonElement, HTMLDivElement>(); |
| 40 | const navigate = useNavigate(); |
| 41 | const queryClient = useQueryClient(); |
| 42 | |
| 43 | const go = (to: string) => { |
| 44 | setOpen(false); |
| 45 | navigate({ to }); |
| 46 | }; |
| 47 | |
| 48 | const logout = async () => { |
| 49 | await api.logout(); |
| 50 | queryClient.clear(); |
| 51 | navigate({ to: "/login" }); |
| 52 | }; |
| 53 | |
| 54 | // 行通到面板边缘(与 Dropdown 同语汇):容器不留内衬,高度由行自身撑 |
| 55 | const item = |
| 56 | "w-full flex items-center gap-2.5 px-3.5 py-2.5 text-[13px] text-neutral-300 hover:bg-white/[0.06] hover:text-white transition-colors"; |
| 57 | |
| 58 | return ( |
| 59 | <div ref={rootRef} className="relative"> |
| 60 | <button |
| 61 | ref={anchorRef} |
| 62 | onClick={() => setOpen((v) => !v)} |
| 63 | className={`flex items-center gap-2 rounded-full py-1 pl-1 pr-3 transition-[background-color,opacity] duration-150 ${ |
| 64 | open ? "opacity-0" : "hover:bg-white/[0.06]" |
| 65 | }`} |
| 66 | > |
| 67 | <Avatar name={user.display_name} size={24} /> |
| 68 | <span className="text-sm text-neutral-300">{user.display_name}</span> |
| 69 | </button> |
| 70 | |
| 71 | {open && ( |
| 72 | <div |
| 73 | ref={panelRef} |
| 74 | className="u-menu-glass absolute right-0 top-0 w-64 rounded-xl shadow-2xl z-50 overflow-hidden" |
| 75 | > |
| 76 | {/* 身份头:再点一下缩回胶囊 */} |
| 77 | <div |
| 78 | onClick={close} |
| 79 | className="flex items-center gap-3 px-3.5 py-3 border-b border-white/10 cursor-pointer hover:bg-white/[0.04] transition-colors" |
| 80 | > |
| 81 | <Avatar name={user.display_name} size={32} /> |
| 82 | <div className="min-w-0 flex-1"> |
| 83 | <div className="flex items-center gap-2"> |
| 84 | <span className="truncate text-[13px] font-medium text-neutral-100"> |
| 85 | {user.display_name} |
| 86 | </span> |
| 87 | {user.is_admin && ( |
| 88 | <span className="u-chip u-chip-neutral !text-[10px] !px-1.5"> |
| 89 | {S.account.adminChip} |
| 90 | </span> |
| 91 | )} |
| 92 | </div> |
| 93 | <div className="truncate text-[11px] text-neutral-500"> |
| 94 | {user.email} |
| 95 | </div> |
| 96 | </div> |
| 97 | </div> |
| 98 | |
| 99 | <div> |
| 100 | <button onClick={() => go("/account")} className={item}> |
| 101 | <UserRound size={13} className="text-neutral-500" /> |
| 102 | {S.account.profile} |
| 103 | </button> |
| 104 | {/* 人人可看:全部可见库 + 我在每个库的身份 */} |
| 105 | <button onClick={() => go("/account/kbs")} className={item}> |
| 106 | <BookMarked size={13} className="text-neutral-500" /> |
| 107 | {S.account.kbsNav} |
| 108 | </button> |
| 109 | {user.is_admin && ( |
| 110 | <button onClick={() => go("/admin")} className={item}> |
| 111 | <ShieldCheck size={13} className="text-neutral-500" /> |
| 112 | {S.account.administration} |
| 113 | </button> |
| 114 | )} |
| 115 | </div> |
| 116 | |
| 117 | {/* 界面语言:看的人自己定,不经过后端(docs/decisions/0004)。 |
| 118 | 每个选项用**它自己的语言**写——看不懂英文的人才认得出"中文" */} |
| 119 | <div className="border-t border-white/10"> |
| 120 | <div className="flex items-center gap-2.5 px-3.5 pt-2.5 pb-1 text-[11px] text-neutral-500"> |
| 121 | <Languages size={13} className="text-neutral-500" /> |
| 122 | {S.account.language} |
| 123 | </div> |
| 124 | {LANGS.map((l) => ( |
| 125 | <button key={l} onClick={() => setLang(l)} className={item}> |
| 126 | <span className="w-[13px] shrink-0"> |
| 127 | {l === lang && ( |
| 128 | <Check size={13} className="text-neutral-400" /> |
| 129 | )} |
| 130 | </span> |
| 131 | {LANG_NAMES[l]} |
| 132 | </button> |
| 133 | ))} |
| 134 | </div> |
| 135 | |
| 136 | <div className="border-t border-white/10"> |
| 137 | <button |
| 138 | onClick={logout} |
| 139 | className={`${item} !text-[var(--u-danger)]`} |
| 140 | > |
| 141 | <LogOut size={13} /> |
| 142 | {S.nav.signOut} |
| 143 | </button> |
| 144 | </div> |
| 145 | </div> |
| 146 | )} |
| 147 | </div> |
| 148 | ); |
| 149 | } |