Files
monkey-ai-web/src/components/layout/headerbar/MobileNavMenu.jsx
T

145 lines
4.6 KiB
React

/*
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 <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { Compass as ActionCompass } from '@/components/common/icons/ActionIcons';
import React, { useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { Button, Dropdown } from 'antd';
import { ExternalLink } from 'lucide-react';
import { isNavigationPathActive } from '../../console/consoleRoutes';
/**
* 移动端主导航入口。
*
* 顶栏宽度在手机上只够放「logo + 主操作 + 工具图标 + 头像」,
* 主导航若继续内联平铺会把右侧按钮挤成一团(横向滚动条也不可见、不可发现)。
* 因此 <768px 时把 mainNavLinks 收进这个下拉菜单,跟主流控制台的做法保持一致。
*
* 目标路径/外链的推导逻辑必须与桌面端 Navigation 保持一致,避免两端跳转不同。
*/
const MobileNavMenu = ({ mainNavLinks, userState, t }) => {
const location = useLocation();
const navigate = useNavigate();
const [visible, setVisible] = useState(false);
if (!mainNavLinks || mainNavLinks.length === 0) {
return null;
}
// 与 Navigation 一致:按完整路径段匹配,避免 /market 命中 /market-detail
const isPathActive = (matchPath) =>
isNavigationPathActive(location.pathname, matchPath);
// 与 Navigation 一致:未登录时需要鉴权的入口统一引导到企业登录
const resolveTarget = (link) => {
if (link.requireAuth && !userState?.user) return '/org/login';
if (link.itemKey === 'console' && !userState?.user) return '/org/login';
return link.to;
};
// 把「一级链接 + 二级子项」拍平成一份菜单项列表
const menuItems = [];
mainNavLinks.forEach((link) => {
if (link.items && link.items.length > 0) {
link.items.forEach((subItem) => {
menuItems.push({
key: `${link.itemKey}-${subItem.itemKey}`,
text: subItem.text,
to: subItem.to,
active: isPathActive(subItem.to),
});
});
return;
}
const targetPath = resolveTarget(link);
if (link.isExternal && targetPath !== '/org/login') {
menuItems.push({
key: link.itemKey,
text: link.text,
externalLink: link.externalLink,
active: false,
});
return;
}
menuItems.push({
key: link.itemKey,
text: link.text,
mobileText: link.mobileText,
to: targetPath,
active: isPathActive(link.activePrefix || targetPath),
});
});
const activeItem = menuItems.find((item) => item.active);
return (
<Dropdown
trigger={['click']}
placement='bottomLeft'
open={visible}
onOpenChange={setVisible}
menu={{
selectedKeys: menuItems
.filter((item) => item.active)
.map((item) => item.key),
items: menuItems.map((item) => ({
key: item.key,
onClick: () => {
setVisible(false);
if (item.externalLink) {
window.open(item.externalLink, '_blank', 'noopener,noreferrer');
} else if (item.to) {
navigate(item.to);
}
},
label: (
<span
className='flex items-center gap-1.5'
aria-current={item.active ? 'page' : undefined}
>
{item.text}
{item.externalLink && (
<ExternalLink size={12} className='opacity-50' />
)}
</span>
),
})),
}}
>
<Button
type='text'
aria-label={t('主导航')}
title={activeItem?.text}
aria-haspopup='menu'
aria-expanded={visible}
icon={<ActionCompass size={16} />}
className='ml-1 px-2 max-w-[9rem] [&_.ant-btn-content]:min-w-0'
>
<span className='ml-1 text-[13px] font-semibold truncate'>
{activeItem?.mobileText || activeItem?.text || t('导航')}
</span>
</Button>
</Dropdown>
);
};
export default MobileNavMenu;