"use client";
import { useRouter, useSearchParams } from "next/navigation";
import { Suspense, useEffect, useRef } from "react";
import { uiStateApi } from "@/lib/api";
import { Header } from "@/components/layout/header";
import { MobileTabBar } from "@/components/layout/mobile-tab-bar";
import { Sidebar } from "@/components/layout/sidebar";
import { MascotChatbot } from "@/features/mascot/mascot-chatbot";
import { AuthProvider, useAuth } from "@/lib/hooks/use-auth";
import { WsProvider } from "@/lib/ws/context";
function DashboardGuard({ children }: { children: React.ReactNode }) {
const { authenticated, loading } = useAuth();
const router = useRouter();
useEffect(() => {
if (!loading && !authenticated) {
router.push("/");
}
}, [authenticated, loading, router]);
if (loading) {
return (
);
}
if (!authenticated) return null;
return <>{children}>;
}
function DashboardShell({ children }: { children: React.ReactNode }) {
const searchParams = useSearchParams();
const router = useRouter();
const restored = useRef(false);
const activeTab = (searchParams.get("tab") ?? "messages") as
| "messages"
| "live"
| "dashboard";
// Restore persisted tab on mount (only if no explicit tab in URL)
useEffect(() => {
if (restored.current) return;
const tabParam = searchParams.get("tab");
if (tabParam) {
restored.current = true;
return; // explicit tab in URL — don't override
}
uiStateApi
.get()
.then((state) => {
restored.current = true;
const savedTab = state.active_tab;
if (savedTab && savedTab !== activeTab) {
router.replace(`/dashboard?tab=${savedTab}`);
}
})
.catch(() => {
restored.current = true;
});
}, [searchParams, activeTab, router]);
// Persist tab changes
useEffect(() => {
if (!restored.current) return;
uiStateApi.save({ active_tab: activeTab }).catch(() => {});
}, [activeTab]);
return (
);
}
export default function DashboardLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
}
>
{children}
);
}