"use client"; import Link from "next/link"; import { useEffect, useState } from "react"; import { Github } from "lucide-react"; import { usePathname, useRouter } from "next/navigation"; import webConfig from "@/constants/common-env"; import { getValidatedAuthSession } from "@/lib/auth-session"; import { cn } from "@/lib/utils"; import { clearStoredAuthSession, type StoredAuthSession } from "@/store/auth"; const adminNavItems = [ { href: "/image", label: "画图" }, { href: "/chat", label: "对话" }, { href: "/accounts", label: "号池管理" }, { href: "/register", label: "注册机" }, { href: "/image-manager", label: "图片管理" }, { href: "/logs", label: "日志管理" }, { href: "/settings", label: "设置" }, ]; const userNavItems = [ { href: "/image", label: "画图" }, { href: "/chat", label: "对话" }, ]; export function TopNav() { const pathname = usePathname(); const router = useRouter(); const [session, setSession] = useState(undefined); useEffect(() => { let active = true; const load = async () => { if (pathname === "/login") { if (!active) { return; } setSession(null); return; } const storedSession = await getValidatedAuthSession(); if (!active) { return; } setSession(storedSession); }; void load(); return () => { active = false; }; }, [pathname]); const handleLogout = async () => { await clearStoredAuthSession(); router.replace("/login"); }; if (pathname === "/login" || session === undefined || !session) { return null; } const navItems = session.role === "admin" ? adminNavItems : userNavItems; const roleLabel = session.role === "admin" ? "管理员" : "普通用户"; const displayName = session.name.trim() || roleLabel; return (
chatgpt2api GitHub
{roleLabel} · {displayName} v{webConfig.appVersion}
); }