'use client'; import { useState, useEffect } from 'react'; import { usePathname, useRouter } from 'next/navigation'; import Link from 'next/link'; import { LayoutDashboard, ShoppingBag, LifeBuoy, Settings, LogOut, Activity, CreditCard, Users, ChevronLeft, ChevronRight, Plane, LucideIcon, CalendarClock, LayoutGrid } from 'lucide-react'; interface MenuItem { name: string; href: string; icon: LucideIcon; } export default function AdminSidebar() { const pathname = usePathname(); const router = useRouter(); // 控制折叠状态 const [isCollapsed, setIsCollapsed] = useState(false); const [mounted, setMounted] = useState(false); // 用于解决 Hydration 不匹配问题 const menu: MenuItem[] = [ { name: '概览', href: '/admin', icon: LayoutDashboard }, { name: '用户管理', href: '/admin/users', icon: Users }, { name: '工单处理', href: '/admin/tickets', icon: LifeBuoy }, { name: '订单管理', href: '/admin/orders', icon: ShoppingBag }, { name: '支付配置', href: '/admin/payments', icon: CreditCard }, { name: '商品配置', href: '/admin/products', icon: Settings }, { name: '系统任务', href: '/admin/tasks', icon: Activity }, { name: 'TROOV Slot监控', href: '/admin/slots', icon: CalendarClock }, // 新增 { name: '卡片管理', href: '/admin/cards', icon: LayoutGrid }, ]; // 初始化:从 localStorage 读取状态 useEffect(() => { setMounted(true); const savedState = localStorage.getItem('sidebar_collapsed'); if (savedState) { setIsCollapsed(JSON.parse(savedState)); } }, []); // 切换并保存状态 const toggleSidebar = () => { const newState = !isCollapsed; setIsCollapsed(newState); localStorage.setItem('sidebar_collapsed', JSON.stringify(newState)); }; const handleLogout = () => { if (confirm('确定要退出管理后台吗?')) { localStorage.removeItem('rsid'); localStorage.removeItem('user_info'); router.push('/login'); } }; // 防止服务端渲染和客户端渲染不一致导致的闪烁 if (!mounted) return null; return ( ); }