layout.tsx 3.05 KB
"use client";

import Link from "next/link";
import { usePathname, useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import { Building2, Briefcase, Database, History, Menu as MenuIcon, Puzzle, ShieldCheck, Users } from "lucide-react";

import { fetchMe } from "@/lib/auth";
import { usePerms } from "@/lib/usePerms";
import { cn } from "@/lib/utils";

const NAV = [
  { href: "/system/user", label: "用户管理", perm: "oa:user:list", icon: Users },
  { href: "/system/role", label: "角色管理", perm: "oa:role:list", icon: ShieldCheck },
  { href: "/system/dept", label: "部门管理", perm: "oa:dept:list", icon: Building2 },
  { href: "/system/post", label: "岗位管理", perm: "oa:post:list", icon: Briefcase },
  { href: "/system/source", label: "资源权限", perm: "oa:source:list", icon: MenuIcon },
  { href: "/system/kb", label: "知识库配置", perm: "kb:list", icon: Database },
  { href: "/system/skills", label: "Skills 管理", perm: "oa:skill:list", icon: Puzzle },
  { href: "/system/log", label: "操作日志", perm: "oa:log:list", icon: History },
];

export default function SystemLayout({ children }: { children: React.ReactNode }) {
  const pathname = usePathname();
  const router = useRouter();
  const { has, loaded } = usePerms();
  const [ready, setReady] = useState(false);

  useEffect(() => {
    fetchMe().then((u) => {
      if (!u) router.push("/login");
      else setReady(true);
    });
  }, [router]);

  if (!ready) return null;

  const visible = loaded ? NAV.filter((n) => has(n.perm)) : NAV;

  return (
    <div className="flex h-full min-h-0 bg-gradient-to-br from-blue-50 via-slate-50 to-blue-50/40 text-slate-800">
      <aside className="flex w-56 shrink-0 flex-col border-r border-blue-100 bg-white/80 backdrop-blur">
        <div className="flex items-center gap-2 px-5 py-5">
          <div className="flex h-9 w-9 items-center justify-center rounded-xl bg-blue-600 text-white shadow-lg shadow-blue-500/25">
            <ShieldCheck className="h-5 w-5" />
          </div>
          <div>
            <div className="text-sm font-bold text-slate-800">系统管理</div>
            <div className="text-[11px] text-slate-400">权限与组织架构</div>
          </div>
        </div>
        <nav className="space-y-1 px-3">
          {visible.map((n) => {
            const Icon = n.icon;
            const active = pathname === n.href;
            return (
              <Link
                key={n.href}
                href={n.href}
                className={cn(
                  "flex items-center gap-2.5 rounded-xl px-3.5 py-2.5 text-sm font-medium transition",
                  active
                    ? "bg-blue-600 text-white shadow-sm shadow-blue-500/25"
                    : "text-slate-600 hover:bg-blue-50 hover:text-blue-700"
                )}
              >
                <Icon className="h-4 w-4" />
                {n.label}
              </Link>
            );
          })}
        </nav>
      </aside>
      <main className="min-w-0 flex-1 overflow-hidden">{children}</main>
    </div>
  );
}