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

129 lines
4.1 KiB
React

import React from 'react';
import { Link, useLocation } from 'react-router-dom';
import { Dropdown } from 'antd';
import {
ChevronDown,
ExternalLink,
} from '@/components/common/icons/ActionIcons';
import { useTranslation } from 'react-i18next';
import SkeletonWrapper from '../components/SkeletonWrapper';
import { isNavigationPathActive } from '../../console/consoleRoutes';
const Navigation = ({ mainNavLinks, isMobile, isLoading, userState }) => {
const location = useLocation();
const { t } = useTranslation();
const renderNavLinks = () => {
const baseClasses =
'ui-header-nav-item flex-shrink-0 flex items-center gap-1.5 font-medium relative';
const defaultClasses = 'text-gray-600 dark:text-neutral-300';
const hoverClasses = 'hover:text-brand-deep dark:hover:text-brand-from';
const activeClasses = 'text-brand-deep dark:text-brand-from';
const spacingClasses = isMobile ? 'px-2 py-1 text-sm' : 'px-4 py-2';
// Match on full path segments so /market doesn't light up on /market-detail
const isPathActive = (matchPath) =>
isNavigationPathActive(location.pathname, matchPath);
return mainNavLinks.map((link) => {
const linkContent = <span className='relative z-10'>{link.text}</span>;
// Calculate target path for main link(未登录默认引导到企业登录)
let targetPath = link.to;
if (link.requireAuth && !userState.user) {
targetPath = '/org/login';
}
if (link.itemKey === 'console' && !userState.user) {
targetPath = '/org/login';
}
// Check if active
let isActive = false;
if (link.activePrefix) {
isActive = isPathActive(link.activePrefix);
} else if (link.items && link.items.length > 0) {
isActive = link.items.some((subItem) => isPathActive(subItem.to));
} else if (!link.isExternal && targetPath) {
isActive = isPathActive(link.activePrefix || targetPath);
}
const finalClasses = `${baseClasses} ${spacingClasses} ${isActive ? `nav-item-active ${activeClasses}` : `${defaultClasses} ${hoverClasses}`}`;
if (link.items && link.items.length > 0) {
return (
<Dropdown
key={link.itemKey}
trigger={['hover', 'click']}
placement='bottomLeft'
menu={{
selectedKeys: link.items
.filter((item) => isPathActive(item.to))
.map((item) => item.itemKey),
items: link.items.map((subItem) => ({
key: subItem.itemKey,
label: <Link to={subItem.to}>{subItem.text}</Link>,
})),
}}
>
<button
type='button'
className={`${finalClasses} cursor-pointer`}
aria-haspopup='menu'
aria-current={isActive ? 'page' : undefined}
>
{linkContent}
<ChevronDown size={14} aria-hidden />
</button>
</Dropdown>
);
}
if (link.isExternal && targetPath !== '/org/login') {
return (
<a
key={link.itemKey}
href={link.externalLink}
target='_blank'
rel='noopener noreferrer'
className={`${baseClasses} ${spacingClasses} ${defaultClasses} ${hoverClasses}`}
>
{linkContent}
<ExternalLink size={12} aria-hidden />
</a>
);
}
return (
<Link
key={link.itemKey}
to={targetPath}
className={finalClasses}
aria-current={isActive ? 'page' : undefined}
>
{linkContent}
</Link>
);
});
};
return (
<nav
aria-label={t('主导航')}
className='ui-header-navigation flex flex-1 items-center gap-1 lg:gap-2 mx-1 sm:mx-3 md:mx-6 overflow-x-auto whitespace-nowrap scrollbar-hide min-w-0'
>
<SkeletonWrapper
loading={isLoading && mainNavLinks.length === 0}
type='navigation'
count={4}
width={60}
height={16}
isMobile={isMobile}
>
{renderNavLinks()}
</SkeletonWrapper>
</nav>
);
};
export default Navigation;