/** * SftpView - SFTP File Browser (Refactored) * * This is the main SFTP view component that provides a dual-pane file browser * for transferring files between local and remote systems. * * Components have been extracted to: * - components/sftp/utils.ts - Utility functions * - components/sftp/SftpBreadcrumb.tsx - Path navigation * - components/sftp/SftpFileRow.tsx - File list row * - components/sftp/SftpTransferItem.tsx - Transfer queue item * - components/sftp/SftpConflictDialog.tsx - Conflict resolution * - components/sftp/SftpPermissionsDialog.tsx - Permissions editor * - components/sftp/SftpHostPicker.tsx - Host selection dialog */ import React, { memo, useCallback, useEffect, useLayoutEffect, useMemo, useRef } from "react"; import { useI18n } from "../application/i18n/I18nProvider"; import { useIsSftpActive } from "../application/state/activeTabStore"; import { useSftpState } from "../application/state/useSftpState"; import { useSftpBackend } from "../application/state/useSftpBackend"; import { HotkeyScheme, KeyBinding } from "../domain/models"; import { logger } from "../lib/logger"; import { useRenderTracker } from "../lib/useRenderTracker"; import { cn } from "../lib/utils"; import { useInstantThemeSwitch } from "../lib/useInstantThemeSwitch"; import { Host, Identity, ProxyProfile, SSHKey } from "../types"; import { resolveGroupDefaults, applyGroupDefaults } from "../domain/groupConfig"; import { materializeHostProxyProfile } from "../domain/proxyProfiles"; import { useSftpFileAssociations } from "../application/state/useSftpFileAssociations"; import { registerEditorSftpWriterScoped } from "../application/state/editorSftpBridge"; import { toast } from "./ui/toast"; // Import extracted components import { SftpTabBar } from "./sftp"; import { SftpPaneView, SftpPaneWrapper } from "./sftp/SftpPaneView"; import { SftpOverlays } from "./sftp/SftpOverlays"; import { Loader2 } from "lucide-react"; // Import context hooks import { SftpContextProvider, activeTabStore } from "./sftp"; import { useSftpViewPaneCallbacks } from "./sftp/hooks/useSftpViewPaneCallbacks"; import { useSftpViewTabs } from "./sftp/hooks/useSftpViewTabs"; import { useSftpKeyboardShortcuts } from "./sftp/hooks/useSftpKeyboardShortcuts"; import { sftpFocusStore, SftpFocusedSide, useSftpFocusedSide } from "./sftp/hooks/useSftpFocusedPane"; import { keepOnlyActivePaneSelections, keepOnlyPaneSelections } from "./sftp/hooks/selectionScope"; // Wrapper component that subscribes to activeTabId for CSS visibility // This isolates the activeTabId subscription - only this component re-renders on tab switch // Uses visibility:hidden pattern from App.tsx for smooth tab switching // Main SftpView component interface SftpViewProps { hosts: Host[]; keys: SSHKey[]; identities: Identity[]; groupConfigs?: import('../domain/models').GroupConfig[]; proxyProfiles?: ProxyProfile[]; updateHosts: (hosts: Host[]) => void; sftpDefaultViewMode: "list" | "tree"; sftpDoubleClickBehavior: "open" | "transfer"; sftpAutoSync: boolean; sftpShowHiddenFiles: boolean; sftpUseCompressedUpload: boolean; hotkeyScheme: HotkeyScheme; keyBindings: KeyBinding[]; editorWordWrap: boolean; setEditorWordWrap: (enabled: boolean) => void; terminalSettings?: { keepaliveInterval: number; keepaliveCountMax: number }; } const SftpViewInner: React.FC = ({ hosts, keys, identities, groupConfigs = [], proxyProfiles = [], updateHosts, sftpDefaultViewMode, sftpDoubleClickBehavior, sftpAutoSync, sftpShowHiddenFiles, sftpUseCompressedUpload, hotkeyScheme, keyBindings, editorWordWrap, setEditorWordWrap, terminalSettings, }) => { const { t } = useI18n(); const isActive = useIsSftpActive(); const rootRef = useRef(null); const dialogActionScopeIdRef = useRef("sftp-main-view"); useInstantThemeSwitch(rootRef); // File watch event handlers (stable refs to avoid re-creating the useSftpState options) const fileWatchHandlers = useMemo(() => ({ onFileWatchSynced: (payload: { remotePath: string }) => { const fileName = payload.remotePath.split('/').pop() || payload.remotePath; toast.success(t('sftp.autoSync.success', { fileName })); logger.info("[SFTP] File auto-synced to remote", payload); }, onFileWatchError: (payload: { error: string }) => { toast.error(t('sftp.autoSync.error', { error: payload.error })); logger.error("[SFTP] File auto-sync failed", payload); }, }), [t]); const sftpOptions = useMemo(() => ({ ...fileWatchHandlers, useCompressedUpload: sftpUseCompressedUpload, defaultShowHiddenFiles: sftpShowHiddenFiles, terminalSettings, }), [fileWatchHandlers, sftpUseCompressedUpload, sftpShowHiddenFiles, terminalSettings]); // Pre-resolve group defaults so SFTP connections inherit group config const effectiveHosts = useMemo(() => { const validProxyProfileIds = new Set(proxyProfiles.map((profile) => profile.id)); return hosts.map(h => { const withGroupDefaults = h.group ? applyGroupDefaults(h, resolveGroupDefaults(h.group, groupConfigs, { validProxyProfileIds }), { validProxyProfileIds }) : applyGroupDefaults(h, {}, { validProxyProfileIds }); return materializeHostProxyProfile(withGroupDefaults, proxyProfiles); }); }, [hosts, groupConfigs, proxyProfiles]); const sftp = useSftpState(effectiveHosts, keys, identities, sftpOptions); // Get backend helpers for file downloads and local filesystem writes. const { showSaveDialog, selectDirectory, startStreamTransfer, listSftp, mkdirLocal, deleteLocalFile, listLocalDir, listDrives, } = useSftpBackend(); // Store sftp in a ref so callbacks can access the latest instance // without needing to re-create when sftp changes const sftpRef = useRef(sftp); sftpRef.current = sftp; // Register this useSftpState's writeTextFileByConnection with the bridge so // the editor tab's save path can reach the active SFTP session. The bridge // supports multiple simultaneous writers (SftpSidePanel inside terminals // also registers its own instance) and dispatches by trying each until one // owns the target connectionId. // // Intentionally no deps: `sftp` identity churns on every SFTP state change // (transfers, pane updates, tab switches), which would make this effect // unregister+reregister constantly. Route through sftpRef so the closure // always reads the latest writeTextFileByConnection; that method is stable // across sftp re-renders (it's a methodsRef-backed dispatcher). useEffect(() => { return registerEditorSftpWriterScoped((connectionId, expectedHostId, filePath, content, encoding) => sftpRef.current.writeTextFileByConnection(connectionId, expectedHostId, filePath, content, encoding), ); }, []); // Store behavior setting in ref for stable callbacks const behaviorRef = useRef(sftpDoubleClickBehavior); behaviorRef.current = sftpDoubleClickBehavior; // Store auto-sync setting in ref for stable callbacks const autoSyncRef = useRef(sftpAutoSync); autoSyncRef.current = sftpAutoSync; // SFTP keyboard shortcuts handler useSftpKeyboardShortcuts({ keyBindings, hotkeyScheme, sftpRef, dialogActionScopeId: dialogActionScopeIdRef.current, isActive, }); // Subscribe to focused side for visual indicator const focusedSide = useSftpFocusedSide(); // Handle pane focus when clicking on a pane container // Clear the opposite side's selection so file operations only affect the focused pane const handlePaneFocus = useCallback((side: SftpFocusedSide, targetTabId?: string) => { const prevSide = sftpFocusStore.getFocusedSide(); sftpFocusStore.setFocusedSide(side); if (prevSide !== side) { if (targetTabId) { keepOnlyPaneSelections(sftpRef.current, { side, tabId: targetTabId }); } else { // Focus side changed — clear other panes but keep the newly focused pane intact. keepOnlyActivePaneSelections(sftpRef.current, side); } } }, []); const handleToggleHiddenFiles = useCallback((side: "left" | "right", paneId: string) => { const sideTabs = side === "left" ? sftpRef.current.leftTabs : sftpRef.current.rightTabs; const pane = sideTabs.tabs.find((tab) => tab.id === paneId); if (!pane) return; sftpRef.current.setShowHiddenFiles(side, paneId, !pane.showHiddenFiles); }, []); // Sync activeTabId to external store (allows child components to subscribe without parent re-render) // Using useLayoutEffect to sync before paint useLayoutEffect(() => { activeTabStore.setActiveTabId("left", sftp.leftTabs.activeTabId); }, [sftp.leftTabs.activeTabId]); useLayoutEffect(() => { activeTabStore.setActiveTabId("right", sftp.rightTabs.activeTabId); }, [sftp.rightTabs.activeTabId]); // 渲染追踪 - 不追踪 activeTabId(现在通过 store 订阅) useRenderTracker("SftpViewInner", { isActive, hostsCount: hosts.length, leftTabsCount: sftp.leftTabs.tabs.length, rightTabsCount: sftp.rightTabs.tabs.length, }); const { getOpenerForFile, setOpenerForExtension } = useSftpFileAssociations(); const getOpenerForFileRef = useRef(getOpenerForFile); getOpenerForFileRef.current = getOpenerForFile; const { leftCallbacks, rightCallbacks, dragCallbacks, draggedFiles, permissionsState, setPermissionsState, showTextEditor, setShowTextEditor, textEditorTarget, setTextEditorTarget, textEditorContent, setTextEditorContent, loadingTextContent, showFileOpenerDialog, setShowFileOpenerDialog, fileOpenerTarget, setFileOpenerTarget, handleSaveTextFile, onPromoteToTab, handleFileOpenerSelect, handleSelectSystemApp, } = useSftpViewPaneCallbacks({ sftpRef, behaviorRef, autoSyncRef, getOpenerForFileRef, setOpenerForExtension, t, listSftp, mkdirLocal, deleteLocalFile, showSaveDialog, selectDirectory, startStreamTransfer, getSftpIdForConnection: sftp.getSftpIdForConnection, listLocalFiles: listLocalDir, listDrives, }); const visibleTransfers = useMemo( () => [...sftp.transfers].filter((t) => !t.parentTaskId).reverse().slice(0, 5), [sftp.transfers], ); const containerStyle: React.CSSProperties = isActive ? {} : { visibility: "hidden", pointerEvents: "none", position: "absolute", zIndex: -1, }; // Don't read activeTabId here - let SftpTabBar and SftpPaneWrapper subscribe to store // This prevents SftpViewInner from re-rendering on tab switch const { leftPanes, rightPanes, leftTabsInfo, rightTabsInfo, showHostPickerLeft, showHostPickerRight, hostSearchLeft, hostSearchRight, setShowHostPickerLeft, setShowHostPickerRight, setHostSearchLeft, setHostSearchRight, handleAddTabLeft, handleAddTabRight, handleCloseTabLeft, handleCloseTabRight, handleSelectTabLeft, handleSelectTabRight, handleReorderTabsLeft, handleReorderTabsRight, handleMoveTabFromLeftToRight, handleMoveTabFromRightToLeft, handleHostSelectLeft, handleHostSelectRight, } = useSftpViewTabs({ sftp, sftpRef }); const handleAddTabLeftWithFocus = useCallback(() => { const tabId = handleAddTabLeft(); handlePaneFocus("left", tabId); }, [handleAddTabLeft, handlePaneFocus]); const handleAddTabRightWithFocus = useCallback(() => { const tabId = handleAddTabRight(); handlePaneFocus("right", tabId); }, [handleAddTabRight, handlePaneFocus]); const handleSelectTabLeftWithFocus = useCallback((tabId: string) => { handleSelectTabLeft(tabId); handlePaneFocus("left", tabId); }, [handlePaneFocus, handleSelectTabLeft]); const handleSelectTabRightWithFocus = useCallback((tabId: string) => { handleSelectTabRight(tabId); handlePaneFocus("right", tabId); }, [handlePaneFocus, handleSelectTabRight]); return (
handlePaneFocus("left")} > {/* Focus indicator triangle */} {focusedSide === "left" && (
)} {/* Left side tab bar - only show when there are tabs */} {leftTabsInfo.length > 0 && ( )}
{leftPanes.map((pane, idx) => ( handleToggleHiddenFiles("left", pane.id)} /> ))} {/* Loading overlay for left pane - shown when loading text content */} {loadingTextContent && textEditorTarget?.side === "left" && (
{t("sftp.status.loading")}
)}
handlePaneFocus("right")} > {/* Focus indicator triangle */} {focusedSide === "right" && (
)} {/* Right side tab bar - only show when there are tabs */} {rightTabsInfo.length > 0 && ( )}
{rightPanes.map((pane, idx) => ( handleToggleHiddenFiles("right", pane.id)} /> ))} {/* Loading overlay for right pane - shown when loading text content */} {loadingTextContent && textEditorTarget?.side === "right" && (
{t("sftp.status.loading")}
)}
); }; const sftpViewAreEqual = (prev: SftpViewProps, next: SftpViewProps): boolean => prev.hosts === next.hosts && prev.keys === next.keys && prev.identities === next.identities && prev.groupConfigs === next.groupConfigs && prev.proxyProfiles === next.proxyProfiles && prev.sftpDefaultViewMode === next.sftpDefaultViewMode && prev.sftpDoubleClickBehavior === next.sftpDoubleClickBehavior && prev.sftpAutoSync === next.sftpAutoSync && prev.sftpShowHiddenFiles === next.sftpShowHiddenFiles && prev.sftpUseCompressedUpload === next.sftpUseCompressedUpload && prev.hotkeyScheme === next.hotkeyScheme && prev.keyBindings === next.keyBindings && prev.editorWordWrap === next.editorWordWrap && prev.setEditorWordWrap === next.setEditorWordWrap && // Only the keepalive fields of terminalSettings affect SFTP connection // resolution today; compare them directly rather than the whole object // so unrelated terminal-setting changes don't tear the panel down. prev.terminalSettings?.keepaliveInterval === next.terminalSettings?.keepaliveInterval && prev.terminalSettings?.keepaliveCountMax === next.terminalSettings?.keepaliveCountMax; export const SftpView = memo(SftpViewInner, sftpViewAreEqual); SftpView.displayName = "SftpView";