顶栏左侧新增切换按钮,点击后左侧菜单在 w-60↔w-0 间平滑伸缩隐藏。 折叠状态通过 SidebarProvider(context) 在侧边栏与顶栏间共享。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
92 lines
3.6 KiB
TypeScript
92 lines
3.6 KiB
TypeScript
"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<HTMLDivElement>(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 (
|
|
<header className="sticky top-0 z-20 flex h-16 items-center justify-between border-b bg-[var(--color-surface)]/90 px-6 backdrop-blur">
|
|
<div className="flex items-center gap-3">
|
|
<button
|
|
type="button"
|
|
onClick={toggle}
|
|
aria-label="收起/展开菜单"
|
|
className="-ml-1.5 rounded-md p-1.5 text-[var(--color-muted-foreground)] transition-colors hover:bg-[var(--color-muted)] hover:text-[var(--color-foreground)]"
|
|
>
|
|
<PanelLeft className="h-[18px] w-[18px]" />
|
|
</button>
|
|
<h1 className="text-base font-semibold">{pageTitle(pathname)}</h1>
|
|
</div>
|
|
|
|
<div className="relative" ref={ref}>
|
|
<button
|
|
onClick={() => setOpen((v) => !v)}
|
|
className="flex items-center gap-2.5 rounded-full py-1 pl-1 pr-2.5 transition-colors hover:bg-[var(--color-muted)]"
|
|
>
|
|
<Avatar name={user.name ?? user.username} src={user.image} size={32} />
|
|
<div className="hidden text-left leading-tight sm:block">
|
|
<div className="text-[13px] font-medium">{user.name ?? user.username}</div>
|
|
<div className="text-[11px] text-[var(--color-muted-foreground)]">{roleLabel}</div>
|
|
</div>
|
|
<ChevronDown className="h-4 w-4 text-[var(--color-muted-foreground)]" />
|
|
</button>
|
|
|
|
{open && (
|
|
<div className="absolute right-0 mt-2 w-48 overflow-hidden rounded-[var(--radius)] border bg-[var(--color-surface)] py-1 shadow-lg">
|
|
<Link
|
|
href="/me"
|
|
onClick={() => setOpen(false)}
|
|
className="flex items-center gap-2.5 px-4 py-2 text-sm hover:bg-[var(--color-muted)]"
|
|
>
|
|
<User className="h-4 w-4" /> 个人中心
|
|
</Link>
|
|
<button
|
|
onClick={() => signOut({ callbackUrl: "/login" })}
|
|
className="flex w-full items-center gap-2.5 px-4 py-2 text-sm text-[var(--color-danger)] hover:bg-[var(--color-danger-soft)]"
|
|
>
|
|
<LogOut className="h-4 w-4" /> 退出登录
|
|
</button>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</header>
|
|
);
|
|
}
|