Files
liandong-crm/src/components/app/topbar.tsx
T

106 lines
4.4 KiB
TypeScript
Raw Normal View History

"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";
import { Avatar } from "@/components/ui/avatar";
import { navSections } from "./nav";
2026-07-11 22:56:04 +08:00
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, toggleMobile, mobileOpen } = useSidebar();
function onMenu() {
if (window.matchMedia("(min-width: 768px)").matches) toggle();
else toggleMobile();
}
useEffect(() => {
function onClick(e: Event) {
if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false);
}
document.addEventListener("pointerdown", onClick);
return () => document.removeEventListener("pointerdown", onClick);
}, []);
const roleLabel = user.roles.includes("super admin") ? "超级管理员" : "业务员";
return (
<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"
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>
<h1 className="truncate text-base font-semibold">{pageTitle(pathname)}</h1>
2026-07-11 22:56:04 +08:00
</div>
<div className="relative" ref={ref}>
<button
type="button"
onClick={() => setOpen((v) => !v)}
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}
>
<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 min-h-11 items-center gap-2.5 px-4 py-2 text-sm hover:bg-[var(--color-muted)] md:min-h-0"
>
<User className="h-4 w-4" /> 个人中心
</Link>
<button
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");
});
}}
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"
>
<LogOut className="h-4 w-4" /> 退出登录
</button>
</div>
)}
</div>
</header>
);
}