From 1ce326e97d2d527a11eda566b1e1298b69a1ee34 Mon Sep 17 00:00:00 2001 From: "Abdelrahman Khattab (via MelvinBot)" Date: Tue, 6 Oct 2026 12:12:06 +0000 Subject: [PATCH 1/8] Keep the expense report pinned to the bottom after the user sends a comment Co-authored-by: Abdelrahman Khattab --- .../useMoneyRequestReportScroll.ts | 24 ++++++----- .../unit/useMoneyRequestReportScrollTest.tsx | 40 +++++++++++++++++++ 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/components/MoneyRequestReportView/useMoneyRequestReportScroll.ts b/src/components/MoneyRequestReportView/useMoneyRequestReportScroll.ts index a8e996a9d037..52ba3c3ebc01 100644 --- a/src/components/MoneyRequestReportView/useMoneyRequestReportScroll.ts +++ b/src/components/MoneyRequestReportView/useMoneyRequestReportScroll.ts @@ -154,6 +154,17 @@ function useMoneyRequestReportScroll({ const stickToBottomRef = useRef(false); const stickToBottomTimeoutRef = useRef(null); + // A single jump from far above can land short of the bottom while rows render, so keep re-pinning on every content size change for a while + const startStickToBottom = () => { + stickToBottomRef.current = true; + if (stickToBottomTimeoutRef.current) { + clearTimeout(stickToBottomTimeoutRef.current); + } + stickToBottomTimeoutRef.current = setTimeout(() => { + stickToBottomRef.current = false; + }, STICK_TO_BOTTOM_DURATION_MS); + }; + // Set when the user taps "Latest messages", cleared once the scroll reaches the bottom or the user scrolls away const pendingMarkAsReadRef = useRef(false); @@ -239,6 +250,7 @@ function useMoneyRequestReportScroll({ } ownActionScrollTimeoutRef.current = setTimeout(() => { ownActionScrollTimeoutRef.current = null; + startStickToBottom(); scrollToBottom(); }, DELAY_FOR_SCROLLING_TO_END); } else { @@ -279,20 +291,14 @@ function useMoneyRequestReportScroll({ } pendingScrollTimeoutRef.current = setTimeout(() => { pendingScrollTimeoutRef.current = null; + startStickToBottom(); scrollToBottom(); }, DELAY_FOR_SCROLLING_TO_END); - }, [visibleReportActions, scrollToBottom]); + }, [visibleReportActions, scrollToBottom, startStickToBottom]); const scrollToLatestMessages = () => { setIsFloatingMessageCounterVisible(false); - - stickToBottomRef.current = true; - if (stickToBottomTimeoutRef.current) { - clearTimeout(stickToBottomTimeoutRef.current); - } - stickToBottomTimeoutRef.current = setTimeout(() => { - stickToBottomRef.current = false; - }, STICK_TO_BOTTOM_DURATION_MS); + startStickToBottom(); if (!hasNewestReportAction) { openReport({ diff --git a/tests/unit/useMoneyRequestReportScrollTest.tsx b/tests/unit/useMoneyRequestReportScrollTest.tsx index 8c2e0270e4e3..a5ed7453e5d6 100644 --- a/tests/unit/useMoneyRequestReportScrollTest.tsx +++ b/tests/unit/useMoneyRequestReportScrollTest.tsx @@ -543,6 +543,46 @@ describe('useMoneyRequestReportScroll', () => { // Then the deferred jump fires for the action the first pass could not find expect(mockScrollToIndex).toHaveBeenCalledWith(LAST_ITEM_INDEX, {animated: false, viewPosition: 1}); }); + + it('should keep re-pinning the list to the bottom while content settles after the user sends a comment', async () => { + // Given a list scrolled to the top and a comment the user just sent + const {result} = await renderScroll(); + applyLayoutAndScroll(result, BOTTOM_OFFSET_AT_TOP); + notifyNewAction(true, makeAction('1')); + act(() => { + jest.advanceTimersByTime(100); + }); + mockScrollToIndex.mockClear(); + + // When rows between the top and the newest message render and grow the list + act(() => { + result.current.onListContentSizeChange(0, LIST_CONTENT_HEIGHT + 500); + }); + + // Then the list is pinned back to the last item, because a single jump from far above can land short of it + expect(mockScrollToIndex).toHaveBeenCalledWith(LAST_ITEM_INDEX, {animated: false, viewPosition: 1}); + }); + + it('should keep re-pinning the list after a deferred jump for a comment that landed late', async () => { + // Given a comment the user sent that only shows up in the rendered list after the event + const {result, rerender} = await renderScroll({visibleReportActions: [makeAction('1')]}); + applyLayoutAndScroll(result, BOTTOM_OFFSET_AT_TOP); + notifyNewAction(true, makeAction('2')); + rerender(buildParams({visibleReportActions: [makeAction('1'), makeAction('2')], reportActionsLength: 2, lastAction: makeAction('2')})); + await waitForBatchedUpdatesWithAct(); + act(() => { + jest.advanceTimersByTime(100); + }); + mockScrollToIndex.mockClear(); + + // When the content height changes while the list settles + act(() => { + result.current.onListContentSizeChange(0, LIST_CONTENT_HEIGHT + 500); + }); + + // Then the deferred jump is also kept at the bottom + expect(mockScrollToIndex).toHaveBeenCalledWith(LAST_ITEM_INDEX, {animated: false, viewPosition: 1}); + }); }); describe('AgentZero thinking indicator', () => { From 5dce362a1e48457978ede368788fbf543975d29f Mon Sep 17 00:00:00 2001 From: "Abdelrahman Khattab (via MelvinBot)" Date: Sat, 10 Oct 2026 11:26:39 +0000 Subject: [PATCH 2/8] Scroll a single-expense report to the user's new comment Revert the useMoneyRequestReportScroll change, which only runs for multi-expense reports. For single-expense reports, scroll straight to the bottom when the unread marker is set but the newest actions are already loaded, and flush a pending scroll-to-bottom on the next frame instead of waiting for a list layout event. Co-authored-by: Abdelrahman Khattab --- .../useMoneyRequestReportScroll.ts | 24 +++++------ src/hooks/useReportActionsScroll.ts | 12 ++++++ .../useReportActionsNewActionLiveTail.ts | 6 ++- .../unit/useMoneyRequestReportScrollTest.tsx | 40 ------------------- .../useReportActionsNewActionLiveTailTest.ts | 35 ++++++++++++++++ tests/unit/useReportActionsScrollTest.tsx | 15 +++++++ 6 files changed, 76 insertions(+), 56 deletions(-) diff --git a/src/components/MoneyRequestReportView/useMoneyRequestReportScroll.ts b/src/components/MoneyRequestReportView/useMoneyRequestReportScroll.ts index 52ba3c3ebc01..a8e996a9d037 100644 --- a/src/components/MoneyRequestReportView/useMoneyRequestReportScroll.ts +++ b/src/components/MoneyRequestReportView/useMoneyRequestReportScroll.ts @@ -154,17 +154,6 @@ function useMoneyRequestReportScroll({ const stickToBottomRef = useRef(false); const stickToBottomTimeoutRef = useRef(null); - // A single jump from far above can land short of the bottom while rows render, so keep re-pinning on every content size change for a while - const startStickToBottom = () => { - stickToBottomRef.current = true; - if (stickToBottomTimeoutRef.current) { - clearTimeout(stickToBottomTimeoutRef.current); - } - stickToBottomTimeoutRef.current = setTimeout(() => { - stickToBottomRef.current = false; - }, STICK_TO_BOTTOM_DURATION_MS); - }; - // Set when the user taps "Latest messages", cleared once the scroll reaches the bottom or the user scrolls away const pendingMarkAsReadRef = useRef(false); @@ -250,7 +239,6 @@ function useMoneyRequestReportScroll({ } ownActionScrollTimeoutRef.current = setTimeout(() => { ownActionScrollTimeoutRef.current = null; - startStickToBottom(); scrollToBottom(); }, DELAY_FOR_SCROLLING_TO_END); } else { @@ -291,14 +279,20 @@ function useMoneyRequestReportScroll({ } pendingScrollTimeoutRef.current = setTimeout(() => { pendingScrollTimeoutRef.current = null; - startStickToBottom(); scrollToBottom(); }, DELAY_FOR_SCROLLING_TO_END); - }, [visibleReportActions, scrollToBottom, startStickToBottom]); + }, [visibleReportActions, scrollToBottom]); const scrollToLatestMessages = () => { setIsFloatingMessageCounterVisible(false); - startStickToBottom(); + + stickToBottomRef.current = true; + if (stickToBottomTimeoutRef.current) { + clearTimeout(stickToBottomTimeoutRef.current); + } + stickToBottomTimeoutRef.current = setTimeout(() => { + stickToBottomRef.current = false; + }, STICK_TO_BOTTOM_DURATION_MS); if (!hasNewestReportAction) { openReport({ diff --git a/src/hooks/useReportActionsScroll.ts b/src/hooks/useReportActionsScroll.ts index 09bf411c31b8..cc9d6bc988e0 100644 --- a/src/hooks/useReportActionsScroll.ts +++ b/src/hooks/useReportActionsScroll.ts @@ -412,6 +412,18 @@ function useReportActionsScroll({ completeLiveTailPruneAfterScrollToBottom(); }; + const flushPendingScrollToBottomFromEffect = useEffectEvent(() => flushPendingScrollToBottom()); + + // The list's onLayout is not enough to flush a pending scroll: a new comment changes the content size, not the + // layout size, so onLayout may never fire. Flush on the next frame so this effect doesn't chain a synchronous setState. + useEffect(() => { + if (!isScrollToBottomEnabled) { + return; + } + const frame = requestAnimationFrame(() => flushPendingScrollToBottomFromEffect()); + return () => cancelAnimationFrame(frame); + }, [isScrollToBottomEnabled]); + // Data is ready at the moment FlashList finishes its first render. const onLoad = () => { if (shouldDisablePillTracking) { diff --git a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts index 0c985a096315..10efdd623565 100644 --- a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts +++ b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts @@ -111,8 +111,12 @@ function useReportActionsNewActionLiveTail({ return; } + // An unread marker only needs openReport when the newest actions are not loaded yet. When they are, they are + // just off-screen, so scroll to them directly instead of waiting on a network round trip. const shouldJumpToLiveTail = - !isOffline && action?.actionName !== CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW && (hasNewerActions || !!linkedReportActionID || unreadMarkerReportActionID); + !isOffline && + action?.actionName !== CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW && + (hasNewerActions || !!linkedReportActionID || (!!unreadMarkerReportActionID && !hasNewestReportAction)); if (shouldJumpToLiveTail) { if (liveTailJumpRef.current.stage === 'idle') { diff --git a/tests/unit/useMoneyRequestReportScrollTest.tsx b/tests/unit/useMoneyRequestReportScrollTest.tsx index a5ed7453e5d6..8c2e0270e4e3 100644 --- a/tests/unit/useMoneyRequestReportScrollTest.tsx +++ b/tests/unit/useMoneyRequestReportScrollTest.tsx @@ -543,46 +543,6 @@ describe('useMoneyRequestReportScroll', () => { // Then the deferred jump fires for the action the first pass could not find expect(mockScrollToIndex).toHaveBeenCalledWith(LAST_ITEM_INDEX, {animated: false, viewPosition: 1}); }); - - it('should keep re-pinning the list to the bottom while content settles after the user sends a comment', async () => { - // Given a list scrolled to the top and a comment the user just sent - const {result} = await renderScroll(); - applyLayoutAndScroll(result, BOTTOM_OFFSET_AT_TOP); - notifyNewAction(true, makeAction('1')); - act(() => { - jest.advanceTimersByTime(100); - }); - mockScrollToIndex.mockClear(); - - // When rows between the top and the newest message render and grow the list - act(() => { - result.current.onListContentSizeChange(0, LIST_CONTENT_HEIGHT + 500); - }); - - // Then the list is pinned back to the last item, because a single jump from far above can land short of it - expect(mockScrollToIndex).toHaveBeenCalledWith(LAST_ITEM_INDEX, {animated: false, viewPosition: 1}); - }); - - it('should keep re-pinning the list after a deferred jump for a comment that landed late', async () => { - // Given a comment the user sent that only shows up in the rendered list after the event - const {result, rerender} = await renderScroll({visibleReportActions: [makeAction('1')]}); - applyLayoutAndScroll(result, BOTTOM_OFFSET_AT_TOP); - notifyNewAction(true, makeAction('2')); - rerender(buildParams({visibleReportActions: [makeAction('1'), makeAction('2')], reportActionsLength: 2, lastAction: makeAction('2')})); - await waitForBatchedUpdatesWithAct(); - act(() => { - jest.advanceTimersByTime(100); - }); - mockScrollToIndex.mockClear(); - - // When the content height changes while the list settles - act(() => { - result.current.onListContentSizeChange(0, LIST_CONTENT_HEIGHT + 500); - }); - - // Then the deferred jump is also kept at the bottom - expect(mockScrollToIndex).toHaveBeenCalledWith(LAST_ITEM_INDEX, {animated: false, viewPosition: 1}); - }); }); describe('AgentZero thinking indicator', () => { diff --git a/tests/unit/useReportActionsNewActionLiveTailTest.ts b/tests/unit/useReportActionsNewActionLiveTailTest.ts index 84f426155b17..3b2f63e6df8b 100644 --- a/tests/unit/useReportActionsNewActionLiveTailTest.ts +++ b/tests/unit/useReportActionsNewActionLiveTailTest.ts @@ -209,6 +209,41 @@ describe('useReportActionsNewActionLiveTail', () => { expect(mockNavigationSetParams).not.toHaveBeenCalled(); expect(setTreatAsNoPaginationAnchor).toHaveBeenCalledWith(true); }); + + it('scrolls straight to the bottom when an unread marker is set but the newest actions are already loaded', () => { + // Given a report scrolled up with an unread marker from another user's comment, and the newest actions already loaded + const setIsFloatingMessageCounterVisible = jest.fn(); + const {result} = renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), { + initialProps: buildParams({unreadMarkerReportActionID: '2', hasNewerActions: false, hasNewestReportAction: true, setIsFloatingMessageCounterVisible}), + }); + + // When the current user sends a comment + act(() => { + newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT})); + }); + + // Then the list scrolls to the bottom directly, without an openReport round trip + expect(mockOpenReport).not.toHaveBeenCalled(); + expect(setIsFloatingMessageCounterVisible).toHaveBeenCalledWith(false); + expect(reportScrollManager.scrollToBottom).toHaveBeenCalledTimes(1); + expect(result.current.isScrollToBottomEnabled).toBe(true); + }); + + it('still loads the live tail when an unread marker is set and the newest actions are not loaded', () => { + // Given an unread marker and a report whose newest actions are not loaded yet + renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), { + initialProps: buildParams({unreadMarkerReportActionID: '2', hasNewerActions: false, hasNewestReportAction: false}), + }); + + // When the current user sends a comment + act(() => { + newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT})); + }); + + // Then openReport fetches the newest actions before the list scrolls + expect(mockOpenReport).toHaveBeenCalledTimes(1); + expect(reportScrollManager.scrollToBottom).not.toHaveBeenCalled(); + }); }); describe('withNavigationFallback stub', () => { diff --git a/tests/unit/useReportActionsScrollTest.tsx b/tests/unit/useReportActionsScrollTest.tsx index c3e78bccf919..2b8ce520df9b 100644 --- a/tests/unit/useReportActionsScrollTest.tsx +++ b/tests/unit/useReportActionsScrollTest.tsx @@ -425,6 +425,8 @@ describe('useReportActionsScroll', () => { mockIsScrollToBottomEnabled = true; const {result} = await renderScroll(); + // Ignore the flush the effect already made on mount, so only the layout-driven call is measured. + jest.clearAllMocks(); act(() => { result.current.flushPendingScrollToBottom(); }); @@ -433,6 +435,19 @@ describe('useReportActionsScroll', () => { expect(mockSetIsScrollToBottomEnabled).toHaveBeenCalledWith(false); expect(mockCompleteLiveTailPrune).toHaveBeenCalledTimes(1); }); + + it('flushes a pending scroll-to-bottom without waiting for a list layout event', async () => { + // Given the live-tail hook asked for a scroll to bottom, as it does after the user sends a comment + mockIsScrollToBottomEnabled = true; + + // When the hook renders and no list onLayout follows, because a new comment changes content size, not layout size + await renderScroll(); + + // Then the pending scroll is still flushed once, and the request is cleared + expect(mockScrollToBottom).toHaveBeenCalledTimes(1); + expect(mockSetIsScrollToBottomEnabled).toHaveBeenCalledWith(false); + expect(mockCompleteLiveTailPrune).toHaveBeenCalledTimes(1); + }); }); describe('onLoad', () => { From 654e67a28494ed4d76e72e5667fa9a2b618e2203 Mon Sep 17 00:00:00 2001 From: "Abdelrahman Khattab (via MelvinBot)" Date: Sun, 11 Oct 2026 21:14:49 +0000 Subject: [PATCH 3/8] Mark the report as read after scrolling to the user's own comment Without this, the New divider stayed visible and the unread marker turned the New messages pill back on after the scroll. Co-authored-by: Abdelrahman Khattab --- src/hooks/useReportActionsScroll.ts | 1 + .../useReportActionsNewActionLiveTail.ts | 10 +++++++ .../useReportActionsNewActionLiveTailTest.ts | 29 ++++++++++++++++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/hooks/useReportActionsScroll.ts b/src/hooks/useReportActionsScroll.ts index cc9d6bc988e0..d8942cb0dac9 100644 --- a/src/hooks/useReportActionsScroll.ts +++ b/src/hooks/useReportActionsScroll.ts @@ -242,6 +242,7 @@ function useReportActionsScroll({ reportScrollManager, setIsFloatingMessageCounterVisible, setActionIdToHighlight, + markNewestActionAsRead, unreadMarkerReportActionID, hasNewerActions, linkedReportActionID, diff --git a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts index 10efdd623565..86a76484bbe0 100644 --- a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts +++ b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts @@ -44,6 +44,7 @@ type UseReportActionsNewActionLiveTailParams = { reportScrollManager: ReportScrollManager; setIsFloatingMessageCounterVisible: (visible: boolean) => void; setActionIdToHighlight: (actionID: string) => void; + markNewestActionAsRead: () => void; unreadMarkerReportActionID: string | null; hasNewerActions: boolean; linkedReportActionID: string | undefined; @@ -75,6 +76,7 @@ function useReportActionsNewActionLiveTail({ reportScrollManager, setIsFloatingMessageCounterVisible, setActionIdToHighlight, + markNewestActionAsRead, unreadMarkerReportActionID, hasNewerActions, linkedReportActionID, @@ -93,6 +95,12 @@ function useReportActionsNewActionLiveTail({ const liveTailJumpRef = useRef<{stage: LiveTailJumpStage}>({stage: 'idle'}); const [isScrollToBottomEnabled, setIsScrollToBottomEnabled] = useState(false); + // Scrolling to the user's own comment must also mark the report as read. Otherwise the "New" divider stays, and the + // unread marker it leaves behind turns the "New messages" pill back on. An effect event keeps it out of effect deps. + const markAsReadAfterScroll = useEffectEvent(() => { + markNewestActionAsRead(); + }); + const scrollToBottomForCurrentUserAction = useEffectEvent((isFromCurrentUser: boolean, action?: OnyxTypes.ReportAction) => { TransitionTracker.runAfterTransitions({ callback: () => { @@ -150,6 +158,7 @@ function useReportActionsNewActionLiveTail({ } else { setIsFloatingMessageCounterVisible(false); reportScrollManager.scrollToBottom(); + markAsReadAfterScroll(); } setIsScrollToBottomEnabled(true); @@ -207,6 +216,7 @@ function useReportActionsNewActionLiveTail({ liveTailJumpRef.current = {stage: 'await_prune'}; setIsFloatingMessageCounterVisible(false); + markAsReadAfterScroll(); // Defer so this effect does not synchronously chain a second render from setState (eslint react-hooks/set-state-in-effect). queueMicrotask(() => { setIsScrollToBottomEnabled(true); diff --git a/tests/unit/useReportActionsNewActionLiveTailTest.ts b/tests/unit/useReportActionsNewActionLiveTailTest.ts index 3b2f63e6df8b..faeaff36742e 100644 --- a/tests/unit/useReportActionsNewActionLiveTailTest.ts +++ b/tests/unit/useReportActionsNewActionLiveTailTest.ts @@ -99,6 +99,7 @@ function buildParams(overrides: Partial = {}): HookParams { reportScrollManager, setIsFloatingMessageCounterVisible: jest.fn(), setActionIdToHighlight: jest.fn(), + markNewestActionAsRead: jest.fn(), unreadMarkerReportActionID: null, hasNewerActions: true, linkedReportActionID: undefined, @@ -213,8 +214,9 @@ describe('useReportActionsNewActionLiveTail', () => { it('scrolls straight to the bottom when an unread marker is set but the newest actions are already loaded', () => { // Given a report scrolled up with an unread marker from another user's comment, and the newest actions already loaded const setIsFloatingMessageCounterVisible = jest.fn(); + const markNewestActionAsRead = jest.fn(); const {result} = renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), { - initialProps: buildParams({unreadMarkerReportActionID: '2', hasNewerActions: false, hasNewestReportAction: true, setIsFloatingMessageCounterVisible}), + initialProps: buildParams({unreadMarkerReportActionID: '2', hasNewerActions: false, hasNewestReportAction: true, setIsFloatingMessageCounterVisible, markNewestActionAsRead}), }); // When the current user sends a comment @@ -227,6 +229,31 @@ describe('useReportActionsNewActionLiveTail', () => { expect(setIsFloatingMessageCounterVisible).toHaveBeenCalledWith(false); expect(reportScrollManager.scrollToBottom).toHaveBeenCalledTimes(1); expect(result.current.isScrollToBottomEnabled).toBe(true); + + // And the report is marked as read, so the "New" divider clears and the pill doesn't come back + expect(markNewestActionAsRead).toHaveBeenCalledTimes(1); + }); + + it('marks the report as read once openReport has loaded the newest actions after the user sends a comment', () => { + // Given a report whose newest actions are not loaded yet + const markNewestActionAsRead = jest.fn(); + const {rerender} = renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), {initialProps: buildParams({markNewestActionAsRead})}); + + // When the current user sends a comment, which starts the live-tail jump through openReport + act(() => { + newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT})); + }); + + // Then nothing is marked as read while the newest actions are still loading + expect(mockOpenReport).toHaveBeenCalledTimes(1); + expect(markNewestActionAsRead).not.toHaveBeenCalled(); + + // When openReport finishes and the newest actions arrive + rerender(buildParams({markNewestActionAsRead, prevIsLoadingInitialReportActions: true, reportLoadingState: {isLoadingInitialReportActions: false}})); + rerender(buildParams({markNewestActionAsRead, prevIsLoadingInitialReportActions: false, reportLoadingState: {isLoadingInitialReportActions: false}, hasNewestReportAction: true})); + + // Then the report is marked as read once, alongside hiding the pill + expect(markNewestActionAsRead).toHaveBeenCalledTimes(1); }); it('still loads the live tail when an unread marker is set and the newest actions are not loaded', () => { From 2a215e8358ece9872f9a037e6c6c6ed6e2902272 Mon Sep 17 00:00:00 2001 From: "Abdelrahman Khattab (via MelvinBot)" Date: Tue, 13 Oct 2026 14:48:53 +0000 Subject: [PATCH 4/8] Remove the unreachable branch and only flush the pending scroll on the next frame in aligned-to-top reports Co-authored-by: Abdelrahman Khattab --- src/hooks/useReportActionsScroll.ts | 10 ++++++---- .../useReportActionsNewActionLiveTail.ts | 10 ---------- tests/unit/useReportActionsScrollTest.tsx | 20 +++++++++++++++---- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/hooks/useReportActionsScroll.ts b/src/hooks/useReportActionsScroll.ts index d8942cb0dac9..43ea47df16d4 100644 --- a/src/hooks/useReportActionsScroll.ts +++ b/src/hooks/useReportActionsScroll.ts @@ -415,15 +415,17 @@ function useReportActionsScroll({ const flushPendingScrollToBottomFromEffect = useEffectEvent(() => flushPendingScrollToBottom()); - // The list's onLayout is not enough to flush a pending scroll: a new comment changes the content size, not the - // layout size, so onLayout may never fire. Flush on the next frame so this effect doesn't chain a synchronous setState. + // In an aligned-to-top report (expense, invoice, transaction thread), the list's onLayout is not enough to flush a + // pending scroll: a new comment changes the content size, not the layout size, so onLayout may never fire. Regular + // chats don't need this, because their first scrollToBottom already lands. Flush on the next frame so this effect + // doesn't chain a synchronous setState. useEffect(() => { - if (!isScrollToBottomEnabled) { + if (!shouldBeAlignedToTop || !isScrollToBottomEnabled) { return; } const frame = requestAnimationFrame(() => flushPendingScrollToBottomFromEffect()); return () => cancelAnimationFrame(frame); - }, [isScrollToBottomEnabled]); + }, [shouldBeAlignedToTop, isScrollToBottomEnabled]); // Data is ready at the moment FlashList finishes its first render. const onLoad = () => { diff --git a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts index 86a76484bbe0..3b930e3e4e52 100644 --- a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts +++ b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts @@ -13,7 +13,6 @@ import TransitionTracker from '@libs/Navigation/TransitionTracker'; import type {ReportsSplitNavigatorParamList} from '@navigation/types'; import CONST from '@src/CONST'; -import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; import type * as OnyxTypes from '@src/types/onyx'; @@ -109,15 +108,6 @@ function useReportActionsNewActionLiveTail({ if (!isFromCurrentUser || (!isReportTopmostSplitNavigator() && !Navigation.getReportRHPActiveRoute())) { return; } - if (!hasNewestReportAction && !isFromCurrentUser) { - if (Navigation.getReportRHPActiveRoute()) { - return; - } - Navigation.setNavigationActionToMicrotaskQueue(() => { - Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(reportID)); - }); - return; - } // An unread marker only needs openReport when the newest actions are not loaded yet. When they are, they are // just off-screen, so scroll to them directly instead of waiting on a network round trip. diff --git a/tests/unit/useReportActionsScrollTest.tsx b/tests/unit/useReportActionsScrollTest.tsx index 2b8ce520df9b..e165349e8f12 100644 --- a/tests/unit/useReportActionsScrollTest.tsx +++ b/tests/unit/useReportActionsScrollTest.tsx @@ -425,8 +425,6 @@ describe('useReportActionsScroll', () => { mockIsScrollToBottomEnabled = true; const {result} = await renderScroll(); - // Ignore the flush the effect already made on mount, so only the layout-driven call is measured. - jest.clearAllMocks(); act(() => { result.current.flushPendingScrollToBottom(); }); @@ -436,8 +434,9 @@ describe('useReportActionsScroll', () => { expect(mockCompleteLiveTailPrune).toHaveBeenCalledTimes(1); }); - it('flushes a pending scroll-to-bottom without waiting for a list layout event', async () => { - // Given the live-tail hook asked for a scroll to bottom, as it does after the user sends a comment + it('flushes a pending scroll-to-bottom without waiting for a list layout event in an aligned-to-top report', async () => { + // Given an expense report, and the live-tail hook asked for a scroll to bottom, as it does after the user sends a comment + mockIsMoneyRequestReport = true; mockIsScrollToBottomEnabled = true; // When the hook renders and no list onLayout follows, because a new comment changes content size, not layout size @@ -448,6 +447,19 @@ describe('useReportActionsScroll', () => { expect(mockSetIsScrollToBottomEnabled).toHaveBeenCalledWith(false); expect(mockCompleteLiveTailPrune).toHaveBeenCalledTimes(1); }); + + it('leaves a pending scroll-to-bottom to the list layout event in a regular chat', async () => { + // Given a regular chat, and the live-tail hook asked for a scroll to bottom + mockIsScrollToBottomEnabled = true; + + // When the hook renders + await renderScroll(); + + // Then nothing flushes on the next frame, because the first scrollToBottom in a regular chat already lands + expect(mockScrollToBottom).not.toHaveBeenCalled(); + expect(mockSetIsScrollToBottomEnabled).not.toHaveBeenCalled(); + expect(mockCompleteLiveTailPrune).not.toHaveBeenCalled(); + }); }); describe('onLoad', () => { From 22bc177d3ebf103dff01dc8c56b3248de80b45b2 Mon Sep 17 00:00:00 2001 From: "Abdelrahman Khattab (via MelvinBot)" Date: Tue, 13 Oct 2026 15:03:59 +0000 Subject: [PATCH 5/8] Skip marking read after the user's comment when newer actions are unloaded or the report is in the background Co-authored-by: Abdelrahman Khattab --- .../useReportActionsNewActionLiveTail.ts | 17 ++++- .../useReportActionsNewActionLiveTailTest.ts | 64 ++++++++++++++++++- 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts index 3b930e3e4e52..8998ae81ad0d 100644 --- a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts +++ b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts @@ -96,10 +96,25 @@ function useReportActionsNewActionLiveTail({ // Scrolling to the user's own comment must also mark the report as read. Otherwise the "New" divider stays, and the // unread marker it leaves behind turns the "New messages" pill back on. An effect event keeps it out of effect deps. + // Marking read moves lastReadTime to now, so skip it while newer actions are not loaded (e.g. offline, where the + // live-tail jump can't run). Otherwise actions the user never saw would come back from the server already read. const markAsReadAfterScroll = useEffectEvent(() => { + if (!hasNewestReportAction || hasNewerActions) { + return; + } markNewestActionAsRead(); }); + // On web a report screen stays mounted after the user switches to another report, so openReport can finish while + // this screen is in the background. Only mark read if the user is still looking at it. The side panel's fallback + // navigation always reports focused. + const markAsReadAfterLiveTailJump = useEffectEvent(() => { + if (!navigation.isFocused()) { + return; + } + markAsReadAfterScroll(); + }); + const scrollToBottomForCurrentUserAction = useEffectEvent((isFromCurrentUser: boolean, action?: OnyxTypes.ReportAction) => { TransitionTracker.runAfterTransitions({ callback: () => { @@ -206,7 +221,7 @@ function useReportActionsNewActionLiveTail({ liveTailJumpRef.current = {stage: 'await_prune'}; setIsFloatingMessageCounterVisible(false); - markAsReadAfterScroll(); + markAsReadAfterLiveTailJump(); // Defer so this effect does not synchronously chain a second render from setState (eslint react-hooks/set-state-in-effect). queueMicrotask(() => { setIsScrollToBottomEnabled(true); diff --git a/tests/unit/useReportActionsNewActionLiveTailTest.ts b/tests/unit/useReportActionsNewActionLiveTailTest.ts index faeaff36742e..9b07a1224002 100644 --- a/tests/unit/useReportActionsNewActionLiveTailTest.ts +++ b/tests/unit/useReportActionsNewActionLiveTailTest.ts @@ -31,7 +31,7 @@ jest.mock('@hooks/useCurrentUserPersonalDetails', () => ({ default: () => ({accountID: 1}), })); -let mockNavigation: Record = {setParams: mockNavigationSetParams, getState: () => ({key: 'stack-report'})}; +let mockNavigation: Record = {setParams: mockNavigationSetParams, getState: () => ({key: 'stack-report'}), isFocused: () => true}; let mockIsInSidePanel = false; jest.mock('@react-navigation/native', () => ({ @@ -119,7 +119,7 @@ describe('useReportActionsNewActionLiveTail', () => { beforeEach(() => { jest.clearAllMocks(); newActionHandler = undefined; - mockNavigation = {setParams: mockNavigationSetParams, getState: () => ({key: 'stack-report'})}; + mockNavigation = {setParams: mockNavigationSetParams, getState: () => ({key: 'stack-report'}), isFocused: () => true}; mockIsInSidePanel = false; }); @@ -250,12 +250,70 @@ describe('useReportActionsNewActionLiveTail', () => { // When openReport finishes and the newest actions arrive rerender(buildParams({markNewestActionAsRead, prevIsLoadingInitialReportActions: true, reportLoadingState: {isLoadingInitialReportActions: false}})); - rerender(buildParams({markNewestActionAsRead, prevIsLoadingInitialReportActions: false, reportLoadingState: {isLoadingInitialReportActions: false}, hasNewestReportAction: true})); + rerender( + buildParams({ + markNewestActionAsRead, + prevIsLoadingInitialReportActions: false, + reportLoadingState: {isLoadingInitialReportActions: false}, + hasNewestReportAction: true, + hasNewerActions: false, + }), + ); // Then the report is marked as read once, alongside hiding the pill expect(markNewestActionAsRead).toHaveBeenCalledTimes(1); }); + it('does not mark the report as read when the user sends a comment offline while newer actions are not loaded', () => { + // Given an offline report with a gap of newer actions on the server. The optimistic comment still sits at the + // top of the newest chain, so hasNewestReportAction is true even though newer actions are missing. + const markNewestActionAsRead = jest.fn(); + renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), { + initialProps: buildParams({isOffline: true, hasNewerActions: true, hasNewestReportAction: true, markNewestActionAsRead}), + }); + + // When the current user sends a comment, which scrolls directly because the live-tail jump can't run offline + act(() => { + newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT})); + }); + + // Then the list still scrolls, but nothing is marked as read, so the unloaded actions stay unread + expect(reportScrollManager.scrollToBottom).toHaveBeenCalledTimes(1); + expect(markNewestActionAsRead).not.toHaveBeenCalled(); + }); + + it('does not mark the report as read when the live-tail jump finishes after the user switched to another report', () => { + // Given a live-tail jump started by the user's comment + const markNewestActionAsRead = jest.fn(); + const setIsFloatingMessageCounterVisible = jest.fn(); + const {rerender} = renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), { + initialProps: buildParams({markNewestActionAsRead, setIsFloatingMessageCounterVisible}), + }); + act(() => { + newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT})); + }); + + // When the user switches to another report, leaving this screen mounted in the background on web, and openReport then finishes + mockNavigation = {...mockNavigation, isFocused: () => false}; + rerender( + buildParams({markNewestActionAsRead, setIsFloatingMessageCounterVisible, prevIsLoadingInitialReportActions: true, reportLoadingState: {isLoadingInitialReportActions: false}}), + ); + rerender( + buildParams({ + markNewestActionAsRead, + setIsFloatingMessageCounterVisible, + prevIsLoadingInitialReportActions: false, + reportLoadingState: {isLoadingInitialReportActions: false}, + hasNewestReportAction: true, + hasNewerActions: false, + }), + ); + + // Then the jump still completes, but the background report is not marked as read + expect(setIsFloatingMessageCounterVisible).toHaveBeenCalledWith(false); + expect(markNewestActionAsRead).not.toHaveBeenCalled(); + }); + it('still loads the live tail when an unread marker is set and the newest actions are not loaded', () => { // Given an unread marker and a report whose newest actions are not loaded yet renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), { From 030fc18c05596264c3adfd1a51d231175bad0f79 Mon Sep 17 00:00:00 2001 From: "Abdelrahman Khattab (via MelvinBot)" Date: Tue, 13 Oct 2026 15:17:23 +0000 Subject: [PATCH 6/8] Revert "Skip marking read after the user's comment when newer actions are unloaded or the report is in the background" This reverts commit 22bc177d3ebf103dff01dc8c56b3248de80b45b2. --- .../useReportActionsNewActionLiveTail.ts | 17 +---- .../useReportActionsNewActionLiveTailTest.ts | 64 +------------------ 2 files changed, 4 insertions(+), 77 deletions(-) diff --git a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts index 8998ae81ad0d..3b930e3e4e52 100644 --- a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts +++ b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts @@ -96,25 +96,10 @@ function useReportActionsNewActionLiveTail({ // Scrolling to the user's own comment must also mark the report as read. Otherwise the "New" divider stays, and the // unread marker it leaves behind turns the "New messages" pill back on. An effect event keeps it out of effect deps. - // Marking read moves lastReadTime to now, so skip it while newer actions are not loaded (e.g. offline, where the - // live-tail jump can't run). Otherwise actions the user never saw would come back from the server already read. const markAsReadAfterScroll = useEffectEvent(() => { - if (!hasNewestReportAction || hasNewerActions) { - return; - } markNewestActionAsRead(); }); - // On web a report screen stays mounted after the user switches to another report, so openReport can finish while - // this screen is in the background. Only mark read if the user is still looking at it. The side panel's fallback - // navigation always reports focused. - const markAsReadAfterLiveTailJump = useEffectEvent(() => { - if (!navigation.isFocused()) { - return; - } - markAsReadAfterScroll(); - }); - const scrollToBottomForCurrentUserAction = useEffectEvent((isFromCurrentUser: boolean, action?: OnyxTypes.ReportAction) => { TransitionTracker.runAfterTransitions({ callback: () => { @@ -221,7 +206,7 @@ function useReportActionsNewActionLiveTail({ liveTailJumpRef.current = {stage: 'await_prune'}; setIsFloatingMessageCounterVisible(false); - markAsReadAfterLiveTailJump(); + markAsReadAfterScroll(); // Defer so this effect does not synchronously chain a second render from setState (eslint react-hooks/set-state-in-effect). queueMicrotask(() => { setIsScrollToBottomEnabled(true); diff --git a/tests/unit/useReportActionsNewActionLiveTailTest.ts b/tests/unit/useReportActionsNewActionLiveTailTest.ts index 9b07a1224002..faeaff36742e 100644 --- a/tests/unit/useReportActionsNewActionLiveTailTest.ts +++ b/tests/unit/useReportActionsNewActionLiveTailTest.ts @@ -31,7 +31,7 @@ jest.mock('@hooks/useCurrentUserPersonalDetails', () => ({ default: () => ({accountID: 1}), })); -let mockNavigation: Record = {setParams: mockNavigationSetParams, getState: () => ({key: 'stack-report'}), isFocused: () => true}; +let mockNavigation: Record = {setParams: mockNavigationSetParams, getState: () => ({key: 'stack-report'})}; let mockIsInSidePanel = false; jest.mock('@react-navigation/native', () => ({ @@ -119,7 +119,7 @@ describe('useReportActionsNewActionLiveTail', () => { beforeEach(() => { jest.clearAllMocks(); newActionHandler = undefined; - mockNavigation = {setParams: mockNavigationSetParams, getState: () => ({key: 'stack-report'}), isFocused: () => true}; + mockNavigation = {setParams: mockNavigationSetParams, getState: () => ({key: 'stack-report'})}; mockIsInSidePanel = false; }); @@ -250,70 +250,12 @@ describe('useReportActionsNewActionLiveTail', () => { // When openReport finishes and the newest actions arrive rerender(buildParams({markNewestActionAsRead, prevIsLoadingInitialReportActions: true, reportLoadingState: {isLoadingInitialReportActions: false}})); - rerender( - buildParams({ - markNewestActionAsRead, - prevIsLoadingInitialReportActions: false, - reportLoadingState: {isLoadingInitialReportActions: false}, - hasNewestReportAction: true, - hasNewerActions: false, - }), - ); + rerender(buildParams({markNewestActionAsRead, prevIsLoadingInitialReportActions: false, reportLoadingState: {isLoadingInitialReportActions: false}, hasNewestReportAction: true})); // Then the report is marked as read once, alongside hiding the pill expect(markNewestActionAsRead).toHaveBeenCalledTimes(1); }); - it('does not mark the report as read when the user sends a comment offline while newer actions are not loaded', () => { - // Given an offline report with a gap of newer actions on the server. The optimistic comment still sits at the - // top of the newest chain, so hasNewestReportAction is true even though newer actions are missing. - const markNewestActionAsRead = jest.fn(); - renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), { - initialProps: buildParams({isOffline: true, hasNewerActions: true, hasNewestReportAction: true, markNewestActionAsRead}), - }); - - // When the current user sends a comment, which scrolls directly because the live-tail jump can't run offline - act(() => { - newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT})); - }); - - // Then the list still scrolls, but nothing is marked as read, so the unloaded actions stay unread - expect(reportScrollManager.scrollToBottom).toHaveBeenCalledTimes(1); - expect(markNewestActionAsRead).not.toHaveBeenCalled(); - }); - - it('does not mark the report as read when the live-tail jump finishes after the user switched to another report', () => { - // Given a live-tail jump started by the user's comment - const markNewestActionAsRead = jest.fn(); - const setIsFloatingMessageCounterVisible = jest.fn(); - const {rerender} = renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), { - initialProps: buildParams({markNewestActionAsRead, setIsFloatingMessageCounterVisible}), - }); - act(() => { - newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT})); - }); - - // When the user switches to another report, leaving this screen mounted in the background on web, and openReport then finishes - mockNavigation = {...mockNavigation, isFocused: () => false}; - rerender( - buildParams({markNewestActionAsRead, setIsFloatingMessageCounterVisible, prevIsLoadingInitialReportActions: true, reportLoadingState: {isLoadingInitialReportActions: false}}), - ); - rerender( - buildParams({ - markNewestActionAsRead, - setIsFloatingMessageCounterVisible, - prevIsLoadingInitialReportActions: false, - reportLoadingState: {isLoadingInitialReportActions: false}, - hasNewestReportAction: true, - hasNewerActions: false, - }), - ); - - // Then the jump still completes, but the background report is not marked as read - expect(setIsFloatingMessageCounterVisible).toHaveBeenCalledWith(false); - expect(markNewestActionAsRead).not.toHaveBeenCalled(); - }); - it('still loads the live tail when an unread marker is set and the newest actions are not loaded', () => { // Given an unread marker and a report whose newest actions are not loaded yet renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), { From 9c7b785f207ea132bb384682ec17f07fc1ab224b Mon Sep 17 00:00:00 2001 From: "Abdelrahman Khattab (via MelvinBot)" Date: Tue, 13 Oct 2026 15:17:23 +0000 Subject: [PATCH 7/8] Revert "Mark the report as read after scrolling to the user's own comment" This reverts commit 654e67a28494ed4d76e72e5667fa9a2b618e2203. --- src/hooks/useReportActionsScroll.ts | 1 - .../useReportActionsNewActionLiveTail.ts | 10 ------- .../useReportActionsNewActionLiveTailTest.ts | 29 +------------------ 3 files changed, 1 insertion(+), 39 deletions(-) diff --git a/src/hooks/useReportActionsScroll.ts b/src/hooks/useReportActionsScroll.ts index 43ea47df16d4..eef062777d87 100644 --- a/src/hooks/useReportActionsScroll.ts +++ b/src/hooks/useReportActionsScroll.ts @@ -242,7 +242,6 @@ function useReportActionsScroll({ reportScrollManager, setIsFloatingMessageCounterVisible, setActionIdToHighlight, - markNewestActionAsRead, unreadMarkerReportActionID, hasNewerActions, linkedReportActionID, diff --git a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts index 3b930e3e4e52..c99fe489b71e 100644 --- a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts +++ b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts @@ -43,7 +43,6 @@ type UseReportActionsNewActionLiveTailParams = { reportScrollManager: ReportScrollManager; setIsFloatingMessageCounterVisible: (visible: boolean) => void; setActionIdToHighlight: (actionID: string) => void; - markNewestActionAsRead: () => void; unreadMarkerReportActionID: string | null; hasNewerActions: boolean; linkedReportActionID: string | undefined; @@ -75,7 +74,6 @@ function useReportActionsNewActionLiveTail({ reportScrollManager, setIsFloatingMessageCounterVisible, setActionIdToHighlight, - markNewestActionAsRead, unreadMarkerReportActionID, hasNewerActions, linkedReportActionID, @@ -94,12 +92,6 @@ function useReportActionsNewActionLiveTail({ const liveTailJumpRef = useRef<{stage: LiveTailJumpStage}>({stage: 'idle'}); const [isScrollToBottomEnabled, setIsScrollToBottomEnabled] = useState(false); - // Scrolling to the user's own comment must also mark the report as read. Otherwise the "New" divider stays, and the - // unread marker it leaves behind turns the "New messages" pill back on. An effect event keeps it out of effect deps. - const markAsReadAfterScroll = useEffectEvent(() => { - markNewestActionAsRead(); - }); - const scrollToBottomForCurrentUserAction = useEffectEvent((isFromCurrentUser: boolean, action?: OnyxTypes.ReportAction) => { TransitionTracker.runAfterTransitions({ callback: () => { @@ -148,7 +140,6 @@ function useReportActionsNewActionLiveTail({ } else { setIsFloatingMessageCounterVisible(false); reportScrollManager.scrollToBottom(); - markAsReadAfterScroll(); } setIsScrollToBottomEnabled(true); @@ -206,7 +197,6 @@ function useReportActionsNewActionLiveTail({ liveTailJumpRef.current = {stage: 'await_prune'}; setIsFloatingMessageCounterVisible(false); - markAsReadAfterScroll(); // Defer so this effect does not synchronously chain a second render from setState (eslint react-hooks/set-state-in-effect). queueMicrotask(() => { setIsScrollToBottomEnabled(true); diff --git a/tests/unit/useReportActionsNewActionLiveTailTest.ts b/tests/unit/useReportActionsNewActionLiveTailTest.ts index faeaff36742e..3b2f63e6df8b 100644 --- a/tests/unit/useReportActionsNewActionLiveTailTest.ts +++ b/tests/unit/useReportActionsNewActionLiveTailTest.ts @@ -99,7 +99,6 @@ function buildParams(overrides: Partial = {}): HookParams { reportScrollManager, setIsFloatingMessageCounterVisible: jest.fn(), setActionIdToHighlight: jest.fn(), - markNewestActionAsRead: jest.fn(), unreadMarkerReportActionID: null, hasNewerActions: true, linkedReportActionID: undefined, @@ -214,9 +213,8 @@ describe('useReportActionsNewActionLiveTail', () => { it('scrolls straight to the bottom when an unread marker is set but the newest actions are already loaded', () => { // Given a report scrolled up with an unread marker from another user's comment, and the newest actions already loaded const setIsFloatingMessageCounterVisible = jest.fn(); - const markNewestActionAsRead = jest.fn(); const {result} = renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), { - initialProps: buildParams({unreadMarkerReportActionID: '2', hasNewerActions: false, hasNewestReportAction: true, setIsFloatingMessageCounterVisible, markNewestActionAsRead}), + initialProps: buildParams({unreadMarkerReportActionID: '2', hasNewerActions: false, hasNewestReportAction: true, setIsFloatingMessageCounterVisible}), }); // When the current user sends a comment @@ -229,31 +227,6 @@ describe('useReportActionsNewActionLiveTail', () => { expect(setIsFloatingMessageCounterVisible).toHaveBeenCalledWith(false); expect(reportScrollManager.scrollToBottom).toHaveBeenCalledTimes(1); expect(result.current.isScrollToBottomEnabled).toBe(true); - - // And the report is marked as read, so the "New" divider clears and the pill doesn't come back - expect(markNewestActionAsRead).toHaveBeenCalledTimes(1); - }); - - it('marks the report as read once openReport has loaded the newest actions after the user sends a comment', () => { - // Given a report whose newest actions are not loaded yet - const markNewestActionAsRead = jest.fn(); - const {rerender} = renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), {initialProps: buildParams({markNewestActionAsRead})}); - - // When the current user sends a comment, which starts the live-tail jump through openReport - act(() => { - newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT})); - }); - - // Then nothing is marked as read while the newest actions are still loading - expect(mockOpenReport).toHaveBeenCalledTimes(1); - expect(markNewestActionAsRead).not.toHaveBeenCalled(); - - // When openReport finishes and the newest actions arrive - rerender(buildParams({markNewestActionAsRead, prevIsLoadingInitialReportActions: true, reportLoadingState: {isLoadingInitialReportActions: false}})); - rerender(buildParams({markNewestActionAsRead, prevIsLoadingInitialReportActions: false, reportLoadingState: {isLoadingInitialReportActions: false}, hasNewestReportAction: true})); - - // Then the report is marked as read once, alongside hiding the pill - expect(markNewestActionAsRead).toHaveBeenCalledTimes(1); }); it('still loads the live tail when an unread marker is set and the newest actions are not loaded', () => { From 2403f6727718e20a455805bcd301a094dd194cee Mon Sep 17 00:00:00 2001 From: "Abdelrahman Khattab (via MelvinBot)" Date: Wed, 14 Oct 2026 21:05:48 +0000 Subject: [PATCH 8/8] Flush a pending scroll-to-bottom on list content size change instead of on the next frame Co-authored-by: Abdelrahman Khattab --- src/hooks/useReportActionsScroll.ts | 16 +----------- src/pages/inbox/report/ReportActionsList.tsx | 1 + tests/ui/ReportActionsListTest.tsx | 17 ++++++++++++ tests/unit/useReportActionsScrollTest.tsx | 27 -------------------- 4 files changed, 19 insertions(+), 42 deletions(-) diff --git a/src/hooks/useReportActionsScroll.ts b/src/hooks/useReportActionsScroll.ts index eef062777d87..dfcd02207383 100644 --- a/src/hooks/useReportActionsScroll.ts +++ b/src/hooks/useReportActionsScroll.ts @@ -113,7 +113,7 @@ type UseReportActionsScrollResult = { scrollToActionBadgeTarget: () => void; - /** Completes a live-tail scroll-to-bottom once the list has laid out; call on every list layout */ + /** Completes a live-tail scroll-to-bottom once the list has laid out; call on every list layout and content size change */ flushPendingScrollToBottom: () => void; /** Whether the list should be pinned to the visual top (transaction thread / money request) */ @@ -412,20 +412,6 @@ function useReportActionsScroll({ completeLiveTailPruneAfterScrollToBottom(); }; - const flushPendingScrollToBottomFromEffect = useEffectEvent(() => flushPendingScrollToBottom()); - - // In an aligned-to-top report (expense, invoice, transaction thread), the list's onLayout is not enough to flush a - // pending scroll: a new comment changes the content size, not the layout size, so onLayout may never fire. Regular - // chats don't need this, because their first scrollToBottom already lands. Flush on the next frame so this effect - // doesn't chain a synchronous setState. - useEffect(() => { - if (!shouldBeAlignedToTop || !isScrollToBottomEnabled) { - return; - } - const frame = requestAnimationFrame(() => flushPendingScrollToBottomFromEffect()); - return () => cancelAnimationFrame(frame); - }, [shouldBeAlignedToTop, isScrollToBottomEnabled]); - // Data is ready at the moment FlashList finishes its first render. const onLoad = () => { if (shouldDisablePillTracking) { diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index e240e744651e..c19330f2baa4 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -530,6 +530,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct onLoad={onLoad} onContentSizeChange={() => { trackVerticalScrolling(undefined); + flushPendingScrollToBottom(); }} /> diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 9b5234228c42..d4b59a15a428 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -160,6 +160,7 @@ type MockInvertedFlashListProps = { data?: OnyxTypes.ReportAction[]; extraData?: unknown; renderItem?: (info: {item: OnyxTypes.ReportAction; index: number}) => React.ReactElement | null; + onContentSizeChange?: () => void; }; const mockInvertedFlashList: jest.MockedFunction<(props: MockInvertedFlashListProps) => null> = jest.requireMock('@components/FlashList/InvertedFlashList'); @@ -830,6 +831,22 @@ describe('ReportActionsList (body)', () => { }); }); + describe('Pending scroll-to-bottom', () => { + it('flushes a pending scroll-to-bottom when the list content size changes', () => { + // Given the list is rendered + mockUseNetwork.mockReturnValue({isOffline: false}); + renderReportActionsList(); + + // When the list content size changes, as it does once FlashList lays out a newly sent comment. In an + // aligned-to-top report this is the only list event that follows, because onLayout does not fire. + getCapturedListProps()?.onContentSizeChange?.(); + + // Then the pending scroll-to-bottom is flushed, so the list lands on the new comment + const scrollHookResult = mockUseReportActionsScroll.mock.results.at(-1)?.value as {flushPendingScrollToBottom: jest.Mock}; + expect(scrollHookResult.flushPendingScrollToBottom).toHaveBeenCalledTimes(1); + }); + }); + describe('Concierge Side Panel', () => { const CONCIERGE_REPORT_ID = '123'; const CURRENT_USER_ACCOUNT_ID = 100; diff --git a/tests/unit/useReportActionsScrollTest.tsx b/tests/unit/useReportActionsScrollTest.tsx index e165349e8f12..c3e78bccf919 100644 --- a/tests/unit/useReportActionsScrollTest.tsx +++ b/tests/unit/useReportActionsScrollTest.tsx @@ -433,33 +433,6 @@ describe('useReportActionsScroll', () => { expect(mockSetIsScrollToBottomEnabled).toHaveBeenCalledWith(false); expect(mockCompleteLiveTailPrune).toHaveBeenCalledTimes(1); }); - - it('flushes a pending scroll-to-bottom without waiting for a list layout event in an aligned-to-top report', async () => { - // Given an expense report, and the live-tail hook asked for a scroll to bottom, as it does after the user sends a comment - mockIsMoneyRequestReport = true; - mockIsScrollToBottomEnabled = true; - - // When the hook renders and no list onLayout follows, because a new comment changes content size, not layout size - await renderScroll(); - - // Then the pending scroll is still flushed once, and the request is cleared - expect(mockScrollToBottom).toHaveBeenCalledTimes(1); - expect(mockSetIsScrollToBottomEnabled).toHaveBeenCalledWith(false); - expect(mockCompleteLiveTailPrune).toHaveBeenCalledTimes(1); - }); - - it('leaves a pending scroll-to-bottom to the list layout event in a regular chat', async () => { - // Given a regular chat, and the live-tail hook asked for a scroll to bottom - mockIsScrollToBottomEnabled = true; - - // When the hook renders - await renderScroll(); - - // Then nothing flushes on the next frame, because the first scrollToBottom in a regular chat already lands - expect(mockScrollToBottom).not.toHaveBeenCalled(); - expect(mockSetIsScrollToBottomEnabled).not.toHaveBeenCalled(); - expect(mockCompleteLiveTailPrune).not.toHaveBeenCalled(); - }); }); describe('onLoad', () => {