/* 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 . For commercial licensing, please contact support@quantumnous.com */ import React, { useContext, useEffect, useMemo, useRef, useState } from 'react'; import { Link, useLocation } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { BarChart3, Key, FileSearch, Receipt, Settings, ChevronDown, ChevronLeft, ChevronRight, CreditCard, Network, Folder, LayoutGrid, Wallet, UserRound, BrainCircuit, LibraryBig, Blocks, ListChecks, Bot, ShieldCheck, Cable, Gauge, Zap, Check, Activity, } from 'lucide-react'; import { useSidebarCollapsed } from '../../hooks/common/useSidebarCollapsed'; import { StatusContext } from '../../context/Status'; import { normalizeSidebarModulesAdmin } from '../../constants/sidebarModules'; import { canAccessOrgSettings } from './orgFeatureFlags'; import { CLOUD_BRAIN_UI_ENABLED, isCloudBrainOrgPath, } from '../../config/cloudBrainUi'; const pathFeatureRequirements = { '/org/cloud-brain': 'cloud_brain', '/org/knowledge': 'knowledge_base', '/org/skill-center': 'skill_market', '/org/skill-assignments': 'skill_market', '/org/experts': 'expert', '/org/agent-audit': 'cloud_brain', '/org/connectors': 'connector', '/org/effectiveness': 'agent_effectiveness', '/org/quality': 'quality_monitor', '/org/departments': 'dept', '/org/projects': 'project', '/org/budgets': 'budget', '/org/requests': 'approval', '/org/project-manage': 'project', }; const isFeatureEnabled = (flags, key) => { if (!key) return true; const value = flags?.[key]; return value === true || value === 'true' || value === '1' || value === 1; }; const OrgSidebar = ({ orgName, orgRole, featureFlags, isDeptManager = false, isProjectManager = false, canManageKnowledge = false, canManageSkills = false, canAssignSkills = false, canManageExperts = false, canManageConnectors = false, canAuditAgentMetadata = false, canViewAgentEffect = false, canViewQuality = false, isMemberActor = true, onNavigate, mobile = false, }) => { const { t } = useTranslation(); const location = useLocation(); const [collapsedState, toggleCollapsed] = useSidebarCollapsed(); // 移动端抽屉里始终展开,不允许折叠 const collapsed = mobile ? false : collapsedState; const [statusState] = useContext(StatusContext); const orgConfig = useMemo( () => normalizeSidebarModulesAdmin(statusState?.status?.SidebarModulesAdmin) .orgCenter, [statusState?.status?.SidebarModulesAdmin], ); const isAdmin = orgRole === 'owner' || orgRole === 'admin' || orgRole === 'super_admin'; // owner/超管可管理完整设置;普通管理员仅在平台授权数据安全自助配置时进入该页。 const canManageSettings = canAccessOrgSettings(orgRole, featureFlags); const roleLabel = useMemo(() => { if (orgRole === 'super_admin') return t('超级管理员'); if (orgRole === 'owner') return t('所有者'); if (orgRole === 'admin') return t('企业管理员'); if (orgRole === 'dept_admin' && isProjectManager) return t('部门/项目管理员'); if (orgRole === 'dept_admin' || isDeptManager) return t('部门管理员'); if (isProjectManager) return t('项目管理员'); return t('成员'); }, [t, orgRole, isDeptManager, isProjectManager]); // 菜单分组(云平台风格的分组导航,给扁平的十几个入口建立层次) const groups = useMemo( () => [ { key: 'overview', label: t('概览') }, { key: 'ai', label: t('AI 能力管理') }, { key: 'org', label: t('组织管理') }, { key: 'governance', label: t('额度与治理') }, { key: 'records', label: t('财务与日志') }, { key: 'settings', label: t('系统设置') }, ], [t], ); // 统一单一品牌色高亮(云平台风格),不再每项一种颜色 const items = useMemo(() => { if (orgConfig?.enabled === false) { return []; } // adminOnly 仅 owner/admin/super_admin 可见;其余所有成员可见 const all = [ { itemKey: '/org/workbench', text: t('工作台'), icon: LayoutGrid, group: 'overview', }, { itemKey: '/org/cloud-brain', text: t('云端大脑'), icon: BrainCircuit, group: 'overview', beta: true, }, { itemKey: '/org/knowledge', text: t('企业知识'), icon: LibraryBig, group: 'overview', adminOnly: true, knowledgePermission: true, beta: true, }, { // 我的技能 + 业务系统授权合并页:成员自助视图,归入概览组, // 让「AI 能力管理」成为纯管理员分组。 itemKey: '/org/my-capabilities', text: t('我的 AI 能力'), icon: Zap, group: 'overview', }, { // 企业技能库 + 企业自研 Skill 合并页;平台技能的启用在技能超市完成 itemKey: '/org/skill-center', text: t('技能中心'), icon: Blocks, group: 'ai', adminOnly: true, skillManage: true, }, { itemKey: '/org/skill-assignments', text: t('分发与锁定'), icon: ListChecks, group: 'ai', assignmentPermission: true, }, { itemKey: '/org/experts', text: t('企业专家工坊'), icon: Bot, group: 'ai', adminOnly: true, expertManage: true, }, { itemKey: '/org/connectors', text: t('Connector 管理'), icon: Cable, group: 'ai', adminOnly: true, connectorManage: true, }, { itemKey: '/org/effectiveness', text: t('效能分析'), icon: Gauge, group: 'overview', adminOnly: true, effectView: true, }, { itemKey: '/org/quality', text: t('调用质量'), icon: Activity, group: 'overview', adminOnly: true, qualityView: true, }, { itemKey: '/org/agent-audit', text: t('Agent 任务审计'), icon: ShieldCheck, group: 'records', agentAudit: true, }, { itemKey: '/org/dashboard', text: t('数据看板'), icon: BarChart3, group: 'overview', adminOnly: true, }, { itemKey: '/org/members', text: t('员工管理'), icon: UserRound, group: 'org', adminOnly: true, }, { itemKey: '/org/dept-manage', text: t('部门管理'), icon: Network, group: 'org', deptScope: true, }, { itemKey: '/org/project-manage', text: t('我的项目'), icon: Folder, group: 'org', projectScope: true, }, { itemKey: '/org/departments', text: t('组织架构'), icon: Network, group: 'org', adminOnly: true, }, { itemKey: '/org/projects', text: t('项目管理'), icon: Folder, group: 'org', adminOnly: true, }, { itemKey: '/org/budgets', text: t('预算中心'), icon: Wallet, group: 'governance', adminOnly: true, }, { itemKey: '/org/requests', text: isAdmin ? t('审批中心') : t('我的申请'), icon: Check, group: 'governance', }, { itemKey: '/org/tokens', text: t('密钥管理'), icon: Key, group: 'governance', }, { itemKey: '/org/recharge', text: t('企业充值'), icon: CreditCard, group: 'records', adminOnly: true, }, { itemKey: '/org/logs', text: isAdmin || isDeptManager ? t('消耗明细') : t('我的消耗明细'), icon: Receipt, group: 'records', }, { itemKey: '/org/audit', text: t('审计日志'), icon: FileSearch, group: 'records', adminOnly: true, }, { itemKey: '/org/personal-settings', text: t('个人设置'), icon: UserRound, group: 'settings', }, { itemKey: '/org/settings', text: t('企业设置'), icon: Settings, group: 'settings', ownerOnly: true, }, ]; const visibilityByPath = { '/org/workbench': 'workbench', '/org/cloud-brain': 'cloudBrain', '/org/knowledge': 'knowledge', '/org/skill-assignments': 'skillAssignments', '/org/experts': 'experts', '/org/agent-audit': 'agentAudit', '/org/connectors': 'connectors', '/org/effectiveness': 'effectiveness', '/org/quality': 'quality', '/org/dashboard': 'dashboard', '/org/members': 'members', '/org/departments': 'departments', '/org/projects': 'projects', '/org/dept-manage': 'deptManage', '/org/project-manage': 'projectManage', '/org/budgets': 'budgets', '/org/requests': 'requests', '/org/tokens': 'tokens', '/org/logs': 'logs', '/org/audit': 'audit', '/org/recharge': 'recharge', '/org/settings': 'settings', }; return all.filter((item) => { if (!CLOUD_BRAIN_UI_ENABLED && isCloudBrainOrgPath(item.itemKey)) { return false; } if (item.adminOnly && !isAdmin) return false; // 平台超管(无成员身份)访问成员型入口必被 member_actor_required 拒绝。 if (!isMemberActor && item.itemKey === '/org/my-capabilities') return false; if (item.knowledgePermission && !canManageKnowledge) return false; if (item.skillManage && !canManageSkills) return false; // 与路由守卫一致:分发页要求 (isAdmin || isDeptManager) && canAssignSkills, // 只查权限位会给无管辖部门的 dept_admin 一个点击即被重定向的死入口。 if ( item.assignmentPermission && !(canAssignSkills && (isAdmin || isDeptManager)) ) return false; if (item.expertManage && !canManageExperts) return false; // 不变量:除 can_assign_skills 外,全部 can_* 都由 // MemberHasAdminPermission 计算,对非 owner/admin 恒 false——即权限位 // 本身已蕴含 isAdmin,菜单无需重复判角色。路由守卫(Organization/index.jsx) // 写成 isAdmin && can_xxx 是防御性冗余,两者当前等价。 // ⚠️ 若将来把某个权限下放给 dept_admin(做法参照 can_assign_skills 的 // `Role == DeptAdmin || MemberHasAdminPermission(...)`),该不变量即被打破, // 必须同步在这里补角色条件,否则会变成"菜单可见但路由守卫拒绝"的死入口。 if (item.agentAudit && !canAuditAgentMetadata) return false; if (item.effectView && !canViewAgentEffect) return false; if (item.qualityView && !canViewQuality) return false; if (item.connectorManage && !canManageConnectors) return false; // 与路由守卫一致:管理员只有 data_security 授权后才显示设置入口。 if (item.ownerOnly && !canManageSettings) return false; // 部门管理:仅部门负责人可见(与路由门控一致;纯管理员用员工管理页,避免点了被重定向) if (item.deptScope && !isDeptManager) return false; // 我的项目:仅项目经理可见(同上,纯管理员用项目管理页) if (item.projectScope && !isProjectManager) return false; const key = visibilityByPath[item.itemKey]; const requiredFeature = pathFeatureRequirements[item.itemKey]; // 合并页跨两个模块开关/功能位,任一页签可见即显示入口(与路由守卫同口径) if (item.itemKey === '/org/my-capabilities') { return ( (orgConfig?.mySkills !== false && isFeatureEnabled(featureFlags, 'skill_market')) || (orgConfig?.myConnectors !== false && isFeatureEnabled(featureFlags, 'connector')) ); } if (item.itemKey === '/org/skill-center') { return ( isFeatureEnabled(featureFlags, 'skill_market') && (orgConfig?.skillLibrary !== false || orgConfig?.privateSkills !== false) ); } if (item.itemKey === '/org/members') { return ( (orgConfig?.members !== false || orgConfig?.memberAccounts !== false) && isFeatureEnabled(featureFlags, requiredFeature) ); } return ( (!key || orgConfig?.[key] !== false) && isFeatureEnabled(featureFlags, requiredFeature) ); }); }, [ t, isAdmin, canManageSettings, isDeptManager, isProjectManager, canManageKnowledge, canManageSkills, canAssignSkills, canManageExperts, canAuditAgentMetadata, canViewAgentEffect, canViewQuality, isMemberActor, canManageConnectors, orgConfig, featureFlags, ]); // 按分组聚合,保持 groups 顺序,自动跳过空组 const groupedItems = useMemo( () => groups .map((g) => ({ ...g, items: items.filter((it) => it.group === g.key) })) .filter((g) => g.items.length > 0), [groups, items], ); const navRef = useRef(null); // 二级分组折叠:仅记录用户手动收起的组,跨会话记忆 const FOLDED_GROUPS_KEY = 'org_sidebar_folded_groups'; const [foldedGroups, setFoldedGroups] = useState(() => { try { const saved = JSON.parse(localStorage.getItem(FOLDED_GROUPS_KEY) || '[]'); return Array.isArray(saved) ? saved : []; } catch { return []; } }); const toggleGroup = (key) => { setFoldedGroups((prev) => { const next = prev.includes(key) ? prev.filter((k) => k !== key) : [...prev, key]; try { localStorage.setItem(FOLDED_GROUPS_KEY, JSON.stringify(next)); } catch { /* 隐私模式等场景下写入失败可忽略,仅丢失记忆 */ } return next; }); }; const getSelectedKey = () => { if (location.pathname === '/org/member-accounts') { return '/org/members'; } if (items.some((item) => item.itemKey === location.pathname)) { return location.pathname; } if (location.pathname === '/org' || location.pathname === '/org/') { return items[0]?.itemKey || ''; } return ''; }; const selectedKey = getSelectedKey(); // 当前路由所在分组自动展开(仅本次会话,不覆盖用户的持久化选择) useEffect(() => { const activeGroup = groupedItems.find((g) => g.items.some((it) => it.itemKey === selectedKey), )?.key; if (!activeGroup) return; setFoldedGroups((prev) => prev.includes(activeGroup) ? prev.filter((k) => k !== activeGroup) : prev, ); }, [selectedKey, groupedItems]); // 选中项滚入可视区(分组较多时避免高亮项被折叠区/滚动区遮住) useEffect(() => { const el = navRef.current?.querySelector('[data-active="true"]'); el?.scrollIntoView({ block: 'nearest' }); }, [selectedKey]); return ( ); }; export default OrgSidebar;