"use client"; import * as React from "react"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { ArrowLeft, PanelLeftClose, PanelLeft, Menu } from "lucide-react"; import { cva, type VariantProps } from "class-variance-authority"; import { cn } from "@/lib/utils"; import { useSidebarCollapsed } from "@/hooks/use-sidebar-collapsed"; import { useDetailSidebarCollapsed } from "@/hooks/use-detail-sidebar-collapsed"; import { useI18n } from "@/i18n/i18n-context"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "@/components/ui/tooltip"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; /** * webUI-style "detail-page" secondary sidebar. Pairs with the primary * application sidebar to give a master-detail layout for entity pages * (knowledge bases, memory stores) that have many sub-routes and rich * persistent context (description, tags, timestamps, etc.). * * The component is generic — consumers compose Header / Menu / Footer * children to fit their entity, mirroring the way webUI's `.detail-layout` * pattern is assembled around different entity types. */ export function DetailSidebar({ className, children, }: { className?: string; children: React.ReactNode; }) { const { collapsed } = useDetailSidebarCollapsed(); // When collapsed, render nothing — the parent `DetailLayout` renders a // `DetailSidebarRestoreButton` so the user can bring the sidebar back. // Returning null (rather than a zero-width aside) keeps the flex math // simple: the right pane just fills the row. if (collapsed) return null; return ( ); } /** * Floating "open detail sidebar" button shown in the right pane when the * detail sidebar is collapsed. Anchored to the top-left of the content * area so it lands roughly where the sidebar's header would have been — * matching VSCode's restore-from-collapsed pattern. */ function DetailSidebarRestoreButton() { const { toggle } = useDetailSidebarCollapsed(); const { t } = useI18n(); return ( {t("sidebar.expand")} ); } const detailIconAccent = cva( "flex h-9 w-9 items-center justify-center rounded-lg [&>svg]:h-[18px] [&>svg]:w-[18px]", { variants: { accent: { primary: "bg-[hsl(var(--primary-soft))] text-[hsl(var(--primary))]", success: "bg-[hsl(var(--success-soft))] text-[hsl(var(--success))]", warning: "bg-[hsl(var(--warning-soft))] text-[hsl(var(--warning))]", danger: "bg-[hsl(var(--danger-soft))] text-[hsl(var(--danger))]", purple: "bg-[hsl(var(--purple-soft))] text-[hsl(var(--purple))]", }, }, defaultVariants: { accent: "primary", }, }, ); interface DetailSidebarHeaderProps extends VariantProps< typeof detailIconAccent > { icon: React.ReactNode; name: React.ReactNode; typeLabel?: React.ReactNode; description?: React.ReactNode; actions?: React.ReactNode; /** Status pills (multimodal / subspace / etc.) rendered as their own * row directly below the icon — kept separate from `name` so they * get their own line and don't crowd the title row. */ pills?: React.ReactNode; /** Tags / chips / dates / extra info. Wrapped in a flex container with * small text already applied. */ extra?: React.ReactNode; } export function DetailSidebarHeader({ icon, accent, name, typeLabel, description, actions, pills, extra, }: DetailSidebarHeaderProps) { const { toggle } = useDetailSidebarCollapsed(); const { t } = useI18n(); return (
{/* Identity row: accent icon on the left, the name + optional type label in the middle (truncates with ellipsis when too long), and consumer actions plus the built-in "collapse sidebar" button on the right. Status pills sit on their own row underneath so they can wrap freely without squeezing the title. */}
{icon}
{typeof name === "string" ? (
{name}
{name}
) : (
{name}
)} {typeLabel ? (
{typeLabel}
) : null}
{/* Right-side controls: consumer actions (delete, etc.) plus a built-in "collapse sidebar" button anchored at the far edge. */}
{actions} {t("sidebar.collapseSidebar")}
{pills ?
{pills}
: null} {description ? (

{description}

) : null} {extra ? (
{extra}
) : null}
); } export interface DetailSidebarMenuItem { /** Full URL including any search params; passed straight to next/link. */ href: string; label: React.ReactNode; icon?: React.ReactNode; /** Optional badge or count rendered on the right edge. */ endAdornment?: React.ReactNode; active?: boolean; children?: DetailSidebarMenuItem[]; } export interface DetailSidebarMenuGroup { label: string; items: DetailSidebarMenuItem[]; } export function DetailSidebarMenu({ header, items = [], groups, className, onNavigate, }: { header?: React.ReactNode; items?: DetailSidebarMenuItem[]; groups?: DetailSidebarMenuGroup[]; className?: string; onNavigate?: () => void; }) { const pathname = usePathname() ?? ""; function renderItems(entries: DetailSidebarMenuItem[], nested = false) { return ( ); } return ( ); } /** The same entity navigation stays reachable when the desktop sidebar is hidden. */ export function DetailSidebarMobile({ header, groups, title, className, }: { header?: React.ReactNode; className?: string; groups: DetailSidebarMenuGroup[]; title: string; }) { const [open, setOpen] = React.useState(false); const { t } = useI18n(); return (
{title} setOpen(false)} />
); } interface DetailSidebarBackProps { href: string; label?: React.ReactNode; } export function DetailSidebarBack({ href, label }: DetailSidebarBackProps) { const { t } = useI18n(); return (
{label ?? t("sidebar.backToList")}
); } /** * Master-detail shell. Uses the "fixed shell, internal scroll" pattern that * Slack / Linear / Notion use for chrome-heavy apps: the layout pins to * the viewport with `position: fixed` so it stays exactly one viewport * tall regardless of `
`'s padding or any sibling page content. The * sidebar fills the full available height as a static column; only the * right content area scrolls. * * The fixed positioning sidesteps the body-scroll / negative-margin math * that an in-flow shell would have to do — `
` keeps its standard * `min-h-screen + px-7 py-5`, body never overflows past the viewport, and * the back button stays anchored to the bottom of the viewport even when * the right pane's content is shorter than one screen. * * Left offset mirrors the primary sidebar's collapsed state via * `useSidebarCollapsed`, keeping the master-detail row in lockstep with * the rest of the application chrome. */ /** * Generic full-viewport application shell. Pins to the viewport with * `position: fixed`, accounts for the primary sidebar's collapse state, * and reserves the mobile primary-nav bar's 56px above. Used as the * foundation for any page that wants a chrome-heavy "app surface" feel * — the master-detail layout, the chat homepage, etc. */ export function AppShellLayout({ children, className, }: { children: React.ReactNode; className?: string; }) { const { collapsed } = useSidebarCollapsed(); return (
{children}
); } export function DetailLayout({ sidebar, children, }: { sidebar: React.ReactNode; children: React.ReactNode; }) { const { collapsed } = useDetailSidebarCollapsed(); return ( {sidebar} {/* Wrap the right pane in a `relative` so the restore button can pin to its top-left without scrolling away with the content. Splitting the previous single `flex-1 overflow-y-auto` into a `relative` outer + an inner `absolute inset-0` scroll container achieves this without changing the visible layout for the expanded case. */}
{collapsed ? : null}
{children}
); } /** * Cancels the surrounding right-pane padding so a child component can * span the full height/width of the detail layout. Used by workbench- * style pages (split form + workspace) that own their internal padding * and scroll regions and don't want to inherit the page-level inset. * * The negative `-mx-7 -my-5` matches the right pane's `px-7 py-5` * padding so the cancellation is exact; mobile uses px-4 / no extra * vertical padding so we only cancel the horizontal axis there. * * `h-full` plus `overflow-hidden` forces the child to be exactly one * pane tall — no body-scroll fall-back — so workbench panes can lay * out flex columns with internal scroll without overflowing the shell. */ export function DetailFullBleed({ className, children, }: { className?: string; children: React.ReactNode; }) { return (
{children}
); } interface WorkbenchLayoutProps { /** Left configuration column (form / parameters). Scrolls internally. */ aside: React.ReactNode; /** Right work surface (chat / results). Owns its own scroll. */ children: React.ReactNode; /** Width of the aside on desktop. Defaults to 360px. */ asideWidth?: number; } /** * Two-pane workbench shell for detail-page subroutes that want a fixed * configuration column on the left and a full-height work surface on the * right (QA test, retrieval test, memory search). On viewports below the * `lg` breakpoint the configuration stacks above the workspace instead * of sitting beside it. * * Wraps with `DetailFullBleed` so the panes go edge-to-edge — pages don't * need to think about the surrounding right-pane padding. */ export function WorkbenchLayout({ aside, children, asideWidth = 360, }: WorkbenchLayoutProps) { return (
{children}
); }