Files
monkey-ai-web/src/hooks/common/useNavigation.js
T
zhanjunhao cb9d836227 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

201 lines
6.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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,
};
};