diff --git a/components/sftp/SftpPaneFileList.scroll.test.ts b/components/sftp/SftpPaneFileList.scroll.test.ts new file mode 100644 index 000000000..de46ad4d0 --- /dev/null +++ b/components/sftp/SftpPaneFileList.scroll.test.ts @@ -0,0 +1,52 @@ +import assert from 'node:assert/strict'; +import test, { after } from 'node:test'; +import React from 'react'; +import { act, create, type ReactTestRenderer } from 'react-test-renderer'; +import { SftpPaneFileList } from './SftpPaneFileList'; +import { TooltipProvider } from '../ui/tooltip'; +import { installDomEnvironment } from '../test-support/renderReactDom'; +import { createEmptyPane } from '../../application/state/sftp/types'; + +const environment = globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }; +const previous = environment.IS_REACT_ACT_ENVIRONMENT; +environment.IS_REACT_ACT_ENVIRONMENT = true; +after(() => { environment.IS_REACT_ACT_ENVIRONMENT = previous; }); + +test('refresh keeps scroll, while explicit sorting and filters reveal the selection', async (t) => { + const dom = installDomEnvironment(); + t.after(() => dom.cleanup()); + let scrolls = 0; + const container = { querySelectorAll: () => [{ dataset: { entryName: 'selected' }, scrollIntoView: () => { scrolls += 1; } }] }; + const noop = () => {}; + const selected = { name: 'selected', type: 'file' as const, size: 1, sizeFormatted: '1 B', lastModified: 0, lastModifiedFormatted: '' }; + let props: React.ComponentProps = { + t: (key) => key, pane: { ...createEmptyPane('pane'), connection: { id: 'conn', hostId: 'host', hostLabel: 'Host', isLocal: false, status: 'connected', currentPath: '/browsed' }, selectedFiles: new Set(['selected']), files: [selected] }, + side: 'left', isPaneFocused: true, + sorting: { sortField: 'name', sortOrder: 'asc', directoriesFirst: true, + columnWidths: { name: 50, modified: 24, size: 7, type: 9, owner: 10 }, + visibleColumns: { name: true, modified: true, size: true, type: true, owner: true }, + handleSort: noop, handleResizeStart: noop, toggleColumnVisibility: noop, toggleDirectoriesFirst: noop }, + fileListRef: { current: container as unknown as HTMLDivElement }, + handleFileListScroll: noop, shouldVirtualize: false, totalHeight: 0, sortedDisplayFiles: [selected], + isDragOverPane: false, draggedFiles: null, onRefresh: noop, onNavigateTo: noop, onClearSelection: noop, + setShowNewFolderDialog: noop, setShowNewFileDialog: noop, getNextUntitledName: () => '', setNewFileName: noop, setFileNameError: noop, + dragOverEntry: null, handleRowSelect: noop, handleRowOpen: noop, handleFileDragStart: noop, + onDragEnd: noop, handleEntryDragOver: noop, handleRowDragLeave: noop, handleEntryDrop: noop, + onCopyToOtherPane: noop, onMoveEntriesToPath: async () => {}, openRenameDialog: noop, openDeleteConfirm: noop, + rowHeight: 28, visibleRows: [], + }; + const render = () => React.createElement(TooltipProvider, null, React.createElement(SftpPaneFileList, props)); + let renderer: ReactTestRenderer; + await act(async () => { renderer = create(render(), { createNodeMock: (element) => element.props['data-section'] === 'terminal-sftp-list' ? container : null }); }); + assert.equal(scrolls, 1); + props = { ...props, pane: { ...props.pane, files: [...props.pane.files] }, sortedDisplayFiles: [...props.sortedDisplayFiles] }; + await act(async () => { renderer.update(render()); }); + assert.equal(scrolls, 1, 'refresh must not pull the viewport back to the selection'); + props = { ...props, sorting: { ...props.sorting, sortOrder: 'desc' }, sortedDisplayFiles: [...props.sortedDisplayFiles] }; + await act(async () => { renderer.update(render()); }); + assert.equal(scrolls, 2, 'sorting retains the prior reveal-selection behavior'); + props = { ...props, pane: { ...props.pane, filter: 'selected' }, sortedDisplayFiles: [...props.sortedDisplayFiles] }; + await act(async () => { renderer.update(render()); }); + assert.equal(scrolls, 3, 'filter changes retain the prior reveal-selection behavior'); + await act(async () => { renderer.unmount(); }); +}); diff --git a/components/sftp/SftpPaneFileList.tsx b/components/sftp/SftpPaneFileList.tsx index 8bff370e3..198b79f06 100644 --- a/components/sftp/SftpPaneFileList.tsx +++ b/components/sftp/SftpPaneFileList.tsx @@ -196,12 +196,23 @@ export const SftpPaneFileList: React.FC = React.memo(({ return () => sftpListOrderStore.clearPane(pane.id); }, [sortedDisplayFiles, pane.id]); - const lastScrolledSelectionRef = useRef | null>(null); + const selectionScrollContext = useMemo(() => ({ + paneId: pane.id, filter: pane.filter, showHiddenFiles: pane.showHiddenFiles, + sortField, sortOrder, directoriesFirst, rowHeight, shouldVirtualize, + }), [ + pane.id, pane.filter, pane.showHiddenFiles, sortField, sortOrder, + directoriesFirst, rowHeight, shouldVirtualize, + ]); + const lastScrolledSelectionRef = useRef<{ + selection: Set; + context: object; + } | null>(null); useEffect(() => { - // A same-directory refresh retains the selection object. Do not pull the - // viewport back to an old selection when the user has scrolled elsewhere. - if (lastScrolledSelectionRef.current === pane.selectedFiles) return; - lastScrolledSelectionRef.current = pane.selectedFiles; + // Refreshes preserve selection identity; explicit sorting/filter/layout + // changes still reveal the selected row using the usual behavior. + const last = lastScrolledSelectionRef.current; + if (last?.selection === pane.selectedFiles && last.context === selectionScrollContext) return; + lastScrolledSelectionRef.current = { selection: pane.selectedFiles, context: selectionScrollContext }; if (pane.selectedFiles.size !== 1) return; const selectedName = Array.from(pane.selectedFiles)[0]; if (!selectedName) return; @@ -225,7 +236,7 @@ export const SftpPaneFileList: React.FC = React.memo(({ currentScrollTop: container.scrollTop, viewportHeight: container.clientHeight, }); - }, [fileListRef, pane.selectedFiles, rowHeight, shouldVirtualize, sortedDisplayFiles]); + }, [fileListRef, pane.selectedFiles, rowHeight, selectionScrollContext, shouldVirtualize, sortedDisplayFiles]); // Use refs for frequently-changing values in context-menu actions const selectedFilesRef = useRef(pane.selectedFiles);