import { useState, useEffect } from 'react' import { useNavigate, useLocation } from 'react-router-dom' import { Layout as AntLayout, Menu, Drawer, Button } from 'antd' import { useMediaQuery } from 'react-responsive' import { WalletOutlined, UserOutlined, UnorderedListOutlined, BarChartOutlined, MenuOutlined, FileTextOutlined, LinkOutlined, AppstoreOutlined } from '@ant-design/icons' import type { MenuProps } from 'antd' import type { ReactNode } from 'react' const { Header, Content, Sider } = AntLayout interface LayoutProps { children: ReactNode } const Layout: React.FC = ({ children }) => { const navigate = useNavigate() const location = useLocation() const isMobile = useMediaQuery({ maxWidth: 768 }) const [mobileMenuOpen, setMobileMenuOpen] = useState(false) // 获取当前选中的菜单项 const getSelectedKeys = (): string[] => { return [location.pathname] } // 获取当前应该打开的父菜单 const getInitialOpenKeys = (): string[] => { const path = location.pathname if (path.startsWith('/templates') || path.startsWith('/copy-trading')) { return ['/copy-trading-management'] } return [] } const [openKeys, setOpenKeys] = useState(getInitialOpenKeys()) // 当路径变化时,自动打开对应的父菜单 useEffect(() => { const path = location.pathname if (path.startsWith('/templates') || path.startsWith('/copy-trading')) { setOpenKeys(['/copy-trading-management']) } }, [location.pathname]) const menuItems: MenuProps['items'] = [ { key: '/accounts', icon: , label: '账户管理' }, { key: '/leaders', icon: , label: 'Leader 管理' }, { key: '/copy-trading-management', icon: , label: '跟单管理', children: [ { key: '/templates', icon: , label: '跟单模板' }, { key: '/copy-trading', icon: , label: '跟单配置' } ] }, { key: '/positions', icon: , label: '仓位管理' }, { key: '/statistics', icon: , label: '统计信息' } ] const handleMenuClick = ({ key }: { key: string }) => { // 如果是父菜单,不导航 if (key === '/copy-trading-management') { return } navigate(key) if (isMobile) { setMobileMenuOpen(false) } } const handleOpenChange = (keys: string[]) => { setOpenKeys(keys) } if (isMobile) { // 移动端布局 return (
Polymarket 跟单
{children} setMobileMenuOpen(false)} open={mobileMenuOpen} bodyStyle={{ padding: 0 }} > ) } // 桌面端布局 return (
Polymarket 跟单
{children} ) } export default Layout