256 lines
8.0 KiB
React
256 lines
8.0 KiB
React
/*
|
||
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 React, { useState } from 'react';
|
||
import { Link, useLocation } from 'react-router-dom';
|
||
import { Avatar, Button as AntButton, Dropdown as AntDropdown } from 'antd';
|
||
import { Button } from '@/components/ui';
|
||
import { ChevronDown, UserRound, KeyRound, LogOut } from 'lucide-react';
|
||
import { getUserAvatarUrl, isAuthPagePath } 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,
|
||
isSelfUseMode,
|
||
headerNavModules,
|
||
rightActions = {},
|
||
sidebarModulesAdmin = {},
|
||
isOrgUser = false,
|
||
logout,
|
||
navigate,
|
||
t,
|
||
}) => {
|
||
const location = useLocation();
|
||
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;
|
||
};
|
||
|
||
// 在登录/注册/重置密码/企业登录页面时,未登录用户不显示登录注册按钮,避免重复
|
||
const isAuthPage = isAuthPagePath(location.pathname);
|
||
|
||
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']}
|
||
placement='bottomRight'
|
||
open={open}
|
||
onOpenChange={setOpen}
|
||
overlayClassName='ui-account-dropdown'
|
||
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>
|
||
)}
|
||
>
|
||
<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>
|
||
<ChevronDown
|
||
size={10}
|
||
strokeWidth={1.5}
|
||
aria-hidden
|
||
className={`ui-header-account-chevron${open ? ' is-open' : ''}`}
|
||
/>
|
||
</AntButton>
|
||
</AntDropdown>
|
||
);
|
||
} else {
|
||
if (isAuthPage) {
|
||
return null;
|
||
}
|
||
|
||
const showRegisterButton = !isSelfUseMode;
|
||
|
||
const commonSizingAndLayoutClass =
|
||
'flex items-center justify-center !py-[10px] !px-1.5';
|
||
|
||
const loginButtonSpecificStyling =
|
||
'!bg-transparent hover:!bg-brand/10 dark:hover:!bg-brand/15 hover:!text-brand-deep !transition-all !duration-200';
|
||
let loginButtonClasses = `${commonSizingAndLayoutClass} ${loginButtonSpecificStyling}`;
|
||
|
||
let registerButtonClasses = `${commonSizingAndLayoutClass}`;
|
||
|
||
const loginButtonTextSpanClass =
|
||
'!text-xs !text-ui-color-text-1 dark:!text-gray-300 !p-1.5';
|
||
const registerButtonTextSpanClass = '!text-xs !text-white !p-1.5';
|
||
|
||
if (showRegisterButton) {
|
||
if (isMobile) {
|
||
loginButtonClasses += ' !rounded-full';
|
||
} else {
|
||
loginButtonClasses += ' !rounded-l-full !rounded-r-none';
|
||
}
|
||
registerButtonClasses += ' !rounded-r-full !rounded-l-none';
|
||
} else {
|
||
loginButtonClasses += ' !rounded-full';
|
||
}
|
||
|
||
return (
|
||
<div className='flex items-center'>
|
||
<Link to='/org/login' className='flex'>
|
||
<Button
|
||
theme='borderless'
|
||
type='tertiary'
|
||
className={loginButtonClasses}
|
||
>
|
||
<span className={loginButtonTextSpanClass}>{t('登录')}</span>
|
||
</Button>
|
||
</Link>
|
||
{showRegisterButton && (
|
||
<div className='hidden md:block'>
|
||
<Link to='/register' className='flex -ml-px'>
|
||
<Button
|
||
theme='solid'
|
||
type='primary'
|
||
className={registerButtonClasses}
|
||
>
|
||
<span className={registerButtonTextSpanClass}>{t('注册')}</span>
|
||
</Button>
|
||
</Link>
|
||
</div>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|
||
};
|
||
|
||
export default UserArea;
|