import { useState, type ReactNode } from "react"; import { toast } from 'sonner'; import { sendToTerminal } from '@/lib/events'; import { useTerminals } from '@/hooks/useTerminals'; import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuSeparator, ContextMenuSub, ContextMenuSubContent, ContextMenuSubTrigger, ContextMenuTrigger, } from '@/components/ui/context-menu'; // Wrap a message body with a right-click context menu offering Copy and // "Send to terminal → ". Send is disabled when nothing is // selected or no terminal panes are open; clicking a target emits a // sendToTerminal event that TerminalPane subscribes to (filtered by pane_id). export function SendToTerminalMenu({ children }: { children: ReactNode }) { const [selection, setSelection] = useState(''); const terminals = useTerminals(); const hasSelection = selection.length > 0; const canSend = hasSelection && terminals.length > 0; return ( { if (open) { const sel = typeof window !== 'undefined' ? window.getSelection()?.toString() ?? '' : ''; setSelection(sel); } }} > {children} { void navigator.clipboard.writeText(selection).catch((err) => { toast.error(err instanceof Error ? err.message : 'copy failed'); }); }} > Copy Send to terminal {terminals.length === 0 ? ( No terminal panes open ) : ( terminals.map((t) => ( sendToTerminal.emit({ pane_id: t.paneId, text: selection })} > {t.label} )) )} ); }