124 lines
6.9 KiB
TypeScript
124 lines
6.9 KiB
TypeScript
/**
|
||
* 顶栏账号胶囊:头像 + 角色 + 二级菜单(我的账号 / 用户管理 / 退出登录)。
|
||
*
|
||
* 环境自检不再常驻一列文字,只在真的有问题时到菜单里冒出来一次。
|
||
* 换页后菜单里的高亮项、角色都可能变,直接收起最省事。
|
||
*/
|
||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||
import { Link, NavLink, useLocation, useNavigate } from 'react-router-dom'
|
||
import { cn } from '../../lib/cn'
|
||
import {
|
||
logout, roleLabel, useAuthStore, useCanEdit, useDisplayName, useIsAdmin
|
||
} from '../../stores/auth'
|
||
import { useSystemStore } from '../../stores/system'
|
||
import { useOutsideClick } from '../../hooks'
|
||
|
||
const ITEM = 'flex min-h-[32px] w-full items-center gap-[9px] rounded-[8px] border border-transparent bg-transparent px-[9px] py-[7px] text-left text-body text-ink-2 no-underline hover:border-line hover:bg-hover hover:text-ink'
|
||
const ICON = 'w-[15px] flex-none text-center text-body text-muted'
|
||
|
||
/** 环境自检结论:有问题才说,没问题时菜单里不占位置 */
|
||
function envIssueOf(health: any): string {
|
||
if (!health) return ''
|
||
if (health.executor === 'celery' && !health.daemon?.running) return '调度守护未运行:新任务会一直排在队列里'
|
||
if (!health.ffmpeg) return 'ffmpeg 缺失:时间线合成会被阻断'
|
||
if (health.ffmpeg_capabilities && health.ffmpeg_capabilities.subtitle_burn === false) return 'ffmpeg 缺 libass:字幕将自动转成内嵌轨'
|
||
return ''
|
||
}
|
||
|
||
export function UserMenu() {
|
||
const navigate = useNavigate()
|
||
const location = useLocation()
|
||
const user = useAuthStore((s) => s.user)
|
||
const displayName = useDisplayName()
|
||
const isAdmin = useIsAdmin()
|
||
const canEdit = useCanEdit()
|
||
const health = useSystemStore((s) => s.health)
|
||
const [open, setOpen] = useState(false)
|
||
const hostRef = useRef<HTMLDivElement | null>(null)
|
||
const close = useCallback(() => setOpen(false), [])
|
||
|
||
useOutsideClick(hostRef, close, open)
|
||
/* 换页后菜单里的高亮项、角色都可能变,直接收起最省事 */
|
||
useEffect(close, [location.pathname, close])
|
||
|
||
if (!user) return null
|
||
const mustChangePassword = !!user.must_change_password
|
||
const initial = (displayName || '?').slice(0, 1).toUpperCase()
|
||
const envIssue = envIssueOf(health)
|
||
|
||
async function signOut() {
|
||
close()
|
||
await logout()
|
||
await navigate({ pathname: '/login', search: '?loggedOut=1' }, { replace: true })
|
||
}
|
||
|
||
return (
|
||
<div ref={hostRef} className="relative flex-none">
|
||
<button type="button" onClick={() => setOpen(!open)} aria-expanded={open} aria-haspopup="menu"
|
||
title={'账号菜单 · ' + displayName}
|
||
className={cn('relative flex h-(--nex-ctl) items-center gap-2 rounded-pill border bg-sunken pl-1 pr-2 text-left text-ink-2 hover:border-line-bright hover:bg-hover hover:text-ink',
|
||
mustChangePassword ? 'border-warn-line' : 'border-line', open && 'border-line-bright bg-hover text-ink')}>
|
||
<span aria-hidden="true" className="grid h-[27px] w-[27px] flex-none place-items-center rounded-pill bg-accent-fill text-body font-bold text-on-accent">{initial}</span>
|
||
<span className="grid max-w-[132px] min-w-0 leading-[1.25] max-narrow:hidden [&_b]:truncate [&_small]:truncate">
|
||
<b className="text-label">{displayName}</b>
|
||
<small className="text-tiny text-faint">{roleLabel(user.role)}{user.login_count ? ` · 第 ${user.login_count} 次登录` : ''}</small>
|
||
</span>
|
||
<span aria-hidden="true" className="flex-none text-caption text-faint transition-transform duration-[180ms]" style={{ transform: open ? 'rotate(180deg)' : undefined }}>⌄</span>
|
||
{/* 待改密码的提醒不能只藏在菜单里:胶囊上先挂一个角标。
|
||
角标挂在胶囊而不是头像上——头像是 27px 的正圆,绝对定位的角标会把
|
||
它自己的 scrollWidth 撑到 30px(内容没被裁,但等于凭空多出横向溢出)。 */}
|
||
{mustChangePassword && (
|
||
<span aria-hidden="true" className="absolute -right-0.5 -top-[3px] grid h-[14px] w-[14px] place-items-center rounded-pill bg-warn-fill text-tiny font-bold text-warn-ink">!</span>
|
||
)}
|
||
</button>
|
||
|
||
{open && (
|
||
<div role="menu" className="absolute right-0 top-[calc(100%+8px)] z-70 grid w-[262px] gap-0.5 rounded-block border border-line-strong bg-panel p-[9px] shadow-[0_18px_44px_rgba(0,0,0,.44)]">
|
||
<div className="flex items-center gap-[9px] border-b border-line-soft px-1 pb-[9px] pt-[3px]">
|
||
<span aria-hidden="true" className="grid h-[27px] w-[27px] flex-none place-items-center rounded-pill bg-accent-fill text-body font-bold text-on-accent">{initial}</span>
|
||
<span className="grid min-w-0 gap-0.5">
|
||
<b className="truncate text-[12.5px]">{displayName}</b>
|
||
<small className="truncate text-tiny text-faint">{user.username}{user.email ? ` · ${user.email}` : ''}</small>
|
||
</span>
|
||
<em className="ml-auto flex-none rounded-pill bg-hover px-[6px] py-0.5 text-tiny not-italic text-ink-2">{roleLabel(user.role)}</em>
|
||
</div>
|
||
|
||
{mustChangePassword && (
|
||
<p className="m-0 rounded-[8px] bg-warn-tint px-[9px] py-[7px] text-tiny leading-[1.5] text-warn-bright [overflow-wrap:anywhere]">
|
||
<Link to="/account?must=1" role="menuitem" onClick={close} className="text-inherit underline">初始密码尚未修改,点此设置新密码 →</Link>
|
||
</p>
|
||
)}
|
||
{!canEdit && <p className="m-0 rounded-[8px] bg-hover px-[9px] py-[7px] text-tiny leading-[1.5] text-faint">观摩角色:只读,写操作会被后端拒绝</p>}
|
||
|
||
<div role="separator" className="my-[5px] mx-0.5 h-px bg-line-soft"></div>
|
||
|
||
<NavLink to="/account" role="menuitem" title="账号信息、修改密码与登录设备" className={ITEM} onClick={close}>
|
||
<i className={ICON}>◍</i><span>我的账号</span>
|
||
</NavLink>
|
||
{isAdmin && (
|
||
<NavLink to="/users" role="menuitem" title="账号、角色与配额" className={ITEM} onClick={close}>
|
||
<i className={ICON}>▤</i><span>用户管理</span>
|
||
</NavLink>
|
||
)}
|
||
|
||
{envIssue && (
|
||
<>
|
||
<div role="separator" className="my-[5px] mx-0.5 h-px bg-line-soft"></div>
|
||
<p className="m-0 rounded-[8px] bg-warn-tint px-[9px] py-[7px] text-tiny leading-[1.5] text-warn-bright [overflow-wrap:anywhere]">{envIssue}</p>
|
||
</>
|
||
)}
|
||
|
||
<div role="separator" className="my-[5px] mx-0.5 h-px bg-line-soft"></div>
|
||
|
||
<button type="button" role="menuitem" title="退出当前会话" onClick={signOut}
|
||
className={cn(ITEM, 'text-danger [&_i]:text-danger hover:border-danger-line hover:bg-danger-tint hover:text-danger-bright hover:[&_i]:text-danger-bright')}>
|
||
<i className={ICON}>⏻</i><span>退出登录</span>
|
||
</button>
|
||
</div>
|
||
)}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
export default UserMenu
|