diff --git a/src/renderer/src/components/right-sidebar/useFileExplorerAutoReveal.ts b/src/renderer/src/components/right-sidebar/useFileExplorerAutoReveal.ts index 81ffb9a23fd..3d95e3d8e50 100644 --- a/src/renderer/src/components/right-sidebar/useFileExplorerAutoReveal.ts +++ b/src/renderer/src/components/right-sidebar/useFileExplorerAutoReveal.ts @@ -1,4 +1,4 @@ -import { useEffect, useRef } from 'react' +import { useCallback, useEffect, useRef } from 'react' import type { Virtualizer } from '@tanstack/react-virtual' import { useAppStore } from '@/store' import type { OpenFile } from '@/store/slices/editor' @@ -36,6 +36,17 @@ export function useFileExplorerAutoReveal({ virtualizer }: UseFileExplorerAutoRevealParams): void { const prevActiveFileIdRef = useRef(null) + const scrollFrameRef = useRef(null) + + const cancelScrollFrame = useCallback((): void => { + if (scrollFrameRef.current === null) { + return + } + cancelAnimationFrame(scrollFrameRef.current) + scrollFrameRef.current = null + }, []) + + useEffect(() => cancelScrollFrame, [cancelScrollFrame]) useEffect(() => { if (activeFileId === prevActiveFileIdRef.current) { @@ -71,7 +82,9 @@ export function useFileExplorerAutoReveal({ setSelectedPath(filePath) const targetIndex = flatRows.findIndex((row) => row.path === filePath) if (targetIndex !== -1) { - requestAnimationFrame(() => { + cancelScrollFrame() + scrollFrameRef.current = requestAnimationFrame(() => { + scrollFrameRef.current = null virtualizer.scrollToIndex(targetIndex, { align: 'auto' }) }) } @@ -90,6 +103,7 @@ export function useFileExplorerAutoReveal({ }, [ activeFileId, activeWorktreeId, + cancelScrollFrame, worktreePath, pendingExplorerReveal, openFiles,