240 lines
7.3 KiB
React
240 lines
7.3 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 {
|
||
ChevronRight as ActionChevronRight,
|
||
ChevronLeft as ActionChevronLeft,
|
||
} from '@/components/common/icons/ActionIcons';
|
||
import React, { useMemo, useContext } from 'react';
|
||
import { Link, useLocation } from 'react-router-dom';
|
||
import {
|
||
isMiniProgramPaymentPath,
|
||
isMiniProgramWebView,
|
||
} from '../../helpers/miniProgram';
|
||
import { useTranslation } from 'react-i18next';
|
||
import {
|
||
LayoutGrid,
|
||
ScrollText,
|
||
Wallet,
|
||
KeyRound,
|
||
UserRound,
|
||
Receipt,
|
||
Gift,
|
||
MonitorDown,
|
||
} from 'lucide-react';
|
||
import { StatusContext } from '../../context/Status';
|
||
import { UserContext } from '../../context/User';
|
||
import { isGroupAllowed } from '../../helpers/navigationVisibility';
|
||
import { useSidebarCollapsed } from '../../hooks/common/useSidebarCollapsed';
|
||
import { useWeyAiDownloadEntry } from '../../hooks/common/useWeyAiWebEntry';
|
||
|
||
const UserCenterSidebar = () => {
|
||
const { t } = useTranslation();
|
||
const location = useLocation();
|
||
const [statusState] = useContext(StatusContext);
|
||
const [userState] = useContext(UserContext);
|
||
const userGroup = userState?.user?.group || 'default';
|
||
const [collapsed, toggleCollapsed] = useSidebarCollapsed();
|
||
// WeyAi 桌面端下载入口:与顶栏「下载 WeyAi」按钮共用同一套判定
|
||
//(下载页开关 + 网页端已配置时沿用其开放范围),两处入口口径一致
|
||
const { visible: weyaiDownloadEnabled } = useWeyAiDownloadEntry();
|
||
|
||
// 获取侧边栏配置
|
||
const sidebarConfig = useMemo(() => {
|
||
const defaultConfig = {
|
||
enabled: true,
|
||
groups: [],
|
||
moduleGroups: {},
|
||
dashboard: true,
|
||
usageRecord: true,
|
||
topup: true,
|
||
invoice: true,
|
||
invite: true,
|
||
settings: true,
|
||
};
|
||
|
||
try {
|
||
const configStr = statusState?.status?.SidebarModulesAdmin;
|
||
if (configStr) {
|
||
const config = JSON.parse(configStr);
|
||
const uc = config.userCenter || {};
|
||
// 向后兼容:只有旧配置同时隐藏了 usageLog 和 taskLog,才隐藏合并后的 usageRecord;
|
||
// 只隐藏其中之一时保持显示,避免用户意外失去本应可见的日志入口
|
||
if (
|
||
uc.usageRecord === undefined &&
|
||
uc.usageLog === false &&
|
||
uc.taskLog === false
|
||
) {
|
||
uc.usageRecord = false;
|
||
}
|
||
const merged = { ...defaultConfig, ...uc };
|
||
return {
|
||
...merged,
|
||
enabled:
|
||
merged.enabled !== false &&
|
||
isGroupAllowed(merged.groups, userGroup),
|
||
};
|
||
}
|
||
} catch (error) {
|
||
// 使用默认配置
|
||
}
|
||
return defaultConfig;
|
||
}, [statusState?.status?.SidebarModulesAdmin, userGroup]);
|
||
|
||
// 统一单一品牌色高亮(云平台风格):菜单项不再各用一种颜色,
|
||
// 激活态统一走 brand(azure),非激活中性灰,靠图标本身区分功能。
|
||
const allItems = useMemo(
|
||
() => [
|
||
{
|
||
itemKey: '/user-center/dashboard',
|
||
configKey: 'dashboard',
|
||
text: t('数据看板'),
|
||
icon: LayoutGrid,
|
||
},
|
||
{
|
||
itemKey: '/user-center/usage-record',
|
||
configKey: 'usageRecord',
|
||
text: t('使用记录'),
|
||
icon: ScrollText,
|
||
},
|
||
{
|
||
itemKey: '/user-center/key-management',
|
||
configKey: 'keyManagement',
|
||
text: t('密钥管理'),
|
||
icon: KeyRound,
|
||
},
|
||
{
|
||
itemKey: '/user-center/topup',
|
||
configKey: 'topup',
|
||
text: t('在线充值'),
|
||
icon: Wallet,
|
||
},
|
||
{
|
||
itemKey: '/user-center/invoice',
|
||
configKey: 'invoice',
|
||
text: t('发票管理'),
|
||
icon: Receipt,
|
||
},
|
||
{
|
||
itemKey: '/user-center/invite',
|
||
configKey: 'invite',
|
||
text: t('邀请有奖'),
|
||
icon: Gift,
|
||
},
|
||
{
|
||
itemKey: '/user-center/settings',
|
||
configKey: 'settings',
|
||
text: t('个人设置'),
|
||
icon: UserRound,
|
||
},
|
||
...(weyaiDownloadEnabled
|
||
? [
|
||
{
|
||
// 指向用户中心内的嵌套路由,保持侧栏不消失;独立的 /weyai/download
|
||
// 留给未登录访客与顶栏/客户端里的链接
|
||
itemKey: '/user-center/weyai-download',
|
||
configKey: 'weyaiDownload',
|
||
text: t('下载 WeyAi 桌面端'),
|
||
icon: MonitorDown,
|
||
},
|
||
]
|
||
: []),
|
||
],
|
||
[t, weyaiDownloadEnabled],
|
||
);
|
||
|
||
// 根据配置过滤菜单项
|
||
const items = useMemo(() => {
|
||
if (!sidebarConfig.enabled) {
|
||
return [];
|
||
}
|
||
const inMiniProgram = isMiniProgramWebView();
|
||
return allItems.filter(
|
||
(item) =>
|
||
sidebarConfig[item.configKey] !== false &&
|
||
isGroupAllowed(
|
||
sidebarConfig.moduleGroups?.[item.configKey],
|
||
userGroup,
|
||
) &&
|
||
!(inMiniProgram && isMiniProgramPaymentPath(item.itemKey)),
|
||
);
|
||
}, [allItems, sidebarConfig, userGroup]);
|
||
|
||
const getSelectedKey = () => {
|
||
if (items.some((item) => item.itemKey === location.pathname)) {
|
||
return location.pathname;
|
||
}
|
||
if (
|
||
location.pathname === '/user-center' ||
|
||
location.pathname === '/user-center/'
|
||
) {
|
||
return '/user-center/dashboard';
|
||
}
|
||
return '';
|
||
};
|
||
|
||
const selectedKey = getSelectedKey();
|
||
|
||
return (
|
||
<aside
|
||
className='ui-user-sidebar ui-center-sidebar'
|
||
data-collapsed={collapsed}
|
||
>
|
||
<nav className='ui-center-sidebar-nav'>
|
||
<div className='ui-org-group-items'>
|
||
{items.map((item) => {
|
||
const active = selectedKey === item.itemKey;
|
||
const Icon = item.icon;
|
||
return (
|
||
<Link
|
||
key={item.itemKey}
|
||
to={item.itemKey}
|
||
className='ui-center-menu-item'
|
||
data-active={active || undefined}
|
||
aria-current={active ? 'page' : undefined}
|
||
title={collapsed ? item.text : undefined}
|
||
>
|
||
<Icon size={16} strokeWidth={1.5} aria-hidden='true' />
|
||
{!collapsed && (
|
||
<span className='ui-center-menu-label'>{item.text}</span>
|
||
)}
|
||
</Link>
|
||
);
|
||
})}
|
||
</div>
|
||
</nav>
|
||
<button
|
||
type='button'
|
||
className='app-action-button ui-center-sidebar-collapse'
|
||
onClick={toggleCollapsed}
|
||
aria-label={collapsed ? t('展开导航') : t('收起导航')}
|
||
>
|
||
{collapsed ? (
|
||
<ActionChevronRight size={16} />
|
||
) : (
|
||
<ActionChevronLeft size={16} />
|
||
)}
|
||
{!collapsed && <span>{t('收起导航')}</span>}
|
||
</button>
|
||
</aside>
|
||
);
|
||
};
|
||
|
||
export default UserCenterSidebar;
|