/* eslint-disable @typescript-eslint/no-explicit-any */ import React, { memo } from 'react'; import { TerminalServerStats } from './TerminalServerStats'; type TerminalViewContext = Record; /** * Shallow-compare every ctx value. rebuilds the ctx object on every * render, but many re-renders (layout/fit/visibility-of-other-panes, suppress * toggles) don't actually change any value passed to the view — notably * `paneLayoutKey`/`isResizing` are consumed by Terminal's hooks and are NOT in * this ctx. Without this memo, every Terminal re-render re-rendered the whole * (expensive) TerminalView. This only skips when EVERY value is referentially * equal, so it can never render stale UI. */ function terminalViewCtxEqual( prev: { ctx: TerminalViewContext }, next: { ctx: TerminalViewContext }, ): boolean { const a = prev.ctx; const b = next.ctx; if (a === b) return true; const aKeys = Object.keys(a); if (aKeys.length !== Object.keys(b).length) return false; for (const key of aKeys) { if (a[key] !== b[key]) return false; } return true; } function TerminalViewInner({ ctx }: { ctx: TerminalViewContext }) { const { Activity, Button, Copy, Maximize2, Radio, Sparkles, TerminalAutocomplete, TerminalComposeBar, TerminalConnectionDialog, TerminalContextMenu, TerminalSearchBar, Tooltip, TooltipContent, TooltipTrigger, ZmodemOverwriteDialog, ZmodemProgressIndicator, auth, autocompleteAcceptTextRef, autocompleteCloseRef, autocompleteHostOs, autocompleteInputRef, autocompleteKeyEventRef, autocompleteRepositionRef, autocompleteSettings, chainProgress, cn, compactToolbar, containerRef, effectiveTheme, error, executeSnippet, executeSnippetCommand, handleAddSelectionToAI, handleCancelConnect, handleCloseDisconnectedSession, handleCloseSearch, handleDismissDisconnectedDialog, handleDragEnter, handleDragLeave, handleDragOver, handleDrop, handleFindNext, handleFindPrevious, handleHostKeyAddAndContinue, handleHostKeyClose, handleHostKeyContinue, handleOsc52ReadResponse, handleRetry, handleSearch, handleTopOverlayMouseDownCapture, hasMouseTracking, hasSelection, host, hotkeyScheme, inWorkspace, isBroadcastEnabled, isCancelling, isComposeBarOpen, isDraggingOver, isFocusMode, isLocalConnection, isSearchOpen, isSupportedOs, isSystemSidebarEligible, isVisible, keyBindings, keys, knownCwdRef, needsHostKeyVerification, onCloseSession, onExpandToFocus, onOpenSystem, onSplitHorizontal, onSplitVertical, onToggleBroadcast, osc52ReadPromptVisible, pendingHostKeyInfo, progressLogs, progressValue, renderControls, searchMatchCount, selectionOverlayPosition, sessionId, sessionRef, setIsComposeBarOpen, setShowLogs, shouldShowConnectionDialog, showLogs, snippets, status, statusDotTone, sudoHintRef, sudoHintText, t, termRef, terminalContextActions, terminalCwdTracker, terminalPreviewVars, terminalSettings, timeLeft, toast, zmodem } = ctx; return ( onCloseSession?.(sessionId) : undefined} onAddSelectionToAI={ctx.onAddSelectionToAI ? handleAddSelectionToAI : undefined} >
{/* Drag and drop overlay */} {isDraggingOver && (
{isLocalConnection ? t("terminal.dragDrop.localTitle") : t("terminal.dragDrop.remoteTitle") }
{isLocalConnection ? t("terminal.dragDrop.localMessage") : t("terminal.dragDrop.remoteMessage") }
)}
{host.label} {host.protocol !== "local" && host.hostname && host.hostname !== "localhost" && ( {t("terminal.statusbar.copyHostname.tooltip", { hostname: host.hostname })} )} {isSystemSidebarEligible && ( {t("terminal.layer.system")} )}
{!compactToolbar && ( )}
{inWorkspace && onToggleBroadcast && ( {isBroadcastEnabled ? t("terminal.toolbar.broadcastDisable") : t("terminal.toolbar.broadcastEnable")} )} {inWorkspace && !isFocusMode && onExpandToFocus && ( {t("terminal.toolbar.focusMode")} )} {renderControls({ showClose: inWorkspace })}
{isSearchOpen && (
)}
{hasSelection && selectionOverlayPosition && ctx.onAddSelectionToAI && handleAddSelectionToAI && (
{t("terminal.selection.addToAIDesc")}
)} {/* Autocomplete — owns the hook + popup in its own component so suggestion/selection updates don't re-render Terminal. Mounted unconditionally; it gates the popup on `visible` internally. */} terminalCwdTracker.getRendererCwd() ?? knownCwdRef.current} onAcceptText={(text) => autocompleteAcceptTextRef.current?.(text)} snippets={snippets} onAcceptSnippet={(snippet) => void executeSnippet(snippet)} themeColors={effectiveTheme.colors} containerRef={containerRef} searchBarOffset={isSearchOpen ? 64 : 30} keyEventRef={autocompleteKeyEventRef} inputRef={autocompleteInputRef} repositionRef={autocompleteRepositionRef} closeRef={autocompleteCloseRef} sudoHintRef={sudoHintRef} sudoHintText={sudoHintText} /> {/* OSC-52 clipboard read prompt */} {osc52ReadPromptVisible && (
{ if (e.key === 'Escape') handleOsc52ReadResponse(false); }} >

{t("terminal.osc52.readPrompt.title")}

{t("terminal.osc52.readPrompt.desc")}

)} {/* Connection dialog: skip for local/serial during connecting phase, but show on error */} {shouldShowConnectionDialog && ( auth.submit(), onSubmitWithoutSave: () => auth.submit({ saveToHost: false }), onCancel: handleCancelConnect, isValid: auth.isValid, }} progressProps={{ timeLeft, isCancelling, progressLogs, onCancelConnect: handleCancelConnect, onCloseSession: handleCloseDisconnectedSession, onRetry: handleRetry, }} /> )} {/* ZMODEM transfer progress indicator */} {zmodem.active && (
)} {/* ZMODEM overwrite conflict dialog */} {zmodem.overwriteRequest && ( )}
{/* Compose Bar (solo sessions only; workspace uses TerminalLayer's global bar) */} {isComposeBarOpen && !inWorkspace && ( { if (sessionRef.current) { executeSnippetCommand(text, false); } }} onSnippetClick={(snippet) => void executeSnippet(snippet)} snippets={snippets} onClose={() => { setIsComposeBarOpen(false); termRef.current?.focus(); }} isBroadcastEnabled={isBroadcastEnabled} themeColors={effectiveTheme.colors} /> )}
); } export const TerminalView = memo(TerminalViewInner, terminalViewCtxEqual); TerminalView.displayName = 'TerminalView';