src/app/superadmin/layout.tsx

component·app·1.5 KB · 53 lignes· Voir l'itinéraire
Annotation non disponible

Lance npm run annotate (nécessite ANTHROPIC_API_KEY dans .env.local) pour générer une annotation française par Claude Haiku 4.5.

1 export

default

Code source· tsx

import { headers, cookies } from "next/headers";
import { redirect } from "next/navigation";
import { getSession } from "@/lib/auth/session";
import { prisma } from "@/lib/prisma/client";
import SuperadminShell from "@/components/superadmin/superadmin-shell";

// Routes auth qui n'ont pas besoin du shell (login uniquement)
const AUTH_ROUTE_PREFIXES = ["/superadmin/login"];
function isAuth(pathname: string) {
  return AUTH_ROUTE_PREFIXES.some((p) => pathname.startsWith(p));
}

async function getCurrentPath(): Promise<string> {
  const h = await headers();
  return h.get("x-pathname") ?? h.get("x-invoke-path") ?? "";
}

export default async function SuperadminLayout({ children }: { children: React.ReactNode }) {
  const session = await getSession();
  const pathname = await getCurrentPath();
  const isAuthRoute = isAuth(pathname);
  const cookieStore = await cookies();
  const initialTheme: "light" | "dark" =
    cookieStore.get("wari-admin-theme")?.value === "dark" ? "dark" : "light";

  if (isAuthRoute) {
    return (
      <div
        className={`min-h-screen bg-gray-50 dark:bg-zinc-950 ${
          initialTheme === "dark" ? "admin-dark" : ""
        }`}
      >
        {children}
      </div>
    );
  }

  if (!session || session.role !== "SUPER_ADMIN") {
    redirect("/superadmin/login");
  }

  const user = await prisma.user.findFirst({
    where: { id: session.userId },
    select: { email: true },
  });

  return (
    <SuperadminShell userEmail={user?.email ?? "superadmin"} initialTheme={initialTheme}>
      {children}
    </SuperadminShell>
  );
}