Skip to content
Merged
2 changes: 1 addition & 1 deletion src/hooks/useReportActionsScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
Expand Down
1 change: 1 addition & 0 deletions src/pages/inbox/report/ReportActionsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct
onLoad={onLoad}
onContentSizeChange={() => {
trackVerticalScrolling(undefined);
flushPendingScrollToBottom();
}}
/>
</ReportActionsListPaddingView>
Expand Down
16 changes: 5 additions & 11 deletions src/pages/inbox/report/useReportActionsNewActionLiveTail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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') {
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/ReportActionsListTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/useReportActionsNewActionLiveTailTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Loading