"use client"; import { Sidebar } from "./Sidebar"; import { TopBar } from "./TopBar"; import { Aurora } from "./Aurora"; import { useChatStore } from "@/lib/chatStore"; import { useBackendSync } from "@/lib/useBackendSync"; import clsx from "clsx"; import { usePathname } from "next/navigation"; import { useEffect } from "react"; export function AppShell({ children }: { children: React.ReactNode }) { const sidebarOpen = useChatStore((s) => s.sidebarOpen); const hydrated = useChatStore((s) => s.hydrated); const pathname = usePathname(); const conversations = useChatStore((s) => s.conversations); const activeId = useChatStore((s) => s.activeId); const newConversation = useChatStore((s) => s.newConversation); // Backend hydration + auto-save (debounced PUT to /conversations) useBackendSync(); // Ensure there is always at least one conversation when on chat route. // Wait until hydration completes so we don't create a placeholder before // the remote state arrives (which would cause a flash + redundant save). useEffect(() => { if (pathname !== "/") return; if (!hydrated) return; const hasAny = Object.keys(conversations).length > 0; const hasActive = activeId && conversations[activeId]; if (!hasAny || !hasActive) newConversation(); }, [pathname, hydrated, conversations, activeId, newConversation]); return (