From 16ef3fdf5bf0b18d978b135f24afbecf0dab6488 Mon Sep 17 00:00:00 2001 From: Minwook Shin <163576506+minwookshin@users.noreply.github.com> Date: Mon, 21 Sep 2026 21:43:10 -0400 Subject: [PATCH] =?UTF-8?q?=E2=8F=B9=EF=B8=8F=20fix:=20Cancel=20Stale=20Th?= =?UTF-8?q?rottled=20Scroll=20Callbacks=20(#16167)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hooks/__tests__/useScrollToRef.spec.tsx | 123 ++++++++++++++++++ client/src/hooks/useScrollToRef.ts | 47 +++++-- 2 files changed, 157 insertions(+), 13 deletions(-) create mode 100644 client/src/hooks/__tests__/useScrollToRef.spec.tsx diff --git a/client/src/hooks/__tests__/useScrollToRef.spec.tsx b/client/src/hooks/__tests__/useScrollToRef.spec.tsx new file mode 100644 index 0000000000..5466eb415c --- /dev/null +++ b/client/src/hooks/__tests__/useScrollToRef.spec.tsx @@ -0,0 +1,123 @@ +import { StrictMode, createRef, useRef } from 'react'; +import { act, fireEvent, render, screen } from '@testing-library/react'; +import type { RefObject } from 'react'; +import useScrollToRef from '../useScrollToRef'; + +const followLabel = 'Follow'; +const jumpLabel = 'Jump'; +const scrollActions = [ + { label: followLabel, behavior: 'instant', interval: 145 }, + { label: jumpLabel, behavior: 'smooth', interval: 750 }, +]; + +function Harness({ + callback, + smoothCallback, + targetRef: suppliedRef, +}: { + callback: () => void; + smoothCallback: () => void; + targetRef?: RefObject; +}) { + const localRef = useRef(null); + const targetRef = suppliedRef ?? localRef; + const { scrollToRef, handleSmoothToRef } = useScrollToRef({ + targetRef, + callback, + smoothCallback, + }); + return ( +
+ + +
+
+ ); +} + +describe('useScrollToRef lifecycle', () => { + beforeEach(() => jest.useFakeTimers()); + afterEach(() => { + jest.clearAllTimers(); + jest.useRealTimers(); + }); + + it.each(['Follow', 'Jump'])('cancels trailing %s work when the chat unmounts', (button) => { + const callback = jest.fn(); + const smoothCallback = jest.fn(); + const { unmount } = render(, { + wrapper: StrictMode, + }); + const scroll = jest.fn(); + screen.getByTestId('end').scrollIntoView = scroll; + fireEvent.click(screen.getByText(button)); + fireEvent.click(screen.getByText(button)); + expect(callback.mock.calls.length + smoothCallback.mock.calls.length).toBe(1); + + unmount(); + act(() => jest.advanceTimersByTime(1000)); + expect(callback.mock.calls.length + smoothCallback.mock.calls.length).toBe(1); + expect(scroll).toHaveBeenCalledTimes(1); + }); + + it.each(['Follow', 'Jump'])('uses the latest callback for pending %s work', (button) => { + const previous = jest.fn(); + const latest = jest.fn(); + const { rerender } = render(, { + wrapper: StrictMode, + }); + const scroll = jest.fn(); + screen.getByTestId('end').scrollIntoView = scroll; + fireEvent.click(screen.getByText(button)); + fireEvent.click(screen.getByText(button)); + rerender(); + act(() => jest.advanceTimersByTime(1000)); + + expect(previous).toHaveBeenCalledTimes(1); + expect(latest).toHaveBeenCalledTimes(1); + expect(scroll).toHaveBeenCalledTimes(2); + }); + + it.each(scrollActions)( + 'preserves leading and trailing $label timing', + ({ label, behavior, interval }) => { + const callback = jest.fn(); + render(, { wrapper: StrictMode }); + const scroll = jest.fn(); + screen.getByTestId('end').scrollIntoView = scroll; + + fireEvent.click(screen.getByText(label)); + expect(scroll).toHaveBeenLastCalledWith({ behavior }); + expect(callback).toHaveBeenCalledTimes(1); + fireEvent.click(screen.getByText(label)); + act(() => jest.advanceTimersByTime(interval - 1)); + expect(callback).toHaveBeenCalledTimes(1); + act(() => jest.advanceTimersByTime(1)); + expect(callback).toHaveBeenCalledTimes(2); + expect(scroll).toHaveBeenLastCalledWith({ behavior }); + }, + ); + + it.each(scrollActions)('cancels queued $label work when the target ref changes', ({ label }) => { + const callback = jest.fn(); + const previousRef = createRef(); + const nextRef = createRef(); + const { rerender } = render( + , + { wrapper: StrictMode }, + ); + const scroll = jest.fn(); + screen.getByTestId('end').scrollIntoView = scroll; + fireEvent.click(screen.getByText(label)); + fireEvent.click(screen.getByText(label)); + + rerender(); + act(() => jest.advanceTimersByTime(1000)); + expect(callback).toHaveBeenCalledTimes(1); + expect(scroll).toHaveBeenCalledTimes(1); + + fireEvent.click(screen.getByText(label)); + expect(callback).toHaveBeenCalledTimes(2); + expect(scroll).toHaveBeenCalledTimes(2); + }); +}); diff --git a/client/src/hooks/useScrollToRef.ts b/client/src/hooks/useScrollToRef.ts index 2fed7c41a3..18acbe7003 100644 --- a/client/src/hooks/useScrollToRef.ts +++ b/client/src/hooks/useScrollToRef.ts @@ -1,5 +1,6 @@ -import { RefObject, useCallback } from 'react'; +import { useEffect, useMemo, useRef } from 'react'; import throttle from 'lodash/throttle'; +import type { RefObject } from 'react'; type TUseScrollToRef = { targetRef: RefObject; @@ -22,25 +23,45 @@ export default function useScrollToRef({ callback, smoothCallback, }: TUseScrollToRef): ScrollToRefReturn { - const logAndScroll = (behavior: 'instant' | 'smooth', callbackFn: () => void) => { - // Debugging: - // console.log(`Scrolling with behavior: ${behavior}, Time: ${new Date().toISOString()}`); - targetRef.current?.scrollIntoView({ behavior }); - callbackFn(); - }; + const callbacksRef = useRef({ callback, smoothCallback }); + useEffect(() => { + callbacksRef.current = { callback, smoothCallback }; + }, [callback, smoothCallback]); - // eslint-disable-next-line react-hooks/exhaustive-deps - const scrollToRef = useCallback( - throttle(() => logAndScroll('instant', callback), 145, { leading: true }), + const scrollToRef = useMemo( + () => + throttle( + () => { + targetRef.current?.scrollIntoView({ behavior: 'instant' }); + callbacksRef.current.callback(); + }, + 145, + { leading: true }, + ), [targetRef], ); - // eslint-disable-next-line react-hooks/exhaustive-deps - const scrollToRefSmooth = useCallback( - throttle(() => logAndScroll('smooth', smoothCallback), 750, { leading: true }), + const scrollToRefSmooth = useMemo( + () => + throttle( + () => { + targetRef.current?.scrollIntoView({ behavior: 'smooth' }); + callbacksRef.current.smoothCallback(); + }, + 750, + { leading: true }, + ), [targetRef], ); + useEffect( + () => () => { + scrollToRef.cancel(); + scrollToRefSmooth.cancel(); + }, + [scrollToRef, scrollToRefSmooth], + ); + const handleSmoothToRef: React.MouseEventHandler = (e) => { e.preventDefault(); scrollToRefSmooth();