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);