"use client"; import { useRouter } from "next/navigation"; import { useSession } from "next-auth/react"; import { type ReactNode, useCallback, useEffect, useId, useState } from "react"; import Sidebar from "../../components/Sidebar"; export default function DashboardLayout({ children }: { children: ReactNode }) { const mainContentId = useId(); const { status } = useSession(); const router = useRouter(); const [isSidebarExpanded, setIsSidebarExpanded] = useState(true); const [isMobile, setIsMobile] = useState(false); useEffect(() => { const updateStatesBasedOnScreen = () => { const screenIsMobile = window.innerWidth < 640; // sm breakpoint for mobile const screenIsSmallDesktop = window.innerWidth < 768 && !screenIsMobile; // between sm and md setIsMobile(screenIsMobile); setIsSidebarExpanded(!screenIsSmallDesktop && !screenIsMobile); }; updateStatesBasedOnScreen(); window.addEventListener("resize", updateStatesBasedOnScreen); return () => window.removeEventListener("resize", updateStatesBasedOnScreen); }, []); // Toggle sidebar handler - used for clicking the toggle button const toggleSidebarHandler = useCallback(() => { setIsSidebarExpanded((prev) => !prev); }, []); // Collapse sidebar handler - used when clicking navigation links on mobile const collapseSidebar = useCallback(() => { if (isMobile) { setIsSidebarExpanded(false); } }, [isMobile]); if (status === "unauthenticated") { router.push("/login"); return (
Redirecting to login...
); } if (status === "loading") { return (
Loading session...
); } return (
{/*
{children}
*/}
{children}
); }