From 3a2a1ea8c08bb0409ddb2ddb27890ba463fa3b73 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Fri, 29 May 2026 21:15:08 -0700 Subject: [PATCH] fix: cancel file explorer reveal scroll frame (#3450) --- .../right-sidebar/useFileExplorerReveal.ts | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/renderer/src/components/right-sidebar/useFileExplorerReveal.ts b/src/renderer/src/components/right-sidebar/useFileExplorerReveal.ts index 0162540a801..ff5fac31f16 100644 --- a/src/renderer/src/components/right-sidebar/useFileExplorerReveal.ts +++ b/src/renderer/src/components/right-sidebar/useFileExplorerReveal.ts @@ -1,4 +1,4 @@ -import { useEffect, useMemo } from 'react' +import { useCallback, useEffect, useMemo, useRef } from 'react' import type { Dispatch, RefObject, SetStateAction } from 'react' import type { Virtualizer } from '@tanstack/react-virtual' import { useAppStore } from '@/store' @@ -43,6 +43,22 @@ export function useFileExplorerReveal({ flashTimeoutRef, virtualizer }: UseFileExplorerRevealParams): void { + const revealScrollFrameRef = useRef(null) + const revealScrollTimeoutRef = useRef(null) + + const cancelRevealScroll = useCallback((): void => { + if (revealScrollFrameRef.current !== null) { + cancelAnimationFrame(revealScrollFrameRef.current) + revealScrollFrameRef.current = null + } + if (revealScrollTimeoutRef.current !== null) { + window.clearTimeout(revealScrollTimeoutRef.current) + revealScrollTimeoutRef.current = null + } + }, []) + + useEffect(() => cancelRevealScroll, [cancelRevealScroll]) + const pendingRevealAncestorDirs = useMemo(() => { if ( !pendingExplorerReveal || @@ -170,8 +186,11 @@ export function useFileExplorerReveal({ }, 2000) } - requestAnimationFrame(() => { - window.setTimeout(() => { + cancelRevealScroll() + revealScrollFrameRef.current = requestAnimationFrame(() => { + revealScrollFrameRef.current = null + revealScrollTimeoutRef.current = window.setTimeout(() => { + revealScrollTimeoutRef.current = null const targetIndex = flatRows.findIndex((row) => row.path === revealPath) if (targetIndex !== -1) { virtualizer.scrollToIndex(targetIndex, { align: 'center' }) @@ -180,6 +199,7 @@ export function useFileExplorerReveal({ }) }, [ activeWorktreeId, + cancelRevealScroll, clearPendingExplorerReveal, dirCache, expanded,