@@ -1,191 +1,253 @@
|
||||
'use client'
|
||||
import {useState} from 'react'
|
||||
import Link from 'next/link'
|
||||
import {usePathname} from 'next/navigation'
|
||||
"use client"
|
||||
import {
|
||||
Activity,
|
||||
BarChart3,
|
||||
ChevronsLeft,
|
||||
ChevronsRight,
|
||||
ClipboardList,
|
||||
Code,
|
||||
Database,
|
||||
DollarSign,
|
||||
FileText,
|
||||
Globe,
|
||||
Home,
|
||||
type LucideIcon,
|
||||
Package,
|
||||
Server,
|
||||
Settings,
|
||||
Shield,
|
||||
Users,
|
||||
} from "lucide-react"
|
||||
import Link from "next/link"
|
||||
import { usePathname } from "next/navigation"
|
||||
import { createContext, type ReactNode, useContext, useState } from "react"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { ScrollArea } from "@/components/ui/scroll-area"
|
||||
import { Separator } from "@/components/ui/separator"
|
||||
|
||||
export type NavigationProps = {}
|
||||
|
||||
// 菜单组接口
|
||||
interface MenuGroup {
|
||||
title: string;
|
||||
items: MenuItem[];
|
||||
// Navigation Context
|
||||
interface NavigationContextType {
|
||||
collapsed: boolean
|
||||
pathname: string
|
||||
isActive: (path: string) => boolean
|
||||
}
|
||||
|
||||
// 菜单项接口
|
||||
interface MenuItem {
|
||||
path: string;
|
||||
icon: string;
|
||||
label: string;
|
||||
const NavigationContext = createContext<NavigationContextType | undefined>(
|
||||
undefined,
|
||||
)
|
||||
|
||||
const useNavigation = () => {
|
||||
const context = useContext(NavigationContext)
|
||||
if (!context) {
|
||||
throw new Error("Navigation components must be used within Navigation")
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
// 定义菜单组
|
||||
const menuGroups: MenuGroup[] = [
|
||||
{
|
||||
title: '概览',
|
||||
items: [
|
||||
{
|
||||
path: '/',
|
||||
icon: 'M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6',
|
||||
label: '首页',
|
||||
},
|
||||
{
|
||||
path: '/statistics',
|
||||
icon: 'M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z',
|
||||
label: '数据统计',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title: 'IP 资源',
|
||||
items: [
|
||||
{
|
||||
path: '/proxy/nodes',
|
||||
icon: 'M21 12a9 9 0 01-9 9m9-9a9 9 0 00-9-9m9 9H3m9 9a9 9 0 01-9-9m9 9c1.657 0 3-4.03 3-9s-1.343-9-3-9m0 18c-1.657 0-3-4.03-3-9s1.343-9 3-9',
|
||||
label: '节点列表',
|
||||
},
|
||||
{
|
||||
path: '/proxy/pools',
|
||||
icon: 'M5 12h14M5 12a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v4a2 2 0 01-2 2M5 12a2 2 0 00-2 2v4a2 2 0 002 2h14a2 2 0 002-2v-4a2 2 0 00-2-2m-2-4h.01M17 16h.01',
|
||||
label: 'IP池管理',
|
||||
},
|
||||
{
|
||||
path: '/proxy/sources',
|
||||
icon: 'M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10',
|
||||
label: '代理源管理',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title: '客户',
|
||||
items: [
|
||||
{
|
||||
path: '/clients',
|
||||
icon: 'M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0z',
|
||||
label: '客户管理',
|
||||
},
|
||||
{path: '/packages', icon: 'M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10', label: '套餐管理'},
|
||||
{
|
||||
path: '/orders',
|
||||
icon: 'M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2',
|
||||
label: '订单管理',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title: '运营',
|
||||
items: [
|
||||
{path: '/api/management', icon: 'M10 20l4-16m4 4l4 4-4 4M6 16l-4-4 4-4', label: 'API管理'},
|
||||
{
|
||||
path: '/traffic',
|
||||
icon: 'M9 17v-2m3 2v-4m3 4v-6m2 10H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z',
|
||||
label: '流量监控',
|
||||
},
|
||||
{
|
||||
path: '/billing',
|
||||
icon: 'M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z',
|
||||
label: '计费系统',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title: '系统',
|
||||
items: [
|
||||
{
|
||||
path: '/settings',
|
||||
icon: 'M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z M15 12a3 3 0 11-6 0 3 3 0 016 0z',
|
||||
label: '系统设置',
|
||||
},
|
||||
{
|
||||
path: '/security',
|
||||
icon: 'M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z',
|
||||
label: '安全管理',
|
||||
},
|
||||
{
|
||||
path: '/logs',
|
||||
icon: 'M9 17v-2m3 2v-4m3 4v-6m2 10H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z',
|
||||
label: '系统日志',
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
// NavigationGroup Component
|
||||
interface NavigationGroupProps {
|
||||
title: string
|
||||
children: ReactNode
|
||||
}
|
||||
|
||||
export default function Navigation(props: NavigationProps) {
|
||||
function NavigationGroup({ title, children }: NavigationGroupProps) {
|
||||
const { collapsed } = useNavigation()
|
||||
|
||||
return (
|
||||
<div className="px-3">
|
||||
{!collapsed && (
|
||||
<h3 className="px-3 text-xs font-semibold text-gray-500 uppercase tracking-wider">
|
||||
{title}
|
||||
</h3>
|
||||
)}
|
||||
<ul className={`${collapsed ? "mt-0" : "mt-2"} space-y-1`}>{children}</ul>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// NavigationItem Component
|
||||
interface NavigationItemProps {
|
||||
href: string
|
||||
icon: LucideIcon
|
||||
label: string
|
||||
}
|
||||
|
||||
function NavigationItem({ href, icon: Icon, label }: NavigationItemProps) {
|
||||
const { collapsed, isActive } = useNavigation()
|
||||
const active = isActive(href)
|
||||
|
||||
return (
|
||||
<li>
|
||||
<Link
|
||||
href={href}
|
||||
className={`flex items-center px-3 py-2 rounded-md transition-colors ${
|
||||
active
|
||||
? "bg-blue-50 text-blue-700"
|
||||
: "text-gray-700 hover:bg-gray-100"
|
||||
}`}
|
||||
>
|
||||
<Icon
|
||||
className={`h-5 w-5 ${active ? "text-blue-600" : "text-gray-500"}`}
|
||||
/>
|
||||
{!collapsed && (
|
||||
<span className="ml-3 font-medium text-sm">{label}</span>
|
||||
)}
|
||||
</Link>
|
||||
</li>
|
||||
)
|
||||
}
|
||||
|
||||
// NavigationSeparator Component
|
||||
function NavigationSeparator() {
|
||||
const { collapsed } = useNavigation()
|
||||
|
||||
if (collapsed) return null
|
||||
|
||||
return (
|
||||
<div className="my-4">
|
||||
<Separator />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// Main Navigation Component
|
||||
export default function Navigation() {
|
||||
const [collapsed, setCollapsed] = useState(false)
|
||||
const pathname = usePathname()
|
||||
|
||||
const isActive = (path: string) => {
|
||||
return path === '/'
|
||||
? pathname === path
|
||||
: pathname.startsWith(path)
|
||||
return path === "/" ? pathname === path : pathname.startsWith(path)
|
||||
}
|
||||
|
||||
const contextValue: NavigationContextType = {
|
||||
collapsed,
|
||||
pathname,
|
||||
isActive,
|
||||
}
|
||||
|
||||
return (
|
||||
<aside
|
||||
className={`bg-white border-r border-gray-200 transition-all duration-300 ease-in-out flex flex-col ${collapsed ? 'w-20' : 'w-64'}`}>
|
||||
{/* Logo */}
|
||||
<div className="h-16 flex items-center px-5 border-b border-gray-200">
|
||||
{!collapsed ? (
|
||||
<span className="text-xl font-bold tracking-wide text-gray-800">管理系统</span>
|
||||
) : (
|
||||
<span className="text-xl font-bold mx-auto text-gray-800">系</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 导航菜单 */}
|
||||
<nav className="flex-1 py-4 overflow-y-auto">
|
||||
<div className="space-y-4">
|
||||
{menuGroups.map((group, groupIndex) => (
|
||||
<div key={groupIndex} className="px-3">
|
||||
{!collapsed && (
|
||||
<h3 className="px-3 text-xs font-semibold text-gray-500 uppercase tracking-wider">
|
||||
{group.title}
|
||||
</h3>
|
||||
)}
|
||||
|
||||
<ul className={`mt-${collapsed ? '0' : '2'} space-y-1`}>
|
||||
{group.items.map((item) => (
|
||||
<li key={item.path}>
|
||||
<Link
|
||||
href={item.path}
|
||||
className={`flex items-center px-3 py-2 rounded-md transition-colors ${
|
||||
isActive(item.path)
|
||||
? 'bg-blue-50 text-blue-700'
|
||||
: 'text-gray-700 hover:bg-gray-100'
|
||||
}`}
|
||||
>
|
||||
<svg
|
||||
className={`h-5 w-5 ${isActive(item.path) ? 'text-blue-600' : 'text-gray-500'}`}
|
||||
fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d={item.icon}/>
|
||||
</svg>
|
||||
{!collapsed && <span className="ml-3 font-medium text-sm">{item.label}</span>}
|
||||
</Link>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
|
||||
{!collapsed && groupIndex < menuGroups.length - 1 && (
|
||||
<div className="my-4 border-t border-gray-200"></div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
<NavigationContext.Provider value={contextValue}>
|
||||
<aside
|
||||
className={`bg-white border-r border-gray-200 transition-all duration-300 ease-in-out flex flex-col ${
|
||||
collapsed ? "w-20" : "w-64"
|
||||
}`}
|
||||
>
|
||||
{/* Logo */}
|
||||
<div className="h-16 flex items-center px-5 border-b border-gray-200">
|
||||
{!collapsed ? (
|
||||
<span className="text-xl font-bold tracking-wide text-gray-800">
|
||||
管理系统
|
||||
</span>
|
||||
) : (
|
||||
<span className="text-xl font-bold mx-auto text-gray-800">系</span>
|
||||
)}
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
{/* 侧边栏底部按钮 */}
|
||||
<div className="p-4 border-t border-gray-200 mt-auto">
|
||||
<button
|
||||
onClick={() => setCollapsed(!collapsed)}
|
||||
className="flex items-center justify-center w-full p-2 text-gray-600 hover:bg-gray-100 rounded-md transition-colors"
|
||||
>
|
||||
<svg className="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path
|
||||
strokeLinecap="round" strokeLinejoin="round" strokeWidth={2}
|
||||
d={collapsed ? 'M13 5l7 7-7 7M5 5l7 7-7 7' : 'M11 19l-7-7 7-7m8 14l-7-7 7-7'}/>
|
||||
</svg>
|
||||
{!collapsed && <span className="ml-2 text-sm">收起菜单</span>}
|
||||
</button>
|
||||
</div>
|
||||
</aside>
|
||||
{/* Navigation Menu */}
|
||||
<ScrollArea className="flex-1 py-4">
|
||||
<nav className="space-y-4">
|
||||
{/* 概览 */}
|
||||
<NavigationGroup title="概览">
|
||||
<NavigationItem href="/" icon={Home} label="首页" />
|
||||
<NavigationItem
|
||||
href="/statistics"
|
||||
icon={BarChart3}
|
||||
label="数据统计"
|
||||
/>
|
||||
</NavigationGroup>
|
||||
|
||||
<NavigationSeparator />
|
||||
|
||||
{/* IP 资源 */}
|
||||
<NavigationGroup title="IP 资源">
|
||||
<NavigationItem
|
||||
href="/proxy/nodes"
|
||||
icon={Globe}
|
||||
label="节点列表"
|
||||
/>
|
||||
<NavigationItem
|
||||
href="/proxy/pools"
|
||||
icon={Server}
|
||||
label="IP池管理"
|
||||
/>
|
||||
<NavigationItem
|
||||
href="/proxy/sources"
|
||||
icon={Database}
|
||||
label="代理源管理"
|
||||
/>
|
||||
</NavigationGroup>
|
||||
|
||||
<NavigationSeparator />
|
||||
|
||||
{/* 客户 */}
|
||||
<NavigationGroup title="客户">
|
||||
<NavigationItem href="/clients" icon={Users} label="客户管理" />
|
||||
<NavigationItem
|
||||
href="/packages"
|
||||
icon={Package}
|
||||
label="套餐管理"
|
||||
/>
|
||||
<NavigationItem
|
||||
href="/orders"
|
||||
icon={ClipboardList}
|
||||
label="订单管理"
|
||||
/>
|
||||
</NavigationGroup>
|
||||
|
||||
<NavigationSeparator />
|
||||
|
||||
{/* 运营 */}
|
||||
<NavigationGroup title="运营">
|
||||
<NavigationItem
|
||||
href="/api/management"
|
||||
icon={Code}
|
||||
label="API管理"
|
||||
/>
|
||||
<NavigationItem
|
||||
href="/traffic"
|
||||
icon={Activity}
|
||||
label="流量监控"
|
||||
/>
|
||||
<NavigationItem
|
||||
href="/billing"
|
||||
icon={DollarSign}
|
||||
label="计费系统"
|
||||
/>
|
||||
</NavigationGroup>
|
||||
|
||||
<NavigationSeparator />
|
||||
|
||||
{/* 系统 */}
|
||||
<NavigationGroup title="系统">
|
||||
<NavigationItem
|
||||
href="/settings"
|
||||
icon={Settings}
|
||||
label="系统设置"
|
||||
/>
|
||||
<NavigationItem href="/security" icon={Shield} label="安全管理" />
|
||||
<NavigationItem href="/logs" icon={FileText} label="系统日志" />
|
||||
</NavigationGroup>
|
||||
</nav>
|
||||
</ScrollArea>
|
||||
|
||||
{/* Toggle Button */}
|
||||
<div className="p-4 border-t border-gray-200 mt-auto">
|
||||
<Button
|
||||
variant="ghost"
|
||||
onClick={() => setCollapsed(!collapsed)}
|
||||
className="w-full justify-center text-gray-600 hover:bg-gray-100"
|
||||
>
|
||||
{collapsed ? (
|
||||
<ChevronsRight className="h-5 w-5" />
|
||||
) : (
|
||||
<>
|
||||
<ChevronsLeft className="h-5 w-5" />
|
||||
<span className="ml-2 text-sm">收起菜单</span>
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</aside>
|
||||
</NavigationContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user