693 lines
20 KiB
React
693 lines
20 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, { useContext, useEffect, useMemo, useState } from 'react';
|
||
import { Link, useLocation } from 'react-router-dom';
|
||
import { useTranslation } from 'react-i18next';
|
||
import { getLucideIcon } from '../../helpers/render';
|
||
import { ChevronDown, ChevronLeft } from 'lucide-react';
|
||
import { useSidebarCollapsed } from '../../hooks/common/useSidebarCollapsed';
|
||
import { useSidebar } from '../../hooks/common/useSidebar';
|
||
import { useMinimumLoadingTime } from '../../hooks/common/useMinimumLoadingTime';
|
||
import { isAdmin, isRoot, showError } from '../../helpers';
|
||
import { StatusContext } from '../../context/Status';
|
||
import { UserContext } from '../../context/User';
|
||
import {
|
||
canAccessDelegatedAdminPage,
|
||
getDelegatedSettingTabs,
|
||
} from '../../helpers/adminDelegation';
|
||
import SkeletonWrapper from './components/SkeletonWrapper';
|
||
import { CLOUD_BRAIN_UI_ENABLED } from '../../config/cloudBrainUi';
|
||
|
||
import { Nav, Button } from '@/components/ui';
|
||
|
||
const routerMap = {
|
||
home: '/',
|
||
channel: '/console/channel',
|
||
'admin-token': '/console/token',
|
||
redemption: '/console/redemption',
|
||
topup: '/console/topup',
|
||
user: '/console/user',
|
||
log: '/console/log',
|
||
midjourney: '/console/midjourney',
|
||
setting: '/console/setting',
|
||
'website-setting': '/console/website-setting',
|
||
'demo-reservations': '/console/demo-reservations',
|
||
about: '/about',
|
||
detail: '/console',
|
||
pricing: '/market',
|
||
task: '/console/task',
|
||
models: '/console/models',
|
||
playground: '/console/playground',
|
||
personal: '/console/personal',
|
||
monitor: '/console/monitor',
|
||
analytics: '/console/analytics',
|
||
'my-commission': '/console/my-commission',
|
||
partner: '/console/partner',
|
||
organizations: '/console/organizations',
|
||
'skill-platform': '/console/skill-platform',
|
||
'expert-platform': '/console/expert-platform',
|
||
order: '/console/order',
|
||
invoice: '/console/invoice',
|
||
subscription: '/console/subscription',
|
||
};
|
||
|
||
// forceExpanded:手机端抽屉态。抽屉本来就是临时全宽浮层,折叠成 60px 图标条毫无意义,
|
||
// 且不应把桌面端的折叠偏好一起改掉,所以只在本次渲染里强制展开。
|
||
const SiderBar = ({ onNavigate = () => {}, forceExpanded = false }) => {
|
||
const { t } = useTranslation();
|
||
const [persistedCollapsed, toggleCollapsed] = useSidebarCollapsed();
|
||
const collapsed = forceExpanded ? false : persistedCollapsed;
|
||
const {
|
||
isModuleVisible,
|
||
hasSectionVisibleModules,
|
||
loading: sidebarLoading,
|
||
} = useSidebar();
|
||
|
||
const [statusState] = useContext(StatusContext);
|
||
const [userState] = useContext(UserContext);
|
||
const delegatedPermissions = statusState?.status?.AdminDelegatedSettingTabs;
|
||
// 系统设置 tab 权限下放:普通管理员在被下放了至少一个 tab 时,才显示「系统设置」入口
|
||
const delegatedSettingTabs = useMemo(
|
||
() => getDelegatedSettingTabs(delegatedPermissions),
|
||
[delegatedPermissions],
|
||
);
|
||
const canAccessSetting =
|
||
isRoot() || (isAdmin() && delegatedSettingTabs.length > 0);
|
||
|
||
const showSkeleton = useMinimumLoadingTime(sidebarLoading, 200);
|
||
|
||
const [selectedKeys, setSelectedKeys] = useState(['home']);
|
||
const [chatItems, setChatItems] = useState([]);
|
||
const [openedKeys, setOpenedKeys] = useState([]);
|
||
const [foldedGroups, setFoldedGroups] = useState([]);
|
||
const location = useLocation();
|
||
const [routerMapState, setRouterMapState] = useState(routerMap);
|
||
|
||
const workspaceItems = useMemo(() => {
|
||
const items = [
|
||
{
|
||
text: t('数据看板'),
|
||
itemKey: 'detail',
|
||
to: '/detail',
|
||
className:
|
||
localStorage.getItem('enable_data_export') === 'true'
|
||
? ''
|
||
: 'tableHiddle',
|
||
},
|
||
{
|
||
text: t('密钥管理'),
|
||
itemKey: 'token',
|
||
to: '/token',
|
||
},
|
||
{
|
||
text: t('使用日志'),
|
||
itemKey: 'log',
|
||
to: '/log',
|
||
},
|
||
{
|
||
text: t('绘图日志'),
|
||
itemKey: 'midjourney',
|
||
to: '/midjourney',
|
||
className:
|
||
localStorage.getItem('enable_drawing') === 'true'
|
||
? ''
|
||
: 'tableHiddle',
|
||
},
|
||
{
|
||
text: t('任务日志'),
|
||
itemKey: 'task',
|
||
to: '/task',
|
||
className:
|
||
localStorage.getItem('enable_task') === 'true' ? '' : 'tableHiddle',
|
||
},
|
||
{
|
||
text: t('监控中心'),
|
||
itemKey: 'monitor',
|
||
to: '/monitor',
|
||
},
|
||
];
|
||
|
||
// 根据配置过滤项目
|
||
const filteredItems = items.filter((item) => {
|
||
const configVisible = isModuleVisible('console', item.itemKey);
|
||
return configVisible;
|
||
});
|
||
|
||
return filteredItems;
|
||
}, [
|
||
localStorage.getItem('enable_data_export'),
|
||
localStorage.getItem('enable_drawing'),
|
||
localStorage.getItem('enable_task'),
|
||
t,
|
||
isModuleVisible,
|
||
]);
|
||
|
||
const financeItems = useMemo(() => {
|
||
const items = [
|
||
{
|
||
text: t('钱包管理'),
|
||
itemKey: 'topup',
|
||
to: '/topup',
|
||
},
|
||
{
|
||
text: t('个人设置'),
|
||
itemKey: 'personal',
|
||
to: '/personal',
|
||
},
|
||
];
|
||
|
||
// 根据配置过滤项目
|
||
const filteredItems = items.filter((item) => {
|
||
const configVisible = isModuleVisible('personal', item.itemKey);
|
||
return configVisible;
|
||
});
|
||
|
||
// 我的提成:新模块不在 sidebar 配置注册表内,直接追加(非销售用户页面为空态)
|
||
filteredItems.push({
|
||
text: t('我的提成'),
|
||
itemKey: 'my-commission',
|
||
to: '/console/my-commission',
|
||
});
|
||
|
||
// 合作伙伴:受平台总开关控制(参数设置页可配),开放后对全部登录用户可见
|
||
if (statusState?.status?.commission_partner_portal_enabled === true) {
|
||
filteredItems.push({
|
||
text: t('合作伙伴'),
|
||
itemKey: 'partner',
|
||
to: '/console/partner',
|
||
});
|
||
}
|
||
|
||
return filteredItems;
|
||
}, [
|
||
t,
|
||
isModuleVisible,
|
||
statusState?.status?.commission_partner_portal_enabled,
|
||
]);
|
||
|
||
const adminItems = useMemo(() => {
|
||
const items = [
|
||
{
|
||
text: t('运营看板'),
|
||
itemKey: 'analytics',
|
||
to: '/console/analytics',
|
||
className: isAdmin() ? '' : 'tableHiddle',
|
||
},
|
||
{
|
||
text: t('渠道管理'),
|
||
itemKey: 'channel',
|
||
to: '/channel',
|
||
className: isAdmin() ? '' : 'tableHiddle',
|
||
},
|
||
{
|
||
text: t('模型管理'),
|
||
itemKey: 'models',
|
||
to: '/console/models',
|
||
className: isAdmin() ? '' : 'tableHiddle',
|
||
},
|
||
{
|
||
text: t('兑换码管理'),
|
||
itemKey: 'redemption',
|
||
to: '/redemption',
|
||
className: isAdmin() ? '' : 'tableHiddle',
|
||
},
|
||
{
|
||
text: t('用户管理'),
|
||
itemKey: 'user',
|
||
to: '/user',
|
||
className: isAdmin() ? '' : 'tableHiddle',
|
||
},
|
||
{
|
||
text: t('会员卡管理'),
|
||
itemKey: 'subscription',
|
||
to: '/console/subscription',
|
||
className: isAdmin() ? '' : 'tableHiddle',
|
||
},
|
||
{
|
||
text: t('密钥管理'),
|
||
itemKey: 'admin-token',
|
||
to: '/console/token',
|
||
className: isAdmin() ? '' : 'tableHiddle',
|
||
},
|
||
{
|
||
text: t('企业管理'),
|
||
itemKey: 'organizations',
|
||
to: '/console/organizations',
|
||
className: isAdmin() ? '' : 'tableHiddle',
|
||
},
|
||
{
|
||
text: t('平台 Skill 中心'),
|
||
itemKey: 'skill-platform',
|
||
to: '/console/skill-platform',
|
||
className: isAdmin() ? '' : 'tableHiddle',
|
||
},
|
||
{
|
||
text: t('平台专家中心'),
|
||
itemKey: 'expert-platform',
|
||
to: '/console/expert-platform',
|
||
className: isAdmin() ? '' : 'tableHiddle',
|
||
},
|
||
{
|
||
text: t('订单管理'),
|
||
itemKey: 'order',
|
||
to: '/console/order',
|
||
className: isAdmin() ? '' : 'tableHiddle',
|
||
},
|
||
{
|
||
text: t('发票管理'),
|
||
itemKey: 'invoice',
|
||
to: '/console/invoice',
|
||
className: isAdmin() ? '' : 'tableHiddle',
|
||
},
|
||
{
|
||
text: t('系统设置'),
|
||
itemKey: 'setting',
|
||
to: '/setting',
|
||
className: canAccessSetting ? '' : 'tableHiddle',
|
||
},
|
||
{
|
||
text: t('官网设置'),
|
||
itemKey: 'website-setting',
|
||
className: isAdmin() ? '' : 'tableHiddle',
|
||
},
|
||
{
|
||
text: t('预约演示'),
|
||
itemKey: 'demo-reservations',
|
||
className: isAdmin() ? '' : 'tableHiddle',
|
||
},
|
||
];
|
||
|
||
// 根据配置过滤项目
|
||
const filteredItems = items.filter((item) => {
|
||
if (
|
||
(item.itemKey === 'website-setting' ||
|
||
item.itemKey === 'demo-reservations') &&
|
||
!canAccessDelegatedAdminPage(
|
||
userState?.user?.role,
|
||
delegatedPermissions,
|
||
item.itemKey,
|
||
)
|
||
) {
|
||
return false;
|
||
}
|
||
if (
|
||
!CLOUD_BRAIN_UI_ENABLED &&
|
||
(item.itemKey === 'skill-platform' ||
|
||
item.itemKey === 'expert-platform')
|
||
) {
|
||
return false;
|
||
}
|
||
// 系统设置入口:被下放了至少一个 tab 的普通管理员强制可见
|
||
// (需绕过 isModuleVisible,因为 GetSelf 默认对普通管理员置 admin.setting=false)。
|
||
// root 及其它情况仍走原有 isModuleVisible 逻辑,保留 root 自行隐藏该入口的能力。
|
||
if (
|
||
item.itemKey === 'setting' &&
|
||
isAdmin() &&
|
||
!isRoot() &&
|
||
delegatedSettingTabs.length > 0
|
||
) {
|
||
return true;
|
||
}
|
||
const configVisible = isModuleVisible('admin', item.itemKey);
|
||
return configVisible;
|
||
});
|
||
|
||
return filteredItems;
|
||
}, [
|
||
isAdmin(),
|
||
isRoot(),
|
||
t,
|
||
isModuleVisible,
|
||
canAccessSetting,
|
||
delegatedSettingTabs,
|
||
delegatedPermissions,
|
||
userState?.user?.role,
|
||
]);
|
||
|
||
const chatMenuItems = useMemo(() => {
|
||
const items = [
|
||
{
|
||
text: t('操练场'),
|
||
itemKey: 'playground',
|
||
to: '/playground',
|
||
},
|
||
{
|
||
text: t('聊天'),
|
||
itemKey: 'chat',
|
||
items: chatItems,
|
||
},
|
||
];
|
||
|
||
// 根据配置过滤项目
|
||
const filteredItems = items.filter((item) => {
|
||
const configVisible = isModuleVisible('chat', item.itemKey);
|
||
return configVisible;
|
||
});
|
||
|
||
return filteredItems;
|
||
}, [chatItems, t, isModuleVisible]);
|
||
|
||
// 更新路由映射,添加聊天路由
|
||
const updateRouterMapWithChats = (chats) => {
|
||
const newRouterMap = { ...routerMap };
|
||
|
||
if (Array.isArray(chats) && chats.length > 0) {
|
||
for (let i = 0; i < chats.length; i++) {
|
||
newRouterMap['chat' + i] = '/console/chat/' + i;
|
||
}
|
||
}
|
||
|
||
setRouterMapState(newRouterMap);
|
||
return newRouterMap;
|
||
};
|
||
|
||
// 加载聊天项
|
||
useEffect(() => {
|
||
let chats = localStorage.getItem('chats');
|
||
if (chats) {
|
||
try {
|
||
chats = JSON.parse(chats);
|
||
if (Array.isArray(chats)) {
|
||
let chatItems = [];
|
||
for (let i = 0; i < chats.length; i++) {
|
||
let shouldSkip = false;
|
||
let chat = {};
|
||
for (let key in chats[i]) {
|
||
let link = chats[i][key];
|
||
if (typeof link !== 'string') continue; // 确保链接是字符串
|
||
if (link.startsWith('fluent')) {
|
||
shouldSkip = true;
|
||
break; // 跳过 Fluent Read
|
||
}
|
||
chat.text = key;
|
||
chat.itemKey = 'chat' + i;
|
||
chat.to = '/console/chat/' + i;
|
||
}
|
||
if (shouldSkip || !chat.text) continue; // 避免推入空项
|
||
chatItems.push(chat);
|
||
}
|
||
setChatItems(chatItems);
|
||
updateRouterMapWithChats(chats);
|
||
}
|
||
} catch (e) {
|
||
showError(t('聊天数据解析失败'));
|
||
}
|
||
}
|
||
}, []);
|
||
|
||
// 根据当前路径设置选中的菜单项
|
||
useEffect(() => {
|
||
const currentPath = location.pathname;
|
||
let matchingKey = Object.keys(routerMapState).find(
|
||
(key) => routerMapState[key] === currentPath,
|
||
);
|
||
|
||
// 处理聊天路由
|
||
if (!matchingKey && currentPath.startsWith('/console/chat/')) {
|
||
const chatIndex = currentPath.split('/').pop();
|
||
if (!isNaN(chatIndex)) {
|
||
matchingKey = 'chat' + chatIndex;
|
||
} else {
|
||
matchingKey = 'chat';
|
||
}
|
||
}
|
||
|
||
// 如果找到匹配的键,更新选中的键
|
||
if (matchingKey) {
|
||
setSelectedKeys([matchingKey]);
|
||
}
|
||
}, [location.pathname, routerMapState]);
|
||
|
||
// 监控折叠状态变化以更新 body class(--sidebar-current-width 由它驱动)。
|
||
// 抽屉态下跳过:body class 是全局的,抽屉临时展开不该顺手把桌面侧栏宽度也改掉。
|
||
useEffect(() => {
|
||
if (forceExpanded) return;
|
||
if (persistedCollapsed) {
|
||
document.body.classList.add('sidebar-collapsed');
|
||
} else {
|
||
document.body.classList.remove('sidebar-collapsed');
|
||
}
|
||
}, [persistedCollapsed, forceExpanded]);
|
||
|
||
// 选中高亮颜色(统一)
|
||
const SELECTED_COLOR = 'var(--ui-color-primary)';
|
||
|
||
const renderGroup = (key, label, items) => {
|
||
const folded = foldedGroups.includes(key);
|
||
return (
|
||
<Nav.Group
|
||
itemKey={key}
|
||
folded={folded}
|
||
text={
|
||
<button
|
||
type='button'
|
||
className='sidebar-group-toggle'
|
||
aria-expanded={!folded}
|
||
onClick={() =>
|
||
setFoldedGroups((previous) =>
|
||
previous.includes(key)
|
||
? previous.filter((group) => group !== key)
|
||
: [...previous, key],
|
||
)
|
||
}
|
||
>
|
||
<span className='truncate'>{label}</span>
|
||
<ChevronDown size={13} className={folded ? '-rotate-90' : ''} />
|
||
</button>
|
||
}
|
||
>
|
||
{items}
|
||
</Nav.Group>
|
||
);
|
||
};
|
||
|
||
// 渲染自定义菜单项
|
||
const renderNavItem = (item) => {
|
||
// 跳过隐藏的项目
|
||
if (item.className === 'tableHiddle') return null;
|
||
|
||
const isSelected = selectedKeys.includes(item.itemKey);
|
||
const textColor = isSelected ? SELECTED_COLOR : 'inherit';
|
||
|
||
return (
|
||
<Nav.Item
|
||
key={item.itemKey}
|
||
itemKey={item.itemKey}
|
||
text={
|
||
<span
|
||
className='truncate font-medium text-sm'
|
||
style={{ color: textColor }}
|
||
>
|
||
{item.text}
|
||
</span>
|
||
}
|
||
icon={
|
||
<div className='sidebar-icon-container flex-shrink-0'>
|
||
{getLucideIcon(item.itemKey, isSelected)}
|
||
</div>
|
||
}
|
||
className={item.className}
|
||
/>
|
||
);
|
||
};
|
||
|
||
// 渲染子菜单项
|
||
const renderSubItem = (item) => {
|
||
if (item.items && item.items.length > 0) {
|
||
const isSelected = selectedKeys.includes(item.itemKey);
|
||
const textColor = isSelected ? SELECTED_COLOR : 'inherit';
|
||
|
||
return (
|
||
<Nav.Sub
|
||
key={item.itemKey}
|
||
itemKey={item.itemKey}
|
||
text={
|
||
<span
|
||
className='truncate font-medium text-sm'
|
||
style={{ color: textColor }}
|
||
>
|
||
{item.text}
|
||
</span>
|
||
}
|
||
icon={
|
||
<div className='sidebar-icon-container flex-shrink-0'>
|
||
{getLucideIcon(item.itemKey, isSelected)}
|
||
</div>
|
||
}
|
||
>
|
||
{item.items.map((subItem) => {
|
||
const isSubSelected = selectedKeys.includes(subItem.itemKey);
|
||
const subTextColor = isSubSelected ? SELECTED_COLOR : 'inherit';
|
||
|
||
return (
|
||
<Nav.Item
|
||
key={subItem.itemKey}
|
||
itemKey={subItem.itemKey}
|
||
text={
|
||
<span
|
||
className='truncate font-medium text-sm'
|
||
style={{ color: subTextColor }}
|
||
>
|
||
{subItem.text}
|
||
</span>
|
||
}
|
||
/>
|
||
);
|
||
})}
|
||
</Nav.Sub>
|
||
);
|
||
} else {
|
||
return renderNavItem(item);
|
||
}
|
||
};
|
||
|
||
return (
|
||
<div
|
||
className='sidebar-container'
|
||
style={{
|
||
width: forceExpanded
|
||
? 'var(--sidebar-width)'
|
||
: 'var(--sidebar-current-width)',
|
||
background: 'var(--surface-sidebar)',
|
||
}}
|
||
>
|
||
<SkeletonWrapper
|
||
loading={showSkeleton}
|
||
type='sidebar'
|
||
className=''
|
||
collapsed={collapsed}
|
||
showAdmin={isAdmin()}
|
||
>
|
||
<Nav
|
||
className='sidebar-nav'
|
||
defaultIsCollapsed={collapsed}
|
||
isCollapsed={collapsed}
|
||
onCollapseChange={toggleCollapsed}
|
||
selectedKeys={selectedKeys}
|
||
itemStyle='sidebar-nav-item'
|
||
hoverStyle='sidebar-nav-item:hover'
|
||
selectedStyle='sidebar-nav-item-selected'
|
||
renderWrapper={({ itemElement, props }) => {
|
||
const to =
|
||
routerMapState[props.itemKey] || routerMap[props.itemKey];
|
||
|
||
// 如果没有路由,直接返回元素
|
||
if (!to) return itemElement;
|
||
|
||
return (
|
||
<Link
|
||
style={{ textDecoration: 'none' }}
|
||
to={to}
|
||
onClick={onNavigate}
|
||
>
|
||
{itemElement}
|
||
</Link>
|
||
);
|
||
}}
|
||
onSelect={(key) => {
|
||
// 如果点击的是已经展开的子菜单的父项,则收起子菜单
|
||
if (openedKeys.includes(key.itemKey)) {
|
||
setOpenedKeys(openedKeys.filter((k) => k !== key.itemKey));
|
||
}
|
||
|
||
setSelectedKeys([key.itemKey]);
|
||
}}
|
||
openKeys={openedKeys}
|
||
onOpenChange={(data) => {
|
||
setOpenedKeys(data.openKeys);
|
||
}}
|
||
>
|
||
{/* 聊天区域 */}
|
||
{hasSectionVisibleModules('chat') &&
|
||
renderGroup(
|
||
'chat-group',
|
||
t('聊天'),
|
||
chatMenuItems.map(renderSubItem),
|
||
)}
|
||
|
||
{/* 控制台区域 */}
|
||
{hasSectionVisibleModules('console') &&
|
||
renderGroup(
|
||
'console-group',
|
||
t('控制台'),
|
||
workspaceItems.map(renderNavItem),
|
||
)}
|
||
|
||
{/* 个人中心区域 */}
|
||
{hasSectionVisibleModules('personal') &&
|
||
renderGroup(
|
||
'personal-group',
|
||
t('个人中心'),
|
||
financeItems.map(renderNavItem),
|
||
)}
|
||
|
||
{/* 管理员区域:按角色、权限下放及菜单配置过滤。 */}
|
||
{isAdmin() &&
|
||
renderGroup(
|
||
'admin-group',
|
||
t('管理员'),
|
||
adminItems.map(renderNavItem),
|
||
)}
|
||
</Nav>
|
||
</SkeletonWrapper>
|
||
|
||
{/* 底部折叠按钮 —— 抽屉态不提供:收起后抽屉会变成 60px 窄条,等于自废 */}
|
||
{!forceExpanded && (
|
||
<div className='sidebar-collapse-button'>
|
||
<SkeletonWrapper
|
||
loading={showSkeleton}
|
||
type='button'
|
||
width={collapsed ? 36 : 156}
|
||
height={24}
|
||
className='w-full'
|
||
>
|
||
<Button
|
||
theme='outline'
|
||
type='tertiary'
|
||
size='small'
|
||
icon={
|
||
<ChevronLeft
|
||
size={16}
|
||
strokeWidth={2.5}
|
||
color='var(--ui-color-text-2)'
|
||
style={{
|
||
transform: collapsed ? 'rotate(180deg)' : 'rotate(0deg)',
|
||
}}
|
||
/>
|
||
}
|
||
onClick={toggleCollapsed}
|
||
aria-label={collapsed ? t('展开侧边栏') : t('收起侧边栏')}
|
||
style={
|
||
collapsed
|
||
? { width: 36, height: 24, padding: 0 }
|
||
: { padding: '4px 12px', width: '100%' }
|
||
}
|
||
>
|
||
{!collapsed ? t('收起侧边栏') : null}
|
||
</Button>
|
||
</SkeletonWrapper>
|
||
</div>
|
||
)}
|
||
</div>
|
||
);
|
||
};
|
||
|
||
export default SiderBar;
|