From 4501ba47ce4968331593fa228e5570a41c2268d8 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 20 Jun 2026 04:42:47 +0000 Subject: [PATCH] Fix sidebar project reveal: stabilize callbacks and add retry loop - Wrap requestSidebarProjectReveal and completeSidebarProjectReveal in useCallback to prevent unstable function identity from resetting the reveal effect on every CommandPalette re-render. - Replace single requestAnimationFrame with a retry loop (up to 20 frames) so the reveal completes even if projectHeaderRef is not yet available on the first frame. --- apps/web/src/components/CommandPalette.tsx | 8 +++---- apps/web/src/components/Sidebar.tsx | 26 +++++++++++++++++----- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/apps/web/src/components/CommandPalette.tsx b/apps/web/src/components/CommandPalette.tsx index eeac56f2cf..52048db0da 100644 --- a/apps/web/src/components/CommandPalette.tsx +++ b/apps/web/src/components/CommandPalette.tsx @@ -376,18 +376,18 @@ export function CommandPalette({ children }: { children: ReactNode }) { const nextSidebarProjectRevealRequestIdRef = useRef(0); const [sidebarProjectRevealRequest, setSidebarProjectRevealRequest] = useState(null); - function requestSidebarProjectReveal(projectRef: ScopedProjectRef): void { + const requestSidebarProjectReveal = useCallback((projectRef: ScopedProjectRef): void => { nextSidebarProjectRevealRequestIdRef.current += 1; setSidebarProjectRevealRequest({ requestId: nextSidebarProjectRevealRequestIdRef.current, projectRef, }); - } - function completeSidebarProjectReveal(requestId: number): void { + }, []); + const completeSidebarProjectReveal = useCallback((requestId: number): void => { setSidebarProjectRevealRequest((current) => current?.requestId === requestId ? null : current, ); - } + }, []); const routeTarget = useParams({ strict: false, select: (params) => resolveThreadRouteTarget(params), diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index e430aa82f3..706ce9310a 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -1179,17 +1179,31 @@ const SidebarProjectItem = memo(function SidebarProjectItem(props: SidebarProjec return; } - const frame = window.requestAnimationFrame(() => { + const revealId = projectRevealRequestId; + let cancelled = false; + let frameId: number; + let attempts = 0; + const maxAttempts = 20; + + function tryScroll() { + if (cancelled) return; const projectHeader = projectHeaderRef.current; - if (!projectHeader) { + if (projectHeader) { + scrollSidebarProjectIntoView(projectHeader); + completeProjectReveal(revealId); return; } - scrollSidebarProjectIntoView(projectHeader); - completeProjectReveal(projectRevealRequestId); - }); + attempts++; + if (attempts < maxAttempts) { + frameId = window.requestAnimationFrame(tryScroll); + } + } + + frameId = window.requestAnimationFrame(tryScroll); return () => { - window.cancelAnimationFrame(frame); + cancelled = true; + window.cancelAnimationFrame(frameId); }; }, [completeProjectReveal, projectRevealRequestId]); const sidebarThreads = useThreadShellsForProjectRefs(project.memberProjectRefs);