"use client"; import { useEffect, useRef, useState } from "react"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { signOut } from "next-auth/react"; import { ChevronDown, User, LogOut, PanelLeft } from "lucide-react"; import { Avatar } from "@/components/ui/avatar"; import { navSections } from "./nav"; import { useSidebar } from "./sidebar-context"; function pageTitle(pathname: string): string { if (pathname === "/") return "控制台"; if (pathname.startsWith("/me")) return "个人中心"; const flat = navSections.flatMap((s) => s.items); const hit = flat .filter((i) => i.href !== "/" && pathname.startsWith(i.href)) .sort((a, b) => b.href.length - a.href.length)[0]; if (pathname.includes("/new")) return "新增" + (hit?.label ?? ""); if (pathname.includes("/edit")) return "编辑" + (hit?.label ?? ""); return hit?.label ?? ""; } export function Topbar({ user, }: { user: { name?: string | null; username: string; roles: string[]; image?: string | null }; }) { const pathname = usePathname(); const [open, setOpen] = useState(false); const ref = useRef(null); const { toggle } = useSidebar(); useEffect(() => { function onClick(e: MouseEvent) { if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false); } document.addEventListener("mousedown", onClick); return () => document.removeEventListener("mousedown", onClick); }, []); const roleLabel = user.roles.includes("super admin") ? "超级管理员" : "业务员"; return (

{pageTitle(pathname)}

{open && (
setOpen(false)} className="flex items-center gap-2.5 px-4 py-2 text-sm hover:bg-[var(--color-muted)]" > 个人中心
)}
); }