"use client"; import { createContext, useCallback, useContext, useEffect, useState } from "react"; import Link from "next/link"; import { usePathname, useRouter } from "next/navigation"; import { PanelLeftCloseIcon, PanelLeftOpenIcon } from "lucide-react"; import { Button } from "@/components/ui/button"; import { cn } from "@/lib/utils"; import type { ChatSummary } from "@/src/store/chats"; /** * Toggleable sidebar — NOT a modal drawer: no backdrop, no focus theft. It * sits inline next to the content and can stay open while working in a * session. Open state persists across pages (provider lives in the root * layout) and across reloads (localStorage). */ const STORAGE_KEY = "lh:sidebar-open"; const SidebarContext = createContext<{ open: boolean; toggle: () => void }>({ open: false, toggle: () => {}, }); export function useSidebar() { return useContext(SidebarContext); } export function SidebarProvider({ children }: { children: React.ReactNode }) { // Start closed so server and client first-render agree (no hydration // mismatch); the stored value is applied after mount. const [open, setOpen] = useState(false); const [mounted, setMounted] = useState(false); useEffect(() => { const stored = localStorage.getItem(STORAGE_KEY); // Default: open (first run introduces the sidebar). setOpen(stored === null ? true : stored === "1"); setMounted(true); }, []); const toggle = useCallback(() => { setOpen((v) => { localStorage.setItem(STORAGE_KEY, v ? "0" : "1"); return !v; }); }, []); return ( {/* transitions gated on mount: no slide-in flash on page load */}
{children}
); } /** Hamburger for page headers — only visible when the sidebar is closed. */ export function SidebarToggle() { const { open, toggle } = useSidebar(); if (open) return null; return ( ); } function SessionsSidebar() { const { open, toggle } = useSidebar(); const [chats, setChats] = useState(null); const pathname = usePathname(); const router = useRouter(); useEffect(() => { if (!open) return; let cancelled = false; fetch("/api/chats", { cache: "no-store" }) .then((r) => r.json()) .then((d: { chats: ChatSummary[] }) => !cancelled && setChats(d.chats)) .catch(() => !cancelled && setChats([])); return () => { cancelled = true; }; }, [open, pathname]); return ( ); } /** The app frame: sidebar inline + content. Rendered once in the root layout. */ export function AppShell({ children }: { children: React.ReactNode }) { return ( <>
{children}
); }