/* eslint-disable @typescript-eslint/no-explicit-any */ import React from "react"; import { HostNotesIndicator } from "../host/HostNotesIndicator"; import { VirtualizedGroupedHostCollection, VirtualizedHostCollection, } from "./VirtualizedHostCollection"; import { VaultEntityIcon, vaultPrimaryIconClass } from "./VaultEntityIcon"; import { clearVaultDropIndicator, getVaultDropIntent, getVaultDropPosition, hasVaultDragType, handleVaultHostDropToGroup, handleVaultRootDrop, markVaultDropIndicator, useVaultGridLayoutAnimation, } from "./vaultReorderDrag"; import { hostCardFocusClassName, isHostClickFocusSelected, resolveGroupActivateAction, resolveHostActivateAction, shouldClearHostFocusOnBackgroundClick, type HostClickBehavior, } from "../../domain/hostClickBehavior"; import type { GroupNode, Host } from "../../domain/models"; import { isPluginHostProtocol } from "../../domain/pluginConnection"; import { OpenDualPaneSftpMenuItem, OpenHostBrowserMenuItem } from "../host/HostTreeContextMenus"; import { VAULT_HOST_DRAG_IDS, isVaultHostDraggable } from "../../domain/vaultHostDrag"; import { getVaultHostRowSubtitle } from "../../domain/localShellHost"; import { beginVaultHostCardDrag } from "./vaultHostDragImage"; import { useVaultHostMarquee } from "./useVaultHostMarquee"; type VaultHostListSectionContext = Record; export const getVaultTreeAutoExpandKey = ( search: string | undefined, selectedTags: string[] | undefined, ): string | undefined => { const normalizedSearch = search?.trim() ?? ""; const normalizedTags = [...(selectedTags ?? [])].sort(); return normalizedSearch || normalizedTags.length > 0 ? JSON.stringify([normalizedSearch, normalizedTags]) : undefined; }; const isRelatedTargetInside = ( currentTarget: HTMLElement, relatedTarget: EventTarget | null, ) => { return ( typeof Node !== "undefined" && relatedTarget instanceof Node && currentTarget.contains(relatedTarget) ); }; const EMPTY_GROUP_PATH_SET = new Set(); const EMPTY_HOST_ID_SET: ReadonlySet = new Set(); const noopSetMultiSelectMode = (_enabled: boolean) => {}; const noopSetSelectedHostIds = (_ids: Set) => {}; export function VaultHostListSection({ ctx }: { ctx: VaultHostListSectionContext }) { const { Badge, Boolean, Button, cancelInlineGroupEdit, CheckSquare, ClipboardCopy, Clock, cn, commitInlineGroupRename, ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger, Copy, displayedGroups, displayedHosts, DistroAvatar, Edit2, FileSymlink, FolderPlus, FolderTree, getDropTargetClasses, getEffectiveHostDistro, groupConfigs, groupedDisplayHosts, handleCopyCredentials, handleCopyHostname, handleDuplicateHost, handleEditGroupConfig, handleEditHost, handleHostConnect, handleOpenHostBrowser, hostClickBehavior: hostClickBehaviorProp, handleUnmanageGroup, hasHostsSidePanel, hostListScrollRef, HostTreeView, isHostsSectionActive, isMultiSelectMode, lastPinnedId, LayoutGrid, managedGroupPaths, moveGroup, moveHostToGroup, moveHostsToGroup, onDeleteHost, Pin, pinnedHosts, Plug, recentHosts, reorderGroup, reorderHost, sanitizeHost, search, selectedGroupPath, selectedGroupPaths, selectedHostIds, selectedTags, sessionCount, setDeleteTargetPath, setDragOverDropTarget, setGroupDragOverDropTarget, setIsDeleteGroupOpen, setIsMultiSelectMode = noopSetMultiSelectMode, setIsNewFolderOpen, setLastPinnedId, setNewFolderName, setSelectedGroupPath, setSelectedHostIds = noopSetSelectedHostIds, setTargetParentPath, shouldHideEmptyRootHostsSection, showRecentHosts, sortMode, Square, Star, startInlineDeleteGroup, startInlineNewGroup, startInlineRenameGroup, t, toggleGroupSelection, toggleHostPinned, toggleHostSelection, Trash2, treeExpandedState, treeViewGroupTree, treeViewHosts, viewMode, visibleDisplayedHosts } = ctx; const hostClickBehavior: HostClickBehavior = hostClickBehaviorProp === 'select' ? 'select' : 'connect'; const multiSelectedGroupPaths: Set = selectedGroupPaths ?? EMPTY_GROUP_PATH_SET; const [draggingHostIds, setDraggingHostIds] = React.useState | null>(null); const draggingHostIdRef = React.useRef(null); const lastPreviewReorderRef = React.useRef(null); const prepareGridLayoutAnimation = useVaultGridLayoutAnimation(hostListScrollRef); const [focusedHostId, setFocusedHostId] = React.useState(null); const [focusedGroupPath, setFocusedGroupPath] = React.useState(null); const hostListFilterFocusKey = React.useMemo( () => getVaultTreeAutoExpandKey(search, selectedTags) ?? "", [search, selectedTags], ); const [prevHostListFilterFocusKey, setPrevHostListFilterFocusKey] = React.useState( hostListFilterFocusKey, ); // Clear keyboard/selection focus as soon as search or tags change so the // virtual list cannot steal DOM focus back from the search input on the // same commit (useEffect would run too late). if (hostListFilterFocusKey !== prevHostListFilterFocusKey) { setPrevHostListFilterFocusKey(hostListFilterFocusKey); setFocusedHostId(null); setFocusedGroupPath(null); } const hostCollectionLayoutKey = [ displayedGroups.length, hasHostsSidePanel ? "panel" : "full", lastPinnedId ?? "", pinnedHosts.length, recentHosts.length, selectedGroupPath ?? "root", showRecentHosts ? "recent" : "hidden", sortMode, viewMode, ].join("|"); // Stable wrappers so HostTreeView memo can skip parent vault re-renders. const handleTreeDeleteHost = React.useCallback( (host: Host) => { onDeleteHost(host.id); }, [onDeleteHost], ); const handleTreeGroupDropClasses = React.useCallback( (path: string) => getDropTargetClasses({ kind: "group", path }), [getDropTargetClasses], ); const treeAutoExpandGroupsKey = React.useMemo( () => getVaultTreeAutoExpandKey(search, selectedTags), [search, selectedTags], ); React.useEffect(() => { if (isMultiSelectMode) { setFocusedHostId(null); setFocusedGroupPath(null); } }, [isMultiSelectMode]); React.useEffect(() => { setFocusedHostId(null); setFocusedGroupPath(null); }, [selectedGroupPath, viewMode, hostClickBehavior]); const { marqueeRect, onPointerDown: onMarqueePointerDown, onClickCapture: onMarqueeClickCapture, } = useVaultHostMarquee({ containerRef: hostListScrollRef, viewMode, isMultiSelectMode: Boolean(isMultiSelectMode), selectedHostIds: selectedHostIds ?? EMPTY_HOST_ID_SET, setIsMultiSelectMode, setSelectedHostIds, }); const activateHost = React.useCallback((host: Host) => { const action = resolveHostActivateAction({ behavior: hostClickBehavior, isMultiSelectMode, focusedHostId, hostId: host.id, }); if (action === "toggle-multi") { toggleHostSelection(host.id); return; } if (action === "select") { setFocusedHostId(host.id); setFocusedGroupPath(null); return; } handleHostConnect(host); }, [focusedHostId, handleHostConnect, hostClickBehavior, isMultiSelectMode, toggleHostSelection]); const focusHost = React.useCallback((host: Host) => { setFocusedHostId(host.id); setFocusedGroupPath(null); }, []); const mainKeyboardHosts = groupedDisplayHosts ? groupedDisplayHosts.flatMap((group) => group.hosts) : visibleDisplayedHosts; const focusHostAndElement = (host: Host) => { focusHost(host); queueMicrotask(() => { const scrollElement = hostListScrollRef.current as HTMLElement | null; const element = [...(scrollElement?.querySelectorAll("[data-host-id]") ?? [])] .find((candidate) => candidate.dataset.hostId === host.id); element?.focus(); }); }; const focusGroupAndElement = (group: GroupNode) => { setFocusedGroupPath(group.path); setFocusedHostId(null); queueMicrotask(() => { const scrollElement = hostListScrollRef.current as HTMLElement | null; const element = [...(scrollElement?.querySelectorAll("[data-group-path]") ?? [])] .find((candidate) => candidate.dataset.groupPath === group.path); element?.focus(); }); }; const keyboardHostSections = [ { key: "pinned", hasItems: pinnedHosts.length > 0, focusEdge: (direction: "previous" | "next") => focusHostAndElement( direction === "next" ? pinnedHosts[0] : pinnedHosts.at(-1)!, ), }, { key: "recent", hasItems: showRecentHosts && recentHosts.length > 0, focusEdge: (direction: "previous" | "next") => focusHostAndElement( direction === "next" ? recentHosts[0] : recentHosts.at(-1)!, ), }, { key: "groups", hasItems: displayedGroups.length > 0, focusEdge: (direction: "previous" | "next") => focusGroupAndElement( direction === "next" ? displayedGroups[0] : displayedGroups.at(-1)!, ), }, { key: "main", hasItems: mainKeyboardHosts.length > 0, focusEdge: (direction: "previous" | "next") => focusHostAndElement( direction === "next" ? mainKeyboardHosts[0] : mainKeyboardHosts.at(-1)!, ), }, ]; const navigateHostSection = ( sectionKey: "pinned" | "recent" | "groups" | "main", direction: "previous" | "next", ) => { const currentSectionIndex = keyboardHostSections.findIndex((section) => section.key === sectionKey); const step = direction === "next" ? 1 : -1; let sectionIndex = currentSectionIndex + step; while (sectionIndex >= 0 && sectionIndex < keyboardHostSections.length) { const section = keyboardHostSections[sectionIndex]; if (section.hasItems) { section.focusEdge(direction); return; } sectionIndex += step; } }; const initialKeyboardHostId = pinnedHosts[0]?.id ?? (showRecentHosts ? recentHosts[0]?.id : undefined) ?? groupedDisplayHosts?.[0]?.hosts[0]?.id ?? visibleDisplayedHosts[0]?.id; const focusedHostIsVisible = Boolean( focusedHostId && [ ...pinnedHosts, ...(showRecentHosts ? recentHosts : []), ...mainKeyboardHosts, ].some((host) => host.id === focusedHostId), ); const getHostTabIndex = (hostId: string) => ( (focusedHostIsVisible ? focusedHostId === hostId : initialKeyboardHostId === hostId) ? 0 : -1 ); const initialKeyboardGroupPath = displayedGroups[0]?.path; const focusedGroupIsVisible = Boolean( focusedGroupPath && displayedGroups.some((group) => group.path === focusedGroupPath), ); const getGroupTabIndex = (groupPath: string) => ( (focusedGroupIsVisible ? focusedGroupPath === groupPath : initialKeyboardGroupPath === groupPath) ? 0 : -1 ); const isHostFocusSelected = (hostId: string) => ( isHostClickFocusSelected({ behavior: hostClickBehavior, isMultiSelectMode, focusedHostId, hostId, }) ); const isGroupFocusSelected = (groupPath: string) => ( hostClickBehavior === "select" && !isMultiSelectMode && focusedGroupPath === groupPath ); const activateGroup = React.useCallback((groupPath: string) => { if (isMultiSelectMode) { toggleGroupSelection(groupPath); return; } const action = resolveGroupActivateAction({ behavior: hostClickBehavior, focusedGroupPath, groupPath, }); if (action === "select") { setFocusedGroupPath(groupPath); setFocusedHostId(null); return; } setSelectedGroupPath(groupPath); }, [focusedGroupPath, hostClickBehavior, isMultiSelectMode, setSelectedGroupPath, toggleGroupSelection]); const handleHostListClick = React.useCallback((event: React.MouseEvent) => { const target = event.target; const clickedWithinHostList = target instanceof Node && event.currentTarget.contains(target); const clickedHostOrGroup = target instanceof Element && !!target.closest("[data-host-id], [data-group-path]"); if (!shouldClearHostFocusOnBackgroundClick({ behavior: hostClickBehavior, isMultiSelectMode, clickedWithinHostList, clickedHostOrGroup, })) return; setFocusedHostId(null); setFocusedGroupPath(null); }, [hostClickBehavior, isMultiSelectMode]); const resetHostDragState = React.useCallback(() => { draggingHostIdRef.current = null; setDraggingHostIds(null); lastPreviewReorderRef.current = null; setDragOverDropTarget(null); }, [setDragOverDropTarget]); const handleHostDragStart = React.useCallback((e: React.DragEvent, hostId: string) => { const ids = beginVaultHostCardDrag({ dataTransfer: e.dataTransfer, hostId, isMultiSelectMode: !!isMultiSelectMode, selectedHostIds, countLabel: (count) => t("vault.hosts.dragSelectionCount", { count }), }); if (!ids) { e.preventDefault(); return; } draggingHostIdRef.current = hostId; setDraggingHostIds(new Set(ids)); lastPreviewReorderRef.current = null; // Grid preview reorder can move the card into another virtual row, and rows // are separate React parents, so the source node gets unmounted mid-drag. // A detached node no longer bubbles dragend up to the container handler, so // bind the cleanup natively on the node itself - it still receives dragend. const sourceNode = e.currentTarget as HTMLElement; const handleNativeDragEnd = () => { sourceNode.removeEventListener("dragend", handleNativeDragEnd); clearVaultDropIndicator(); resetHostDragState(); }; sourceNode.addEventListener("dragend", handleNativeDragEnd); }, [isMultiSelectMode, resetHostDragState, selectedHostIds, t]); const renderHostEditButton = (host: any, compact = false) => ( ); const renderGroupEditButton = (groupPath: string, compact = false) => ( ); return
:not([data-vault-marquee])~:not([data-vault-marquee])]:mt-3", viewMode === "tree" ? "pt-1.5" : "pt-0", !isHostsSectionActive && "hidden", marqueeRect && "select-none", )} data-section="vault-host-list" onClick={handleHostListClick} onClickCapture={onMarqueeClickCapture} onPointerDown={onMarqueePointerDown} onDragOverCapture={(e) => { const target = (e.target as Element | null)?.closest("[data-host-id], [data-group-path]"); if (hasVaultDragType(e.dataTransfer, VAULT_HOST_DRAG_IDS)) { const groupTarget = target instanceof HTMLElement && target.hasAttribute("data-group-path"); if (!groupTarget) return; } if (target) e.preventDefault(); if (!(target instanceof HTMLElement)) return; const draggedGroupPath = e.dataTransfer.getData("group-path"); const isDraggingGroup = hasVaultDragType(e.dataTransfer, "group-path"); const targetGroupPath = target.getAttribute("data-group-path"); if (isDraggingGroup && targetGroupPath && draggedGroupPath !== targetGroupPath) { const intent = getVaultDropIntent(target, e.clientX, e.clientY, viewMode === "grid"); if (intent === "inside") { clearVaultDropIndicator(); return; } markVaultDropIndicator(target, intent, viewMode === "grid" ? "x" : "y"); return; } if (viewMode !== "grid") { markVaultDropIndicator(target, getVaultDropPosition(target, e.clientX, e.clientY)); return; } const draggedHostId = draggingHostIdRef.current || e.dataTransfer.getData("host-id"); const targetHostId = target.getAttribute("data-host-id"); if (!draggedHostId || !targetHostId || draggedHostId === targetHostId) return; const position = getVaultDropPosition(target, e.clientX, e.clientY, true); const previewKey = `${draggedHostId}:${targetHostId}:${position}`; if (lastPreviewReorderRef.current === previewKey) return; prepareGridLayoutAnimation(); lastPreviewReorderRef.current = previewKey; reorderHost(draggedHostId, targetHostId, position); }} onDragOver={(e) => { const target = (e.target as Element | null)?.closest("[data-host-id], [data-group-path]"); if (hasVaultDragType(e.dataTransfer, VAULT_HOST_DRAG_IDS)) { const groupTarget = target instanceof HTMLElement && target.hasAttribute("data-group-path"); if (!groupTarget) return; } if (!(target instanceof HTMLElement) || viewMode === "grid") return; const draggedGroupPath = e.dataTransfer.getData("group-path"); const isDraggingGroup = hasVaultDragType(e.dataTransfer, "group-path"); const targetGroupPath = target.getAttribute("data-group-path"); if (isDraggingGroup && targetGroupPath && draggedGroupPath !== targetGroupPath) { const intent = getVaultDropIntent(target, e.clientX, e.clientY, false); if (intent === "inside") { clearVaultDropIndicator(); return; } markVaultDropIndicator(target, intent); return; } markVaultDropIndicator(target, getVaultDropPosition(target, e.clientX, e.clientY)); }} onDropCapture={(e) => { clearVaultDropIndicator(); const draggedHostId = e.dataTransfer.getData("host-id"); const draggedGroupPath = e.dataTransfer.getData("group-path"); const target = (e.target as Element | null)?.closest("[data-host-id], [data-group-path]"); // Always clear the dimmed drag styling: in grid view the live preview // reorder can leave the dragged card itself under the cursor, which // used to fall through every branch below without resetting. if (draggedHostId) resetHostDragState(); if (!(target instanceof HTMLElement)) return; const targetHostId = target.getAttribute("data-host-id"); const targetGroupPath = target.getAttribute("data-group-path"); if ( draggedHostId && targetHostId && draggedHostId !== targetHostId && !hasVaultDragType(e.dataTransfer, VAULT_HOST_DRAG_IDS) ) { e.preventDefault(); e.stopPropagation(); const position = getVaultDropPosition(target, e.clientX, e.clientY, viewMode === "grid"); const previewKey = `${draggedHostId}:${targetHostId}:${position}`; if (viewMode !== "grid" || lastPreviewReorderRef.current !== previewKey) { prepareGridLayoutAnimation(); reorderHost(draggedHostId, targetHostId, position); } resetHostDragState(); return; } if (draggedGroupPath && targetGroupPath && draggedGroupPath !== targetGroupPath) { const intent = getVaultDropIntent(target, e.clientX, e.clientY, viewMode === "grid"); if (intent === "inside") return; prepareGridLayoutAnimation(); const handled = reorderGroup(draggedGroupPath, targetGroupPath, intent); if (handled) { e.preventDefault(); e.stopPropagation(); } } }} onDragEndCapture={() => { clearVaultDropIndicator(); resetHostDragState(); }} > {marqueeRect && ( ; }