import { AgentSidebar, focusAgentChat, isAgentChatHomeHandoffActive, navigateWithAgentChatViewTransition, useAgentChatHomeHandoff, useAgentChatHomeHandoffLinks, } from "@agent-native/core/client/agent-chat"; import { useT } from "@agent-native/core/client/i18n"; import { IconMenu2 } from "@tabler/icons-react"; import { useEffect, useState } from "react"; import { useLocation, useNavigate } from "react-router"; import { Sidebar } from "@/components/layout/Sidebar"; import { Button } from "@/components/ui/button"; import { Sheet, SheetContent, SheetDescription, SheetTitle, } from "@/components/ui/sheet"; import { TAB_ID } from "@/lib/tab-id"; const SIDEBAR_COLLAPSE_KEY = "brain.sidebar.collapsed"; function readSidebarCollapsed() { if (typeof window === "undefined") return false; try { return window.localStorage.getItem(SIDEBAR_COLLAPSE_KEY) === "1"; } catch { return false; } } export function Layout({ children }: { children: React.ReactNode }) { const location = useLocation(); const navigate = useNavigate(); const t = useT(); const [mobileSidebarOpen, setMobileSidebarOpen] = useState(false); const [sidebarCollapsed, setSidebarCollapsed] = useState(readSidebarCollapsed); const isAskRoute = location.pathname === "/"; const chatHomeHandoffActive = useAgentChatHomeHandoff({ storageKey: "brain", activePath: location.pathname, enabled: !isAskRoute, }); const chatHomeHandoffPending = isAgentChatHomeHandoffActive("brain"); useAgentChatHomeHandoffLinks({ storageKey: "brain", chatPath: "/", requireActiveHandoff: true, }); useEffect(() => { setMobileSidebarOpen(false); }, [location.pathname]); useEffect(() => { try { window.localStorage.setItem( SIDEBAR_COLLAPSE_KEY, sidebarCollapsed ? "1" : "0", ); } catch { // Ignore storage failures; the in-memory preference still works. } }, [sidebarCollapsed]); const sidebarFrame = ( <>