/* 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 { useMemo } from 'react'; import { getConfigGroups, isConfigEnabled, isGroupAllowed, } from '../../helpers/navigationVisibility'; import { getHeaderModuleRequireAuth } from '../../constants/headerNavModules'; import { useWeyAiWebEntry } from './useWeyAiWebEntry'; export const useNavigation = ( t, docsLink, headerNavModules, userRole = 0, userGroup = 'default', orgEntry = {}, ) => { // 解构成基础类型,避免父组件每次渲染传入新对象字面量导致 useMemo 失效 const { showOrgEntry = false, orgEntryPath = '/org', showPartnerEntry = false, } = orgEntry; // WeyAi 网页端入口:按 /api/status 开关与 /api/client/v1/web/access 决定是否显示 const { visible: weyaiVisible, url: weyaiUrl } = useWeyAiWebEntry(); const mainNavLinks = useMemo(() => { const isAdminUser = userRole >= 10; // 默认配置,如果没有传入配置则显示所有模块 const defaultModules = { home: { enabled: true, groups: [] }, console: { enabled: true, groups: [] }, experienceCenter: { enabled: true, requireAuth: false, groups: [] }, keyManagement: { enabled: true, requireAuth: false, groups: [] }, userCenter: { enabled: true, requireAuth: false, groups: [] }, pricing: { enabled: true, requireAuth: false, groups: [] }, docs: { enabled: true, requireAuth: false, groups: [] }, weyai: { enabled: true, groups: [] }, about: { enabled: true, groups: [] }, }; // 合并默认配置和传入配置,确保新增模块有默认值 const modules = headerNavModules ? { ...defaultModules, ...headerNavModules } : defaultModules; // 模块是否对当前用户可见:开关 + 「可见用户分组」。顶栏管理里每个模块都有 // 分组控件,过滤却一度只看开关,分组配了等于没配;右侧功能按钮 // (isHeaderActionButtonVisible)一直是看分组的,这里对齐 const isModuleVisible = (key) => { const module = modules[key]; return ( isConfigEnabled(module) && isGroupAllowed(getConfigGroups(module), userGroup) ); }; const isInternalDocsLink = (link) => { if (!link) return false; if (link === '/docs' || link === '/docs/') return true; if (link.startsWith('/docs')) return true; try { const url = new URL(link, window.location.origin); const currentOrigin = window.location.origin; if (url.origin === currentOrigin && url.pathname.startsWith('/docs')) { return true; } } catch (_e) { return false; } return false; }; const getDocsLinkConfig = () => { const baseConfig = { itemKey: 'docs', requireAuth: getHeaderModuleRequireAuth(modules, 'docs'), }; if (isInternalDocsLink(docsLink) || !docsLink) { return { ...baseConfig, text: t('文档'), to: '/docs', }; } return { ...baseConfig, text: t('文档'), isExternal: true, externalLink: docsLink, }; }; const allLinks = [ // 平台开放且具备新版伙伴权限时展示;放在前面,窄屏也容易找到。 ...(showPartnerEntry ? [ { text: t('伙伴工作台'), itemKey: 'partnerWorkspace', mobileText: t('导航'), to: '/console/my-commission', requireAuth: true, alwaysShow: true, }, ] : []), { text: t('密钥管理'), itemKey: 'keyManagement', to: '/user-center/key-management', requireAuth: getHeaderModuleRequireAuth(modules, 'keyManagement'), }, { text: t('体验中心'), itemKey: 'experienceCenter', to: '/experience-center', requireAuth: getHeaderModuleRequireAuth(modules, 'experienceCenter'), }, { text: t('模型超市'), itemKey: 'pricing', to: '/market', requireAuth: getHeaderModuleRequireAuth(modules, 'pricing'), }, { text: t('技能超市'), itemKey: 'skillMarket', to: '/skill-market', requireAuth: getHeaderModuleRequireAuth(modules, 'skillMarket'), }, getDocsLinkConfig(), { text: t('用户中心'), itemKey: 'userCenter', to: '/user-center', requireAuth: getHeaderModuleRequireAuth(modules, 'userCenter'), }, // 企业中心:仅当用户属于某个企业时作为一级入口展示(不跟随 headerNavModules 配置, // 由 showOrgEntry 统一控制可见性,故标记 alwaysShow 跳过模块开关过滤) ...(showOrgEntry ? [ { text: t('企业中心'), itemKey: 'orgCenter', to: orgEntryPath || '/org', activePrefix: '/org', requireAuth: true, alwaysShow: true, }, ] : []), // WeyAi 网页端:外链到网关的 /sso/start(每次点击都重新走一遍免登录授权), // 需要登录;管理员可在「顶栏管理」里关闭 ...(weyaiVisible ? [ { text: t('WeyAi'), itemKey: 'weyai', isExternal: true, externalLink: weyaiUrl, requireAuth: true, }, ] : []), { text: t('控制台'), itemKey: 'console', to: '/console/analytics', activePrefix: '/console', adminOnly: true, requireAuth: true, }, ]; return allLinks.filter((link) => { if (link.adminOnly && !isAdminUser) { return false; } if (link.alwaysShow) { return true; } return isModuleVisible(link.itemKey); }); }, [ t, docsLink, headerNavModules, userRole, userGroup, showOrgEntry, orgEntryPath, showPartnerEntry, weyaiVisible, weyaiUrl, ]); return { mainNavLinks, }; };