import { ArrowDownToLine, Bot, BotOff, Check, Coffee, Copy, Maximize2, SquareTerminal, Star, X, type LucideIcon } from 'lucide-react' import { createContext, useContext, useState, type ReactNode } from 'react' import { copyText } from '../../lib/clipboard.ts' import { cn } from '../../lib/utils.ts' export type TerminalAffordances = { hover?: boolean actions?: boolean labels?: boolean } const DEFAULTS: Required = { hover: true, actions: true, labels: false } const AffordanceContext = createContext>(DEFAULTS) export function useAffordances(): Required { return useContext(AffordanceContext) } const RESOLVED = new Map>() // One object per combination: the result is a context value, and a fresh one re-renders every action on every commit. export function resolveAffordances(value: TerminalAffordances | boolean | undefined): Required { if (value === true || value === undefined) { return DEFAULTS } const { hover, actions, labels } = value === false ? { hover: false, actions: false, labels: false } : { ...DEFAULTS, ...value } const key = `${hover}${actions}${labels}` let resolved = RESOLVED.get(key) if (!resolved) { resolved = { hover, actions, labels } RESOLVED.set(key, resolved) } return resolved } export function AffordanceProvider({ value, children }: { value: Required; children: ReactNode }) { return {children} } export type ActionPlacement = 'below' | 'inline' const PlacementContext = createContext('below') export function ActionPlacementProvider({ value, children }: { value: ActionPlacement; children: ReactNode }) { return {children} } export function WithActions({ actions, children, className, placement, }: { actions: ReactNode children: ReactNode className?: string placement?: ActionPlacement }) { const { actions: enabled } = useAffordances() const inherited = useContext(PlacementContext) if (!enabled) { return <>{children} } return (
{children}
{actions}
) } type Tone = 'magenta' | 'yellow' | 'red' | 'danger' function ActionButton({ icon: Icon, word, label, tone, pressed, filled, onPress, }: { icon: LucideIcon word: string label: string tone?: Tone pressed?: boolean filled?: boolean onPress: () => void }) { const { labels } = useAffordances() return ( ) } // Bookmarks are a host concern (which items, where they persist); the transcript only needs // membership and a toggle. A missing provider renders no action at all. export type BookmarkHandle = { has: (itemId: string) => boolean toggle: (itemId: string) => void } const BookmarkContext = createContext(undefined) export function BookmarkProvider({ value, children }: { value: BookmarkHandle | undefined; children: ReactNode }) { return {children} } export function BookmarkAction({ id }: { id: string }) { const handle = useContext(BookmarkContext) if (!handle) { return null } const active = handle.has(id) return ( handle.toggle(id)} /> ) } // Present only where the engine can move a running tool call to the background (Claude's Ctrl+B). export type TaskControlHandle = { background: (toolUseId?: string) => void } const TaskControlContext = createContext(undefined) export function TaskControlProvider({ value, children }: { value: TaskControlHandle | undefined; children: ReactNode }) { return {children} } export function useTaskControl(): TaskControlHandle | undefined { return useContext(TaskControlContext) } export function BackgroundAction({ toolUseId }: { toolUseId: string }) { const handle = useContext(TaskControlContext) if (!handle) { return null } return ( handle.background(toolUseId)} /> ) } export function OpenSubagentAction({ onOpen, label = 'Open sub-agent' }: { onOpen: () => void; label?: string }) { return } export function OpenShellAction({ onOpen, label = 'Open terminal' }: { onOpen: () => void; label?: string }) { return } export function KillShellAction({ onKill, label = 'Kill this shell' }: { onKill: () => void; label?: string }) { return } export function AgentWriteAction({ granted, label, onToggle }: { granted: boolean; label: string; onToggle: () => void }) { return ( ) } export function KeepAwakeAction({ held, label, onToggle }: { held: boolean; label: string; onToggle: () => void }) { return ( ) } export function CopyAction({ text, label = 'Copy' }: { text: string; label?: string }) { const [copied, setCopied] = useState(false) return ( { void copyText(text).then((ok) => { if (!ok) { return } setCopied(true) setTimeout(() => setCopied(false), 1200) }) }} /> ) } export function AgentWriteIcon({ granted, className }: { granted: boolean; className?: string }) { const Icon = granted ? Bot : BotOff return }