Files
monkey-ai-web/src/components/layout/headerbar/UserArea.jsx
T
zhanjunhao 2aa9d73325 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

176 lines
5.3 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { ChevronDown as ActionChevronDown } from '@/components/common/icons/ActionIcons';
import React, { useState } from 'react';
import { Avatar, Button as AntButton, Dropdown as AntDropdown } from 'antd';
import { UserRound, KeyRound, LogOut } from 'lucide-react';
import { getUserAvatarUrl } from '../../../helpers';
import SkeletonWrapper from '../components/SkeletonWrapper';
import { isHeaderModuleEnabled } from '../../../constants/headerNavModules';
// 头像菜单项:图标框 + 标题 + 说明(原型 account-menu-label)
const AccountMenuLabel = ({ icon, tone, title, description }) => (
<div className='ui-account-menu-label'>
<span className={`ui-account-menu-icon ui-account-menu-icon-${tone}`}>
{icon}
</span>
<span>
{title}
<small>{description}</small>
</span>
</div>
);
const MENU_ICON_PROPS = { size: 18, strokeWidth: 1.65, 'aria-hidden': true };
const UserArea = ({
userState,
isLoading,
isMobile,
headerNavModules,
rightActions = {},
sidebarModulesAdmin = {},
isOrgUser = false,
logout,
navigate,
t,
}) => {
const [open, setOpen] = useState(false);
const isActionVisible = (key) => rightActions?.[key] !== false;
const hasHeaderModuleConfig =
headerNavModules && Object.keys(headerNavModules).length > 0;
const isTopNavModuleVisible = (key) =>
!hasHeaderModuleConfig || isHeaderModuleEnabled(headerNavModules, key);
const isSidebarModuleVisible = (sectionKey, moduleKey) => {
const section = sidebarModulesAdmin?.[sectionKey];
if (!section) return true;
return section.enabled !== false && section[moduleKey] !== false;
};
if (isLoading) {
return (
<SkeletonWrapper
loading={true}
type='userArea'
width={50}
isMobile={isMobile}
/>
);
}
if (userState.user) {
const displayName = userState.user.display_name || userState.user.username;
const avatarUrl = getUserAvatarUrl(userState.user);
const avatarInitial = displayName[0].toUpperCase();
const showSettings =
isActionVisible('userSettings') &&
isTopNavModuleVisible('userCenter') &&
isSidebarModuleVisible('userCenter', 'settings');
// 「密钥管理快捷入口」由 rightActions.keyManagement 单独控制(运营设置→顶栏管理)。
// 个人用户的 /user-center/key-management 路由不依赖顶栏模块是否显示,故顶栏导航隐藏该模块时
// 头像菜单仍保留入口;企业员工只有在其顶栏模块放开时路由才被放行,继续联动判断。
const showKeyManagement =
isActionVisible('keyManagement') &&
(!isOrgUser || isTopNavModuleVisible('keyManagement'));
const items = [];
if (showSettings) {
items.push({
key: 'settings',
label: (
<AccountMenuLabel
tone='user'
icon={<UserRound {...MENU_ICON_PROPS} />}
title={t('个人设置')}
description={t('管理账户信息')}
/>
),
onClick: () => navigate('/user-center/settings'),
});
}
if (showKeyManagement) {
items.push({
key: 'keys',
label: (
<AccountMenuLabel
tone='key'
icon={<KeyRound {...MENU_ICON_PROPS} />}
title={t('密钥管理')}
description={t('查看和管理 API 密钥')}
/>
),
onClick: () => navigate('/user-center/key-management'),
});
}
items.push({ key: 'account-divider', type: 'divider' });
items.push({
key: 'logout',
label: (
<AccountMenuLabel
tone='logout'
icon={<LogOut {...MENU_ICON_PROPS} />}
title={t('退出登录')}
description={t('安全退出账户')}
/>
),
onClick: logout,
});
return (
<AntDropdown
trigger={['hover', 'click']}
placement='bottomRight'
open={open}
onOpenChange={setOpen}
menu={{ items, onClick: () => setOpen(false) }}
popupRender={(menu) => (
<div className='ui-account-panel'>
<div className='ui-account-summary'>
<Avatar
shape='square'
size={40}
src={avatarUrl}
className='ui-account-avatar'
>
{avatarInitial}
</Avatar>
<div>
<strong>{displayName}</strong>
<span>{userState.user.email || t('已登录')}</span>
</div>
</div>
{menu}
</div>
)}
classNames={{ root: 'ui-account-dropdown' }}
>
<AntButton
type='text'
className='ui-header-account'
aria-haspopup='menu'
aria-expanded={open}
>
<Avatar
shape='square'
size={38}
src={avatarUrl}
className='ui-account-avatar'
>
{avatarInitial}
</Avatar>
<span className='ui-header-account-name'>{displayName}</span>
<ActionChevronDown
size={10}
strokeWidth={1.5}
aria-hidden
className={`ui-header-account-chevron${open ? ' is-open' : ''}`}
/>
</AntButton>
</AntDropdown>
);
}
return null;
};
export default UserArea;