import React, { useState, useEffect } from 'react'; import { Outlet, Link, useLocation, useNavigate } from 'react-router-dom'; import { Layout as AntLayout, Menu, Button, Dropdown, Avatar } from 'antd'; import { HomeOutlined, CheckSquareOutlined, ScheduleOutlined, RobotOutlined, BarChartOutlined, UserOutlined, LogoutOutlined, MenuFoldOutlined, MenuUnfoldOutlined } from '@ant-design/icons'; import { useAuth } from '../context/AuthContext'; const { Header, Sider, Content } = AntLayout; const Layout = () => { const { user, logout } = useAuth(); const location = useLocation(); const navigate = useNavigate(); const [collapsed, setCollapsed] = useState(false); const [isMobile, setIsMobile] = useState(window.innerWidth < 768); // 监听窗口大小变化 useEffect(() => { const handleResize = () => { setIsMobile(window.innerWidth < 768); if (window.innerWidth < 768) { setCollapsed(true); } }; window.addEventListener('resize', handleResize); handleResize(); return () => window.removeEventListener('resize', handleResize); }, []); const toggleCollapsed = () => { setCollapsed(!collapsed); }; // 当前选中的菜单项 const selectedKey = (() => { const path = location.pathname; if (path === '/') return '1'; if (path === '/tasks') return '2'; if (path === '/schedule') return '3'; if (path === '/ai-chat') return '4'; if (path === '/stats') return '5'; return '1'; })(); // 用户菜单 const userMenu = { items: [ { key: '1', label: (
{user?.email}
), }, { key: '2', label: (
退出登录
), } ], }; return (
{!collapsed && 任务管理系统}
, label: '仪表盘', onClick: () => navigate('/') }, { key: '2', icon: , label: '任务管理', onClick: () => navigate('/tasks') }, { key: '3', icon: , label: '时间安排', onClick: () => navigate('/schedule') }, { key: '4', icon: , label: 'AI 助手', onClick: () => navigate('/ai-chat') }, { key: '5', icon: , label: '数据统计', onClick: () => navigate('/stats') }, ]} />
{/* 移动端侧边栏蒙层 */} {isMobile && !collapsed && (
)} {/* 移动端收起时显示展开按钮 */} {isMobile && collapsed && (