"use client"; import { useSession } from "next-auth/react"; import { useRouter, usePathname } from "next/navigation"; import { useEffect } from "react"; import Image from "next/image"; import { Footer } from "@/components/layout/Footer"; import { CommonHeader } from "@/components/layout/CommonHeader"; import { PresenceProvider } from "@/components/providers/presence-provider"; // Pages that don't require authentication const publicPaths = [ "/home", "/user/dashboard", "/user/profiles", "/user/profile/", // Agent profile view (includes /user/profile/[id]) ]; // Check if current path is public const isPublicPath = (pathname: string) => { return publicPaths.some( (path) => pathname === path || pathname.startsWith(path), ); }; export default function UserLayout({ children, }: { children: React.ReactNode; }) { const { data: session, status } = useSession(); const router = useRouter(); const pathname = usePathname(); // const isDashboard = pathname === "/user/dashboard"; const isDashboard = pathname === "/user/dashboard" || pathname === "/home"; const isPublic = isPublicPath(pathname); useEffect(() => { if (status === "loading") return; // Allow public pages without authentication. // Agents are intentionally allowed to land on /user/dashboard — the logo // points there for all roles, so this layout must not bounce them away. if (isPublic) { return; } // Don't redirect to /login here for protected pages — the middleware handles that. // Doing it here causes race conditions during logout (layout detects session loss // and redirects to /login via client-side navigation, conflicting with server-side // logout redirect). if (!session) return; // Redirect agents to agent dashboard for protected user pages const userRole = (session.user as any)?.role; if (userRole === "AGENT") { router.replace("/agent/dashboard"); } }, [session, status, router, pathname, isPublic]); const splashLoading = (