diff --git a/src/hooks/useReportActionsScroll.ts b/src/hooks/useReportActionsScroll.ts index 09bf411c31b8..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) */ 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/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts index 0c985a096315..c99fe489b71e 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'; @@ -101,18 +100,13 @@ 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. 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/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/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', () => {