2026-07-08 15:02:03 +08:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
|
|
|
import Link from "next/link";
|
|
|
|
|
import { usePathname } from "next/navigation";
|
|
|
|
|
import { signOut } from "next-auth/react";
|
2026-07-11 22:56:04 +08:00
|
|
|
import { ChevronDown, User, LogOut, PanelLeft } from "lucide-react";
|
2026-07-08 15:02:03 +08:00
|
|
|
import { Avatar } from "@/components/ui/avatar";
|
|
|
|
|
import { navSections } from "./nav";
|
2026-07-11 22:56:04 +08:00
|
|
|
import { useSidebar } from "./sidebar-context";
|
2026-07-08 15:02:03 +08:00
|
|
|
|
|
|
|
|
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);
|
2026-08-29 21:55:10 +08:00
|
|
|
const { toggle, toggleMobile, mobileOpen } = useSidebar();
|
|
|
|
|
|
|
|
|
|
function onMenu() {
|
|
|
|
|
if (window.matchMedia("(min-width: 768px)").matches) toggle();
|
|
|
|
|
else toggleMobile();
|
|
|
|
|
}
|
2026-07-08 15:02:03 +08:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-08-29 21:55:10 +08:00
|
|
|
function onClick(e: Event) {
|
2026-07-08 15:02:03 +08:00
|
|
|
if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false);
|
|
|
|
|
}
|
2026-08-29 21:55:10 +08:00
|
|
|
document.addEventListener("pointerdown", onClick);
|
|
|
|
|
return () => document.removeEventListener("pointerdown", onClick);
|
2026-07-08 15:02:03 +08:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const roleLabel = user.roles.includes("super admin") ? "超级管理员" : "业务员";
|
|
|
|
|
|
|
|
|
|
return (
|
2026-08-29 21:55:10 +08:00
|
|
|
<header className="sticky top-0 z-20 flex h-[calc(4rem+env(safe-area-inset-top))] items-center justify-between border-b bg-[var(--color-surface)]/90 px-3 pt-[env(safe-area-inset-top)] backdrop-blur md:px-6">
|
|
|
|
|
<div className="flex min-w-0 items-center gap-2 md:gap-3">
|
2026-07-11 22:56:04 +08:00
|
|
|
<button
|
|
|
|
|
type="button"
|
2026-08-29 21:55:10 +08:00
|
|
|
onClick={onMenu}
|
|
|
|
|
aria-label="打开/关闭菜单"
|
|
|
|
|
aria-expanded={mobileOpen}
|
|
|
|
|
className="-ml-1 flex h-11 w-11 items-center justify-center rounded-md text-[var(--color-muted-foreground)] transition-colors hover:bg-[var(--color-muted)] hover:text-[var(--color-foreground)] md:h-auto md:w-auto md:p-1.5"
|
2026-07-11 22:56:04 +08:00
|
|
|
>
|
|
|
|
|
<PanelLeft className="h-[18px] w-[18px]" />
|
|
|
|
|
</button>
|
2026-08-29 21:55:10 +08:00
|
|
|
<h1 className="truncate text-base font-semibold">{pageTitle(pathname)}</h1>
|
2026-07-11 22:56:04 +08:00
|
|
|
</div>
|
2026-07-08 15:02:03 +08:00
|
|
|
|
|
|
|
|
<div className="relative" ref={ref}>
|
|
|
|
|
<button
|
2026-08-29 21:55:10 +08:00
|
|
|
type="button"
|
2026-07-08 15:02:03 +08:00
|
|
|
onClick={() => setOpen((v) => !v)}
|
2026-08-29 21:55:10 +08:00
|
|
|
className="flex min-h-11 items-center gap-2.5 rounded-full py-1 pl-1 pr-2.5 transition-colors hover:bg-[var(--color-muted)]"
|
|
|
|
|
aria-expanded={open}
|
2026-07-08 15:02:03 +08:00
|
|
|
>
|
|
|
|
|
<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)}
|
2026-08-29 21:55:10 +08:00
|
|
|
className="flex min-h-11 items-center gap-2.5 px-4 py-2 text-sm hover:bg-[var(--color-muted)] md:min-h-0"
|
2026-07-08 15:02:03 +08:00
|
|
|
>
|
|
|
|
|
<User className="h-4 w-4" /> 个人中心
|
|
|
|
|
</Link>
|
|
|
|
|
<button
|
2026-07-13 16:21:17 +08:00
|
|
|
onClick={() => {
|
|
|
|
|
// redirect:false + 浏览器同源跳转,避免服务端 baseUrl 误用
|
|
|
|
|
// 监听地址 0.0.0.0 或 AUTH_URL 配错时拼出 http://0.0.0.0:3100/login
|
|
|
|
|
void signOut({ redirect: false }).then(() => {
|
|
|
|
|
window.location.assign("/login");
|
|
|
|
|
});
|
|
|
|
|
}}
|
2026-08-29 21:55:10 +08:00
|
|
|
className="flex min-h-11 w-full items-center gap-2.5 px-4 py-2 text-sm text-[var(--color-danger)] hover:bg-[var(--color-danger-soft)] md:min-h-0"
|
2026-07-08 15:02:03 +08:00
|
|
|
>
|
|
|
|
|
<LogOut className="h-4 w-4" /> 退出登录
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</header>
|
|
|
|
|
);
|
|
|
|
|
}
|