Sidebar.tsx 2.27 KB
"use client";

import { useEffect, useState } from "react";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { BookOpen, Database, FileText, Settings, UserRound } from "lucide-react";
import { cn } from "@/lib/utils";
import { fetchMe } from "@/lib/auth";

interface NavItem {
  href: string;
  label: string;
  icon: typeof BookOpen;
  /** 仅管理员可见 */
  adminOnly?: boolean;
}

const NAV_ITEMS: NavItem[] = [
  { href: "/", label: "Wiki 原始文件", icon: FileText },
  { href: "/wiki-repo", label: "Wiki 知识库", icon: BookOpen },
  { href: "/rag", label: "RAG 工作台", icon: Database },
  { href: "/profile", label: "我的画像", icon: UserRound },
  { href: "/system/user", label: "系统管理", icon: Settings, adminOnly: true },
];

export default function Sidebar() {
  const pathname = usePathname();
  const [isAdmin, setIsAdmin] = useState(false);

  useEffect(() => {
    fetchMe()
      .then((user) => setIsAdmin(Boolean(user?.isSuperadmin)))
      .catch(() => setIsAdmin(false));
  }, []);

  const visible = NAV_ITEMS.filter((item) => !item.adminOnly || isAdmin);

  return (
    <aside className="w-16 flex flex-col items-center py-6 bg-slate-900 text-slate-400 shrink-0">
      <div className="w-10 h-10 bg-blue-600 rounded-xl flex items-center justify-center text-white mb-8 shadow-lg shadow-blue-500/20">
        <BookOpen className="w-6 h-6 fill-current" />
      </div>
      <nav className="flex-1 flex flex-col gap-6">
        {visible.map(({ href, label, icon: Icon }) => {
          const active = pathname === href || (href !== "/" && pathname.startsWith(href));
          return (
            <Link
              key={href}
              href={href}
              className={cn(
                "p-2 rounded-lg transition relative group",
                active ? "bg-slate-800 text-white" : "hover:bg-slate-800 hover:text-slate-200"
              )}
              title={label}
            >
              <Icon className="w-5 h-5" />
              <span className="absolute left-full ml-2 px-2 py-1 bg-slate-800 text-white text-xs rounded opacity-0 group-hover:opacity-100 pointer-events-none whitespace-nowrap z-50">
                {label}
              </span>
            </Link>
          );
        })}
      </nav>
    </aside>
  );
}