"use client"; import Link from "next/link"; import { useEffect, useState } from "react"; import { Menu } from "lucide-react"; import { usePathname, useRouter } from "next/navigation"; import { HeaderActions } from "@/components/header-actions"; import { Sheet, SheetClose, SheetContent, SheetFooter, SheetHeader, SheetTitle, SheetTrigger } from "@/components/ui/sheet"; 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: "/accounts", label: "号池管理" }, { href: "/register", label: "注册机" }, { href: "/image-manager", label: "图片管理" }, { href: "/logs", label: "日志管理" }, { href: "/debug", label: "调试" }, { href: "/settings", label: "设置" }, ]; const userNavItems = [{ href: "/image", 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; const baseUrl = webConfig.apiUrl.replace(/\/$/, "") || window.location.origin; const canvasHref = `https://infinite-canvas-cpco.onrender.com?apiKey=${encodeURIComponent(session.key)}&baseUrl=${encodeURIComponent(baseUrl)}`; const canvasItem = { href: canvasHref, label: "无限画布", external: true }; const allNavItems = [canvasItem, ...navItems.map((item) => ({ ...item, external: false }))]; return (
打开导航 chatgpt2api {roleLabel} · {displayName} chatgpt2api
{roleLabel} · {displayName}
); }