import AdminSidebar from "@/components/admin/AdminSidebar"; import AdminAccessDenied from "@/components/admin/AdminAccessDenied"; import { getAdminUser } from "@/lib/admin-permissions"; import { getActiveBrandId } from "@/lib/brand-scope"; import { listBrandsForAdmin } from "@/actions/brands"; import { redirect } from "next/navigation"; import "@/styles/admin-design-system.css"; import { ToastProvider } from "@/components/admin/Toast"; import { ToastContainer } from "@/components/admin/ToastContainer"; // Admin layout calls getAdminUser() which reads cookies(). Without this, // Next.js tries to prerender the entire /admin/* tree statically and the // first page that hits cookies() aborts the build with DYNAMIC_SERVER_USAGE. export const dynamic = "force-dynamic"; // Toast provider wrapper component function ToastProviderWrapper({ children }: { children: React.ReactNode }) { return ( {children} ); } export default async function AdminLayout({ children }: { children: React.ReactNode }) { let adminUser = null; let authError: string | null = null; // Robust auth with try-catch to prevent crashes try { adminUser = await getAdminUser(); } catch (error) { console.error("Admin auth error:", error); authError = "Failed to verify authentication. Please try again."; } // Auth verification failed if (authError) { return (
); } // Not authenticated if (!adminUser) { return (
); } // Must change password if (adminUser.must_change_password) { redirect("/change-password"); } // Resolve the active brand (URL > cookie > legacy > first of brand_ids) const activeBrandId = await getActiveBrandId(adminUser); // Fetch accessible brands for the sidebar BrandSelector. We do this // unconditionally — `listBrandsForAdmin` is cheap and the sidebar // decides whether to show the dropdown. const brands = await listBrandsForAdmin(); return (
{children}
); }