Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
/*
|
||||
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 React, { useState } from 'react';
|
||||
import { useLocation, useNavigate } from 'react-router-dom';
|
||||
import { Button, Dropdown } from '@douyinfe/semi-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 (
|
||||
<Dropdown
|
||||
trigger='click'
|
||||
position='bottomLeft'
|
||||
visible={visible}
|
||||
onVisibleChange={setVisible}
|
||||
render={
|
||||
<Dropdown.Menu className='!min-w-[168px] !p-1.5 !rounded-xl !border !border-gray-100 dark:!border-neutral-800 !shadow-xl !shadow-gray-200/50 dark:!shadow-black/30 !backdrop-blur-xl !bg-white/95 dark:!bg-neutral-900/95'>
|
||||
{menuItems.map((item) => (
|
||||
<Dropdown.Item
|
||||
key={item.key}
|
||||
onClick={() => {
|
||||
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'
|
||||
}`}
|
||||
>
|
||||
<span className='flex items-center gap-1.5'>
|
||||
{item.text}
|
||||
{item.externalLink && (
|
||||
<ExternalLink size={12} className='opacity-50' />
|
||||
)}
|
||||
</span>
|
||||
</Dropdown.Item>
|
||||
))}
|
||||
</Dropdown.Menu>
|
||||
}
|
||||
>
|
||||
<Button
|
||||
theme='borderless'
|
||||
type='tertiary'
|
||||
aria-label={t('主导航')}
|
||||
aria-haspopup='menu'
|
||||
aria-expanded={visible}
|
||||
icon={<Compass size={16} />}
|
||||
className='!ml-1 !px-2 !h-8 max-w-[9rem] !rounded-xl !text-current hover:!bg-brand/10 dark:hover:!bg-brand/15 hover:!text-brand-deep dark:hover:!text-brand-from !transition-all !duration-200 active:!scale-95 [&_.semi-button-content]:min-w-0'
|
||||
>
|
||||
<span className='ml-1 text-[13px] font-semibold truncate'>
|
||||
{activeItem ? activeItem.text : t('导航')}
|
||||
</span>
|
||||
</Button>
|
||||
</Dropdown>
|
||||
);
|
||||
};
|
||||
|
||||
export default MobileNavMenu;
|
||||
Reference in New Issue
Block a user