import { memo, useEffect, useId, useState, type CSSProperties, type KeyboardEvent, type ReactNode } from 'react' import { Box, Check, ChevronLeft, ChevronRight, Cpu, Paperclip, Shield, ShieldCheck, SlidersHorizontal, Target } from 'lucide-react' import type { AgentInfo } from '@opencode-manager/shared/opencode' import { useAgents, useSession } from '@/hooks/useOpenCode' import { useContextUsage } from '@/hooks/useContextUsage' import { useVariants } from '@/hooks/useVariants' import type { ModelSelectionSession } from '@/hooks/useModelSelection' import { useMobile } from '@/hooks/useMobile' import { usePermissionModeToggle } from '@/hooks/usePermissionModeToggle' import { useSandboxToggle } from '@/hooks/useSandboxToggle' import { isLocalOpenCodeServer, useOpenCodeServerId } from '@/contexts/opencode-server-context' import { getPrimaryAgents } from '@/lib/primaryAgents' import { getAgentStyleVars } from '@/lib/agent-colors' import { shouldShowSandboxResources, formatSandboxResources } from '@/lib/sandboxResources' import { CONTEXT_USAGE_TEXT_CLASS, contextUsageAccessibleLabel, contextUsageTone, formatContextUsage, } from '@/lib/contextUsage' import { cn } from '@/lib/utils' import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover' import { BottomSheet, BottomSheetContent, BottomSheetHeader } from '@/components/ui/bottom-sheet' import { getIconToggleButtonClassName } from '@/components/ui/icon-toggle-button' import { ContextUsageMeter } from '@/components/session/ContextUsageMeter' import { ModelPicker } from '@/components/model/ModelPicker' import { SessionSandboxResourcesDialog } from './SessionSandboxResourcesDialog' interface SessionConfigMenuProps { sessionID: string directory?: string currentAgent: string isBashMode: boolean onAgentChange: (agentId: string) => void modelName: string modelSelectionSession?: ModelSelectionSession modelPickerOpen: boolean onModelPickerOpenChange: (open: boolean) => void isCompacting: boolean onCompact: () => void goalArmed: boolean goalDisabled: boolean goalLabel: string onToggleGoal: () => void onAttachFile: () => void } type SelectableAgent = Pick type SessionMenuView = 'main' | 'agent' | 'model' type SessionMenuItemRole = 'menuitem' | 'menuitemradio' | 'menuitemcheckbox' const BACK_BUTTON_CLASS = 'flex shrink-0 items-center justify-center rounded-full border border-border bg-muted text-foreground/80 hover:bg-accent' const backButtonClassName = (mobile: boolean) => cn(BACK_BUTTON_CLASS, mobile ? 'h-11 w-11' : 'h-9 w-9') const capitalize = (value: string) => value.charAt(0).toUpperCase() + value.slice(1).toLowerCase() const findAgent = (agents: SelectableAgent[], agentId: string): SelectableAgent | undefined => agents.find((agent) => agent.id.toLowerCase() === agentId.toLowerCase()) const chipClassName = (mobile: boolean) => cn( 'inline-flex shrink-0 items-center justify-center rounded-md border text-xs font-medium outline-none transition-colors focus-visible:ring-2 focus-visible:ring-ring', mobile ? 'min-h-9 px-3' : 'h-7 px-2', ) const rowClassName = (mobile: boolean) => cn( 'flex w-full min-w-0 items-center gap-2 rounded-md pl-2 pr-2 text-left text-sm text-foreground outline-none transition-colors hover:bg-accent focus:bg-accent', mobile ? 'min-h-11 py-2' : 'py-1.5', ) const menuItems = (container: HTMLElement) => Array.from(container.querySelectorAll('[role^="menuitem"]')).filter( (item) => !item.matches(':disabled') && item.getAttribute('aria-disabled') !== 'true', ) const handleMenuKeyDown = (event: KeyboardEvent) => { const items = menuItems(event.currentTarget) if (items.length === 0) return const currentIndex = items.findIndex((item) => item === document.activeElement) const lastIndex = items.length - 1 let nextIndex: number | null = null if (event.key === 'ArrowDown') { nextIndex = currentIndex < 0 ? 0 : (currentIndex + 1) % items.length } else if (event.key === 'ArrowUp') { nextIndex = currentIndex < 0 ? lastIndex : (currentIndex + items.length - 1) % items.length } else if (event.key === 'Home') { nextIndex = 0 } else if (event.key === 'End') { nextIndex = lastIndex } if (nextIndex === null) return event.preventDefault() items[nextIndex]?.focus() } interface SessionMenuItemProps { role: SessionMenuItemRole checked?: boolean disabled?: boolean onSelect: () => void className?: string style?: CSSProperties title?: string ariaLabel?: string children: ReactNode } function SessionMenuItem({ role, checked, disabled, onSelect, className, style, title, ariaLabel, children, }: SessionMenuItemProps) { return ( ) } function SwitchPill({ checked }: { checked: boolean }) { return (