"use client" import Image from "next/image" import Link from "next/link" import { usePathname } from "next/navigation" import { useEffect, useRef, useState } from "react" export default function Appbar() { const [currentUser] = useState({ name: "张三", avatar: "/avatar.png", role: "管理员", }) const [showDropdown, setShowDropdown] = useState(false) const [showNotifications, setShowNotifications] = useState(false) const [notifications] = useState([ { id: 1, title: "系统通知", content: "您有新的待审核内容", time: "10分钟前", read: false, }, { id: 2, title: "安全提醒", content: "您的账号于昨天登录了新设备", time: "1小时前", read: true, }, { id: 3, title: "系统更新", content: "系统将在今晚进行例行维护", time: "2小时前", read: true, }, ]) const pathname = usePathname() const dropdownRef = useRef(null) const notificationRef = useRef(null) // 处理点击外部关闭下拉菜单 useEffect(() => { function handleClickOutside(event: MouseEvent) { if ( dropdownRef.current && !dropdownRef.current.contains(event.target as Node) ) { setShowDropdown(false) } if ( notificationRef.current && !notificationRef.current.contains(event.target as Node) ) { setShowNotifications(false) } } document.addEventListener("mousedown", handleClickOutside) return () => document.removeEventListener("mousedown", handleClickOutside) }, []) // 根据路径生成面包屑 const generateBreadcrumbs = () => { const paths = pathname.split("/").filter(Boolean) const breadcrumbs = [ { path: "/", label: "首页" }, ...paths.map((path, index) => { const url = `/${paths.slice(0, index + 1).join("/")}` const label = getBreadcrumbLabel(path) return { path: url, label } }), ] return breadcrumbs } const getBreadcrumbLabel = (path: string) => { const labels: Record = { dashboard: "控制台", content: "内容管理", articles: "文章管理", media: "媒体库", users: "用户管理", roles: "角色权限", settings: "系统设置", logs: "系统日志", } return labels[path] || path } const breadcrumbs = generateBreadcrumbs() const unreadCount = notifications.filter(n => !n.read).length return (
{/* 面包屑导航 */}
{breadcrumbs.map((crumb, index) => (
{index > 0 && ( )} {crumb.label}
))}
{/* 右侧用户信息和工具栏 */}
{/* 搜索框 */}
{/* 通知图标 */}
{/* 通知下拉面板 */} {showNotifications && (

通知

{notifications.length > 0 ? ( notifications.map(notification => (

{notification.title}

{notification.time}

{notification.content}

)) ) : (

暂无通知

)}
查看全部通知
)}
{/* 分隔线 */}
{/* 用户下拉菜单 */}
{/* 用户下拉内容 */} {showDropdown && (

{currentUser.name}

{currentUser.role}

个人资料 账号设置 帮助中心
退出登录
)}
) }