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: (
{name || t('common.guest')}
{account && account !== name &&
{account}
}
), }, { type: 'divider' }, ...(onAdmin && user?.role === 'admin' ? [{ key: 'admin', icon: , label: t('common.adminCenter') }] : []), ...(onHome ? [{ key: 'home', icon: , label: t('common.myDevices') }] : []), ...(onLogout ? [ { type: 'divider' as const }, { key: 'logout', danger: true, icon: , label: t('common.logout') }, ] : []), ] const onClick: MenuProps['onClick'] = ({ key }) => { if (key === 'admin') onAdmin?.() else if (key === 'home') onHome?.() else if (key === 'logout') onLogout?.() } return (
{title} {badge}
) }