Files
monkey-ai-web/src/components/layout/headerbar/UserArea.jsx
T
2026-11-19 10:48:17 +08:00

195 lines
6.0 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.
/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
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;