import React, { useState } from 'react'; import { Outlet, useNavigate, useLocation } from 'react-router-dom'; import { motion } from 'framer-motion'; import { FileText, Warehouse, CreditCard, LogOut, Home, ChevronRight, Factory, Menu, X } from 'lucide-react'; import { useAuth } from '../../contexts/AuthContext'; import { useResponsive } from '../../hooks/useResponsive'; import { STORAGE_KEYS } from '../../config/app'; interface NavItem { id: string; icon: React.ElementType; label: string; path: string; } const navItems: NavItem[] = [ { id: 'home', icon: Home, label: '首页', path: '/textile' }, { id: 'plans', icon: FileText, label: '计划总览', path: '/textile/plans' }, { id: 'fabric', icon: Warehouse, label: '已生产坯布', path: '/textile/fabric-warehouse' }, { id: 'yarn', icon: Warehouse, label: '原料纱仓库', path: '/textile/yarn-warehouse' }, { id: 'payment', icon: CreditCard, label: '待结款', path: '/textile/payments' } ]; export function TextileLayout() { const navigate = useNavigate(); const location = useLocation(); const { auth, logout } = useAuth(); const { isMobile } = useResponsive(); // 从 localStorage 读取侧边栏状态,默认展开 const [sidebarCollapsed, setSidebarCollapsed] = useState(() => { const saved = localStorage.getItem(STORAGE_KEYS.textileSidebarCollapsed); return saved ? JSON.parse(saved) : false; }); const [mobileMenuOpen, setMobileMenuOpen] = useState(false); // 保存侧边栏状态到 localStorage const toggleSidebar = () => { const newState = !sidebarCollapsed; setSidebarCollapsed(newState); localStorage.setItem(STORAGE_KEYS.textileSidebarCollapsed, JSON.stringify(newState)); }; const isActive = (path: string) => { const currentPath = location.pathname; // 首页精确匹配 if (path === '/textile') { return currentPath === '/textile'; } // 精确匹配当前路径 if (currentPath === path) return true; // 对于 /textile/plans 路径,不应该匹配子路径 if (path === '/textile/plans') { return currentPath === '/textile/plans'; } // 对于其他路径,匹配子路径 if (currentPath.startsWith(path + '/')) return true; return false; }; const handleLogout = async () => { await logout(); navigate('/login'); }; return (
{/* 桌面端侧边栏 - 固定不动,无入场动画 */} {!isMobile && ( )} {/* 主内容区 - 移动端无侧边栏,桌面端有侧边栏 */}
); }