/* 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, { useState } from 'react'; import { useLocation, useNavigate } from 'react-router-dom'; import { Button, Dropdown } from '@/components/ui'; import { Compass, ExternalLink } from 'lucide-react'; /** * 移动端主导航入口。 * * 顶栏宽度在手机上只够放「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) => { if (!matchPath) return false; if (matchPath === '/') return location.pathname === '/'; return ( location.pathname === matchPath || location.pathname.startsWith(`${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, to: targetPath, active: isPathActive(link.activePrefix || targetPath), }); }); const activeItem = menuItems.find((item) => item.active); return ( {menuItems.map((item) => ( { setVisible(false); if (item.externalLink) { window.open( item.externalLink, '_blank', 'noopener,noreferrer', ); } else if (item.to) { navigate(item.to); } }} className={`!rounded-lg !my-0.5 !px-3 !py-2 !text-sm !transition-colors ${ item.active ? '!bg-brand/10 dark:!bg-brand-deep/40 !text-brand-deep dark:!text-brand-from !font-semibold' : '!text-gray-700 dark:!text-neutral-200' }`} > {item.text} {item.externalLink && ( )} ))} } > ); }; export default MobileNavMenu;