import { ChevronRight, Folder, FolderOpen, Server } from 'lucide-react'; import React, { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { useI18n } from '../../application/i18n/I18nProvider'; import { hostTreeInlineGroupEditStore, useHostTreeInlineGroupEdit, } from '../../application/state/hostTreeInlineGroupEditStore'; import { useHostTreeInlineHostEdit } from '../../application/state/hostTreeInlineHostEditStore'; import { useVaultHostTreeActions } from '../../application/state/vaultHostTreeActionsStore'; import { TERMINAL_HOST_TREE_DEFAULT_WIDTH, TERMINAL_HOST_TREE_MAX_WIDTH, TERMINAL_HOST_TREE_MIN_WIDTH, terminalHostTreeStore, useTerminalHostTreeOpen, } from '../../application/state/terminalHostTreeStore'; import { TERMINAL_HOST_TREE_ANIMATION_EASING, TERMINAL_HOST_TREE_ANIMATION_MS, } from '../../application/state/terminalHostTreeAnimation'; import { scheduleChromeLayoutAnimation } from '../../application/state/useActiveChromeTheme'; import { terminalLayoutSuppressStore } from '../../application/state/terminalLayoutSuppressStore'; import { useStoredNumber } from '../../application/state/useStoredNumber'; import { useTreeExpandedState } from '../../application/state/useTreeExpandedState'; import { applyHostLabelRename } from '../../domain/host'; import { ensureAncestorPathsExpanded } from '../../domain/hostGroupPathMutations'; import { buildHostGroupTree, collectGroupTreePaths } from '../../domain/hostGroupTree'; import { flattenHostGroupTree, hostTreeFlatRowContainsHost, hostTreeFlatRowKey, type HostTreeFlatRow, } from '../../domain/hostGroupTreeFlat'; import { STORAGE_KEY_TERMINAL_HOST_TREE_WIDTH, STORAGE_KEY_VAULT_HOSTS_TREE_EXPANDED, } from '../../infrastructure/config/storageKeys'; import { cn } from '../../lib/utils'; import type { GroupNode, Host, TerminalTheme } from '../../types'; import { HostTreeGroupContextMenuContent, HostTreeHostContextMenuContent } from '../host/HostTreeContextMenus'; import { HostTreeGroupInlineRenameInput } from '../host/HostTreeGroupInlineRenameInput'; import { MessageResponse } from '../ai-elements/message'; import { DistroAvatar } from '../DistroAvatar'; import { ContextMenu, ContextMenuTrigger } from '../ui/context-menu'; import { HoverCard, HoverCardContent, HoverCardTrigger } from '../ui/hover-card'; import { TREE_ROW_HEIGHT } from '../sftp/SftpPaneTreeNode'; import { FixedSizeVirtualList, type FixedSizeVirtualListHandle } from '../ui/FixedSizeVirtualList'; import { TerminalHostTreeToolbar, type HostTreeToolbarPanel, } from './TerminalHostTreeToolbar'; const SIDEBAR_MIN_WIDTH = TERMINAL_HOST_TREE_MIN_WIDTH; const SIDEBAR_DEFAULT_WIDTH = TERMINAL_HOST_TREE_DEFAULT_WIDTH; const SIDEBAR_MAX_WIDTH = TERMINAL_HOST_TREE_MAX_WIDTH; const HOST_TREE_DRAG_HOST_ID = 'host-id'; const HOST_TREE_DRAG_GROUP_PATH = 'group-path'; type HostTreeDropTarget = | { kind: 'root' } | { kind: 'group'; path: string }; interface TerminalHostTreeSidebarProps { enabled?: boolean; surfaceVisible?: boolean; hosts: Host[]; customGroups: string[]; resolvedPreviewTheme: TerminalTheme; activeHostId?: string | null; onConnect: (host: Host) => void; onCreateLocalTerminal?: () => void; } type HostTreeTheme = { termBg: string; termFg: string; mutedFg: string; separator: string; rowHoverBg: string; rowActiveBg: string; rowDropBg: string; folderFg: string; }; export function isTerminalHostTreeSidebarVisible( isOpen: boolean, enabled = true, surfaceVisible = true, ): boolean { return surfaceVisible && enabled && isOpen; } export function getTerminalHostTreeSidebarShellStyle( isVisible: boolean, layoutWidth: number, shellTransition: string, ): React.CSSProperties { return { width: layoutWidth, transition: shellTransition, pointerEvents: isVisible ? 'auto' : 'none', }; } export function getTerminalHostTreeSidebarPanelStyle({ isVisible, displayWidth, panelTransition, theme, }: { isVisible: boolean; displayWidth: number; panelTransition: string; theme: HostTreeTheme; }): React.CSSProperties { return { width: displayWidth, opacity: 1, transition: panelTransition, backgroundColor: theme.termBg, color: theme.termFg, borderRight: isVisible ? `1px solid ${theme.separator}` : '1px solid transparent', }; } export function getTerminalHostTreeLayoutTargetWidth(isVisible: boolean, displayWidth: number): number { return isVisible ? displayWidth : 0; } export function getTerminalHostTreeInitialLayoutWidth(): number { return 0; } export const applyTerminalHostTreeHostRename = applyHostLabelRename; export function shouldShowTerminalHostHoverCard( hoveredHostId: string | null, editingHostId: string | null, ): boolean { return Boolean(hoveredHostId) && hoveredHostId !== editingHostId; } export function getTerminalHostTreeMeasuredLayoutWidth( element: Pick | null, fallbackWidth: number, ): number { const measuredWidth = element?.getBoundingClientRect().width; return typeof measuredWidth === 'number' && Number.isFinite(measuredWidth) ? Math.max(0, measuredWidth) : Math.max(0, fallbackWidth); } function hostMatchesSearch(host: Host, search: string): boolean { const s = search.toLowerCase(); return ( host.label.toLowerCase().includes(s) || host.hostname.toLowerCase().includes(s) || host.tags.some((tag) => tag.toLowerCase().includes(s)) || (host.notes?.toLowerCase().includes(s) ?? false) ); } function filterGroupNode( node: GroupNode, search: string, preservePaths?: ReadonlySet, ): GroupNode | null { const matchingHosts = search ? node.hosts.filter((host) => hostMatchesSearch(host, search)) : node.hosts; const childNodes = Object.values(node.children) .map((child) => filterGroupNode(child as GroupNode, search, preservePaths)) .filter((child): child is GroupNode => child !== null); if (!search) return node; if (preservePaths?.has(node.path) || matchingHosts.length > 0 || childNodes.length > 0) { return { ...node, hosts: matchingHosts, children: Object.fromEntries(childNodes.map((child) => [child.name, child])), }; } return null; } function pruneEmptyGroupNode( node: GroupNode, preservePaths?: ReadonlySet, ): GroupNode | null { const childNodes = Object.values(node.children) .map((child) => pruneEmptyGroupNode(child as GroupNode, preservePaths)) .filter((child): child is GroupNode => child !== null); if (preservePaths?.has(node.path) || node.hosts.length > 0 || childNodes.length > 0) { return { ...node, children: Object.fromEntries(childNodes.map((child) => [child.name, child])), }; } return null; } const TerminalHostTreeHostHoverCard: React.FC<{ host: Host }> = ({ host }) => { const { t } = useI18n(); const protocol = host.protocol || 'ssh'; const port = host.port ?? 22; const username = host.username?.trim(); const notes = host.notes?.trim(); const rows = [ [t('terminal.layer.hostTree.details.host'), host.hostname], [t('terminal.layer.hostTree.details.user'), username], [t('terminal.layer.hostTree.details.port'), String(port)], [t('terminal.layer.hostTree.details.protocol'), protocol.toUpperCase()], [t('terminal.layer.hostTree.details.group'), host.group], [t('terminal.layer.hostTree.details.tags'), host.tags?.join(', ')], ].filter((entry): entry is [string, string] => Boolean(entry[1])); return (
{host.label}
{rows.map(([label, value]) => (
{label} {value}
))}
{notes && (
{t('hostDetails.notes.label')}
{notes}
)}
); }; type HostTreeFlatRowProps = { row: HostTreeFlatRow; activeHostId?: string | null; expandedPaths: Set; searchActive: boolean; canDrag: boolean; isDragOver: boolean; isInlineEditing: boolean; inlineEditInitialName?: string; onConnect: (host: Host) => void; onTogglePath: (path: string) => void; onDragOverTarget: (target: HostTreeDropTarget) => void; onDragLeaveRow: (event: React.DragEvent) => void; onDropToParent: (targetParent: string | null, dataTransfer: DataTransfer) => void; theme: HostTreeTheme; menuActions: ReturnType; }; const HostTreeFlatRowItem = memo(({ row, activeHostId, expandedPaths, searchActive, canDrag, isDragOver, isInlineEditing, inlineEditInitialName, onConnect, onTogglePath, onDragOverTarget, onDragLeaveRow, onDropToParent, theme, menuActions, }) => { if (row.kind === 'host') { const isActive = activeHostId === row.host.id; const hostDropParent = row.host.group || null; const canShowHoverCard = shouldShowTerminalHostHoverCard( row.host.id, isInlineEditing ? row.host.id : null, ); const rowBody = (
{ if (!canDrag || isInlineEditing) return; event.dataTransfer.setData(HOST_TREE_DRAG_HOST_ID, row.host.id); event.dataTransfer.effectAllowed = 'move'; }} onDragOver={(event) => { if (!canDrag) return; event.preventDefault(); event.stopPropagation(); onDragOverTarget(hostDropParent ? { kind: 'group', path: hostDropParent } : { kind: 'root' }); }} onDragLeave={onDragLeaveRow} onDrop={(event) => { if (!canDrag) return; event.preventDefault(); event.stopPropagation(); onDropToParent(hostDropParent, event.dataTransfer); }} onMouseEnter={(event) => { if (!isActive && !isDragOver) event.currentTarget.style.backgroundColor = theme.rowHoverBg; }} onMouseLeave={(event) => { if (!isActive && !isDragOver) event.currentTarget.style.backgroundColor = ''; }} onDoubleClick={() => { if (!isInlineEditing) onConnect(row.host); }} onKeyDown={(event) => { if (isInlineEditing) return; if (event.key === 'Enter' || event.key === ' ') { event.preventDefault(); onConnect(row.host); } }} > {isInlineEditing && menuActions && inlineEditInitialName ? ( ) : ( {row.host.label} )} {row.host.protocol && row.host.protocol !== 'ssh' && ( {row.host.protocol} )}
); if (!menuActions) return rowBody; return ( {rowBody} {canShowHoverCard && } ); } const { node, depth } = row; const isExpanded = searchActive || expandedPaths.has(node.path); const hasChildren = Object.keys(node.children).length > 0; const hasHosts = node.hosts.length > 0; const isManaged = menuActions?.managedGroupPaths?.has(node.path) ?? false; const rowBody = (
{ if (!canDrag || isInlineEditing) return; event.dataTransfer.setData(HOST_TREE_DRAG_GROUP_PATH, node.path); event.dataTransfer.effectAllowed = 'move'; }} onDragOver={(event) => { if (!canDrag) return; event.preventDefault(); event.stopPropagation(); onDragOverTarget({ kind: 'group', path: node.path }); }} onDragLeave={onDragLeaveRow} onDrop={(event) => { if (!canDrag) return; event.preventDefault(); event.stopPropagation(); onDropToParent(node.path, event.dataTransfer); }} onMouseEnter={(event) => { if (!isDragOver) event.currentTarget.style.backgroundColor = theme.rowHoverBg; }} onMouseLeave={(event) => { if (!isDragOver) event.currentTarget.style.backgroundColor = ''; }} onClick={() => { if (isInlineEditing) return; onTogglePath(node.path); }} onKeyDown={(event) => { if (isInlineEditing) return; if (event.key === 'Enter' || event.key === ' ') { event.preventDefault(); onTogglePath(node.path); } }} > {(hasChildren || hasHosts) && ( )} {isExpanded ? : } {isInlineEditing && menuActions && inlineEditInitialName ? ( ) : ( {node.name} )}
); if (!menuActions) return rowBody; return ( {rowBody} ); }, (prev, next) => { if (prev.row !== next.row) return false; if (prev.expandedPaths !== next.expandedPaths) return false; if (prev.searchActive !== next.searchActive) return false; if (prev.canDrag !== next.canDrag) return false; if (prev.isDragOver !== next.isDragOver) return false; if (prev.isInlineEditing !== next.isInlineEditing) return false; if (prev.inlineEditInitialName !== next.inlineEditInitialName) return false; if (prev.theme !== next.theme) return false; if (prev.menuActions !== next.menuActions) return false; if (prev.onDragOverTarget !== next.onDragOverTarget) return false; if (prev.onDragLeaveRow !== next.onDragLeaveRow) return false; if (prev.onDropToParent !== next.onDropToParent) return false; if (prev.onTogglePath !== next.onTogglePath) return false; if (prev.onConnect !== next.onConnect) return false; if (prev.activeHostId === next.activeHostId) return true; if (prev.row.kind === 'host') { return prev.row.host.id !== prev.activeHostId && prev.row.host.id !== next.activeHostId; } const affectsRow = hostTreeFlatRowContainsHost(prev.row, prev.activeHostId) || hostTreeFlatRowContainsHost(prev.row, next.activeHostId); return !affectsRow; }); HostTreeFlatRowItem.displayName = 'HostTreeFlatRowItem'; const TerminalHostTreeSidebarInner: React.FC = ({ enabled = true, surfaceVisible = true, hosts, customGroups, resolvedPreviewTheme, activeHostId, onConnect, onCreateLocalTerminal, }) => { const { t } = useI18n(); const isOpen = useTerminalHostTreeOpen(); const isVisible = isTerminalHostTreeSidebarVisible(isOpen, enabled, surfaceVisible); const [search, setSearch] = useState(''); const [selectedTags, setSelectedTags] = useState([]); const [expandedPanel, setExpandedPanel] = useState(null); const [isResizing, setIsResizing] = useState(false); const [resizePreviewWidth, setResizePreviewWidth] = useState(null); const [dragOverTarget, setDragOverTarget] = useState(null); const shellRef = useRef(null); const [sidebarWidth, setSidebarWidth, persistSidebarWidth] = useStoredNumber( STORAGE_KEY_TERMINAL_HOST_TREE_WIDTH, SIDEBAR_DEFAULT_WIDTH, { min: SIDEBAR_MIN_WIDTH, max: SIDEBAR_MAX_WIDTH }, ); const { expandedPaths, togglePath, ensurePathExpanded, expandAll, collapseAll } = useTreeExpandedState( STORAGE_KEY_VAULT_HOSTS_TREE_EXPANDED, ); const menuActions = useVaultHostTreeActions(); const inlineEdit = useHostTreeInlineGroupEdit(); const inlineHostEdit = useHostTreeInlineHostEdit(); const listRef = useRef(null); const theme = useMemo(() => { const termBg = resolvedPreviewTheme.colors.background; const termFg = resolvedPreviewTheme.colors.foreground; return { termBg, termFg, mutedFg: `color-mix(in srgb, ${termFg} 55%, ${termBg} 45%)`, separator: `color-mix(in srgb, ${termFg} 10%, ${termBg} 90%)`, rowHoverBg: `color-mix(in srgb, ${termFg} 8%, transparent)`, rowActiveBg: `color-mix(in srgb, ${termFg} 14%, transparent)`, rowDropBg: `color-mix(in srgb, ${termFg} 20%, transparent)`, folderFg: `color-mix(in srgb, ${termFg} 75%, ${termBg} 25%)`, }; }, [resolvedPreviewTheme]); const allTags = useMemo(() => { const tags = new Set(); for (const host of hosts) { for (const tag of host.tags ?? []) { tags.add(tag); } } return Array.from(tags).sort((a, b) => a.localeCompare(b)); }, [hosts]); const tagFilteredHosts = useMemo(() => { if (selectedTags.length === 0) return hosts; return hosts.filter((host) => selectedTags.some((tag) => host.tags?.includes(tag))); }, [hosts, selectedTags]); const { groupTree, ungroupedHosts } = useMemo( () => buildHostGroupTree(tagFilteredHosts, customGroups), [tagFilteredHosts, customGroups], ); const searchTerm = search.trim(); const searchActive = searchTerm.length > 0; const tagsActive = selectedTags.length > 0; const treeExpandAll = searchActive || tagsActive; const preservePaths = useMemo(() => { if (!inlineEdit?.groupPath) return undefined; return new Set([inlineEdit.groupPath]); }, [inlineEdit?.groupPath]); const filteredTree = useMemo(() => { let tree = groupTree; if (tagsActive) { tree = tree .map((node) => pruneEmptyGroupNode(node, preservePaths)) .filter((node): node is GroupNode => node !== null); } if (!searchActive) return tree; return tree .map((node) => filterGroupNode(node, searchTerm, preservePaths)) .filter((node): node is GroupNode => node !== null); }, [groupTree, preservePaths, searchActive, searchTerm, tagsActive]); const filteredUngrouped = useMemo(() => { if (!searchActive) return ungroupedHosts; return ungroupedHosts.filter((host) => hostMatchesSearch(host, searchTerm)); }, [searchActive, searchTerm, ungroupedHosts]); const flatRows = useMemo(() => { return flattenHostGroupTree({ groupNodes: filteredTree, ungroupedHosts: filteredUngrouped, expandedPaths, searchActive: treeExpandAll, }); }, [expandedPaths, filteredTree, filteredUngrouped, treeExpandAll]); const canDrag = Boolean(menuActions) && !searchActive && !tagsActive; const handleNewRootGroup = useCallback(() => { if (!menuActions) return; setSearch(''); setSelectedTags([]); setExpandedPanel(null); menuActions.onNewGroup(); }, [menuActions]); useEffect(() => { if (!inlineEdit?.isNew || !inlineEdit.groupPath) return; const parentPath = inlineEdit.groupPath.split('/').filter(Boolean).slice(0, -1).join('/'); if (!parentPath) return; ensureAncestorPathsExpanded(parentPath, ensurePathExpanded); }, [ensurePathExpanded, inlineEdit?.groupPath, inlineEdit?.isNew]); const handleCreateLocalTerminal = useCallback(() => { onCreateLocalTerminal?.(); }, [onCreateLocalTerminal]); const allGroupPaths = useMemo(() => collectGroupTreePaths(groupTree), [groupTree]); const handleExpandAll = useCallback(() => { expandAll(allGroupPaths); }, [allGroupPaths, expandAll]); const handleCollapseAll = useCallback(() => { collapseAll(); }, [collapseAll]); const canExpandCollapse = allGroupPaths.length > 0 && !searchActive && !tagsActive; const handleCollapse = useCallback(() => { terminalHostTreeStore.setIsOpen(false); }, []); const clearDragOver = useCallback(() => { setDragOverTarget(null); }, []); const handleDropToParent = useCallback((targetParent: string | null, dataTransfer: DataTransfer) => { if (!menuActions) return; const hostId = dataTransfer.getData(HOST_TREE_DRAG_HOST_ID); const groupPath = dataTransfer.getData(HOST_TREE_DRAG_GROUP_PATH); if (hostId) menuActions.moveHostToGroup(hostId, targetParent); if (groupPath) menuActions.moveGroup(groupPath, targetParent); clearDragOver(); }, [clearDragOver, menuActions]); const handleDragOverTarget = useCallback((target: HostTreeDropTarget) => { setDragOverTarget(target); }, []); const handleDragLeaveRow = useCallback((event: React.DragEvent) => { const nextTarget = event.relatedTarget; if (nextTarget instanceof Node && event.currentTarget.contains(nextTarget)) { return; } clearDragOver(); }, [clearDragOver]); const handleRootDragOver = useCallback((event: React.DragEvent) => { if (!canDrag) return; event.preventDefault(); setDragOverTarget({ kind: 'root' }); }, [canDrag]); const handleRootDragLeave = useCallback((event: React.DragEvent) => { const nextTarget = event.relatedTarget; if (nextTarget instanceof Node && event.currentTarget.contains(nextTarget)) { return; } clearDragOver(); }, [clearDragOver]); const handleRootDrop = useCallback((event: React.DragEvent) => { if (!canDrag) return; event.preventDefault(); handleDropToParent(null, event.dataTransfer); }, [canDrag, handleDropToParent]); const handleListPointerDownCapture = useCallback((event: React.PointerEvent) => { if (!menuActions) return; const editingGroupPath = inlineEdit?.groupPath; const editingHostId = inlineHostEdit?.hostId; if (!editingGroupPath && !editingHostId) return; const target = event.target; if (!(target instanceof Element)) return; if (target.closest('[data-inline-group-edit="true"]')) return; const row = target.closest('[data-section="terminal-host-tree-sidebar-row"]'); if (!row) return; if (editingGroupPath && row.getAttribute('data-group-path') === editingGroupPath) return; if (editingHostId && row.getAttribute('data-host-id') === editingHostId) return; if (editingGroupPath) menuActions.cancelInlineGroupEdit(); if (editingHostId) menuActions.cancelInlineHostEdit(); }, [inlineEdit?.groupPath, inlineHostEdit?.hostId, menuActions]); useEffect(() => { if (!inlineEdit?.shouldScrollIntoView || !inlineEdit.isNew) return; const index = flatRows.findIndex( (row) => row.kind === 'group' && row.node.path === inlineEdit.groupPath, ); if (index < 0) return; const frame = requestAnimationFrame(() => { listRef.current?.scrollToIndex(index, 'center'); hostTreeInlineGroupEditStore.markScrollHandled(); }); return () => cancelAnimationFrame(frame); }, [expandedPaths, flatRows, inlineEdit]); const isRowDragOver = useCallback((row: HostTreeFlatRow) => { if (!dragOverTarget) return false; if (dragOverTarget.kind === 'root') { return row.kind === 'host' && !row.host.group; } if (row.kind === 'group') { return dragOverTarget.path === row.node.path; } return Boolean(row.host.group && row.host.group === dragOverTarget.path); }, [dragOverTarget]); const renderFlatRow = useCallback((row: HostTreeFlatRow) => ( ), [ activeHostId, canDrag, expandedPaths, inlineEdit, inlineHostEdit, handleDragLeaveRow, handleDragOverTarget, handleDropToParent, isRowDragOver, menuActions, onConnect, treeExpandAll, theme, togglePath, ]); const shellTransition = isResizing || !surfaceVisible ? 'none' : `width ${TERMINAL_HOST_TREE_ANIMATION_MS}ms ${TERMINAL_HOST_TREE_ANIMATION_EASING}`; const panelTransition = isResizing ? 'none' : `border-color ${TERMINAL_HOST_TREE_ANIMATION_MS}ms ease-out`; const handleResizeStart = useCallback((event: React.MouseEvent) => { if (!isVisible) return; event.preventDefault(); setIsResizing(true); terminalLayoutSuppressStore.begin(); const startX = event.clientX; const startWidth = sidebarWidth; let rafId: number | null = null; const onMouseMove = (moveEvent: MouseEvent) => { const next = Math.max( SIDEBAR_MIN_WIDTH, Math.min(SIDEBAR_MAX_WIDTH, startWidth + moveEvent.clientX - startX), ); if (rafId !== null) return; rafId = requestAnimationFrame(() => { rafId = null; setResizePreviewWidth(next); }); }; const onMouseUp = (upEvent: MouseEvent) => { if (rafId !== null) cancelAnimationFrame(rafId); const next = Math.max( SIDEBAR_MIN_WIDTH, Math.min(SIDEBAR_MAX_WIDTH, startWidth + upEvent.clientX - startX), ); setSidebarWidth(next); persistSidebarWidth(next); setResizePreviewWidth(null); setIsResizing(false); terminalLayoutSuppressStore.end(); window.removeEventListener('mousemove', onMouseMove); window.removeEventListener('mouseup', onMouseUp); }; window.addEventListener('mousemove', onMouseMove); window.addEventListener('mouseup', onMouseUp); }, [isVisible, persistSidebarWidth, setSidebarWidth, sidebarWidth]); const displayWidth = resizePreviewWidth ?? sidebarWidth; const targetLayoutWidth = getTerminalHostTreeLayoutTargetWidth(isVisible, displayWidth); const [shellWidth, setShellWidth] = useState(getTerminalHostTreeInitialLayoutWidth); const cancelSyncLayoutWidthRef = useRef<(() => void) | null>(null); const prevIsVisibleRef = useRef(isVisible); const syncLayoutWidthFromShell = useCallback((fallbackWidth = targetLayoutWidth) => { terminalHostTreeStore.setLayoutWidth( getTerminalHostTreeMeasuredLayoutWidth(shellRef.current, fallbackWidth), ); }, [targetLayoutWidth]); useEffect(() => { const el = shellRef.current; if (!el) return; const ro = new ResizeObserver(() => { if (isResizing) return; syncLayoutWidthFromShell(); }); ro.observe(el); syncLayoutWidthFromShell(); return () => ro.disconnect(); }, [isResizing, syncLayoutWidthFromShell]); useEffect(() => { if (prevIsVisibleRef.current === isVisible) return; prevIsVisibleRef.current = isVisible; const el = shellRef.current; if (!el) return; terminalLayoutSuppressStore.begin(); let ended = false; const finish = () => { if (ended) return; ended = true; terminalLayoutSuppressStore.end(); }; const onTransitionEnd = (event: TransitionEvent) => { if (event.target !== el || event.propertyName !== 'width') return; syncLayoutWidthFromShell(); finish(); }; el.addEventListener('transitionend', onTransitionEnd); const timer = window.setTimeout(() => { syncLayoutWidthFromShell(); finish(); }, TERMINAL_HOST_TREE_ANIMATION_MS + 80); return () => { el.removeEventListener('transitionend', onTransitionEnd); window.clearTimeout(timer); finish(); }; }, [isVisible, syncLayoutWidthFromShell]); useEffect(() => { const el = shellRef.current; if (!el) return; const onTransitionEnd = (event: TransitionEvent) => { if (event.target !== el || event.propertyName !== 'width' || isResizing) return; syncLayoutWidthFromShell(); }; el.addEventListener('transitionend', onTransitionEnd); return () => el.removeEventListener('transitionend', onTransitionEnd); }, [isResizing, syncLayoutWidthFromShell]); useEffect(() => { cancelSyncLayoutWidthRef.current?.(); cancelSyncLayoutWidthRef.current = null; if (isResizing) { setShellWidth(targetLayoutWidth); terminalHostTreeStore.setLayoutWidth(targetLayoutWidth); return; } if (!surfaceVisible) { setShellWidth(0); terminalHostTreeStore.setLayoutWidth(0); return; } syncLayoutWidthFromShell(); cancelSyncLayoutWidthRef.current = scheduleChromeLayoutAnimation(() => { cancelSyncLayoutWidthRef.current = null; setShellWidth(targetLayoutWidth); }); return () => { cancelSyncLayoutWidthRef.current?.(); cancelSyncLayoutWidthRef.current = null; }; }, [isResizing, surfaceVisible, syncLayoutWidthFromShell, targetLayoutWidth]); return (
{isVisible && (
)}
{flatRows.length === 0 ? (
{t('terminal.layer.hostTree.empty')}
) : ( ref={listRef} items={flatRows} itemHeight={TREE_ROW_HEIGHT} className="[scrollbar-width:none] [-ms-overflow-style:none] [&::-webkit-scrollbar]:hidden" contentClassName="py-0" getItemKey={hostTreeFlatRowKey} renderItem={renderFlatRow} /> )}
); }; export const TerminalHostTreeSidebar = memo( TerminalHostTreeSidebarInner, (prev, next) => ( prev.hosts === next.hosts && prev.enabled === next.enabled && prev.surfaceVisible === next.surfaceVisible && prev.customGroups === next.customGroups && prev.resolvedPreviewTheme === next.resolvedPreviewTheme && prev.activeHostId === next.activeHostId && prev.onConnect === next.onConnect && prev.onCreateLocalTerminal === next.onCreateLocalTerminal ), ); TerminalHostTreeSidebar.displayName = 'TerminalHostTreeSidebar';