nex_buddy/web/src/components/TopBar.tsx

95 lines
3.0 KiB
TypeScript

import { Avatar, Button, Dropdown, Space, Typography } from 'antd'
import type { MenuProps } from 'antd'
import { useTranslation } from 'react-i18next'
import LangSwitch from './LangSwitch'
import ThemeSwitch from './ThemeSwitch'
import { ChevronDownIcon, LogoutIcon, ShieldIcon, UserIcon } from './icons'
const { Title, Text } = Typography
export type TopBarUser = {
nickname?: string | null
email?: string | null
phone?: string | null
role?: string
} | null
type Props = {
title: string
badge?: React.ReactNode
user?: TopBarUser
/** 管理后台入口,仅管理员可见 */
onAdmin?: () => void
/** 回到用户中心 */
onHome?: () => void
onLogout?: () => void
}
/**
* 统一顶栏:左侧品牌,右侧只留三个控件(语言 / 主题 / 账号菜单)。
* 之前 Home 与 Admin 各写一套,把"管理后台 / 退出"平铺成按钮,窄屏必挤成两行;
* 收进账号下拉后,无论几个入口都只占一个位置。
*/
export default function TopBar({ title, badge, user, onAdmin, onHome, onLogout }: Props) {
const { t } = useTranslation()
const name = user?.nickname || user?.email || ''
const account = user?.email || user?.phone || name
const initial = (name.trim()[0] || '?').toUpperCase()
const items: MenuProps['items'] = [
{
key: 'who',
disabled: true,
label: (
<div className="account-meta">
<div className="account-meta-name">{name || t('common.guest')}</div>
{account && account !== name && <div className="account-meta-sub">{account}</div>}
</div>
),
},
{ type: 'divider' },
...(onAdmin && user?.role === 'admin'
? [{ key: 'admin', icon: <ShieldIcon size={15} />, label: t('common.adminCenter') }]
: []),
...(onHome ? [{ key: 'home', icon: <UserIcon size={15} />, label: t('common.myDevices') }] : []),
...(onLogout
? [
{ type: 'divider' as const },
{ key: 'logout', danger: true, icon: <LogoutIcon size={15} />, label: t('common.logout') },
]
: []),
]
const onClick: MenuProps['onClick'] = ({ key }) => {
if (key === 'admin') onAdmin?.()
else if (key === 'home') onHome?.()
else if (key === 'logout') onLogout?.()
}
return (
<header className="topbar">
<Space size={10} align="center" className="topbar-brand">
<span className="brand-dot" />
<Title level={5} className="topbar-title">
{title}
</Title>
{badge}
</Space>
<Space size={8} align="center" className="topbar-actions">
<LangSwitch />
<ThemeSwitch />
<Dropdown menu={{ items, onClick }} trigger={['click']} placement="bottomRight" arrow>
<Button type="text" className="account-btn">
<Avatar size={26} className="account-avatar">
{initial}
</Avatar>
<Text className="account-name hide-sm">{name}</Text>
<ChevronDownIcon size={13} className="account-caret" />
</Button>
</Dropdown>
</Space>
</header>
)
}