import { type ReactNode } from 'react' import { IconPlus, IconSandbox, IconSmartHome } from '@tabler/icons-react' import { useQueryClient } from '@tanstack/react-query' import { Link, useLocation } from 'wouter' import { useAppConfig } from '@/client/api/app-config' import { useReorderWorkspaces, useWorkspaces } from '@/client/features/home/api' import { BROWSER_TAB_ID } from '@/client/lib/browser-tab-id' import { UpdateButton as UpdateButtonComponent, useUpdateControl } from '@/client/features/update/update-control' import { workspaceKeys } from '@/client/api/workspace-keys' import { useWorkspaceEvent } from '@/client/runtime/use-workspace-events' import { CreateWorkspaceDialog } from '@/client/features/workspace/add/create-workspace-dialog' import { useAccount } from '@/client/features/account/account-provider' import { AccountMenu } from '@/client/features/account/account-menu' import { DemoDialog } from '@/client/features/demo/demo-dialog' import { ReorderableList } from '@/client/components/shared/reorderable-list' import type { ReorderableRenderState } from '@/client/components/shared/reorderable-list' import { WorkspaceIcon } from '@/client/components/shared/workspace-icon' import { Button, buttonVariants } from '@/client/components/ui/button' import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/client/components/ui/tooltip' import { workspaceDisplayName } from '@/client/features/workspace/workspace-presentation' import { cn } from '@/client/lib/cn' import type { WorkspaceEntry } from '@/lib/types' function sidebarNavButtonClass(active: boolean): string { return cn( buttonVariants({ variant: 'ghost', size: 'icon-lg' }), active && 'bg-accent text-accent-foreground' ) } type SidebarLayoutProps = { // Full panel content — the page supplies its own header (compose it with // ) and body. children?: ReactNode showWorkspaces?: boolean } // App shell: a slim icon rail beside the page content. The page owns the panel's // header and body. export function SidebarLayout({ children, showWorkspaces = true }: SidebarLayoutProps) { const { data: workspaces } = useWorkspaces() // Identity, theme, and registry changes can all alter a workspace's sidebar // presentation. Refetch the list so the sidebar reflects them live. Exact: // `workspaceKeys.all` is the // prefix of every workspace query — a prefix invalidation would refetch // transcripts, widgets, and MCP probes in every connected client. const qc = useQueryClient() useWorkspaceEvent(e => { if ( e.type === 'workspace:updated' || e.type === 'theme:updated' || e.type === 'workspaces-list:updated' ) { // Our own icon and order changes are already in the list cache. Own theme // saves still refetch: only the layout cache holds those optimistically. if (e.type !== 'theme:updated' && e.origin === BROWSER_TAB_ID) return qc.invalidateQueries({ queryKey: workspaceKeys.all, exact: true }) } }) return (
{children}
) } type SidebarProps = { workspaces: WorkspaceEntry[] } function Sidebar({ workspaces }: SidebarProps) { const { environment } = useAppConfig() const { demo } = useAccount() const reorder = useReorderWorkspaces() return ( ) } function UpdateButton() { const updateControl = useUpdateControl() return updateControl.status === 'available' ? ( ) : null } type WorkspaceButtonProps = { workspace: WorkspaceEntry dragOverlay?: boolean dragState?: ReorderableRenderState } function WorkspaceButton({ workspace, dragOverlay = false, dragState }: WorkspaceButtonProps) { const [location] = useLocation() const href = `/workspace/${workspace.id}` const label = workspaceDisplayName(workspace) // A workspace URL carries a destination (`/workspace/:id/views/orders`), so the // rail matches the workspace segment, not the whole path. The trailing slash // keeps `/workspace/ws1` from lighting up a sibling `/workspace/ws1-other`. const active = location === href || location.startsWith(`${href}/`) const icon = (
) const labelElement = ( {label} ) if (dragOverlay) { return ( {icon} {labelElement} ) } return ( { if (dragState?.isDragging) event.preventDefault() }} className={cn( 'group flex w-14 flex-col items-center rounded-xl outline-none', dragState?.isDragging && 'invisible' )} {...dragState?.dragHandleProps} > {icon} {label} ) }