import React, { useState, useEffect, useContext } from 'react'; import { Button, Switch, Row, Col } from '@/components/ui'; import { LayoutGrid, MessageSquare, BarChart2, User, Shield, } from 'lucide-react'; import { API, showSuccess, showError } from '../../../../helpers'; import { StatusContext } from '../../../../context/Status'; import { useUserPermissions } from '../../../../hooks/common/useUserPermissions'; import { useSidebar } from '../../../../hooks/common/useSidebar'; const SidebarSection = ({ t }) => { const [statusState] = useContext(StatusContext); const [sidebarLoading, setSidebarLoading] = useState(false); const [sidebarModulesUser, setSidebarModulesUser] = useState({ chat: { enabled: true, playground: true, chat: true }, console: { enabled: true, detail: true, token: true, log: true, midjourney: true, task: true, monitor: true, }, personal: { enabled: true, topup: true, personal: true }, admin: { enabled: true, analytics: true, channel: true, models: true, redemption: true, user: true, subscription: true, 'admin-token': true, organizations: true, order: true, invoice: true, setting: true, }, }); const [adminConfig, setAdminConfig] = useState(null); const { hasSidebarSettingsPermission, isSidebarSectionAllowed, isSidebarModuleAllowed, } = useUserPermissions(); const { refreshUserConfig } = useSidebar(); const handleSectionChange = (sectionKey) => (checked) => { setSidebarModulesUser((prev) => ({ ...prev, [sectionKey]: { ...prev[sectionKey], enabled: checked }, })); }; const handleModuleChange = (sectionKey, moduleKey) => (checked) => { setSidebarModulesUser((prev) => ({ ...prev, [sectionKey]: { ...prev[sectionKey], [moduleKey]: checked }, })); }; const saveSidebarSettings = async () => { setSidebarLoading(true); try { const res = await API.put('/api/user/self', { sidebar_modules: JSON.stringify(sidebarModulesUser), }); if (res.data.success) { showSuccess(t('侧边栏设置保存成功')); await refreshUserConfig(); } else { showError(res.data.message); } } catch (error) { showError(t('保存失败')); } setSidebarLoading(false); }; const resetSidebarModules = () => { setSidebarModulesUser({ chat: { enabled: true, playground: true, chat: true }, console: { enabled: true, detail: true, token: true, log: true, midjourney: true, task: true, }, personal: { enabled: true, topup: true, personal: true }, admin: { enabled: true, channel: true, models: true, redemption: true, user: true, setting: true, }, }); }; useEffect(() => { const loadSidebarConfigs = async () => { try { if (statusState?.status?.SidebarModulesAdmin) { setAdminConfig(JSON.parse(statusState.status.SidebarModulesAdmin)); } const userRes = await API.get('/api/user/self'); if (userRes.data.success && userRes.data.data.sidebar_modules) { setSidebarModulesUser(JSON.parse(userRes.data.data.sidebar_modules)); } } catch (error) { console.error('加载边栏配置失败:', error); } }; loadSidebarConfigs(); }, [statusState]); const isAllowedByAdmin = (sectionKey, moduleKey = null) => { if (!adminConfig) return true; if (moduleKey) { return ( adminConfig[sectionKey]?.enabled && adminConfig[sectionKey]?.[moduleKey] ); } return adminConfig[sectionKey]?.enabled; }; // 区域配置 const sectionConfigs = [ { key: 'chat', title: t('聊天区域'), description: t('操练场和聊天功能'), icon: MessageSquare, gradient: 'from-blue-500 to-cyan-500', modules: [ { key: 'playground', title: t('操练场'), description: t('AI模型测试环境'), }, { key: 'chat', title: t('聊天'), description: t('聊天会话管理') }, ], }, { key: 'console', title: t('控制台区域'), description: t('数据管理和日志查看'), icon: BarChart2, gradient: 'from-emerald-500 to-teal-500', modules: [ { key: 'detail', title: t('数据看板'), description: t('系统数据统计') }, { key: 'token', title: t('密钥管理'), description: t('API密钥管理') }, { key: 'log', title: t('使用日志'), description: t('API使用记录') }, { key: 'midjourney', title: t('绘图日志'), description: t('绘图任务记录'), }, { key: 'task', title: t('任务日志'), description: t('系统任务记录') }, ], }, { key: 'personal', title: t('个人中心区域'), description: t('用户个人功能'), icon: User, gradient: 'from-brand-from to-pink-500', modules: [ { key: 'topup', title: t('钱包管理'), description: t('余额充值管理') }, { key: 'personal', title: t('个人设置'), description: t('个人信息设置'), }, ], }, { key: 'admin', title: t('管理员区域'), description: t('系统管理功能'), icon: Shield, gradient: 'from-orange-500 to-red-500', modules: [ { key: 'channel', title: t('渠道管理'), description: t('API渠道配置') }, { key: 'models', title: t('模型管理'), description: t('AI模型配置') }, { key: 'redemption', title: t('兑换码管理'), description: t('兑换码生成管理'), }, { key: 'user', title: t('用户管理'), description: t('用户账户管理') }, { key: 'setting', title: t('系统设置'), description: t('系统参数配置'), }, ], }, ] .filter((section) => isSidebarSectionAllowed(section.key)) .map((section) => ({ ...section, modules: section.modules.filter((module) => isSidebarModuleAllowed(section.key, module.key), ), })) .filter( (section) => section.modules.length > 0 && isAllowedByAdmin(section.key), ); if (!hasSidebarSettingsPermission()) { return (
{t('个性化设置侧边栏的显示功能')}
{section.description}