From 7d4be685df226373baed9f0bae4386eddeb49079 Mon Sep 17 00:00:00 2001 From: chrispader Date: Wed, 9 Sep 2026 13:49:59 +0200 Subject: [PATCH 01/61] feat: migrate report actions to chronological LegendList --- patches/react-native/details.md | 3 +- ...act-native+0.86.0+002+fixMVCPAndroid.patch | 12 +- .../CellRendererComponent.tsx | 30 -- .../FlashList/InvertedFlashList/index.tsx | 28 -- src/components/FlashList/index.tsx | 2 +- src/components/FlashList/types.ts | 8 +- .../FlatList/FlatList/index.ios.tsx | 2 +- src/components/FlatList/FlatList/index.tsx | 2 +- .../KeyboardDismissibleFlatList/index.tsx | 2 +- .../MoneyRequestReportActionsList.tsx | 6 +- .../LocalPDFReceiptPreview/index.tsx | 9 +- .../ReceiptPDFOverlay/index.tsx | 6 +- .../useEmitComposerScrollEvents/index.ts | 11 +- src/hooks/useReportActionsListModel.ts | 4 + src/hooks/useReportActionsScroll.ts | 159 ++------ .../useReportScrollManager/index.native.ts | 7 +- src/hooks/useReportScrollManager/index.ts | 7 +- src/pages/inbox/ActionListContext.tsx | 14 +- src/pages/inbox/ActionListTypes.ts | 31 ++ .../report/MoneyReportContentCreated.tsx | 1 + .../ReportActionCompose/useEditMessage.ts | 2 +- .../inbox/report/ReportActionIndexContext.tsx | 26 +- src/pages/inbox/report/ReportActionItem.tsx | 19 +- .../report/ReportActionItemContentCreated.tsx | 1 + .../report/ReportActionItemMessageEdit.tsx | 4 +- src/pages/inbox/report/ReportActionsList.tsx | 242 +++++++++--- .../actionContents/ActionContentRouter.tsx | 2 +- .../actionContents/ChatMessageContent.tsx | 1 + .../report/shouldFollowActionBadgeTarget.ts | 12 +- .../report/useFollowActionBadgeTarget.ts | 4 +- .../useReportActionsNewActionLiveTail.ts | 20 +- .../useReportUnreadMessageScrollTracking.ts | 4 +- tests/ui/PaginationTest.tsx | 17 +- tests/ui/ReportActionsListTest.tsx | 353 ++++++++++++++++-- tests/unit/ReportActionsListThresholdTest.tsx | 64 ++-- tests/unit/hooks/useEditMessage.test.ts | 16 +- .../unit/shouldFollowActionBadgeTargetTest.ts | 8 +- .../useReportActionsNewActionLiveTailTest.ts | 25 +- tests/unit/useReportActionsScrollTest.tsx | 186 ++------- ...seReportUnreadMessageScrollTrackingTest.ts | 48 +++ 40 files changed, 849 insertions(+), 549 deletions(-) delete mode 100644 src/components/FlashList/InvertedFlashList/CellRendererComponent.tsx delete mode 100644 src/components/FlashList/InvertedFlashList/index.tsx create mode 100644 src/pages/inbox/ActionListTypes.ts diff --git a/patches/react-native/details.md b/patches/react-native/details.md index 4395fa022dbb..7b69f3793020 100644 --- a/patches/react-native/details.md +++ b/patches/react-native/details.md @@ -9,9 +9,10 @@ ### [react-native+0.86.0+002+fixMVCPAndroid.patch](react-native+0.86.0+002+fixMVCPAndroid.patch) -- Reason: Fixes content jumping issues with `MaintainVisibleContentPosition` on Android, particularly in bidirectional pagination scenarios. The patch makes two key improvements: +- Reason: Fixes content jumping issues with `MaintainVisibleContentPosition` on Android, particularly in bidirectional pagination scenarios. The patch: 1. Changes when the first visible view is calculated - now happens on scroll events instead of during Fabric's willMountItems lifecycle, which was causing incorrect updates 2. Improves first visible view selection logic to handle Fabric's z-index-based view reordering by finding the view with the smallest position that's still greater than the scroll position + 3. Preserves a positioned, zero-sized first child as a scroll anchor. LegendList moves this anchor to compensate for item measurements. Selecting its surrounding container or rejecting its empty frame prevents native scroll compensation and makes the chat jump as estimated rows shrink. - Upstream PR/issue: https://github.com/facebook/react-native/pull/46247 - E/App issue: 🛑 - PR Introducing Patch: https://github.com/Expensify/App/pull/46315 (introduced), https://github.com/Expensify/App/pull/45289 (refactored) diff --git a/patches/react-native/react-native+0.86.0+002+fixMVCPAndroid.patch b/patches/react-native/react-native+0.86.0+002+fixMVCPAndroid.patch index 223cce3db9b3..90b705c6df69 100644 --- a/patches/react-native/react-native+0.86.0+002+fixMVCPAndroid.patch +++ b/patches/react-native/react-native+0.86.0+002+fixMVCPAndroid.patch @@ -35,9 +35,16 @@ index 2bee605..ba26e7b 100644 for (i in config.minIndexForVisible until contentView.childCount) { val child = contentView.getChildAt(i) -@@ -128,27 +135,49 @@ internal class MaintainVisibleScrollPositionHelper( +@@ -128,27 +135,57 @@ internal class MaintainVisibleScrollPositionHelper( val position = if (horizontal) child.x + child.width else child.y + child.height ++ // Virtualized lists can use a zero-sized, positioned first child as their scroll anchor. ++ // Preserve it instead of choosing the container that holds all rendered items. ++ if (i == config.minIndexForVisible && child.width == 0 && child.height == 0 && position > currentScroll) { ++ firstVisibleView = child ++ break ++ } ++ // If the child is partially visible or this is the last child, select it as the anchor. - if (position > currentScroll || i == contentView.childCount - 1) { - firstVisibleViewRef = WeakReference(child) @@ -70,7 +77,8 @@ index 2bee605..ba26e7b 100644 } + val frame = Rect() + firstVisibleView.getHitRect(frame) -+ if (frame.width() > 0 || frame.height() > 0) { ++ // Zero-sized anchors have a meaningful position even though they have no area. ++ if (frame.width() > 0 || frame.height() > 0 || frame.left != 0 || frame.top != 0) { + prevFirstVisibleFrame = frame + } else { + prevFirstVisibleFrame = null diff --git a/src/components/FlashList/InvertedFlashList/CellRendererComponent.tsx b/src/components/FlashList/InvertedFlashList/CellRendererComponent.tsx deleted file mode 100644 index bc11ccf61296..000000000000 --- a/src/components/FlashList/InvertedFlashList/CellRendererComponent.tsx +++ /dev/null @@ -1,30 +0,0 @@ -import type {StyleProp, ViewProps, ViewStyle} from 'react-native'; - -import React from 'react'; -import {View} from 'react-native'; - -type CellRendererComponentProps = ViewProps & { - index: number; - style?: StyleProp; -}; - -function CellRendererComponent(props: CellRendererComponentProps) { - return ( - - ); -} - -export default CellRendererComponent; diff --git a/src/components/FlashList/InvertedFlashList/index.tsx b/src/components/FlashList/InvertedFlashList/index.tsx deleted file mode 100644 index b343f95b8be1..000000000000 --- a/src/components/FlashList/InvertedFlashList/index.tsx +++ /dev/null @@ -1,28 +0,0 @@ -import type FlatListRefType from '@components/FlashList/types'; - -import type {FlashListProps} from '@shopify/flash-list'; - -import React from 'react'; - -import FlashList from '..'; -import CellRendererComponent from './CellRendererComponent'; - -type InvertedFlashListProps = FlashListProps & { - data: T[]; - keyExtractor: (item: T, index: number) => string; - - /** Ref to the underlying list instance. */ - ref: FlatListRefType; -}; - -function InvertedFlashList(props: InvertedFlashListProps) { - return ( - - {...props} - inverted - CellRendererComponent={CellRendererComponent} - /> - ); -} - -export default InvertedFlashList; diff --git a/src/components/FlashList/index.tsx b/src/components/FlashList/index.tsx index e98507c9a3bd..f9a7b9528735 100644 --- a/src/components/FlashList/index.tsx +++ b/src/components/FlashList/index.tsx @@ -7,7 +7,7 @@ import {FlashList as ShopifyFlashList} from '@shopify/flash-list'; import React from 'react'; function FlashList({onScroll: onScrollProp, inverted, ...restProps}: FlashListProps) { - const emitComposerScrollEvents = useEmitComposerScrollEvents({enabled: true, inverted}); + const emitComposerScrollEvents = useEmitComposerScrollEvents({enabled: !!inverted}); const handleScroll = (e: NativeSyntheticEvent) => { onScrollProp?.(e); diff --git a/src/components/FlashList/types.ts b/src/components/FlashList/types.ts index cf7718d3d148..8563ec9959a4 100644 --- a/src/components/FlashList/types.ts +++ b/src/components/FlashList/types.ts @@ -1,7 +1,3 @@ -import type {RefObject} from 'react'; -import type {FlatList} from 'react-native'; +import type ActionListRefType from '@pages/inbox/ActionListTypes'; -/** Ref to the underlying list instance attached via `ref={}`. */ -type FlatListRefType = RefObject | null> | null; - -export default FlatListRefType; +export default ActionListRefType; diff --git a/src/components/FlatList/FlatList/index.ios.tsx b/src/components/FlatList/FlatList/index.ios.tsx index 5fc8e9b546a7..7892a044825c 100644 --- a/src/components/FlatList/FlatList/index.ios.tsx +++ b/src/components/FlatList/FlatList/index.ios.tsx @@ -42,7 +42,7 @@ function CustomFlatList({ [onMomentumScrollEnd], ); - const emitComposerScrollEvents = useEmitComposerScrollEvents({enabled: !enableAnimatedKeyboardDismissal, inverted: restProps.inverted}); + const emitComposerScrollEvents = useEmitComposerScrollEvents({enabled: !enableAnimatedKeyboardDismissal && !!restProps.inverted}); const handleScroll = useCallback( (e: NativeSyntheticEvent) => { onScrollProp?.(e); diff --git a/src/components/FlatList/FlatList/index.tsx b/src/components/FlatList/FlatList/index.tsx index fb977c60a232..9c13ff4c7068 100644 --- a/src/components/FlatList/FlatList/index.tsx +++ b/src/components/FlatList/FlatList/index.tsx @@ -245,7 +245,7 @@ function MVCPFlatList({ }; }, []); - const emitComposerScrollEvents = useEmitComposerScrollEvents({enabled: true, inverted: restProps.inverted}); + const emitComposerScrollEvents = useEmitComposerScrollEvents({enabled: !!restProps.inverted}); const handleScroll = useCallback( (e: NativeSyntheticEvent) => { onScrollProp?.(e); diff --git a/src/components/KeyboardDismissibleFlatList/index.tsx b/src/components/KeyboardDismissibleFlatList/index.tsx index 043dba4d39db..3e8109d2d0e6 100644 --- a/src/components/KeyboardDismissibleFlatList/index.tsx +++ b/src/components/KeyboardDismissibleFlatList/index.tsx @@ -11,7 +11,7 @@ import {useKeyboardDismissibleFlatListActions} from './KeyboardDismissibleFlatLi function KeyboardDismissibleFlatList({onScroll: onScrollProp, inverted, ref, ...restProps}: AnimatedFlatListWithCellRendererProps) { const {onScroll: onScrollHandleKeyboard} = useKeyboardDismissibleFlatListActions(); - const emitComposerScrollEvents = useEmitComposerScrollEvents({enabled: true, inverted}); + const emitComposerScrollEvents = useEmitComposerScrollEvents({enabled: !!inverted}); const additionalOnScroll = useAnimatedScrollHandler({ onScroll: emitComposerScrollEvents, diff --git a/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx b/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx index 1b8f09edaa03..6e491a2fd1c7 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx @@ -639,8 +639,12 @@ function MoneyRequestReportActionsList({onLayout}: MoneyRequestReportListProps) hasNextActionMadeBySameActor(visibleReportActions, indexWithinReportActions, isOffline); const shouldDisableContextMenuForConciergeDraft = isDraftPendingCompletion && draftReportActionID === reportAction.reportActionID; + // This value cannot be memoized, because it is based on the indexWithinReportActions which changes on every render. + // eslint-disable-next-line react/jsx-no-constructed-context-values + const reportActionIndexContextValue = {index: indexWithinReportActions, isNewest: indexWithinReportActions === visibleReportActions.length - 1}; + return ( - + (undefined); - const [pageAspectRatio, setPageAspectRatio] = useState(undefined); + const [failedToLoad, setFailedToLoad] = useReportActionItemState(false); + const [containerSize, setContainerSize] = useReportActionItemState<{width: number; height: number} | undefined>(undefined); + const [pageAspectRatio, setPageAspectRatio] = useReportActionItemState(undefined); const handleDocumentLoadSuccess = (pdf: PDFDocumentProxy) => { pdf.getPage(1) diff --git a/src/components/ReportActionItem/ReceiptPDFOverlay/index.tsx b/src/components/ReportActionItem/ReceiptPDFOverlay/index.tsx index 6e64cd2d4184..24c0014f920c 100644 --- a/src/components/ReportActionItem/ReceiptPDFOverlay/index.tsx +++ b/src/components/ReportActionItem/ReceiptPDFOverlay/index.tsx @@ -3,6 +3,8 @@ import useThemeStyles from '@hooks/useThemeStyles'; import addEncryptedAuthTokenToURL from '@libs/addEncryptedAuthTokenToURL'; +import {useReportActionItemState} from '@pages/inbox/report/ReportActionIndexContext'; + import variables from '@styles/variables'; import {retrieveMaxCanvasArea, retrieveMaxCanvasHeight, retrieveMaxCanvasWidth} from '@userActions/CanvasSize'; @@ -10,7 +12,7 @@ import {retrieveMaxCanvasArea, retrieveMaxCanvasHeight, retrieveMaxCanvasWidth} import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import React, {useEffect, useState} from 'react'; +import React, {useEffect} from 'react'; import {PDFPreviewer} from 'react-fast-pdf'; import {View} from 'react-native'; @@ -52,7 +54,7 @@ function ReceiptPDFOverlay({sourceURL, isAuthTokenRequired = true, onLoadFailure // Track which URL failed so hasFailed resets automatically when fileURL changes (e.g. after auth token refresh), // mirroring the pattern in ThumbnailImage. No useEffect needed — the comparison runs synchronously during render. - const [failedURL, setFailedURL] = useState(null); + const [failedURL, setFailedURL] = useReportActionItemState(null); const hasFailed = failedURL !== null && failedURL === fileURL; // If the PDF can't be rendered, fall back to the thumbnail underneath by rendering nothing. diff --git a/src/hooks/useEmitComposerScrollEvents/index.ts b/src/hooks/useEmitComposerScrollEvents/index.ts index ba65bfcdcf5e..cfd42ee98773 100644 --- a/src/hooks/useEmitComposerScrollEvents/index.ts +++ b/src/hooks/useEmitComposerScrollEvents/index.ts @@ -5,19 +5,16 @@ import {DeviceEventEmitter} from 'react-native'; type UseEmitComposerScrollEventsOptions = { enabled?: boolean; - inverted: boolean | null | undefined; }; /** * This is used to trigger scroll behavior in the composer on web. On native, this is a no-op. - * The scroll events are only emitted when the list is inverted, since it is only used in the report screen in combination with the composer. * Since our custom FlatList implementation can either be a `KeyboardDismissibleFlatList` or a regular `FlatList`, * we need to emit the scroll events inside the scroll handler of the specific implementation. - * @param inverted - Whether the list is inverted. * @returns A function that can be used to emit the scroll events. */ function useEmitComposerScrollEvents(options?: UseEmitComposerScrollEventsOptions) { - const {enabled = true, inverted} = options ?? {}; + const {enabled = true} = options ?? {}; const lastScrollEvent = useRef(null); const scrollEndTimeout = useRef(null); @@ -28,7 +25,7 @@ function useEmitComposerScrollEvents(options?: UseEmitComposerScrollEventsOption * invokes the onScroll callback function from props. */ const onScroll = () => { - if (!enabled || !inverted) { + if (!enabled) { return; } @@ -44,7 +41,7 @@ function useEmitComposerScrollEvents(options?: UseEmitComposerScrollEventsOption * Emits when the scrolling has ended. */ const onScrollEnd = () => { - if (!enabled || !inverted) { + if (!enabled) { return; } @@ -67,7 +64,7 @@ function useEmitComposerScrollEvents(options?: UseEmitComposerScrollEventsOption * */ const emitComposerScrollEvents = () => { - if (!enabled || !inverted) { + if (!enabled) { return; } diff --git a/src/hooks/useReportActionsListModel.ts b/src/hooks/useReportActionsListModel.ts index b3e438fd3adb..67e39bdd688d 100644 --- a/src/hooks/useReportActionsListModel.ts +++ b/src/hooks/useReportActionsListModel.ts @@ -146,7 +146,11 @@ function useReportActionsListModel(reportID: string, isReportLoadPending: boolea const state = { report, hasOnceLoadedReportActions, + hasOlderActions, hasNewerActions, + isLoadingOlderReportActions, + hasLoadingOlderReportActionsError, + oldestReportActionID: currentReportOldestActionID, sortedAllReportActions, oldestUnreadReportAction, transactionThreadReport, diff --git a/src/hooks/useReportActionsScroll.ts b/src/hooks/useReportActionsScroll.ts index b899c31b1312..3f5ce23335be 100644 --- a/src/hooks/useReportActionsScroll.ts +++ b/src/hooks/useReportActionsScroll.ts @@ -34,8 +34,6 @@ import useNetworkWithOfflineStatus from './useNetworkWithOfflineStatus'; import useOnyx from './useOnyx'; import usePrevious from './usePrevious'; import useReportScrollManager from './useReportScrollManager'; -import useScrollToEndOnNewMessageReceived from './useScrollToEndOnNewMessageReceived'; -import useWindowDimensions from './useWindowDimensions'; type UseReportActionsScrollParams = { /** The Concierge chat report */ @@ -55,15 +53,13 @@ type UseReportActionsScrollParams = { /** Sorted actions that should be visible to the user */ sortedVisibleReportActions: OnyxTypes.ReportAction[]; - /** Actions actually rendered by the list (may include a synthetic draft), used for mount scroll positioning */ + /** Actions actually rendered by the list in chronological order (may include a synthetic draft), used for scroll positioning */ renderedVisibleReportActions: OnyxTypes.ReportAction[]; /** Extracts the list key for an action; used to locate the initial scroll target */ keyExtractor: (item: OnyxTypes.ReportAction) => string; /** Whether the user has scrolled past the "visible" threshold */ - hasScrolledOverThreshold: boolean; - /** Marks the newest action as read and clears any pending skipped mark-as-read */ markNewestActionAsRead: () => void; @@ -79,9 +75,6 @@ type UseReportActionsScrollParams = { /** Whether the report has newer actions to load */ hasNewerActions: boolean; - /** Stable key that changes when a streamed concierge draft becomes visible, used to trigger autoscroll */ - draftAutoScrollKey: string; - /** The index of the action badge target in the rendered actions list (-1 if none) */ actionBadgeTargetIndex: number; @@ -112,28 +105,16 @@ type UseReportActionsScrollResult = { scrollToActionBadgeTarget: () => void; - /** Completes a live-tail scroll-to-bottom once the list has laid out; call on every list layout */ - flushPendingScrollToBottom: () => void; - /** Whether the list should be pinned to the visual top (transaction thread / money request) */ shouldBeAlignedToTop: boolean; - /** Whether the list should focus to the visual top on mount */ - shouldFocusToTopOnMount: boolean; - - /** The initial scroll target key for the list */ - initialScrollKey: string | undefined; - - /** maintainVisibleContentPosition config for the inverted list */ - maintainVisibleContentPosition: {disabled: boolean; autoscrollToBottomThreshold?: number; animateAutoScrollToBottom?: boolean}; - /** The index the list should scroll to on mount (undefined to keep default position) */ initialScrollIndex: number | undefined; /** Positioning params (viewPosition/viewOffset) paired with initialScrollIndex */ initialScrollIndexParams: {viewPosition?: number; viewOffset?: number} | undefined; - /** onLoad handler that disables autoscroll-to-top once the initial render settles */ + /** onLoad handler that enables pill tracking after initial positioning settles */ onLoad: () => void; }; @@ -146,13 +127,11 @@ function useReportActionsScroll({ sortedVisibleReportActions, renderedVisibleReportActions, keyExtractor, - hasScrolledOverThreshold, markNewestActionAsRead, completeSkippedMarkAsRead, unreadMarkerReportActionID, unreadMarkerReportActionIndex, hasNewerActions, - draftAutoScrollKey, actionBadgeTargetIndex, sortedAllReportActionsForPagination, treatAsNoPaginationAnchor, @@ -160,7 +139,6 @@ function useReportActionsScroll({ }: UseReportActionsScrollParams): UseReportActionsScrollResult { const reportScrollManager = useReportScrollManager(); const {scrollOffsetRef} = useActionListContext(); - const {windowHeight} = useWindowDimensions(); const route = useRoute>(); const linkedReportActionID = route?.params?.reportActionID; const backTo = route?.params?.backTo; @@ -204,18 +182,8 @@ function useReportActionsScroll({ } const shouldFocusToTopOnMount = shouldBeAlignedToTop && !initialScrollKey && !shouldScrollToLatestOnOpen; - const shouldMaintainVisibleContentPosition = hasScrolledOverThreshold || shouldFocusToTopOnMount; - const [shouldAutoscrollToBottom, setShouldAutoscrollToBottom] = useState(shouldFocusToTopOnMount); const [shouldDisablePillTracking, setShouldDisablePillTracking] = useState(!!initialScrollKey); - const maintainVisibleContentPosition = { - disabled: !shouldMaintainVisibleContentPosition, - // Focus-to-top mode: once autoscroll is released, keep the threshold at 0 rather than - // removing it — FlashList only clears its pending-autoscroll flag while threshold >= 0, - // otherwise the next content change (e.g. mark-as-unread) scrolls back to top. - ...(shouldFocusToTopOnMount ? {autoscrollToBottomThreshold: shouldAutoscrollToBottom ? CONST.REPORT.ACTIONS.ACTION_VISIBLE_THRESHOLD : 0, animateAutoScrollToBottom: false} : {}), - }; - const {isFloatingMessageCounterVisible, setIsFloatingMessageCounterVisible, isActionBadgeAboveViewport, trackVerticalScrolling, onViewableItemsChanged, updatePillVisibility} = useReportUnreadMessageScrollTracking({ reportID, @@ -223,7 +191,7 @@ function useReportActionsScroll({ onUnreadActionVisible: completeSkippedMarkAsRead, hasNewerActions, unreadMarkerReportActionIndex, - isInverted: true, + isInverted: false, shouldDisablePillTracking, onTrackScrolling: (event: NativeSyntheticEvent) => { scrollOffsetRef.current = event.nativeEvent.contentOffset.y; @@ -247,7 +215,7 @@ function useReportActionsScroll({ hasNewerActions, linkedReportActionID, hasNewestReportAction, - sortedVisibleReportActions, + renderedVisibleReportActions, sortedAllReportActionsForPagination, reportActionPages, setTreatAsNoPaginationAnchor, @@ -256,57 +224,17 @@ function useReportActionsScroll({ reportLoadingState, }); - useScrollToEndOnNewMessageReceived({ - sizeChangeType: 'changed', - scrollOffsetRef, - lastActionID: lastAction?.reportActionID, - visibleActionsLength: sortedVisibleReportActions.length, - hasNewestReportAction, - setIsFloatingMessageCounterVisible, - scrollToEnd: reportScrollManager.scrollToBottom, - // Include reportID so list-length / last-id baselines reset when the same screen instance shows another report. - resetKey: `${reportID}:${linkedReportActionID}`, - }); - - const previousDraftAutoScrollKey = usePrevious(draftAutoScrollKey); - + // Consume an explicit live-tail request after the data render, not on an unrelated future + // viewport layout (for example, opening the keyboard after the user has scrolled away). + // Incoming messages and streaming draft growth are followed by LegendList itself. useEffect(() => { - if (!draftAutoScrollKey || previousDraftAutoScrollKey === draftAutoScrollKey) { - return; - } - - if (scrollOffsetRef.current >= CONST.REPORT.ACTIONS.AUTOSCROLL_TO_TOP_THRESHOLD || !hasNewestReportAction) { + if (!isScrollToBottomEnabled) { return; } - - setIsFloatingMessageCounterVisible(false); - requestAnimationFrame(() => { - reportScrollManager.scrollToBottom(); - }); - }, [draftAutoScrollKey, hasNewestReportAction, previousDraftAutoScrollKey, reportScrollManager, scrollOffsetRef, setIsFloatingMessageCounterVisible]); - - const scheduleInitialScrollToBottom = useEffectEvent(() => { - if (initialScrollKey) { - return undefined; - } - - return TransitionTracker.runAfterTransitions({ - callback: () => { - if (shouldFocusToTopOnMount) { - return; - } - setIsFloatingMessageCounterVisible(false); - reportScrollManager.scrollToBottom(); - }, - waitForUpcomingTransition: true, - }); - }); - - // The initial scroll-to-bottom must be scheduled exactly once, on mount; re-running it as deps change would yank the user back down while they read history. - useEffect(() => { - const handle = scheduleInitialScrollToBottom(); - return () => handle?.cancel(); - }, []); + reportScrollManager.scrollToBottom(); + setIsScrollToBottomEnabled(false); + completeLiveTailPruneAfterScrollToBottom(); + }, [isScrollToBottomEnabled, reportScrollManager, setIsScrollToBottomEnabled, completeLiveTailPruneAfterScrollToBottom]); // Clear the shouldScrollToLatest route param once the mount scroll above has consumed it, so a later remount of // this report doesn't pull the user down again. MoneyRequestReportActionsList clears it the same way for the @@ -393,64 +321,33 @@ function useReportActionsScroll({ if (actionBadgeTargetIndex < 0) { return; } - reportScrollManager.scrollToIndex(actionBadgeTargetIndex, {viewPosition: 1, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}); + reportScrollManager.scrollToIndex(actionBadgeTargetIndex, {viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}); }; - const flushPendingScrollToBottom = () => { - if (!isScrollToBottomEnabled) { - return; - } - reportScrollManager.scrollToBottom(); - setIsScrollToBottomEnabled(false); - completeLiveTailPruneAfterScrollToBottom(); - }; - - // Data is ready at the moment FlashList finishes its first render. + // Data is ready when LegendList finishes its first render. const onLoad = () => { - if (shouldDisablePillTracking) { - // Wait one frame so the initial positioning can settle, then disable it. - requestAnimationFrame(() => { - setShouldDisablePillTracking(false); - updatePillVisibility(); - }); - } - if (!shouldFocusToTopOnMount) { + if (!shouldDisablePillTracking) { return; } - if (!reportLoadingState?.hasOnceLoadedReportActions && !isOffline) { - return; - } - // Wait one frame so the initial autoscroll-to-top can settle, then disable it. - requestAnimationFrame(() => setShouldAutoscrollToBottom(false)); - }; - const prevHasOnceLoadedReportActions = usePrevious(reportLoadingState?.hasOnceLoadedReportActions); - // Data finished initial loading after the list mounted. onLoad has already fired, so we need - // a separate trigger to turn off autoscroll-to-top. - useEffect(() => { - if (!shouldFocusToTopOnMount || !shouldAutoscrollToBottom) { - return; - } - if (prevHasOnceLoadedReportActions || !reportLoadingState?.hasOnceLoadedReportActions) { - return; - } - requestAnimationFrame(() => setShouldAutoscrollToBottom(false)); - }, [shouldFocusToTopOnMount, shouldAutoscrollToBottom, prevHasOnceLoadedReportActions, reportLoadingState?.hasOnceLoadedReportActions]); + // Wait one frame so the initial positioning can settle, then disable it. + requestAnimationFrame(() => { + setShouldDisablePillTracking(false); + updatePillVisibility(); + }); + }; // Decide where the list should be positioned on mount. - // 1. If we're opening a linked message (initialScrollKey), find that action in the list and scroll it to the top - // of the viewport (viewPosition: 1) with a small offset so the message above is partly visible. - // 2. Otherwise, if the report should be opened at top (ex: for transaction threads), scroll to the top message and offset by - // the window height so we land at top of the top message for sure. + // 1. If we're opening a linked or unread message, find that action in the chronological list. + // 2. Otherwise, aligned-to-top reports start at the first action. const targetIndex = initialScrollKey ? renderedVisibleReportActions.findIndex((item) => keyExtractor(item) === initialScrollKey) : -1; let initialScrollIndex: number | undefined; let initialScrollIndexParams: {viewPosition?: number; viewOffset?: number} | undefined; - if (targetIndex > 0) { + if (targetIndex >= 0) { initialScrollIndex = targetIndex; - initialScrollIndexParams = {viewPosition: 1, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}; + initialScrollIndexParams = {viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}; } else if (shouldFocusToTopOnMount) { - initialScrollIndex = renderedVisibleReportActions.length - 1; - initialScrollIndexParams = {viewOffset: windowHeight}; + initialScrollIndex = 0; } return { @@ -460,11 +357,7 @@ function useReportActionsScroll({ isActionBadgeAboveViewport, scrollToBottomAndMarkReportAsRead, scrollToActionBadgeTarget, - flushPendingScrollToBottom, shouldBeAlignedToTop, - shouldFocusToTopOnMount, - initialScrollKey, - maintainVisibleContentPosition, initialScrollIndex, initialScrollIndexParams, onLoad, diff --git a/src/hooks/useReportScrollManager/index.native.ts b/src/hooks/useReportScrollManager/index.native.ts index 97c9a92cf1f5..c1823e3dc0a8 100644 --- a/src/hooks/useReportScrollManager/index.native.ts +++ b/src/hooks/useReportScrollManager/index.native.ts @@ -21,17 +21,14 @@ function useReportScrollManager(): ReportScrollManagerData { listRef.current.scrollToIndex({index, animated, viewOffset, viewPosition}); }; - /** - * Scroll to the bottom of the inverted FlatList. - * When FlatList is inverted it's "bottom" is really it's top - */ + /** Scroll to the bottom of the chronological action list. */ const scrollToBottom = () => { const listRef = getListRef(); if (!listRef?.current) { return; } - listRef.current.scrollToIndex({animated: false, index: 0}); + listRef.current.scrollToEnd({animated: false}); }; /** diff --git a/src/hooks/useReportScrollManager/index.ts b/src/hooks/useReportScrollManager/index.ts index a2e615e7d6d6..09ce64a5687c 100644 --- a/src/hooks/useReportScrollManager/index.ts +++ b/src/hooks/useReportScrollManager/index.ts @@ -18,17 +18,14 @@ function useReportScrollManager(): ReportScrollManagerData { listRef.current.scrollToIndex({index, animated, viewOffset, viewPosition}); }; - /** - * Scroll to the bottom of the inverted FlatList. - * When FlatList is inverted it's "bottom" is really it's top - */ + /** Scroll to the bottom of the chronological action list. */ const scrollToBottom = () => { const listRef = getListRef(); if (!listRef?.current) { return; } - listRef.current.scrollToIndex({animated: false, index: 0}); + listRef.current.scrollToEnd({animated: false}); }; /** diff --git a/src/pages/inbox/ActionListContext.tsx b/src/pages/inbox/ActionListContext.tsx index a2764b32c88b..f620b9e53724 100644 --- a/src/pages/inbox/ActionListContext.tsx +++ b/src/pages/inbox/ActionListContext.tsx @@ -1,10 +1,10 @@ -import type FlatListRefType from '@components/FlashList/types'; - import type {ReactNode, RefObject} from 'react'; -import type {FlatList} from 'react-native'; import React, {createContext, useContext, useLayoutEffect, useRef} from 'react'; +import type ActionListRefType from './ActionListTypes'; +import type {ActionListRef} from './ActionListTypes'; + type ActionListContextType = { scrollOffsetRef: RefObject; @@ -12,10 +12,10 @@ type ActionListContextType = { getScrollOffset: () => number; /** Each list publishes its locally-owned ref on mount; pass `null` to clear on unmount. */ - registerListRef: (ref: FlatListRefType) => void; + registerListRef: (ref: ActionListRefType) => void; /** Reads the currently registered list ref. Call from handlers only, never during render. */ - getListRef: () => FlatListRefType; + getListRef: () => ActionListRefType; }; const ActionListContext = createContext({ @@ -35,7 +35,7 @@ function useActionListContext() { */ function useActionListRef() { const {registerListRef} = useActionListContext(); - const listRef = useRef(null); + const listRef = useRef(null); useLayoutEffect(() => { registerListRef(listRef); @@ -49,7 +49,7 @@ function useActionListRef() { function ActionListContextProvider({children}: {children: ReactNode}) { // Each list owns its own ref locally and publishes it here on mount; only the register/get // callbacks live in context, so attaching `ref={}` stays local to each list. - const listRefHolder = useRef(null); + const listRefHolder = useRef(null); const scrollOffsetRef = useRef(0); const value: ActionListContextType = { diff --git a/src/pages/inbox/ActionListTypes.ts b/src/pages/inbox/ActionListTypes.ts new file mode 100644 index 000000000000..d429168c46cb --- /dev/null +++ b/src/pages/inbox/ActionListTypes.ts @@ -0,0 +1,31 @@ +import type {RefObject} from 'react'; + +type ScrollToIndexParams = { + animated?: boolean; + index: number; + viewOffset?: number; + viewPosition?: number; +}; + +type ScrollToOffsetParams = { + animated?: boolean; + offset: number; +}; + +type ScrollToEndParams = { + animated?: boolean; +}; + +/** Common imperative API used by the report scroll manager across FlatList, FlashList, and LegendList. */ +type ActionListRef = { + scrollToIndex: (params: ScrollToIndexParams) => void; + scrollToOffset: (params: ScrollToOffsetParams) => void; + scrollToEnd: (params?: ScrollToEndParams) => void; + getNativeScrollRef?: () => unknown; +}; + +/** Ref to the underlying list instance attached via `ref={}`. */ +type ActionListRefType = RefObject | null; + +export default ActionListRefType; +export type {ActionListRef}; diff --git a/src/pages/inbox/report/MoneyReportContentCreated.tsx b/src/pages/inbox/report/MoneyReportContentCreated.tsx index 46096456685d..efd9f93c761b 100644 --- a/src/pages/inbox/report/MoneyReportContentCreated.tsx +++ b/src/pages/inbox/report/MoneyReportContentCreated.tsx @@ -75,6 +75,7 @@ function MoneyReportContentCreated({report, policy, transaction, transactionThre (0); +type ReportActionPosition = { + index: number; + isNewest: boolean; + isRecycling?: boolean; +}; + +const ReportActionIndexContext = createContext({index: 0, isNewest: false}); + +/** + * Uses LegendList's recycling-aware state in the main report list and behaves like useState in shared, non-recycled lists. + */ +function useReportActionItemState(initialState: State | (() => State)): [State, Dispatch>] { + const {isRecycling = false} = useContext(ReportActionIndexContext); + const state = useState(initialState); + const recyclingState = useRecyclingState(initialState); + return isRecycling ? [...recyclingState] : state; +} +export {useReportActionItemState}; export default ReportActionIndexContext; diff --git a/src/pages/inbox/report/ReportActionItem.tsx b/src/pages/inbox/report/ReportActionItem.tsx index 7011ba259e63..37c041d5b16e 100644 --- a/src/pages/inbox/report/ReportActionItem.tsx +++ b/src/pages/inbox/report/ReportActionItem.tsx @@ -79,12 +79,13 @@ import {isEmptyObject, isEmptyValueObject} from '@src/types/utils/EmptyObject'; import type {GestureResponderEvent, TextInput} from 'react-native'; import type {OnyxEntry} from 'react-native-onyx'; +import {useRecyclingEffect} from '@legendapp/list/react-native'; import {useNavigation} from '@react-navigation/native'; import {isTrackIntentUserSelector} from '@selectors/Onboarding'; import {personalDetailsDisplayNameSelector} from '@selectors/PersonalDetails'; import {deepEqual} from 'fast-equals'; import mapValues from 'lodash/mapValues'; -import React, {useContext, useEffect, useRef, useState} from 'react'; +import React, {useContext, useEffect, useRef} from 'react'; import {Keyboard, View} from 'react-native'; import type {ContextMenuAnchor} from './ContextMenu/ReportActionContextMenu'; @@ -95,6 +96,7 @@ import MiniReportActionContextMenu from './ContextMenu/MiniReportActionContextMe import {hideContextMenu, hideDeleteModal, isActiveReportAction, showContextMenu} from './ContextMenu/ReportActionContextMenu'; import LinkPreviewer from './LinkPreviewer'; import {useReportActionActiveEdit} from './ReportActionEditMessageContext'; +import {useReportActionItemState} from './ReportActionIndexContext'; import ReportActionItemContentCreated from './ReportActionItemContentCreated'; import ReportActionItemFrame from './ReportActionItemFrame'; import ReportActionItemThread from './ReportActionItemThread'; @@ -203,16 +205,19 @@ function ReportActionItem({ const theme = useTheme(); const styles = useThemeStyles(); const StyleUtils = useStyleUtils(); - const [isContextMenuActive, setIsContextMenuActive] = useState(() => isActiveReportAction(action.reportActionID)); - const [isEmojiPickerActive, setIsEmojiPickerActive] = useState(); - const [isPaymentMethodPopoverActive, setIsPaymentMethodPopoverActive] = useState(); - const [isHidden, setIsHidden] = useState(false); + const [isContextMenuActive, setIsContextMenuActive] = useReportActionItemState(() => isActiveReportAction(action.reportActionID)); + const [isEmojiPickerActive, setIsEmojiPickerActive] = useReportActionItemState(undefined); + const [isPaymentMethodPopoverActive, setIsPaymentMethodPopoverActive] = useReportActionItemState(undefined); + const [isHidden, setIsHidden] = useReportActionItemState(false); const {isActiveReportAction: isActiveReactionListReportAction, hideReactionList} = useContext(ReactionListContext); const {updateHiddenAttachments} = useContext(AttachmentModalContext); const popoverAnchorRef = useRef>(null); const downloadedPreviews = useRef([]); + useRecyclingEffect(() => { + downloadedPreviews.current = []; + }); const isReportActionLinked = linkedReportActionID && action.reportActionID && linkedReportActionID === action.reportActionID; - const [isReportActionActive, setIsReportActionActive] = useState(!!isReportActionLinked); + const [isReportActionActive, setIsReportActionActive] = useReportActionItemState(!!isReportActionLinked); const shouldBreakGrouping = shouldBreakAccessibilityGrouping(); const isScreenReaderActive = Accessibility.useScreenReaderStatus(); @@ -356,7 +361,7 @@ function ReportActionItem({ return; } setIsHidden(false); - }, [latestDecision, action]); + }, [latestDecision, action, setIsHidden]); const toggleContextMenuFromActiveReportAction = () => { setIsContextMenuActive(isActiveReportAction(action.reportActionID)); diff --git a/src/pages/inbox/report/ReportActionItemContentCreated.tsx b/src/pages/inbox/report/ReportActionItemContentCreated.tsx index c284acd7a82a..d8fe6333c1b3 100644 --- a/src/pages/inbox/report/ReportActionItemContentCreated.tsx +++ b/src/pages/inbox/report/ReportActionItemContentCreated.tsx @@ -96,6 +96,7 @@ function ReportActionItemContentCreated({parentReportAction, transactionID, draf (undefined); + const emitComposerScrollEvents = useEmitComposerScrollEvents({enabled: true}); useEffect(() => { didLayout.current = false; + lastRequestedOldestActionIDRef.current = undefined; }, [reportID]); + useEffect(() => { + if (isLoadingOlderReportActions && !hasLoadingOlderReportActionsError) { + return; + } + lastRequestedOldestActionIDRef.current = undefined; + }, [isLoadingOlderReportActions, hasLoadingOlderReportActionsError]); + useLinkedMessageOfflineLoading({reportID: report?.reportID ?? reportID, reportActionIDFromRoute}); - // Remount the list when the deep-linked message or unread anchor changes (scroll positioning), or when the report changes. - const listID = [reportID, reportActionIDFromRoute, hasOnceLoadedReportActions ? undefined : oldestUnreadReportAction?.reportActionID].join(':'); + // OpenReport can first provide a tiny cached page and then replace it with the hydrated page. Remounting + // gives the complete dataset a fresh initial layout so initialScrollAtEnd targets its actual end. + const listID = [ + reportID, + reportActionIDFromRoute, + hasOnceLoadedReportActions ? 'hydrated' : 'initial', + hasOnceLoadedReportActions ? undefined : oldestUnreadReportAction?.reportActionID, + ].join(':'); const [reportNameValuePairs] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${reportID}`); const isReportArchived = !!isArchivedReport(reportNameValuePairs); @@ -172,6 +236,24 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct const {getScrollOffset} = useActionListContext(); const listRef = useActionListRef(); + const legendListRef = useRef(null); + + useImperativeHandle( + listRef, + (): ActionListRef => ({ + getNativeScrollRef: () => legendListRef.current?.getNativeScrollRef(), + scrollToEnd: (options) => { + legendListRef.current?.scrollToEnd(options); + }, + scrollToIndex: (options) => { + legendListRef.current?.scrollToIndex(options); + }, + scrollToOffset: (options) => { + legendListRef.current?.scrollToOffset(options); + }, + }), + [], + ); const {draftReportAction, isDraftPendingCompletion} = useConciergeDraft(); const {clearDraft, revealDraftFromReportAction} = useConciergeDraftActions(); @@ -181,7 +263,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct const [hasScrolledOverThreshold, setHasScrolledOverThreshold] = useState(() => getScrollOffset() >= CONST.REPORT.ACTIONS.ACTION_VISIBLE_THRESHOLD); - const {unreadMarkerReportActionID, unreadMarkerReportActionIndex} = useUnreadMarker({ + const {unreadMarkerReportActionID} = useUnreadMarker({ reportID, sortedVisibleReportActions, sortedReportActions, @@ -234,10 +316,25 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct return visibleReportActionsWithDraft; })(); + const [initialReportActionsSnapshot, setInitialReportActionsSnapshot] = useState<{reportActions: OnyxTypes.ReportAction[]; reportID: string}>(); + const hasInitialReportActionsSnapshot = initialReportActionsSnapshot?.reportID === reportID; + + // OpenReport starts with a tiny cached page before replacing it with the hydrated page. Keep that + // already-visible page mounted until hydration finishes instead of exposing intermediate estimated + // layouts. The hydrated list then mounts from scratch using the full dataset. + if (!hasOnceLoadedReportActions && !hasInitialReportActionsSnapshot && renderedVisibleReportActions.length > 0) { + setInitialReportActionsSnapshot({reportActions: renderedVisibleReportActions, reportID}); + } + + const reportActionsToRender = !hasOnceLoadedReportActions && hasInitialReportActionsSnapshot ? initialReportActionsSnapshot.reportActions : renderedVisibleReportActions; + + // Report actions are stored newest-first. LegendList intentionally has no inverted mode, so + // give it chronological data and use its normal start/end and scrolling semantics. + const listData = reportActionsToRender.toReversed(); + const draftMessageHTML = draftReportAction ? getReportActionMessage(draftReportAction)?.html : undefined; const draftReportActionID = draftReportAction?.reportActionID; const isSyntheticDraftVisible = !!draftReportAction && renderedVisibleReportActions !== sortedVisibleReportActions; - const draftAutoScrollKey = isSyntheticDraftVisible ? `${draftReportAction.reportActionID}:${draftMessageHTML ?? ''}` : ''; useEffect(() => { if (!draftReportAction || isSyntheticDraftVisible) { @@ -255,9 +352,10 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct revealDraftFromReportAction(persistedDraftReportAction); }, [draftReportAction, persistedDraftReportAction, revealDraftFromReportAction]); - // Find the index of the action badge target in the rendered actions list (which is what the FlatList uses as data) + // Find the index of the action badge target in the chronological data rendered by LegendList. const actionBadgeTargetID = reportAttributes?.actionTargetReportActionID; - const actionBadgeTargetIndex = actionBadgeTargetID ? renderedVisibleReportActions.findIndex((action) => action.reportActionID === actionBadgeTargetID) : -1; + const actionBadgeTargetIndex = actionBadgeTargetID ? listData.findIndex((action) => action.reportActionID === actionBadgeTargetID) : -1; + const unreadMarkerListIndex = unreadMarkerReportActionID ? listData.findIndex((action) => action.reportActionID === unreadMarkerReportActionID) : -1; const { trackVerticalScrolling, @@ -266,12 +364,9 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct isActionBadgeAboveViewport, scrollToBottomAndMarkReportAsRead, scrollToActionBadgeTarget, - flushPendingScrollToBottom, shouldBeAlignedToTop, - shouldFocusToTopOnMount, initialScrollIndex, initialScrollIndexParams, - maintainVisibleContentPosition, onLoad, } = useReportActionsScroll({ reportID, @@ -280,33 +375,60 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct transactionThreadReport, parentReportAction, sortedVisibleReportActions, - renderedVisibleReportActions, + renderedVisibleReportActions: listData, keyExtractor, - hasScrolledOverThreshold, markNewestActionAsRead, completeSkippedMarkAsRead, unreadMarkerReportActionID, - unreadMarkerReportActionIndex, + unreadMarkerReportActionIndex: unreadMarkerListIndex, hasNewerActions, - draftAutoScrollKey, actionBadgeTargetIndex, sortedAllReportActionsForPagination: sortedAllReportActions ?? [], treatAsNoPaginationAnchor, setTreatAsNoPaginationAnchor, }); - const trackScrollPositionAndThreshold = (event: NativeSyntheticEvent) => { - trackVerticalScrolling(event); - setHasScrolledOverThreshold(event.nativeEvent.contentOffset.y >= CONST.REPORT.ACTIONS.ACTION_VISIBLE_THRESHOLD); + const [loadedInitialViewportListID, setLoadedInitialViewportListID] = useState(); + const shouldShowInitialViewportSkeleton = !isOffline && (!hasOnceLoadedReportActions || loadedInitialViewportListID !== listID); + + const handleListLoad = () => { + onLoad(); + setLoadedInitialViewportListID(listID); }; - const loadOlderChatsOnEndReached = () => { - if (showHiddenHistory) { + const loadOlderChatsOnStartReached = () => { + if (showHiddenHistory || isOffline || !hasOlderActions || !oldestReportActionID || lastRequestedOldestActionIDRef.current === oldestReportActionID) { return; } + + lastRequestedOldestActionIDRef.current = oldestReportActionID; loadOlderChats(false); }; + const trackScrollPositionAndThreshold = (event: NativeSyntheticEvent) => { + const {contentOffset, contentSize, layoutMeasurement} = event.nativeEvent; + const distanceFromBottom = Math.max(0, contentSize.height - layoutMeasurement.height - contentOffset.y); + const isNearStart = contentOffset.y <= layoutMeasurement.height * PAGINATION_THRESHOLD; + + if (isNearStart) { + loadOlderChatsOnStartReached(); + } else { + lastRequestedOldestActionIDRef.current = undefined; + } + + const bottomRelativeEvent = { + ...event, + nativeEvent: { + ...event.nativeEvent, + contentOffset: {...contentOffset, y: distanceFromBottom}, + }, + }; + + trackVerticalScrolling(bottomRelativeEvent); + setHasScrolledOverThreshold(distanceFromBottom >= CONST.REPORT.ACTIONS.ACTION_VISIBLE_THRESHOLD); + emitComposerScrollEvents(); + }; + const loadNewerChatsAfterTransitions = () => { if (!isSearchTopmostFullScreenRoute()) { loadNewerChats(false); @@ -327,7 +449,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct reportID, actionTargetReportActionID: reportAttributes?.actionTargetReportActionID, actionBadgeTargetIndex, - renderedVisibleReportActions, + renderedVisibleReportActions: listData, scrollToActionBadgeTarget, }); @@ -355,11 +477,12 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct return isExpenseReport(report) || isIOUReport(report) || isInvoiceReport(report); })(); - const renderItem = ({item: reportAction, index}: ListRenderItemInfo) => { + const renderItem = ({item: reportAction, index}: LegendListRenderItemProps) => { const shouldDisableContextMenuForConciergeDraft = isDraftPendingCompletion && draftReportActionID === reportAction.reportActionID; + const reportActionIndex = reportActionsToRender.length - index - 1; return ( - + 1} + shouldDisplayReplyDivider={reportActionsToRender.length > 1} isFirstVisibleReportAction={firstVisibleReportActionID === reportAction.reportActionID} shouldUseThreadDividerLine={shouldUseThreadDividerLine} isHarvestCreatedExpenseReport={isHarvestCreatedExpenseReportAction} @@ -456,46 +579,49 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct report={report} isReportArchived={isReportArchived} > - { - recordTimeToMeasureItemLayout(event); - flushPendingScrollToBottom(); - }} + onLayout={recordTimeToMeasureItemLayout} onScroll={trackScrollPositionAndThreshold} onViewableItemsChanged={onViewableItemsChanged} extraData={extraData} key={listID} - overrideProps={{ - isInvertedVirtualizedList: true, - contentOffset: shouldFocusToTopOnMount ? {x: 0, y: windowHeight} : undefined, - }} - getItemType={(item) => item.actionName} - initialScrollIndex={initialScrollIndex} - initialScrollIndexParams={initialScrollIndexParams} - maintainVisibleContentPosition={maintainVisibleContentPosition} - onLoad={onLoad} - onContentSizeChange={() => { - trackVerticalScrolling(undefined); - }} + getItemType={getItemType} + initialScrollAtEnd={initialScrollIndex === undefined} + initialScrollIndex={initialScrollIndex === undefined ? undefined : {index: initialScrollIndex, ...initialScrollIndexParams}} + alignItemsAtEnd={!shouldBeAlignedToTop} + // Only follow the real latest page. Older/linked windows must retain their visible anchor. + maintainScrollAtEnd={!hasNewerActions && {animated: false}} + // Leave the end-follow region as soon as the user starts reading older messages. + maintainScrollAtEndThreshold={0.01} + maintainVisibleContentPosition + onLoad={handleListLoad} + onContentSizeChange={() => trackVerticalScrolling(undefined)} /> + {shouldShowInitialViewportSkeleton && ( + + + + )} ); diff --git a/src/pages/inbox/report/actionContents/ActionContentRouter.tsx b/src/pages/inbox/report/actionContents/ActionContentRouter.tsx index 0e8f9d76f666..5f55dfb63898 100644 --- a/src/pages/inbox/report/actionContents/ActionContentRouter.tsx +++ b/src/pages/inbox/report/actionContents/ActionContentRouter.tsx @@ -204,7 +204,7 @@ function ActionContentRouter({ if (action.actionName === CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW) { return ( {isEditingInline ? ( = 0 && actionBadgeTargetIndex < prevActionBadgeTargetIndex; + return prevActionBadgeTargetIndex >= 0 && actionBadgeTargetIndex > prevActionBadgeTargetIndex; } export default shouldFollowActionBadgeTarget; diff --git a/src/pages/inbox/report/useFollowActionBadgeTarget.ts b/src/pages/inbox/report/useFollowActionBadgeTarget.ts index b15df8c3a2ff..67c952452920 100644 --- a/src/pages/inbox/report/useFollowActionBadgeTarget.ts +++ b/src/pages/inbox/report/useFollowActionBadgeTarget.ts @@ -18,10 +18,10 @@ type UseFollowActionBadgeTargetParams = { /** The report action the badge currently targets (the oldest preview still requiring action) */ actionTargetReportActionID: string | undefined; - /** Index of the current target in the rendered (inverted) list, or -1 when it is not rendered */ + /** Index of the current target in the chronological list, or -1 when it is not rendered */ actionBadgeTargetIndex: number; - /** The rendered (inverted) report actions the list is displaying */ + /** The chronological report actions the list is displaying */ renderedVisibleReportActions: OnyxTypes.ReportAction[]; /** Scrolls the list to the current action-badge target */ diff --git a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts index da424fdf5e13..bb150ca6d99a 100644 --- a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts +++ b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts @@ -48,7 +48,8 @@ type UseReportActionsNewActionLiveTailParams = { hasNewerActions: boolean; linkedReportActionID: string | undefined; hasNewestReportAction: boolean; - sortedVisibleReportActions: OnyxTypes.ReportAction[]; + /** Actions rendered by the list in chronological order. */ + renderedVisibleReportActions: OnyxTypes.ReportAction[]; sortedAllReportActionsForPagination: OnyxTypes.ReportAction[]; reportActionPages: OnyxTypes.Pages | undefined; setTreatAsNoPaginationAnchor: (value: boolean) => void; @@ -61,9 +62,8 @@ type LiveTailJumpStage = 'idle' | 'open_report' | 'await_scroll' | 'await_prune' /** * Owns subscribe-to-new-action scrolling, live-tail jump (openReport → scroll → prune), and the - * deferred scroll + pagination prune after layout. Uses useEffectEvent for the Pusher subscription handler so it - * always sees the latest props without mirror refs. The layout-time prune step uses useCallback so callers can invoke - * it from list `onLayout` outside this hook. + * deferred scroll + pagination prune after the data render. Uses useEffectEvent for the Pusher subscription handler so it + * always sees the latest props without mirror refs. The prune callback completes the explicit scroll request in the caller. */ function useReportActionsNewActionLiveTail({ conciergeChat, @@ -80,7 +80,7 @@ function useReportActionsNewActionLiveTail({ hasNewerActions, linkedReportActionID, hasNewestReportAction, - sortedVisibleReportActions, + renderedVisibleReportActions, sortedAllReportActionsForPagination, reportActionPages, setTreatAsNoPaginationAnchor, @@ -132,14 +132,14 @@ function useReportActionsNewActionLiveTail({ return; } - const index = sortedVisibleReportActions.findIndex((item) => item.reportActionID === action?.reportActionID); + const index = renderedVisibleReportActions.findIndex((item) => item.reportActionID === action?.reportActionID); if (action?.actionName === CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW) { - if (index > 0) { + setIsFloatingMessageCounterVisible(false); + if (index >= 0 && index < renderedVisibleReportActions.length - 1) { setTimeout(() => { reportScrollManager.scrollToIndex(index); }, 100); } else { - setIsFloatingMessageCounterVisible(false); reportScrollManager.scrollToBottom(); } if (action?.reportActionID) { @@ -147,10 +147,8 @@ function useReportActionsNewActionLiveTail({ } } else { setIsFloatingMessageCounterVisible(false); - reportScrollManager.scrollToBottom(); + setIsScrollToBottomEnabled(true); } - - setIsScrollToBottomEnabled(true); }, }); }); diff --git a/src/pages/inbox/report/useReportUnreadMessageScrollTracking.ts b/src/pages/inbox/report/useReportUnreadMessageScrollTracking.ts index 053d23522914..8f41679a75fd 100644 --- a/src/pages/inbox/report/useReportUnreadMessageScrollTracking.ts +++ b/src/pages/inbox/report/useReportUnreadMessageScrollTracking.ts @@ -157,11 +157,9 @@ export default function useReportUnreadMessageScrollTracking({ ref.current.onUnreadActionVisible(); } - // Track whether the action badge target is above the viewport (i.e., not visible and at a higher index in the inverted list) + // Track whether the action badge target is above the viewport. const badgeTargetIndex = ref.current.actionBadgeTargetIndex; if (badgeTargetIndex !== -1) { - // In an inverted list, higher indexes are "above" (older messages). The target is above the viewport - // when its index is greater than the max visible index. const isAbove = isInverted ? badgeTargetIndex > maxIndex : badgeTargetIndex < minIndex; setIsActionBadgeAboveViewport(isAbove); } else { diff --git a/tests/ui/PaginationTest.tsx b/tests/ui/PaginationTest.tsx index fd23ffdab5bf..f393af39fb38 100644 --- a/tests/ui/PaginationTest.tsx +++ b/tests/ui/PaginationTest.tsx @@ -43,6 +43,7 @@ const LIST_CONTENT_SIZE = { width: 300, height: 600, }; +const LIST_END_OFFSET = LIST_CONTENT_SIZE.height - LIST_SIZE.height; const TEN_MINUTES_AGO = subMinutes(new Date(), 10); const REPORT_ID = '1'; @@ -315,7 +316,7 @@ describe('Pagination', () => { TestHelper.expectAPICommandToHaveBeenCalled('GetNewerActions', 0); // Scrolling here should not trigger a new network request. - scrollToOffset(LIST_CONTENT_SIZE.height); + scrollToOffset(LIST_END_OFFSET); await waitForBatchedUpdatesWithAct(); scrollToOffset(0); await waitForBatchedUpdatesWithAct(); @@ -339,7 +340,7 @@ describe('Pagination', () => { TestHelper.expectAPICommandToHaveBeenCalled('GetNewerActions', 0); // Scrolling here should trigger a new network request. - scrollToOffset(LIST_CONTENT_SIZE.height); + scrollToOffset(0); await waitForBatchedUpdatesWithAct(); TestHelper.expectAPICommandToHaveBeenCalled('OpenReport', 1); @@ -370,8 +371,8 @@ describe('Pagination', () => { jest.requireMock('@react-navigation/native').triggerTransitionEnd(); }); // Due to https://github.com/facebook/react-native/commit/3485e9ed871886b3e7408f90d623da5c018da493 - // we need to scroll too to trigger `onStartReached` which triggers other updates - scrollToOffset(0); + // we need to scroll too to trigger `onEndReached` which triggers other updates + scrollToOffset(LIST_END_OFFSET); // ReportScreen relies on the onLayout event to receive updates from onyx. triggerListLayout(); await waitForNetworkPromises(); @@ -393,10 +394,10 @@ describe('Pagination', () => { TestHelper.expectAPICommandToHaveBeenCalledWith('GetNewerActions', 0, {reportID: REPORT_ID, reportActionID: '5'}); // Simulate the maintainVisibleContentPosition scroll adjustment, so it is now possible to scroll down more. - scrollToOffset(500); - await waitForBatchedUpdatesWithAct(); scrollToOffset(0); await waitForBatchedUpdatesWithAct(); + scrollToOffset(LIST_END_OFFSET); + await waitForBatchedUpdatesWithAct(); // We now have 10 messages. 5 from the initial OpenReport and 5 from the GetNewerActions call. expect(getReportActions()).toHaveLength(10); @@ -405,10 +406,10 @@ describe('Pagination', () => { TestHelper.expectAPICommandToHaveBeenCalled('GetOlderActions', 0); TestHelper.expectAPICommandToHaveBeenCalled('GetNewerActions', 2); - scrollToOffset(500); - await waitForBatchedUpdatesWithAct(); scrollToOffset(0); await waitForBatchedUpdatesWithAct(); + scrollToOffset(LIST_END_OFFSET); + await waitForBatchedUpdatesWithAct(); // When there are no newer actions, we don't want to trigger GetNewerActions again. TestHelper.expectAPICommandToHaveBeenCalled('OpenReport', 3); diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 0b1aac4cf61e..4a0b2c90a543 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -1,4 +1,4 @@ -import {render, screen} from '@testing-library/react-native'; +import {act, render, screen} from '@testing-library/react-native'; import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; import {useIsReportLoadPending} from '@hooks/useInFlightRequests'; @@ -86,7 +86,9 @@ const mockUseConciergeSessionState = useConciergeSessionState as jest.MockedFunc const mockUseConciergeSessionActions = useConciergeSessionActions as jest.MockedFunction; function getMockReportLoadingState(selector: unknown, hasOnceLoadedReportActions = true) { - return selector === reportActionsListLoadingStateSelector ? {hasOnceLoadedReportActions, isLoadingInitialReportActions: false} : undefined; + return selector === reportActionsListLoadingStateSelector + ? {hasOnceLoadedReportActions, isLoadingInitialReportActions: false, isLoadingOlderReportActions: false, hasLoadingOlderReportActionsError: false} + : undefined; } const defaultPaginatedReportActionsResult: ReturnType = { @@ -113,10 +115,12 @@ const defaultSidePanelState: ReturnType = { jest.mock('@hooks/useCopySelectionHelper', () => jest.fn()); jest.mock('@hooks/useCurrentUserPersonalDetails', () => jest.fn()); +const mockLoadOlderChats = jest.fn(); jest.mock('@hooks/useLoadReportActions', () => - jest.fn(() => ({ - loadOlderChats: jest.fn(), + jest.fn(({reportActions}: {reportActions: OnyxTypes.ReportAction[]}) => ({ + loadOlderChats: mockLoadOlderChats, loadNewerChats: jest.fn(), + currentReportOldestActionID: reportActions.at(-1)?.reportActionID, })), ); jest.mock('@hooks/usePrevious', () => jest.fn()); @@ -124,11 +128,30 @@ jest.mock('@hooks/usePrevious', () => jest.fn()); const mockUseCurrentUserPersonalDetails = useCurrentUserPersonalDetails as jest.MockedFunction; // We mount the public ReportActionsList (the skeleton guard + its content) and observe what the content -// feeds the list via InvertedFlashList's `data`. The heavy scroll/marker hooks have their own unit tests, +// feeds chronological data directly to LegendList. The heavy scroll/marker hooks have their own unit tests, // so they are stubbed here to isolate the skeleton logic. Because the guard only mounts the content when // the skeleton is not showing, these stubs double as a probe for dormancy: while a skeleton renders the // content is never mounted, so useMarkAsRead/useReportActionsScroll are never called. -jest.mock('@components/FlashList/InvertedFlashList', () => jest.fn(() => null)); +const mockLegendListMount = jest.fn(); +const mockLegendListUnmount = jest.fn(); +let mockShouldCallLegendListOnLoad = true; +jest.mock('@legendapp/list/react-native', () => { + const reactModule = jest.requireActual('react'); + return { + LegendList: jest.fn(({onLoad}: {onLoad?: () => void}) => { + reactModule.useEffect(() => { + mockLegendListMount(); + if (mockShouldCallLegendListOnLoad) { + onLoad?.(); + } + return () => { + mockLegendListUnmount(); + }; + }, []); + return null; + }), + }; +}); jest.mock('@hooks/useUnreadMarker', () => jest.fn(() => ({unreadMarkerReportActionID: null, unreadMarkerReportActionIndex: -1}))); jest.mock('@hooks/useMarkAsRead', () => jest.fn(() => ({markNewestActionAsRead: jest.fn(), completeSkippedMarkAsRead: jest.fn()}))); jest.mock('@hooks/useReportActionsScroll', () => @@ -140,11 +163,9 @@ jest.mock('@hooks/useReportActionsScroll', () => isActionBadgeAboveViewport: false, scrollToBottomAndMarkReportAsRead: jest.fn(), scrollToActionBadgeTarget: jest.fn(), - flushPendingScrollToBottom: jest.fn(), shouldBeAlignedToTop: false, - shouldFocusToTopOnMount: false, - initialScrollKey: undefined, - shouldAutoscrollToBottom: false, + initialScrollIndex: undefined, + initialScrollIndexParams: undefined, onLoad: jest.fn(), })), ); @@ -156,18 +177,35 @@ jest.mock('@pages/inbox/report/ReportActionsListPaddingView', () => { jest.mock('@pages/inbox/report/UserTypingEventListener', () => jest.fn(() => null)); jest.mock('@pages/inbox/report/ReportActionItemCreated', () => jest.fn(() => null)); -type MockInvertedFlashListProps = { +type MockLegendListProps = { + alignItemsAtEnd?: boolean; data?: OnyxTypes.ReportAction[]; + drawDistance?: number; extraData?: unknown; + getItemType?: (item: OnyxTypes.ReportAction) => string; + initialScrollAtEnd?: boolean; + maintainScrollAtEnd?: {animated: boolean} | false; + maintainScrollAtEndThreshold?: number; + maintainVisibleContentPosition?: boolean; + onLoad?: () => void; + recycleItems?: boolean; renderItem?: (info: {item: OnyxTypes.ReportAction; index: number}) => React.ReactElement | null; + onStartReached?: () => void; + onScroll?: (event: { + nativeEvent: { + contentOffset: {x: number; y: number}; + contentSize: {height: number; width: number}; + layoutMeasurement: {height: number; width: number}; + }; + }) => void; }; -const mockInvertedFlashList: jest.MockedFunction<(props: MockInvertedFlashListProps) => null> = jest.requireMock('@components/FlashList/InvertedFlashList'); +const {LegendList: mockLegendList} = jest.requireMock<{LegendList: jest.MockedFunction<(props: MockLegendListProps) => null>}>('@legendapp/list/react-native'); const mockReportActionItemCreated: jest.Mock = jest.requireMock('@pages/inbox/report/ReportActionItemCreated'); -/** Returns the report actions the body fed into the (mocked) inverted list on its latest render. */ -const getCapturedVisibleActions = (): OnyxTypes.ReportAction[] | undefined => mockInvertedFlashList.mock.calls.at(-1)?.at(0)?.data; -const getCapturedListProps = (): MockInvertedFlashListProps | undefined => mockInvertedFlashList.mock.calls.at(-1)?.at(0); +/** Returns the chronological report actions the body fed into the mocked LegendList on its latest render. */ +const getCapturedVisibleActions = (): OnyxTypes.ReportAction[] | undefined => mockLegendList.mock.calls.at(-1)?.at(0)?.data; +const getCapturedListProps = (): MockLegendListProps | undefined => mockLegendList.mock.calls.at(-1)?.at(0); const getRenderedReportActionsListItemProps = (reportAction: OnyxTypes.ReportAction, index = 0): {shouldDisableContextMenuForConciergeDraft?: boolean} => { const renderedItem = getCapturedListProps()?.renderItem?.({item: reportAction, index}); @@ -191,6 +229,7 @@ const getRenderedReportActionsListItemProps = (reportAction: OnyxTypes.ReportAct const mockUseMarkAsRead: jest.Mock = jest.requireMock('@hooks/useMarkAsRead'); const mockUseReportActionsScroll: jest.Mock = jest.requireMock('@hooks/useReportActionsScroll'); const mockMarkOpenReportEnd: jest.Mock = jest.requireMock('@libs/telemetry/markOpenReportEnd'); +let mockHasOnceLoadedReportActions = true; jest.mock('@libs/actions/Report', () => ({ updateLoadingInitialReportAction: jest.fn(), @@ -233,6 +272,19 @@ const mockReportActions: OnyxTypes.ReportAction[] = [ }, ]; +const olderMockReportAction: OnyxTypes.ReportAction = { + reportActionID: '0', + actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, + created: '2022-12-31', + actorAccountID: 125, + message: [{type: 'COMMENT', html: 'Older message', text: 'Older message'}], + originalMessage: {}, + shouldShow: true, + person: [{type: 'TEXT', style: 'strong', text: 'Older User'}], + pendingAction: null, + errors: {}, +}; + const renderReportActionsList = (props: {reportID?: string} = {}) => { const reportID = props.reportID ?? mockReport.reportID; return render( @@ -252,6 +304,8 @@ describe('ReportActionsList (body)', () => { beforeEach(() => { jest.clearAllMocks(); + mockHasOnceLoadedReportActions = true; + mockShouldCallLegendListOnLoad = true; mockUseIsReportLoadPending.mockReturnValue(false); mockUseCurrentUserPersonalDetails.mockReturnValue({ @@ -314,7 +368,7 @@ describe('ReportActionsList (body)', () => { return [false, {status: 'loaded'}]; } if (key.includes('reportLoadingState')) { - return [getMockReportLoadingState(options?.selector), {status: 'loaded'}]; + return [getMockReportLoadingState(options?.selector, mockHasOnceLoadedReportActions), {status: 'loaded'}]; } if (key.includes('reportActions')) { return [[], {status: 'loaded'}]; @@ -334,6 +388,248 @@ describe('ReportActionsList (body)', () => { await Onyx.clear(); }); + it('delegates end following and size corrections to LegendList without a full-viewport threshold', () => { + mockUseNetwork.mockReturnValue({isOffline: false}); + renderReportActionsList(); + + expect(getCapturedListProps()?.maintainScrollAtEnd).toEqual({animated: false}); + expect(getCapturedListProps()?.maintainScrollAtEndThreshold).toBe(0.01); + expect(getCapturedListProps()?.maintainVisibleContentPosition).toBe(true); + }); + + it('initially aligns the seed page to the end', () => { + mockUseNetwork.mockReturnValue({isOffline: false}); + mockHasOnceLoadedReportActions = false; + renderReportActionsList(); + + expect(getCapturedListProps()?.initialScrollAtEnd).toBe(true); + expect(getCapturedListProps()?.alignItemsAtEnd).toBe(true); + expect(getCapturedListProps()?.maintainScrollAtEnd).toEqual({animated: false}); + }); + + it('does not follow the end of a page that still has newer actions to load', () => { + mockUseNetwork.mockReturnValue({isOffline: false}); + mockUsePaginatedReportActions.mockReturnValue({ + ...defaultPaginatedReportActionsResult, + reportActions: mockReportActions, + hasNewerActions: true, + }); + renderReportActionsList(); + + expect(getCapturedListProps()?.maintainScrollAtEnd).toBe(false); + expect(getCapturedListProps()?.maintainVisibleContentPosition).toBe(true); + }); + + it('remounts the list when the initial report actions finish hydrating', async () => { + mockUseNetwork.mockReturnValue({isOffline: false}); + mockHasOnceLoadedReportActions = false; + const view = renderReportActionsList(); + + expect(mockLegendListMount).toHaveBeenCalledTimes(1); + expect(mockLegendListUnmount).not.toHaveBeenCalled(); + + mockHasOnceLoadedReportActions = true; + // The mocked Onyx hook does not own state, so changing its return value cannot schedule the + // rerender that the real Onyx subscription causes. Change a prop to trigger that render. + view.rerender( + , + ); + await waitForBatchedUpdatesWithAct(); + + expect(mockLegendListMount).toHaveBeenCalledTimes(2); + expect(mockLegendListUnmount).toHaveBeenCalledTimes(1); + expect(getCapturedListProps()?.initialScrollAtEnd).toBe(true); + expect(getCapturedListProps()?.maintainScrollAtEnd).toEqual({animated: false}); + }); + + it('keeps the initial viewport covered until the hydrated LegendList finishes rendering it', async () => { + mockUseNetwork.mockReturnValue({isOffline: false}); + mockHasOnceLoadedReportActions = false; + mockShouldCallLegendListOnLoad = false; + const view = renderReportActionsList(); + + expect(screen.getByTestId('ReportActionsSkeletonView')).toBeTruthy(); + + act(() => { + getCapturedListProps()?.onLoad?.(); + }); + expect(screen.getByTestId('ReportActionsSkeletonView')).toBeTruthy(); + + mockHasOnceLoadedReportActions = true; + view.rerender( + , + ); + await waitForBatchedUpdatesWithAct(); + + expect(screen.getByTestId('ReportActionsSkeletonView')).toBeTruthy(); + + act(() => { + getCapturedListProps()?.onLoad?.(); + }); + expect(screen.queryByTestId('ReportActionsSkeletonView')).toBeNull(); + }); + + it('keeps the initial actions visible until the hydrated page is complete', async () => { + mockUseNetwork.mockReturnValue({isOffline: false}); + mockHasOnceLoadedReportActions = false; + const groupingSpy = jest.spyOn(ReportActionsUtils, 'isConsecutiveActionMadeByPreviousActor'); + const view = renderReportActionsList(); + + expect(getCapturedVisibleActions()).toHaveLength(mockReportActions.length); + + mockUsePaginatedReportActions.mockReturnValue({ + ...defaultPaginatedReportActionsResult, + reportActions: [...mockReportActions, olderMockReportAction], + }); + view.rerender( + , + ); + await waitForBatchedUpdatesWithAct(); + + expect(getCapturedVisibleActions()).toHaveLength(mockReportActions.length); + expect(getCapturedVisibleActions()?.some((action) => action.reportActionID === olderMockReportAction.reportActionID)).toBe(false); + + const snapshotActions = getCapturedVisibleActions(); + const oldestSnapshotAction = snapshotActions?.at(0); + if (!oldestSnapshotAction || !snapshotActions) { + throw new Error('Expected the initial action snapshot to remain visible'); + } + getRenderedReportActionsListItemProps(oldestSnapshotAction); + expect(groupingSpy).toHaveBeenLastCalledWith(snapshotActions.toReversed(), snapshotActions.length - 1, false); + groupingSpy.mockRestore(); + + mockHasOnceLoadedReportActions = true; + view.rerender( + , + ); + await waitForBatchedUpdatesWithAct(); + + expect(getCapturedVisibleActions()).toHaveLength(mockReportActions.length + 1); + expect(getCapturedVisibleActions()?.some((action) => action.reportActionID === olderMockReportAction.reportActionID)).toBe(true); + }); + + it('limits the render buffer and enables item recycling', () => { + mockUseNetwork.mockReturnValue({isOffline: false}); + renderReportActionsList(); + + const listProps = getCapturedListProps(); + + expect(listProps?.drawDistance).toBe(1500); + expect(listProps?.recycleItems).toBe(true); + }); + + it('groups comments by layout characteristics for measurement estimates', () => { + mockUseNetwork.mockReturnValue({isOffline: false}); + renderReportActionsList(); + + const getItemType = getCapturedListProps()?.getItemType; + const comment = mockReportActions.at(1); + if (!comment) { + throw new Error('Expected comment report action fixture'); + } + + expect(getItemType?.(comment)).toBe(`${CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT}-short`); + expect( + getItemType?.({ + ...comment, + reportActionID: 'medium-comment', + message: [{type: 'COMMENT', html: 'Medium comment', text: 'a'.repeat(200)}], + }), + ).toBe(`${CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT}-medium`); + expect( + getItemType?.({ + ...comment, + reportActionID: 'long-comment', + message: [{type: 'COMMENT', html: 'Long comment', text: 'a'.repeat(600)}], + }), + ).toBe(`${CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT}-long`); + expect( + getItemType?.({ + ...comment, + reportActionID: 'extra-long-comment', + message: [{type: 'COMMENT', html: 'Extra long comment', text: 'a'.repeat(1500)}], + }), + ).toBe(`${CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT}-extra-long`); + expect( + getItemType?.({ + ...comment, + reportActionID: 'attachment', + isAttachmentOnly: true, + }), + ).toBe(`${CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT}-attachment`); + expect( + getItemType?.({ + ...comment, + reportActionID: 'link-preview', + linkMetadata: [{url: 'https://example.com'}], + }), + ).toBe(`${CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT}-link-preview-short`); + }); + + it('continues loading older pages from scroll events when LegendList does not report reaching the start', () => { + mockUseNetwork.mockReturnValue({isOffline: false}); + mockUsePaginatedReportActions.mockReturnValue({ + ...defaultPaginatedReportActionsResult, + reportActions: mockReportActions, + hasOlderActions: true, + }); + const view = renderReportActionsList(); + + const listProps = getCapturedListProps(); + const createScrollEvent = (offset: number) => ({ + nativeEvent: { + contentOffset: {x: 0, y: offset}, + contentSize: {height: 1000, width: 300}, + layoutMeasurement: {height: 500, width: 300}, + }, + }); + + act(() => { + listProps?.onScroll?.(createScrollEvent(0)); + }); + expect(mockLoadOlderChats).toHaveBeenCalledTimes(1); + + act(() => { + listProps?.onStartReached?.(); + listProps?.onScroll?.(createScrollEvent(0)); + }); + expect(mockLoadOlderChats).toHaveBeenCalledTimes(1); + + mockUsePaginatedReportActions.mockReturnValue({ + ...defaultPaginatedReportActionsResult, + reportActions: [...mockReportActions, olderMockReportAction], + hasOlderActions: true, + }); + view.rerender( + , + ); + + act(() => { + getCapturedListProps()?.onScroll?.(createScrollEvent(0)); + }); + expect(mockLoadOlderChats).toHaveBeenCalledTimes(2); + }); + describe('Concierge Draft Context Menu', () => { const conciergeDraftReportAction: OnyxTypes.ReportAction = { reportID: mockReport.reportID, @@ -670,10 +966,10 @@ describe('ReportActionsList (body)', () => { renderReportActionsList({reportID: CONCIERGE_REPORT_ID}); - expect(mockInvertedFlashList).toHaveBeenCalled(); + expect(mockLegendList).toHaveBeenCalled(); const passedActions = getCapturedVisibleActions(); expect(passedActions?.length).toBeGreaterThanOrEqual(1); - expect(passedActions?.at(0)?.reportActionID).toBe(CONST.CONCIERGE_GREETING_ACTION_ID); + expect(passedActions?.some((action) => action.reportActionID === CONST.CONCIERGE_GREETING_ACTION_ID)).toBe(true); }); it('should not show welcome state when not in side panel', () => { @@ -737,7 +1033,7 @@ describe('ReportActionsList (body)', () => { // Welcome should not be shown since user has sent a message expect(mockReportActionItemCreated).not.toHaveBeenCalled(); // ReportActionsList should be rendered with filtered actions - expect(mockInvertedFlashList).toHaveBeenCalled(); + expect(mockLegendList).toHaveBeenCalled(); }); }); @@ -830,7 +1126,7 @@ describe('ReportActionsList (body)', () => { renderReportActionsList({reportID: CONCIERGE_REPORT_ID}); - expect(mockInvertedFlashList).toHaveBeenCalled(); + expect(mockLegendList).toHaveBeenCalled(); const passedActions = getCapturedVisibleActions(); expect(passedActions?.some((a) => a.reportActionID === CONST.CONCIERGE_GREETING_ACTION_ID)).toBe(true); expect(passedActions?.some((a) => a.reportActionID === 'old-user-msg')).toBe(false); @@ -848,7 +1144,7 @@ describe('ReportActionsList (body)', () => { renderReportActionsList({reportID: CONCIERGE_REPORT_ID}); - expect(mockInvertedFlashList).toHaveBeenCalled(); + expect(mockLegendList).toHaveBeenCalled(); const passedActions = getCapturedVisibleActions(); expect(passedActions?.some((a) => a.reportActionID === 'old-user-msg')).toBe(true); expect(passedActions?.some((a) => a.reportActionID === 'old-concierge-msg')).toBe(true); @@ -881,7 +1177,7 @@ describe('ReportActionsList (body)', () => { renderReportActionsList({reportID: CONCIERGE_REPORT_ID}); - expect(mockInvertedFlashList).toHaveBeenCalled(); + expect(mockLegendList).toHaveBeenCalled(); const passedActions = getCapturedVisibleActions(); // After user sends a message, the greeting stays visible alongside session actions expect(passedActions?.some((a) => a.reportActionID === CONST.CONCIERGE_GREETING_ACTION_ID)).toBe(true); @@ -899,7 +1195,7 @@ describe('ReportActionsList (body)', () => { renderReportActionsList({reportID: CONCIERGE_REPORT_ID}); - expect(mockInvertedFlashList).toHaveBeenCalled(); + expect(mockLegendList).toHaveBeenCalled(); const passedActions = getCapturedVisibleActions(); // With no session, old messages should not be shown expect(passedActions?.some((a) => a.reportActionID === 'old-user-msg')).toBe(false); @@ -945,7 +1241,7 @@ describe('ReportActionsList (body)', () => { renderReportActionsList({reportID: CONCIERGE_REPORT_ID}); - expect(mockInvertedFlashList).toHaveBeenCalled(); + expect(mockLegendList).toHaveBeenCalled(); const passedActions = getCapturedVisibleActions(); // New user with no prior messages — onboarding messages pass through (no filtering) expect(passedActions?.some((a) => a.reportActionID === 'onboarding-msg')).toBe(true); @@ -968,9 +1264,10 @@ describe('ReportActionsList (body)', () => { expect(mockStartSession).toHaveBeenCalled(); }); - it('should render cached actions without a skeleton on refresh when hasOnceLoadedReportActions resets but actions are cached', () => { + it('should cover cached actions until the refreshed report finishes hydrating', () => { // Simulates a page refresh: hasOnceLoadedReportActions is RAM-only and resets to false, - // but report actions persist in Onyx cache. We should render them immediately (production behavior). + // but report actions persist in Onyx cache. Keep the cached list covered until OpenReport + // finishes so the final hydrated viewport is the first report content the user sees. setupMainDMConciergeMocks(SESSION_START, false, false); mockUsePaginatedReportActions.mockReturnValue({ @@ -981,8 +1278,8 @@ describe('ReportActionsList (body)', () => { renderReportActionsList({reportID: CONCIERGE_REPORT_ID}); - expect(screen.queryByTestId('ReportActionsSkeletonView')).toBeNull(); - expect(mockInvertedFlashList).toHaveBeenCalled(); + expect(screen.getByTestId('ReportActionsSkeletonView')).toBeTruthy(); + expect(mockLegendList).toHaveBeenCalled(); }); it('should show a skeleton on a cold load when hasOnceLoadedReportActions is false and there are no cached actions', () => { diff --git a/tests/unit/ReportActionsListThresholdTest.tsx b/tests/unit/ReportActionsListThresholdTest.tsx index d8ce64ac8449..61c2508361be 100644 --- a/tests/unit/ReportActionsListThresholdTest.tsx +++ b/tests/unit/ReportActionsListThresholdTest.tsx @@ -30,38 +30,52 @@ import wrapOnyxWithWaitForBatchedUpdates from '../utils/wrapOnyxWithWaitForBatch const THRESHOLD = CONST.REPORT.ACTIONS.ACTION_VISIBLE_THRESHOLD; -type ScrollEvent = {nativeEvent: {contentOffset: {y: number}}}; +type ScrollEvent = { + nativeEvent: { + contentOffset: {x: number; y: number}; + contentSize: {height: number; width: number}; + layoutMeasurement: {height: number; width: number}; + }; +}; type CapturedListProps = { - maintainVisibleContentPosition?: {disabled: boolean}; + maintainVisibleContentPosition?: boolean | {data: boolean}; onScroll?: (event: ScrollEvent) => void; }; -// Capture the props the list is rendered with so we can observe `maintainVisibleContentPosition`, whose -// `disabled` flag is `!(hasScrolledOverThreshold || shouldFocusToTopOnMount)`. With no deep-link the latter -// is false, so `!disabled` mirrors the boolean under test. +// Capture the props the list is rendered with so we can verify data anchoring remains enabled while the +// list crosses the visible-action threshold. let capturedListProps: CapturedListProps = {}; -// Every value the maintain-visible-content-position flag has held (`!disabled`), in render order. `[0]` is the -// value on the list's very first render — the property that matters, since it must be right before any effect runs. -let mockMvcpHistory: Array = []; -// `!disabled` from the captured `maintainVisibleContentPosition`, or `undefined` before the list first renders. +// Whether the captured LegendList configuration enables data-based maintain-visible-content-position. function isMvcpEnabled() { const config = capturedListProps.maintainVisibleContentPosition; - return config ? !config.disabled : undefined; + return config === true || (typeof config === 'object' && config.data); } -jest.mock('@components/FlashList/InvertedFlashList', () => { +jest.mock('@legendapp/list/react-native', () => { const {forwardRef} = jest.requireActual('react'); return { - __esModule: true, - default: forwardRef((props) => { + // The second parameter is intentionally unused; forwardRef requires it to avoid a React development warning. + // eslint-disable-next-line @typescript-eslint/no-unused-vars + LegendList: forwardRef((props, ref) => { capturedListProps = props; - mockMvcpHistory.push(props.maintainVisibleContentPosition ? !props.maintainVisibleContentPosition.disabled : undefined); return null; }), }; }); +function createScrollEvent(distanceFromBottom: number): ScrollEvent { + const contentHeight = 1000; + const viewportHeight = 500; + return { + nativeEvent: { + contentOffset: {x: 0, y: contentHeight - viewportHeight - distanceFromBottom}, + contentSize: {height: contentHeight, width: 300}, + layoutMeasurement: {height: viewportHeight, width: 300}, + }, + }; +} + jest.mock('@react-navigation/native', () => { const actualNav = jest.requireActual('@react-navigation/native'); return { @@ -120,7 +134,6 @@ async function renderList(initialOffset: number) { beforeEach(async () => { capturedListProps = {}; - mockMvcpHistory = []; setHasRadio(true); wrapOnyxWithWaitForBatchedUpdates(Onyx); await act(async () => { @@ -145,34 +158,31 @@ afterEach(async () => { await waitForBatchedUpdates(); }); -describe('ReportActionsList hasScrolledOverThreshold', () => { - it('enables maintainVisibleContentPosition on first render when mounted while scrolled past the threshold', async () => { +describe('ReportActionsList maintainVisibleContentPosition', () => { + it('enables data anchoring on first render when mounted while scrolled past the threshold', async () => { await renderList(THRESHOLD + 50); - // Must be true on the FIRST render, not merely after an effect settles — deferring this to an effect - // would let the mount-time mark-as-read path observe a wrong `isScrolledToEnd`. - expect(mockMvcpHistory.at(0)).toBe(true); expect(isMvcpEnabled()).toBe(true); }); - it('leaves maintainVisibleContentPosition off on first render when mounted at the bottom (offset below threshold)', async () => { + it('enables data anchoring at the bottom so initial hydration preserves the visible tail', async () => { await renderList(0); - expect(isMvcpEnabled()).toBe(false); + expect(isMvcpEnabled()).toBe(true); }); - it('flips the flag as the user scrolls across the threshold', async () => { + it('keeps data anchoring enabled as the user scrolls across the threshold', async () => { await renderList(0); - expect(isMvcpEnabled()).toBe(false); + expect(isMvcpEnabled()).toBe(true); act(() => { - capturedListProps.onScroll?.({nativeEvent: {contentOffset: {y: THRESHOLD + 50}}}); + capturedListProps.onScroll?.(createScrollEvent(THRESHOLD + 50)); }); expect(isMvcpEnabled()).toBe(true); act(() => { - capturedListProps.onScroll?.({nativeEvent: {contentOffset: {y: 0}}}); + capturedListProps.onScroll?.(createScrollEvent(0)); }); - expect(isMvcpEnabled()).toBe(false); + expect(isMvcpEnabled()).toBe(true); }); }); diff --git a/tests/unit/hooks/useEditMessage.test.ts b/tests/unit/hooks/useEditMessage.test.ts index aebd3cdc0186..ea9a5a836dde 100644 --- a/tests/unit/hooks/useEditMessage.test.ts +++ b/tests/unit/hooks/useEditMessage.test.ts @@ -58,9 +58,10 @@ jest.mock('@hooks/useReportIsArchived', () => ({ default: () => false, })); +const mockScrollToBottom = jest.fn(); jest.mock('@hooks/useReportScrollManager', () => ({ __esModule: true, - default: () => ({scrollToIndex: jest.fn()}), + default: () => ({scrollToBottom: mockScrollToBottom}), })); jest.mock('@libs/ReportUtils', () => { @@ -142,4 +143,17 @@ describe('useEditMessage', () => { const args = mockShowDeleteModal.mock.calls.at(0); expect(args?.[1]?.reportActionID).toBe(props.reportAction?.reportActionID); }); + + it('scrolls to the bottom after deleting the newest message draft', () => { + const {hook} = renderUseEditMessage({shouldScrollToLastMessage: true}); + + act(() => { + hook.result.current.publishDraft(' '); + }); + act(() => { + mockShowDeleteModal.mock.calls.at(0)?.[3]?.(); + }); + + expect(mockScrollToBottom).toHaveBeenCalledTimes(1); + }); }); diff --git a/tests/unit/shouldFollowActionBadgeTargetTest.ts b/tests/unit/shouldFollowActionBadgeTargetTest.ts index e0ab679a0ce4..2f2b9328b90c 100644 --- a/tests/unit/shouldFollowActionBadgeTargetTest.ts +++ b/tests/unit/shouldFollowActionBadgeTargetTest.ts @@ -4,17 +4,17 @@ const BASE_PARAMS = { isProduction: false, actionTargetReportActionID: '200', prevActionTargetReportActionID: '100', - actionBadgeTargetIndex: 2, + actionBadgeTargetIndex: 7, prevActionBadgeTargetIndex: 5, }; describe('shouldFollowActionBadgeTarget', () => { - it('follows the target when it advances to a newer (lower-index) preview', () => { + it('follows the target when it advances to a newer (higher-index) preview', () => { expect(shouldFollowActionBadgeTarget(BASE_PARAMS)).toBe(true); }); - it('does not follow when the target moves to an older (higher-index) preview, e.g. while paginating', () => { - expect(shouldFollowActionBadgeTarget({...BASE_PARAMS, actionBadgeTargetIndex: 7})).toBe(false); + it('does not follow when the target moves to an older (lower-index) preview, e.g. while paginating', () => { + expect(shouldFollowActionBadgeTarget({...BASE_PARAMS, actionBadgeTargetIndex: 2})).toBe(false); }); it('does not follow when the target index is unchanged', () => { diff --git a/tests/unit/useReportActionsNewActionLiveTailTest.ts b/tests/unit/useReportActionsNewActionLiveTailTest.ts index 87e435b002a5..cf372cf3122c 100644 --- a/tests/unit/useReportActionsNewActionLiveTailTest.ts +++ b/tests/unit/useReportActionsNewActionLiveTailTest.ts @@ -101,7 +101,7 @@ function buildParams(overrides: Partial = {}): HookParams { hasNewerActions: true, linkedReportActionID: undefined, hasNewestReportAction: false, - sortedVisibleReportActions: [], + renderedVisibleReportActions: [], sortedAllReportActionsForPagination: [], reportActionPages: undefined, setTreatAsNoPaginationAnchor: jest.fn(), @@ -120,6 +120,29 @@ describe('useReportActionsNewActionLiveTail', () => { mockIsInSidePanel = false; }); + it('requests one post-render scroll for a sent comment instead of also scrolling immediately', () => { + const {result} = renderHook(() => useReportActionsNewActionLiveTail(buildParams({hasNewerActions: false, hasNewestReportAction: true}))); + + act(() => { + newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT})); + }); + + expect(result.current.isScrollToBottomEnabled).toBe(true); + expect(reportScrollManager.scrollToBottom).not.toHaveBeenCalled(); + }); + + it('does not queue a bottom scroll that would compete with a report-preview target', () => { + const preview = getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW}); + const {result} = renderHook(() => useReportActionsNewActionLiveTail(buildParams({hasNewerActions: false, hasNewestReportAction: true, renderedVisibleReportActions: [preview]}))); + + act(() => { + newActionHandler?.(true, preview); + }); + + expect(reportScrollManager.scrollToBottom).toHaveBeenCalledTimes(1); + expect(result.current.isScrollToBottomEnabled).toBe(false); + }); + it('threads the conciergeChat report through to the catch-up openReport call', () => { const conciergeChat = {reportID: 'concierge-live-tail-1'}; renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), {initialProps: buildParams({conciergeChat})}); diff --git a/tests/unit/useReportActionsScrollTest.tsx b/tests/unit/useReportActionsScrollTest.tsx index 0cf03c9ff5e6..118361255128 100644 --- a/tests/unit/useReportActionsScrollTest.tsx +++ b/tests/unit/useReportActionsScrollTest.tsx @@ -47,6 +47,7 @@ jest.mock('@hooks/useReportScrollManager', () => ({ const mockSetIsFloatingMessageCounterVisible = jest.fn(); const mockTrackVerticalScrolling = jest.fn(); const mockOnViewableItemsChanged = jest.fn(); +const mockUpdatePillVisibility = jest.fn(); let mockIsFloatingMessageCounterVisible = false; let mockIsActionBadgeAboveViewport = false; jest.mock('@pages/inbox/report/useReportUnreadMessageScrollTracking', () => ({ @@ -57,6 +58,7 @@ jest.mock('@pages/inbox/report/useReportUnreadMessageScrollTracking', () => ({ isActionBadgeAboveViewport: mockIsActionBadgeAboveViewport, trackVerticalScrolling: mockTrackVerticalScrolling, onViewableItemsChanged: mockOnViewableItemsChanged, + updatePillVisibility: mockUpdatePillVisibility, }), })); @@ -73,12 +75,6 @@ jest.mock('@pages/inbox/report/useReportActionsNewActionLiveTail', () => ({ }), })); -// --- useScrollToEndOnNewMessageReceived --- -jest.mock('@hooks/useScrollToEndOnNewMessageReceived', () => ({ - __esModule: true, - default: jest.fn(), -})); - // --- TransitionTracker --- const mockTransitionCallbacks: Array<() => void> = []; jest.mock('@libs/Navigation/TransitionTracker', () => ({ @@ -183,13 +179,11 @@ function buildParams(overrides: Partial = {}): ScrollParams { sortedVisibleReportActions: [makeAction('1')], renderedVisibleReportActions: [makeAction('1')], keyExtractor: (item: ReportAction) => item.reportActionID, - hasScrolledOverThreshold: false, markNewestActionAsRead: mockMarkNewestActionAsRead, completeSkippedMarkAsRead: mockCompleteSkippedMarkAsRead, unreadMarkerReportActionID: null, unreadMarkerReportActionIndex: -1, hasNewerActions: false, - draftAutoScrollKey: '', actionBadgeTargetIndex: -1, sortedAllReportActionsForPagination: [], treatAsNoPaginationAnchor: false, @@ -221,10 +215,6 @@ function flushTransitions() { }); } -function setReportLoadingState(value: {isLoadingInitialReportActions?: boolean; hasOnceLoadedReportActions?: boolean}) { - return Onyx.merge(`${ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE}${REPORT_ID}`, value); -} - describe('useReportActionsScroll', () => { beforeAll(() => { Onyx.init({keys: ONYXKEYS}); @@ -240,6 +230,9 @@ describe('useReportActionsScroll', () => { mockIsFloatingMessageCounterVisible = false; mockIsActionBadgeAboveViewport = false; mockIsScrollToBottomEnabled = false; + mockSetIsScrollToBottomEnabled.mockImplementation((enabled: boolean) => { + mockIsScrollToBottomEnabled = enabled; + }); mockIsTransactionThread = false; mockIsSentMoneyReportAction = false; mockIsReportPreviewAction = false; @@ -259,9 +252,6 @@ describe('useReportActionsScroll', () => { const {result} = await renderScroll(); expect(result.current.shouldBeAlignedToTop).toBe(false); - expect(result.current.shouldFocusToTopOnMount).toBe(false); - expect(result.current.maintainVisibleContentPosition.disabled).toBe(true); - expect(result.current.maintainVisibleContentPosition.autoscrollToBottomThreshold).toBeUndefined(); }); it('is aligned to top and focuses to top on mount for a transaction thread report', async () => { @@ -270,8 +260,6 @@ describe('useReportActionsScroll', () => { const {result} = await renderScroll(); expect(result.current.shouldBeAlignedToTop).toBe(true); - expect(result.current.shouldFocusToTopOnMount).toBe(true); - expect(result.current.maintainVisibleContentPosition?.autoscrollToBottomThreshold).toBe(CONST.REPORT.ACTIONS.ACTION_VISIBLE_THRESHOLD); }); it('is aligned to top for a money request report', async () => { @@ -290,22 +278,25 @@ describe('useReportActionsScroll', () => { expect(result.current.shouldBeAlignedToTop).toBe(true); }); - it('uses the linked report action as the initial scroll key', async () => { + it('positions a linked report action at chronological index zero', async () => { mockRouteParams = {reportActionID: LINKED_ACTION_ID}; - const {result} = await renderScroll({sortedVisibleReportActions: [makeAction(LINKED_ACTION_ID)]}); + const linkedAction = makeAction(LINKED_ACTION_ID); + const {result} = await renderScroll({sortedVisibleReportActions: [linkedAction], renderedVisibleReportActions: [linkedAction]}); - expect(result.current.initialScrollKey).toBe(LINKED_ACTION_ID); - expect(result.current.shouldFocusToTopOnMount).toBe(false); + expect(result.current.initialScrollIndex).toBe(0); + expect(result.current.initialScrollIndexParams).toEqual({viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}); }); - it('falls back to the unread marker action as the initial scroll key', async () => { + it('positions an unread marker at chronological index zero', async () => { + const unreadAction = makeAction(UNREAD_ACTION_ID); const {result} = await renderScroll({ unreadMarkerReportActionID: UNREAD_ACTION_ID, - sortedVisibleReportActions: [makeAction(UNREAD_ACTION_ID)], + sortedVisibleReportActions: [unreadAction], + renderedVisibleReportActions: [unreadAction], }); - expect(result.current.initialScrollKey).toBe(UNREAD_ACTION_ID); + expect(result.current.initialScrollIndex).toBe(0); }); it('suppresses the initial scroll key for an aligned-to-top CREATED anchor action', async () => { @@ -316,9 +307,8 @@ describe('useReportActionsScroll', () => { sortedVisibleReportActions: [makeAction(LINKED_ACTION_ID, {actionName: CONST.REPORT.ACTIONS.TYPE.CREATED})], }); - expect(result.current.initialScrollKey).toBeUndefined(); - // No key + aligned-to-top → focus to top. - expect(result.current.shouldFocusToTopOnMount).toBe(true); + expect(result.current.initialScrollIndex).toBe(0); + expect(result.current.initialScrollIndexParams).toBeUndefined(); }); it('does not focus to top for a single-expense money request report opened from the X Replies link', async () => { @@ -329,7 +319,6 @@ describe('useReportActionsScroll', () => { // Still aligned to top so short reports keep their layout, but the mount position is the latest message. expect(result.current.shouldBeAlignedToTop).toBe(true); - expect(result.current.shouldFocusToTopOnMount).toBe(false); expect(result.current.initialScrollIndex).toBeUndefined(); }); @@ -340,7 +329,6 @@ describe('useReportActionsScroll', () => { const {result} = await renderScroll(); expect(result.current.shouldBeAlignedToTop).toBe(true); - expect(result.current.shouldFocusToTopOnMount).toBe(false); }); }); @@ -403,135 +391,48 @@ describe('useReportActionsScroll', () => { result.current.scrollToActionBadgeTarget(); }); - expect(mockScrollToIndex).toHaveBeenCalledWith(5, {viewPosition: 1, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}); + expect(mockScrollToIndex).toHaveBeenCalledWith(5, {viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}); }); }); - describe('flushPendingScrollToBottom', () => { + describe('pending live-tail requests', () => { it('does nothing when scroll-to-bottom is not enabled', async () => { mockIsScrollToBottomEnabled = false; - const {result} = await renderScroll(); - act(() => { - result.current.flushPendingScrollToBottom(); - }); + await renderScroll(); expect(mockScrollToBottom).not.toHaveBeenCalled(); expect(mockSetIsScrollToBottomEnabled).not.toHaveBeenCalled(); expect(mockCompleteLiveTailPrune).not.toHaveBeenCalled(); }); - it('scrolls, disables itself and prunes when scroll-to-bottom is enabled', async () => { + it('consumes the request after render without waiting for a future viewport layout', async () => { mockIsScrollToBottomEnabled = true; - const {result} = await renderScroll(); - act(() => { - result.current.flushPendingScrollToBottom(); - }); + const {rerender} = await renderScroll(); expect(mockScrollToBottom).toHaveBeenCalledTimes(1); expect(mockSetIsScrollToBottomEnabled).toHaveBeenCalledWith(false); expect(mockCompleteLiveTailPrune).toHaveBeenCalledTimes(1); - }); - }); - - describe('onLoad', () => { - it('does nothing when the list is not configured to focus to top on mount', async () => { - const {result} = await renderScroll(); - act(() => { - result.current.onLoad(); - }); - - // Stays disabled with no autoscroll threshold for a regular chat. - expect(result.current.maintainVisibleContentPosition.disabled).toBe(true); - expect(result.current.maintainVisibleContentPosition.autoscrollToBottomThreshold).toBeUndefined(); - }); - - it('waits for the report actions to have loaded before disabling autoscroll-to-top', async () => { - mockIsTransactionThread = true; - // No loading state → onLoad bails. - - const {result} = await renderScroll(); - expect(result.current.maintainVisibleContentPosition?.autoscrollToBottomThreshold).toBe(CONST.REPORT.ACTIONS.ACTION_VISIBLE_THRESHOLD); - - act(() => { - result.current.onLoad(); - }); - - expect(result.current.maintainVisibleContentPosition?.autoscrollToBottomThreshold).toBe(CONST.REPORT.ACTIONS.ACTION_VISIBLE_THRESHOLD); - }); - - it('disables autoscroll-to-top after a frame once report actions have loaded', async () => { - mockIsTransactionThread = true; - await setReportLoadingState({isLoadingInitialReportActions: false, hasOnceLoadedReportActions: true}); - - const {result} = await renderScroll(); - expect(result.current.maintainVisibleContentPosition?.autoscrollToBottomThreshold).toBe(CONST.REPORT.ACTIONS.ACTION_VISIBLE_THRESHOLD); - - act(() => { - result.current.onLoad(); - }); - - // The threshold must drop to 0 (not undefined) so FlashList keeps clearing its internal pending-autoscroll flag. - expect(result.current.maintainVisibleContentPosition?.autoscrollToBottomThreshold).toBe(0); - }); - - it('disables autoscroll-to-top when report actions finish loading after the list has mounted', async () => { - mockIsTransactionThread = true; - - const {result} = await renderScroll(); - expect(result.current.maintainVisibleContentPosition?.autoscrollToBottomThreshold).toBe(CONST.REPORT.ACTIONS.ACTION_VISIBLE_THRESHOLD); - - // Load completes after mount → companion effect turns autoscroll off. - await act(async () => { - await setReportLoadingState({isLoadingInitialReportActions: false, hasOnceLoadedReportActions: true}); - await waitForBatchedUpdates(); - }); - // The threshold must drop to 0 (not undefined) so FlashList keeps clearing its internal pending-autoscroll flag. - expect(result.current.maintainVisibleContentPosition?.autoscrollToBottomThreshold).toBe(0); + mockScrollOffsetRef.current = 9999; + rerender(buildParams()); + expect(mockScrollToBottom).toHaveBeenCalledTimes(1); }); }); describe('effects', () => { - it('schedules an initial scroll-to-bottom on mount for a regular chat report', async () => { - await renderScroll(); - - expect(mockScrollToBottom).not.toHaveBeenCalled(); - flushTransitions(); - - expect(mockSetIsFloatingMessageCounterVisible).toHaveBeenCalledWith(false); - expect(mockScrollToBottom).toHaveBeenCalledTimes(1); - }); - - it('does not scroll to bottom on mount when there is an initial scroll key', async () => { - mockRouteParams = {reportActionID: LINKED_ACTION_ID}; - - await renderScroll({sortedVisibleReportActions: [makeAction(LINKED_ACTION_ID)]}); - flushTransitions(); - - expect(mockScrollToBottom).not.toHaveBeenCalled(); - }); + it('leaves incoming-message following to LegendList', async () => { + mockScrollOffsetRef.current = 0; - it('does not scroll to bottom on mount when the list focuses to top', async () => { - mockIsTransactionThread = true; + const {rerender} = await renderScroll(); - await renderScroll(); - flushTransitions(); + const actions = [makeAction('2'), makeAction('1')]; + rerender(buildParams({sortedVisibleReportActions: actions, renderedVisibleReportActions: actions.toReversed()})); expect(mockScrollToBottom).not.toHaveBeenCalled(); }); - it('scrolls to bottom on mount for a single-expense money request report opened from the X Replies link', async () => { - mockIsMoneyRequestReport = true; - mockRouteParams = {shouldScrollToLatest: 'true'}; - - await renderScroll(); - flushTransitions(); - - expect(mockScrollToBottom).toHaveBeenCalledTimes(1); - }); - it('clears the X Replies flag once it has been applied', async () => { mockIsMoneyRequestReport = true; mockRouteParams = {shouldScrollToLatest: 'true'}; @@ -541,32 +442,15 @@ describe('useReportActionsScroll', () => { expect(mockSetParams).toHaveBeenCalledWith({shouldScrollToLatest: undefined}); }); - it('does not clear the X Replies flag when it was never set', async () => { - mockIsMoneyRequestReport = true; - - await renderScroll(); - - expect(mockSetParams).not.toHaveBeenCalled(); - }); - - it('auto-scrolls to bottom when a new draft key arrives near the bottom and the newest action is present', async () => { + it('does not schedule a competing scroll when a streamed draft grows', async () => { mockScrollOffsetRef.current = 0; - const {rerender} = await renderScroll({draftAutoScrollKey: ''}); - - rerender(buildParams({draftAutoScrollKey: 'draft-1'})); - - expect(mockSetIsFloatingMessageCounterVisible).toHaveBeenCalledWith(false); - expect(mockScrollToBottom).toHaveBeenCalled(); - }); - - it('does not auto-scroll on a new draft key when scrolled away from the bottom', async () => { - mockScrollOffsetRef.current = 9999; - - const {rerender} = await renderScroll({draftAutoScrollKey: ''}); + const draft = makeAction('2', {message: [{type: 'COMMENT', text: 'Hello', html: '

Hello

'}]}); + const {rerender} = await renderScroll({renderedVisibleReportActions: [makeAction('1'), draft]}); mockScrollToBottom.mockClear(); - rerender(buildParams({draftAutoScrollKey: 'draft-1'})); + const updatedDraft: ReportAction = {...draft, message: [{type: 'COMMENT', text: 'Hello, here is the rest of the reply.', html: '

Hello, here is the rest of the reply.

'}]}; + rerender(buildParams({renderedVisibleReportActions: [makeAction('1'), updatedDraft]})); expect(mockScrollToBottom).not.toHaveBeenCalled(); }); diff --git a/tests/unit/useReportUnreadMessageScrollTrackingTest.ts b/tests/unit/useReportUnreadMessageScrollTrackingTest.ts index ecc1f04d688f..09c0d77e37d8 100644 --- a/tests/unit/useReportUnreadMessageScrollTrackingTest.ts +++ b/tests/unit/useReportUnreadMessageScrollTrackingTest.ts @@ -227,6 +227,32 @@ describe('useReportUnreadMessageScrollTracking', () => { expect(onUnreadActionVisibleLocalMockFn).toHaveBeenCalledTimes(1); expect(result.current.isFloatingMessageCounterVisible).toBe(false); }); + + it('tracks an unread marker in a chronological list', () => { + const offsetRef = {current: 0}; + const {result} = renderHook(() => + useReportUnreadMessageScrollTracking({ + reportID, + currentVerticalScrollingOffsetRef: offsetRef, + onUnreadActionVisible: onUnreadActionVisibleMockFn, + unreadMarkerReportActionIndex: 5, + isInverted: false, + onTrackScrolling: onTrackScrollingMockFn, + hasNewerActions: false, + }), + ); + + act(() => { + result.current.onViewableItemsChanged({viewableItems: [{index: 3, key: 'reportActions_3', isViewable: true, item: {}}], changed: []}); + }); + expect(result.current.isFloatingMessageCounterVisible).toBe(true); + + act(() => { + result.current.onViewableItemsChanged({viewableItems: [{index: 5, key: 'reportActions_5', isViewable: true, item: {}}], changed: []}); + }); + expect(result.current.isFloatingMessageCounterVisible).toBe(false); + expect(onUnreadActionVisibleMockFn).toHaveBeenCalled(); + }); }); describe('action badge above viewport tracking', () => { @@ -281,6 +307,28 @@ describe('useReportUnreadMessageScrollTracking', () => { expect(result.current.isActionBadgeAboveViewport).toBe(true); }); + it('returns isActionBadgeAboveViewport as true for a lower index above a chronological viewport', () => { + const offsetRef = {current: 0}; + const {result} = renderHook(() => + useReportUnreadMessageScrollTracking({ + reportID, + currentVerticalScrollingOffsetRef: offsetRef, + onUnreadActionVisible: onUnreadActionVisibleMockFn, + onTrackScrolling: onTrackScrollingMockFn, + hasNewerActions: false, + unreadMarkerReportActionIndex: -1, + isInverted: false, + actionBadgeTargetIndex: 1, + }), + ); + + act(() => { + result.current.onViewableItemsChanged({viewableItems: [{index: 3, key: 'reportActions_3', isViewable: true, item: {}}], changed: []}); + }); + + expect(result.current.isActionBadgeAboveViewport).toBe(true); + }); + it('returns isActionBadgeAboveViewport as false when action badge target is visible in viewport', () => { const offsetRef = {current: 0}; const {result} = renderHook(() => From ba852b260a53ce70660ae2454689dd64f5718329 Mon Sep 17 00:00:00 2001 From: chrispader Date: Thu, 10 Sep 2026 14:34:21 +0200 Subject: [PATCH 02/61] fix: address LegendList report action review --- .../MoneyRequestReportActionsList.tsx | 46 ++++---- .../ReportActionCompose/useEditMessage.ts | 13 ++- .../inbox/report/ReportActionIndexContext.tsx | 5 +- .../report/ReportActionItemMessageEdit.tsx | 4 +- src/pages/inbox/report/ReportActionsList.tsx | 13 ++- .../perf-test/ReportActionsList.perf-test.tsx | 4 +- ...equestReportActionsListRejectModalTest.tsx | 101 ++++++++++++++++-- tests/ui/ReportActionItemMessageEditTest.tsx | 25 ++++- tests/ui/ReportActionsListTest.tsx | 13 +-- tests/unit/hooks/useEditMessage.test.ts | 15 +++ 10 files changed, 192 insertions(+), 47 deletions(-) diff --git a/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx b/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx index 72aacd5b03c7..f9c917933222 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx @@ -51,7 +51,7 @@ import {useActionListContext, useActionListRef} from '@pages/inbox/ActionListCon import {useAgentZeroStatus} from '@pages/inbox/AgentZeroStatusContext'; import {useConciergeDraft} from '@pages/inbox/ConciergeDraftContext'; import FloatingMessageCounter from '@pages/inbox/report/FloatingMessageCounter'; -import ReportActionIndexContext from '@pages/inbox/report/ReportActionIndexContext'; +import ReportActionIndexContext, {ReportActionScrollToNewestContext} from '@pages/inbox/report/ReportActionIndexContext'; import ReportActionsListItemRenderer from '@pages/inbox/report/ReportActionsListItemRenderer'; import {getUnreadMarkerReportAction} from '@pages/inbox/report/shouldDisplayNewMarkerOnReportAction'; import useReportUnreadMessageScrollTracking from '@pages/inbox/report/useReportUnreadMessageScrollTracking'; @@ -638,27 +638,32 @@ function MoneyRequestReportActionsList({onLayout}: MoneyRequestReportListProps) // This value cannot be memoized, because it is based on the indexWithinReportActions which changes on every render. // eslint-disable-next-line react/jsx-no-constructed-context-values - const reportActionIndexContextValue = {index: indexWithinReportActions, isNewest: indexWithinReportActions === visibleReportActions.length - 1}; + const reportActionIndexContextValue = { + index: indexWithinReportActions, + isNewest: indexWithinReportActions === visibleReportActions.length - 1, + }; return ( - - 1} - isFirstVisibleReportAction={firstVisibleReportActionID === reportAction.reportActionID} - shouldHideThreadDividerLine - linkedReportActionID={linkedReportActionID} - isHarvestCreatedExpenseReport={shouldShowHarvestCreatedAction} - shouldDisableContextMenuForConciergeDraft={shouldDisableContextMenuForConciergeDraft} - /> - + + + 1} + isFirstVisibleReportAction={firstVisibleReportActionID === reportAction.reportActionID} + shouldHideThreadDividerLine + linkedReportActionID={linkedReportActionID} + isHarvestCreatedExpenseReport={shouldShowHarvestCreatedAction} + shouldDisableContextMenuForConciergeDraft={shouldDisableContextMenuForConciergeDraft} + /> + + ); }, [ @@ -674,6 +679,7 @@ function MoneyRequestReportActionsList({onLayout}: MoneyRequestReportListProps) shouldShowHarvestCreatedAction, draftReportActionID, isDraftPendingCompletion, + scrollToBottom, ], ); diff --git a/src/pages/inbox/report/ReportActionCompose/useEditMessage.ts b/src/pages/inbox/report/ReportActionCompose/useEditMessage.ts index f4657b695142..6552c2cfcbf3 100644 --- a/src/pages/inbox/report/ReportActionCompose/useEditMessage.ts +++ b/src/pages/inbox/report/ReportActionCompose/useEditMessage.ts @@ -23,6 +23,7 @@ type UseEditMessageProps = { originalReportID: string | undefined; reportAction: OnyxTypes.ReportAction | null | undefined; shouldScrollToLastMessage?: boolean; + scrollToLastMessage?: () => void; debouncedCommentMaxLengthValidation: DebouncedFuncLeading<(value: string) => boolean>; composerRef: React.RefObject; }; @@ -30,7 +31,15 @@ type UseEditMessageProps = { /** * Delete the draft of the comment being edited. This will take the comment out of "edit mode" with the old content. */ -function useEditMessage({reportID, originalReportID, reportAction, shouldScrollToLastMessage = false, debouncedCommentMaxLengthValidation, composerRef}: UseEditMessageProps) { +function useEditMessage({ + reportID, + originalReportID, + reportAction, + shouldScrollToLastMessage = false, + scrollToLastMessage, + debouncedCommentMaxLengthValidation, + composerRef, +}: UseEditMessageProps) { const reportScrollManager = useReportScrollManager(); const {email} = useCurrentUserPersonalDetails(); @@ -52,7 +61,7 @@ function useEditMessage({reportID, originalReportID, reportAction, shouldScrollT // Scroll to the last comment after editing to make sure the whole comment is clearly visible in the report. if (shouldScrollToLastMessage) { - reportScrollManager.scrollToBottom(); + (scrollToLastMessage ?? reportScrollManager.scrollToBottom)(); } } diff --git a/src/pages/inbox/report/ReportActionIndexContext.tsx b/src/pages/inbox/report/ReportActionIndexContext.tsx index 4cb8f9a04ecd..04a1a0d22bd0 100644 --- a/src/pages/inbox/report/ReportActionIndexContext.tsx +++ b/src/pages/inbox/report/ReportActionIndexContext.tsx @@ -19,6 +19,9 @@ type ReportActionPosition = { const ReportActionIndexContext = createContext({index: 0, isNewest: false}); +/** Lets shared list implementations provide their own reliable way to reach the newest action. */ +const ReportActionScrollToNewestContext = createContext<(() => void) | undefined>(undefined); + /** * Uses LegendList's recycling-aware state in the main report list and behaves like useState in shared, non-recycled lists. */ @@ -29,5 +32,5 @@ function useReportActionItemState(initialState: State | (() => State)): [ return isRecycling ? [...recyclingState] : state; } -export {useReportActionItemState}; +export {ReportActionScrollToNewestContext, useReportActionItemState}; export default ReportActionIndexContext; diff --git a/src/pages/inbox/report/ReportActionItemMessageEdit.tsx b/src/pages/inbox/report/ReportActionItemMessageEdit.tsx index 9933a42fa34f..18642635b577 100644 --- a/src/pages/inbox/report/ReportActionItemMessageEdit.tsx +++ b/src/pages/inbox/report/ReportActionItemMessageEdit.tsx @@ -58,7 +58,7 @@ import Suggestions from './ReportActionCompose/Suggestions'; import useDebouncedCommentMaxLengthValidation from './ReportActionCompose/useDebouncedCommentMaxLengthValidation'; import useEditMessage from './ReportActionCompose/useEditMessage'; import {useReportActionActiveEdit, useReportActionActiveEditActions} from './ReportActionEditMessageContext'; -import ReportActionIndexContext from './ReportActionIndexContext'; +import ReportActionIndexContext, {ReportActionScrollToNewestContext} from './ReportActionIndexContext'; import shouldUseEmojiPickerSelection from './shouldUseEmojiPickerSelection'; import useDebouncedSaveDraft from './useDebouncedSaveDraft'; import useDraftMessageVideoAttributeCache from './useDraftMessageVideoAttributeCache'; @@ -87,6 +87,7 @@ const DEFAULT_MODAL_VALUE = { function ReportActionItemMessageEdit({action, reportID, originalReportID, policyID, ref}: ReportActionItemMessageEditProps) { const {index, isNewest} = useContext(ReportActionIndexContext); + const scrollToNewestAction = useContext(ReportActionScrollToNewestContext); const [preferredSkinTone = CONST.EMOJI_DEFAULT_SKIN_TONE] = useOnyx(ONYXKEYS.PREFERRED_EMOJI_SKIN_TONE); const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(reportID)}`); const [reportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(reportID)}`); @@ -259,6 +260,7 @@ function ReportActionItemMessageEdit({action, reportID, originalReportID, policy originalReportID, reportAction: action, shouldScrollToLastMessage: isNewest, + scrollToLastMessage: scrollToNewestAction, debouncedCommentMaxLengthValidation, composerRef, }); diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index 51d6101678d5..1d72b790cc49 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -95,14 +95,19 @@ const REPORT_ACTION_COMMENT_SIZE = { EXTRA_LONG: 'extra-long', } as const; +// Character-count boundaries used to bucket comments so LegendList can recycle similarly sized items. +const REPORT_ACTION_COMMENT_SHORT_MAX_LENGTH = 80; +const REPORT_ACTION_COMMENT_MEDIUM_MAX_LENGTH = 320; +const REPORT_ACTION_COMMENT_LONG_MAX_LENGTH = 1200; + function getReportActionCommentSize(messageLength: number): string { - if (messageLength <= 80) { + if (messageLength <= REPORT_ACTION_COMMENT_SHORT_MAX_LENGTH) { return REPORT_ACTION_COMMENT_SIZE.SHORT; } - if (messageLength <= 320) { + if (messageLength <= REPORT_ACTION_COMMENT_MEDIUM_MAX_LENGTH) { return REPORT_ACTION_COMMENT_SIZE.MEDIUM; } - if (messageLength <= 1200) { + if (messageLength <= REPORT_ACTION_COMMENT_LONG_MAX_LENGTH) { return REPORT_ACTION_COMMENT_SIZE.LONG; } return REPORT_ACTION_COMMENT_SIZE.EXTRA_LONG; @@ -600,6 +605,8 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct keyboardShouldPersistTaps="handled" onLayout={recordTimeToMeasureItemLayout} onScroll={trackScrollPositionAndThreshold} + onStartReached={loadOlderChatsOnStartReached} + onStartReachedThreshold={PAGINATION_THRESHOLD} onViewableItemsChanged={onViewableItemsChanged} extraData={extraData} key={listID} diff --git a/tests/perf-test/ReportActionsList.perf-test.tsx b/tests/perf-test/ReportActionsList.perf-test.tsx index d39902dc322c..a1ab104f6a2b 100644 --- a/tests/perf-test/ReportActionsList.perf-test.tsx +++ b/tests/perf-test/ReportActionsList.perf-test.tsx @@ -120,7 +120,9 @@ function ReportActionsListWrapper() { ); } -test('[ReportActionsList] should render ReportActionsList with 500 reportActions stored', async () => { +// LegendList's behavior mock eagerly renders the supplied data, while the previous FlashList test used +// its virtualized implementation. Start a renderer-specific baseline instead of comparing those harnesses. +test('[ReportActionsList] should render LegendList with 500 reportActions stored', async () => { const scenario = async () => { await screen.findByTestId('report-actions-list'); }; diff --git a/tests/ui/MoneyRequestReportActionsListRejectModalTest.tsx b/tests/ui/MoneyRequestReportActionsListRejectModalTest.tsx index da7168b6e9c3..717e79d489d9 100644 --- a/tests/ui/MoneyRequestReportActionsListRejectModalTest.tsx +++ b/tests/ui/MoneyRequestReportActionsListRejectModalTest.tsx @@ -11,6 +11,9 @@ import {useIsReportLoadPending} from '@hooks/useInFlightRequests'; import type * as InFlightRequests from '@hooks/useInFlightRequests'; import useNetwork from '@hooks/useNetwork'; +import {ActionListContextProvider} from '@pages/inbox/ActionListContext'; +import type * as ReportActionIndexContexts from '@pages/inbox/report/ReportActionIndexContext'; + import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type SCREENS from '@src/SCREENS'; @@ -31,6 +34,12 @@ const FAKE_POLICY_ID = 'FAKE_POLICY_001'; const FAKE_ACCOUNT_ID = 15593135; const FAKE_TRANSACTION_ID = 'FAKE_TXN_001'; const FAKE_EMAIL = 'testuser@example.com'; +const MOCK_UNIFIED_LAST_ITEM_INDEX = 37; +const mockScrollToIndex = jest.fn(); +const mockScrollToEnd = jest.fn(); +const mockScrollToOffset = jest.fn(); +let mockReportActionPosition: {index: number; isNewest: boolean} | undefined; +let mockScrollToNewestAction: (() => void) | undefined; jest.mock('@react-navigation/native', () => ({ ...jest.requireActual('@react-navigation/native'), @@ -80,12 +89,38 @@ jest.mock('@libs/Navigation/Navigation', () => ({ jest.mock('@components/MoneyRequestReportView/MoneyRequestReportTransactionList', () => { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const {View} = require('react-native'); - return ({listFooterComponent, isLoadingInitialActions}: {listFooterComponent?: React.ReactElement; isLoadingInitialActions: boolean}) => ( - - {isLoadingInitialActions ? : null} - {listFooterComponent} - - ); + const ReactActual = jest.requireActual('react'); + return ({ + listFooterComponent, + isLoadingInitialActions, + listRef, + onLastItemIndexChange, + visibleReportActions, + renderReportAction, + }: { + listFooterComponent?: React.ReactElement; + isLoadingInitialActions: boolean; + listRef: React.Ref<{ + scrollToIndex: typeof mockScrollToIndex; + scrollToEnd: typeof mockScrollToEnd; + scrollToOffset: typeof mockScrollToOffset; + }>; + onLastItemIndexChange?: (index: number) => void; + visibleReportActions: ReportAction[]; + renderReportAction: (reportAction: ReportAction, index: number) => React.ReactElement; + }) => { + ReactActual.useImperativeHandle(listRef, () => ({scrollToIndex: mockScrollToIndex, scrollToEnd: mockScrollToEnd, scrollToOffset: mockScrollToOffset})); + ReactActual.useLayoutEffect(() => onLastItemIndexChange?.(MOCK_UNIFIED_LAST_ITEM_INDEX), [onLastItemIndexChange]); + const newestAction = visibleReportActions.at(-1); + + return ( + + {isLoadingInitialActions ? : null} + {newestAction ? renderReportAction(newestAction, visibleReportActions.length - 1) : null} + {listFooterComponent} + + ); + }; }); jest.mock('@components/MoneyRequestReportView/SearchMoneyRequestReportEmptyState', () => { @@ -159,7 +194,17 @@ jest.mock('@hooks/useMobileSelectionMode', () => jest.fn(() => true)); jest.mock('@hooks/useResponsiveLayoutOnWideRHP', () => jest.fn(() => ({shouldUseNarrowLayout: true}))); jest.mock('@hooks/useFilterSelectedTransactions', () => jest.fn()); jest.mock('@hooks/useLoadReportActions', () => jest.fn(() => ({loadOlderChats: jest.fn(), loadNewerChats: jest.fn()}))); -jest.mock('@pages/inbox/report/ReportActionsListItemRenderer', () => jest.fn(() => null)); +jest.mock('@pages/inbox/report/ReportActionsListItemRenderer', () => { + const ReactActual = jest.requireActual('react'); + const {default: ReportActionIndexContextActual, ReportActionScrollToNewestContext: ReportActionScrollToNewestContextActual} = + jest.requireActual('@pages/inbox/report/ReportActionIndexContext'); + + return jest.fn(() => { + mockReportActionPosition = ReactActual.useContext(ReportActionIndexContextActual); + mockScrollToNewestAction = ReactActual.useContext(ReportActionScrollToNewestContextActual); + return null; + }); +}); jest.mock('@hooks/useParentReportAction', () => jest.fn(() => undefined)); jest.mock('@navigation/helpers/isSearchTopmostFullScreenRoute', () => jest.fn(() => false)); @@ -223,9 +268,23 @@ const mockReportAction = createMock { return render( - + @@ -250,6 +309,8 @@ describe('MoneyRequestReportActionsList - Reject Educational Modal', () => { beforeEach(async () => { jest.clearAllMocks(); + mockReportActionPosition = undefined; + mockScrollToNewestAction = undefined; jest.spyOn(NativeNavigation, 'useIsFocused').mockReturnValue(true); mockUseIsReportLoadPending.mockReturnValue(false); mockUseNetwork.mockReturnValue({isOffline: false}); @@ -259,6 +320,30 @@ describe('MoneyRequestReportActionsList - Reject Educational Modal', () => { }); }); + it('should use the unified list index when the newest action requests a bottom scroll', async () => { + await act(async () => { + await Onyx.multiSet({ + [`${ONYXKEYS.COLLECTION.REPORT}${FAKE_REPORT_ID}` as const]: mockReport, + [`${ONYXKEYS.COLLECTION.POLICY}${FAKE_POLICY_ID}` as const]: mockPolicy, + [`${ONYXKEYS.COLLECTION.TRANSACTION}${FAKE_TRANSACTION_ID}` as const]: mockTransaction, + [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${FAKE_REPORT_ID}` as const]: { + [mockReportAction.reportActionID]: mockReportAction, + [mockCommentReportAction.reportActionID]: mockCommentReportAction, + }, + [`${ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE}${FAKE_REPORT_ID}` as const]: {isLoadingInitialReportActions: false, hasOnceLoadedReportActions: true}, + [ONYXKEYS.SESSION]: {accountID: FAKE_ACCOUNT_ID, email: FAKE_EMAIL} as Session, + }); + }); + + renderComponent(); + await waitForBatchedUpdatesWithAct(); + + expect(mockReportActionPosition).toEqual(expect.objectContaining({isNewest: true})); + act(() => mockScrollToNewestAction?.()); + expect(mockScrollToIndex).toHaveBeenCalledWith({index: MOCK_UNIFIED_LAST_ITEM_INDEX, animated: false, viewPosition: 1}); + expect(mockScrollToEnd).not.toHaveBeenCalled(); + }); + it('should show reject educational modal when reject option is selected and explanation has NOT been dismissed', async () => { await act(async () => { await Onyx.multiSet({ diff --git a/tests/ui/ReportActionItemMessageEditTest.tsx b/tests/ui/ReportActionItemMessageEditTest.tsx index 302edd2eb643..af137b294ba1 100644 --- a/tests/ui/ReportActionItemMessageEditTest.tsx +++ b/tests/ui/ReportActionItemMessageEditTest.tsx @@ -9,6 +9,7 @@ import {editReportComment} from '@libs/actions/Report'; import * as ReportActionContextMenu from '@pages/inbox/report/ContextMenu/ReportActionContextMenu'; import {ReportActionEditMessageContextProvider} from '@pages/inbox/report/ReportActionEditMessageContext'; +import ReportActionIndexContext, {ReportActionScrollToNewestContext} from '@pages/inbox/report/ReportActionIndexContext'; import type {ReportActionItemMessageEditProps} from '@pages/inbox/report/ReportActionItemMessageEdit'; import ReportActionItemMessageEdit from '@pages/inbox/report/ReportActionItemMessageEdit'; import {draftMessageVideoAttributeCache} from '@pages/inbox/report/useDraftMessageVideoAttributeCache'; @@ -86,13 +87,17 @@ function ReportScreenProviders({children}: PropsWithChildren) { return {children}; } -const renderReportActionItemMessageEdit = (props?: Partial) => { +const renderReportActionItemMessageEdit = (props?: Partial, listContext?: {index: number; isNewest: boolean; scrollToNewestAction?: () => void}) => { return render( - + + + + + , ); }; @@ -210,5 +215,15 @@ describe('ReportActionCompose Integration Tests', () => { expect(videoAttributeCache?.[videoSource]).toContain('data-expensify-height'); expect(videoAttributeCache?.[videoSource]).toContain('data-expensify-width'); }); + + it('should use the list-specific scroll after saving the newest message', () => { + const scrollToNewestAction = jest.fn(); + renderReportActionItemMessageEdit(undefined, {index: 9, isNewest: true, scrollToNewestAction}); + + fireEvent.changeText(screen.getByTestId('composer'), 'Edited message'); + fireEvent.press(screen.getByLabelText('common.saveChanges')); + + expect(scrollToNewestAction).toHaveBeenCalledTimes(1); + }); }); }); diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 4a0b2c90a543..35957761cbb8 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -188,6 +188,7 @@ type MockLegendListProps = { maintainScrollAtEndThreshold?: number; maintainVisibleContentPosition?: boolean; onLoad?: () => void; + onStartReachedThreshold?: number; recycleItems?: boolean; renderItem?: (info: {item: OnyxTypes.ReportAction; index: number}) => React.ReactElement | null; onStartReached?: () => void; @@ -549,21 +550,21 @@ describe('ReportActionsList (body)', () => { getItemType?.({ ...comment, reportActionID: 'medium-comment', - message: [{type: 'COMMENT', html: 'Medium comment', text: 'a'.repeat(200)}], + message: [{type: 'COMMENT', html: 'Medium comment', text: 'a'.repeat(320)}], }), ).toBe(`${CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT}-medium`); expect( getItemType?.({ ...comment, reportActionID: 'long-comment', - message: [{type: 'COMMENT', html: 'Long comment', text: 'a'.repeat(600)}], + message: [{type: 'COMMENT', html: 'Long comment', text: 'a'.repeat(1200)}], }), ).toBe(`${CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT}-long`); expect( getItemType?.({ ...comment, reportActionID: 'extra-long-comment', - message: [{type: 'COMMENT', html: 'Extra long comment', text: 'a'.repeat(1500)}], + message: [{type: 'COMMENT', html: 'Extra long comment', text: 'a'.repeat(1201)}], }), ).toBe(`${CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT}-extra-long`); expect( @@ -582,7 +583,7 @@ describe('ReportActionsList (body)', () => { ).toBe(`${CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT}-link-preview-short`); }); - it('continues loading older pages from scroll events when LegendList does not report reaching the start', () => { + it('loads older pages when LegendList reaches the start and deduplicates the scroll fallback', () => { mockUseNetwork.mockReturnValue({isOffline: false}); mockUsePaginatedReportActions.mockReturnValue({ ...defaultPaginatedReportActionsResult, @@ -601,12 +602,12 @@ describe('ReportActionsList (body)', () => { }); act(() => { - listProps?.onScroll?.(createScrollEvent(0)); + listProps?.onStartReached?.(); }); expect(mockLoadOlderChats).toHaveBeenCalledTimes(1); + expect(listProps?.onStartReachedThreshold).toBe(0.75); act(() => { - listProps?.onStartReached?.(); listProps?.onScroll?.(createScrollEvent(0)); }); expect(mockLoadOlderChats).toHaveBeenCalledTimes(1); diff --git a/tests/unit/hooks/useEditMessage.test.ts b/tests/unit/hooks/useEditMessage.test.ts index ea9a5a836dde..721b905757a3 100644 --- a/tests/unit/hooks/useEditMessage.test.ts +++ b/tests/unit/hooks/useEditMessage.test.ts @@ -156,4 +156,19 @@ describe('useEditMessage', () => { expect(mockScrollToBottom).toHaveBeenCalledTimes(1); }); + + it('uses the list-specific bottom scroll after deleting the newest message draft', () => { + const scrollToLastMessage = jest.fn(); + const {hook} = renderUseEditMessage({shouldScrollToLastMessage: true, scrollToLastMessage}); + + act(() => { + hook.result.current.publishDraft(' '); + }); + act(() => { + mockShowDeleteModal.mock.calls.at(0)?.[3]?.(); + }); + + expect(scrollToLastMessage).toHaveBeenCalledTimes(1); + expect(mockScrollToBottom).not.toHaveBeenCalled(); + }); }); From 332a7b34e1da18ee5f261c5e6db43e5901879f2b Mon Sep 17 00:00:00 2001 From: chrispader Date: Thu, 10 Sep 2026 18:24:54 +0200 Subject: [PATCH 03/61] fix: stabilize LegendList initial loading --- .../MoneyRequestReportView.tsx | 8 +-- src/components/ReportActionsSkeletonCover.tsx | 38 +++++++++++++ src/hooks/useReportActionsListModel.ts | 2 + src/pages/inbox/report/ReportActionsList.tsx | 17 ++---- .../report/ReportActionsLoadingSkeleton.tsx | 4 +- tests/ui/MoneyRequestReportViewTest.tsx | 46 +++++++++++++++- tests/ui/ReportActionsListTest.tsx | 55 +++++++++++++++++-- tests/ui/ReportActionsTest.tsx | 2 + tests/unit/ReportActionsSkeletonCoverTest.tsx | 41 ++++++++++++++ 9 files changed, 191 insertions(+), 22 deletions(-) create mode 100644 src/components/ReportActionsSkeletonCover.tsx create mode 100644 tests/unit/ReportActionsSkeletonCoverTest.tsx diff --git a/src/components/MoneyRequestReportView/MoneyRequestReportView.tsx b/src/components/MoneyRequestReportView/MoneyRequestReportView.tsx index d411a44189a6..088194236dc0 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestReportView.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestReportView.tsx @@ -3,7 +3,7 @@ import MoneyReportHeader from '@components/MoneyReportHeader'; import MoneyRequestHeader from '@components/MoneyRequestHeader'; import OfflineWithFeedback from '@components/OfflineWithFeedback'; import MoneyRequestReceiptView from '@components/ReportActionItem/MoneyRequestReceiptView'; -import ReportActionsSkeletonView from '@components/ReportActionsSkeletonView'; +import ReportActionsSkeletonCover from '@components/ReportActionsSkeletonCover'; import ReportHeaderSkeletonView from '@components/ReportHeaderSkeletonView'; import {useIsAppLoadPending, useIsReportLoadPending} from '@hooks/useInFlightRequests'; @@ -105,7 +105,7 @@ function InitialLoadingSkeleton({styles, onLayout}: {styles: ThemeStyles; onLayo {}} /> - + ); } @@ -217,7 +217,7 @@ function MoneyRequestReportView({report, reportIDFromRoute, reportLoadingState, } if (shouldShowEmptyActionsSkeleton) { - return ; + return ; } if (!report) { @@ -228,7 +228,7 @@ function MoneyRequestReportView({report, reportIDFromRoute, reportLoadingState, return ( - + {shouldDisplayReportFooter ? : null} ); diff --git a/src/components/ReportActionsSkeletonCover.tsx b/src/components/ReportActionsSkeletonCover.tsx new file mode 100644 index 000000000000..9390dcc5721b --- /dev/null +++ b/src/components/ReportActionsSkeletonCover.tsx @@ -0,0 +1,38 @@ +import useThemeStyles from '@hooks/useThemeStyles'; + +import React from 'react'; +import {View} from 'react-native'; + +import ReportActionsSkeletonView from './ReportActionsSkeletonView'; + +type ReportActionsSkeletonCoverProps = { + /** Whether the skeleton rows animate */ + shouldAnimate?: boolean; + + /** Whether the cover should be positioned over already-mounted content */ + shouldOverlay?: boolean; +}; + +/** Fills the report-actions viewport with a consistently positioned loading skeleton. */ +function ReportActionsSkeletonCover({shouldAnimate = true, shouldOverlay = false}: ReportActionsSkeletonCoverProps) { + const styles = useThemeStyles(); + + return ( + + + + ); +} + +export default ReportActionsSkeletonCover; diff --git a/src/hooks/useReportActionsListModel.ts b/src/hooks/useReportActionsListModel.ts index 67e39bdd688d..0a2a9989d38b 100644 --- a/src/hooks/useReportActionsListModel.ts +++ b/src/hooks/useReportActionsListModel.ts @@ -56,6 +56,7 @@ function useReportActionsListModel(reportID: string, isReportLoadPending: boolea }); const hasOnceLoadedReportActions = reportLoadingState?.hasOnceLoadedReportActions; const isLoadingInitialReportActions = reportLoadingState?.isLoadingInitialReportActions; + const isInitialReportLoadPending = !hasOnceLoadedReportActions && (isReportLoadPending || isLoadingInitialReportActions !== false); const isLoadingOlderReportActions = reportLoadingState?.isLoadingOlderReportActions; const hasLoadingOlderReportActionsError = reportLoadingState?.hasLoadingOlderReportActionsError; @@ -146,6 +147,7 @@ function useReportActionsListModel(reportID: string, isReportLoadPending: boolea const state = { report, hasOnceLoadedReportActions, + isInitialReportLoadPending, hasOlderActions, hasNewerActions, isLoadingOlderReportActions, diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index 853923a4ee76..c5b652899539 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -1,5 +1,6 @@ import {renderScrollComponent as renderActionSheetAwareScrollView} from '@components/ActionSheetAwareScrollView'; import MerchantRuleSuggestionBanner from '@components/MerchantRuleSuggestionBanner'; +import ReportActionsSkeletonCover from '@components/ReportActionsSkeletonCover'; import ReportActionsSkeletonView from '@components/ReportActionsSkeletonView'; import useConciergeSessionStartTime from '@hooks/useConciergeSessionStartTime'; @@ -65,7 +66,6 @@ import {LegendList} from '@legendapp/list/react-native'; import {useRoute} from '@react-navigation/native'; import {isTrackIntentUserSelector} from '@selectors/Onboarding'; import React, {useEffect, useImperativeHandle, useRef, useState} from 'react'; -import {View} from 'react-native'; import FloatingMessageCounter from './FloatingMessageCounter'; import ReportActionIndexContext from './ReportActionIndexContext'; @@ -163,6 +163,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct const { report, hasOnceLoadedReportActions, + isInitialReportLoadPending, hasOlderActions, hasNewerActions, isLoadingOlderReportActions, @@ -401,7 +402,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct }); const [loadedInitialViewportListID, setLoadedInitialViewportListID] = useState(); - const shouldShowInitialViewportSkeleton = !isOffline && (!hasOnceLoadedReportActions || loadedInitialViewportListID !== listID); + const shouldShowInitialViewportSkeleton = !isOffline && (isInitialReportLoadPending || loadedInitialViewportListID !== listID); const handleListLoad = () => { onLoad(); @@ -574,7 +575,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct // It narrows `report` to non-undefined for the render below and stays a safe fallback if the report // is cleared mid-session while the latch keeps the content mounted. if (!report) { - return ; + return ; } return ( @@ -610,6 +611,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct keyExtractor={keyExtractor} drawDistance={REPORT_ACTIONS_DRAW_DISTANCE} recycleItems + experimental_hideItemsUntilMeasured renderScrollComponent={renderActionSheetAwareScrollView} contentContainerStyle={styles.chatContentScrollView} onEndReached={loadNewerChatsAfterTransitions} @@ -637,14 +639,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct onLoad={handleListLoad} onContentSizeChange={() => trackVerticalScrolling(undefined)} /> - {shouldShowInitialViewportSkeleton && ( - - - - )} + {shouldShowInitialViewportSkeleton && } ); diff --git a/src/pages/inbox/report/ReportActionsLoadingSkeleton.tsx b/src/pages/inbox/report/ReportActionsLoadingSkeleton.tsx index 2fc926b894d0..d0e137110e40 100644 --- a/src/pages/inbox/report/ReportActionsLoadingSkeleton.tsx +++ b/src/pages/inbox/report/ReportActionsLoadingSkeleton.tsx @@ -1,4 +1,4 @@ -import ReportActionsSkeletonView from '@components/ReportActionsSkeletonView'; +import ReportActionsSkeletonCover from '@components/ReportActionsSkeletonCover'; import useCancelSendMessageSpanOnSkeleton from '@hooks/useCancelSendMessageSpanOnSkeleton'; import type {SkeletonName} from '@hooks/useCancelSendMessageSpanOnSkeleton'; @@ -28,7 +28,7 @@ type ReportActionsLoadingSkeletonProps = { function ReportActionsLoadingSkeleton({reportID, skeletonName, shouldAnimate = true, shouldMarkOpenReportEnd = true}: ReportActionsLoadingSkeletonProps) { useCancelSendMessageSpanOnSkeleton(reportID, skeletonName); useMarkOpenReportEndOnSkeleton(reportID, shouldMarkOpenReportEnd); - return ; + return ; } ReportActionsLoadingSkeleton.displayName = 'ReportActionsLoadingSkeleton'; diff --git a/tests/ui/MoneyRequestReportViewTest.tsx b/tests/ui/MoneyRequestReportViewTest.tsx index f0dc787734b5..00723a42cc92 100644 --- a/tests/ui/MoneyRequestReportViewTest.tsx +++ b/tests/ui/MoneyRequestReportViewTest.tsx @@ -1,9 +1,11 @@ /* eslint-disable @typescript-eslint/no-unsafe-type-assertion */ -import {render} from '@testing-library/react-native'; +import {render, screen} from '@testing-library/react-native'; import MoneyRequestReportActionsList from '@components/MoneyRequestReportView/MoneyRequestReportActionsList'; import MoneyRequestReportView from '@components/MoneyRequestReportView/MoneyRequestReportView'; +import ReportActionsSkeletonView from '@components/ReportActionsSkeletonView'; +import {useIsAppLoadPending, useIsReportLoadPending} from '@hooks/useInFlightRequests'; import useNetwork from '@hooks/useNetwork'; import useOnyx from '@hooks/useOnyx'; import usePaginatedReportActions from '@hooks/usePaginatedReportActions'; @@ -34,6 +36,10 @@ jest.mock('@hooks/useOnyx', () => jest.fn()); jest.mock('@hooks/useResponsiveLayout', () => jest.fn()); jest.mock('@hooks/usePaginatedReportActions', () => jest.fn()); jest.mock('@hooks/useReportTransactionsCollection', () => jest.fn()); +jest.mock('@hooks/useInFlightRequests', () => ({ + useIsAppLoadPending: jest.fn(), + useIsReportLoadPending: jest.fn(), +})); // useThemeStyles throws without a ; return a proxy that yields an empty style object // for any key so the (mostly-mocked) tree renders without wiring up the full provider stack. @@ -54,6 +60,8 @@ jest.mock('@components/MoneyReportHeader', () => jest.fn(() => null)); jest.mock('@components/MoneyRequestHeader', () => jest.fn(() => null)); jest.mock('@components/CollapsibleHeaderOnKeyboard', () => jest.fn(() => null)); jest.mock('@components/ReportActionItem/MoneyRequestReceiptView', () => jest.fn(() => null)); +jest.mock('@components/ReportActionsSkeletonView', () => jest.fn(() => null)); +jest.mock('@components/ReportHeaderSkeletonView', () => jest.fn(() => null)); jest.mock('@pages/inbox/report/ReportFooter', () => jest.fn(() => null)); jest.mock('@components/OfflineWithFeedback', () => { const reactModule = jest.requireActual('react'); @@ -61,12 +69,15 @@ jest.mock('@components/OfflineWithFeedback', () => { }); const mockUseNetwork = useNetwork as jest.MockedFunction; +const mockUseIsAppLoadPending = jest.mocked(useIsAppLoadPending); +const mockUseIsReportLoadPending = jest.mocked(useIsReportLoadPending); const mockUseOnyx = useOnyx as jest.MockedFunction; const mockUseResponsiveLayout = useResponsiveLayout as jest.MockedFunction; const mockUsePaginatedReportActions = usePaginatedReportActions as jest.MockedFunction; const mockUseReportTransactionsCollection = useReportTransactionsCollection as jest.MockedFunction; const mockMoneyRequestReportActionsList = MoneyRequestReportActionsList as jest.MockedFunction; const mockReportActionsListBody = ReportActionsList as jest.MockedFunction; +const mockReportActionsSkeletonView = jest.mocked(ReportActionsSkeletonView); const mockUserTypingEventListener = UserTypingEventListener as jest.MockedFunction; const defaultPaginatedReportActionsResult: ReturnType = { @@ -134,6 +145,8 @@ describe('MoneyRequestReportView', () => { jest.clearAllMocks(); mockUseNetwork.mockReturnValue({isOffline: false}); + mockUseIsAppLoadPending.mockReturnValue(false); + mockUseIsReportLoadPending.mockReturnValue(false); mockUsePaginatedReportActions.mockReturnValue(defaultPaginatedReportActionsResult); mockUseReportTransactionsCollection.mockReturnValue({}); mockUseResponsiveLayout.mockReturnValue({ @@ -176,6 +189,37 @@ describe('MoneyRequestReportView', () => { expect(MoneyRequestReportUtils.shouldWaitForTransactions).toHaveBeenLastCalledWith(mockReport, [], mockReportLoadingState, false, false); }); + it('uses the bottom-padded cover while the report is waiting for transactions', () => { + jest.spyOn(MoneyRequestReportUtils, 'shouldWaitForTransactions').mockReturnValue(true); + + renderMoneyRequestReportView(jest.fn()); + + expect(screen.getByTestId('ReportActionsSkeletonCover')).toBeTruthy(); + expect(mockReportActionsListBody).not.toHaveBeenCalled(); + expect(mockMoneyRequestReportActionsList).not.toHaveBeenCalled(); + }); + + it('uses a static bottom-padded cover while report actions are empty', () => { + jest.spyOn(ReportActionsUtils, 'getFilteredReportActionsForReportView').mockReturnValue([]); + + renderMoneyRequestReportView(jest.fn()); + + expect(screen.getByTestId('ReportActionsSkeletonCover')).toBeTruthy(); + expect(mockReportActionsSkeletonView.mock.calls.at(-1)?.at(0)).toEqual(expect.objectContaining({shouldAnimate: false})); + expect(mockReportActionsListBody).not.toHaveBeenCalled(); + expect(mockMoneyRequestReportActionsList).not.toHaveBeenCalled(); + }); + + it('uses the bottom-padded cover while the app is loading', () => { + mockUseIsAppLoadPending.mockReturnValue(true); + + renderMoneyRequestReportView(jest.fn()); + + expect(screen.getByTestId('ReportActionsSkeletonCover')).toBeTruthy(); + expect(mockReportActionsListBody).not.toHaveBeenCalled(); + expect(mockMoneyRequestReportActionsList).not.toHaveBeenCalled(); + }); + it('mounts the chat list body and the typing listener (not the table view) for a transaction-thread report', () => { const onLayout = jest.fn(); diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 35957761cbb8..4b54308c3c83 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -85,9 +85,9 @@ const mockUseConciergeDraftActions = useConciergeDraftActions as jest.MockedFunc const mockUseConciergeSessionState = useConciergeSessionState as jest.MockedFunction; const mockUseConciergeSessionActions = useConciergeSessionActions as jest.MockedFunction; -function getMockReportLoadingState(selector: unknown, hasOnceLoadedReportActions = true) { +function getMockReportLoadingState(selector: unknown, hasOnceLoadedReportActions = true, isLoadingInitialReportActions = false) { return selector === reportActionsListLoadingStateSelector - ? {hasOnceLoadedReportActions, isLoadingInitialReportActions: false, isLoadingOlderReportActions: false, hasLoadingOlderReportActionsError: false} + ? {hasOnceLoadedReportActions, isLoadingInitialReportActions, isLoadingOlderReportActions: false, hasLoadingOlderReportActionsError: false} : undefined; } @@ -181,6 +181,8 @@ type MockLegendListProps = { alignItemsAtEnd?: boolean; data?: OnyxTypes.ReportAction[]; drawDistance?: number; + // eslint-disable-next-line @typescript-eslint/naming-convention + experimental_hideItemsUntilMeasured?: boolean; extraData?: unknown; getItemType?: (item: OnyxTypes.ReportAction) => string; initialScrollAtEnd?: boolean; @@ -231,6 +233,7 @@ const mockUseMarkAsRead: jest.Mock = jest.requireMock('@hooks/useMarkAsRead'); const mockUseReportActionsScroll: jest.Mock = jest.requireMock('@hooks/useReportActionsScroll'); const mockMarkOpenReportEnd: jest.Mock = jest.requireMock('@libs/telemetry/markOpenReportEnd'); let mockHasOnceLoadedReportActions = true; +let mockIsLoadingInitialReportActions = false; jest.mock('@libs/actions/Report', () => ({ updateLoadingInitialReportAction: jest.fn(), @@ -306,6 +309,7 @@ describe('ReportActionsList (body)', () => { beforeEach(() => { jest.clearAllMocks(); mockHasOnceLoadedReportActions = true; + mockIsLoadingInitialReportActions = false; mockShouldCallLegendListOnLoad = true; mockUseIsReportLoadPending.mockReturnValue(false); @@ -369,7 +373,7 @@ describe('ReportActionsList (body)', () => { return [false, {status: 'loaded'}]; } if (key.includes('reportLoadingState')) { - return [getMockReportLoadingState(options?.selector, mockHasOnceLoadedReportActions), {status: 'loaded'}]; + return [getMockReportLoadingState(options?.selector, mockHasOnceLoadedReportActions, mockIsLoadingInitialReportActions), {status: 'loaded'}]; } if (key.includes('reportActions')) { return [[], {status: 'loaded'}]; @@ -450,6 +454,7 @@ describe('ReportActionsList (body)', () => { it('keeps the initial viewport covered until the hydrated LegendList finishes rendering it', async () => { mockUseNetwork.mockReturnValue({isOffline: false}); mockHasOnceLoadedReportActions = false; + mockIsLoadingInitialReportActions = true; mockShouldCallLegendListOnLoad = false; const view = renderReportActionsList(); @@ -478,6 +483,47 @@ describe('ReportActionsList (body)', () => { expect(screen.queryByTestId('ReportActionsSkeletonView')).toBeNull(); }); + it('releases the initial viewport cover after a terminal OpenReport failure', () => { + mockUseNetwork.mockReturnValue({isOffline: false}); + mockHasOnceLoadedReportActions = false; + mockIsLoadingInitialReportActions = false; + mockShouldCallLegendListOnLoad = false; + renderReportActionsList(); + + expect(screen.getByTestId('ReportActionsSkeletonCover')).toBeTruthy(); + + act(() => { + getCapturedListProps()?.onLoad?.(); + }); + + expect(screen.queryByTestId('ReportActionsSkeletonCover')).toBeNull(); + }); + + it('keeps the initial viewport covered while OpenReport is queued despite stale stored loading state', () => { + mockUseNetwork.mockReturnValue({isOffline: false}); + mockHasOnceLoadedReportActions = false; + mockIsLoadingInitialReportActions = false; + mockUseIsReportLoadPending.mockReturnValue(true); + mockShouldCallLegendListOnLoad = false; + const view = renderReportActionsList(); + + act(() => { + getCapturedListProps()?.onLoad?.(); + }); + expect(screen.getByTestId('ReportActionsSkeletonCover')).toBeTruthy(); + + mockUseIsReportLoadPending.mockReturnValue(false); + view.rerender( + , + ); + + expect(screen.queryByTestId('ReportActionsSkeletonCover')).toBeNull(); + }); + it('keeps the initial actions visible until the hydrated page is complete', async () => { mockUseNetwork.mockReturnValue({isOffline: false}); mockHasOnceLoadedReportActions = false; @@ -533,6 +579,7 @@ describe('ReportActionsList (body)', () => { expect(listProps?.drawDistance).toBe(1500); expect(listProps?.recycleItems).toBe(true); + expect(listProps?.experimental_hideItemsUntilMeasured).toBe(true); }); it('groups comments by layout characteristics for measurement estimates', () => { @@ -1101,7 +1148,7 @@ describe('ReportActionsList (body)', () => { return [false, {status: 'loaded'}]; } if (key.includes('reportLoadingState')) { - return [getMockReportLoadingState(options?.selector, hasOnceLoadedReportActions), {status: 'loaded'}]; + return [getMockReportLoadingState(options?.selector, hasOnceLoadedReportActions, !hasOnceLoadedReportActions), {status: 'loaded'}]; } if (key.includes('reportActions')) { return [[], {status: 'loaded'}]; diff --git a/tests/ui/ReportActionsTest.tsx b/tests/ui/ReportActionsTest.tsx index 18f7f63e2496..6099444f6a52 100644 --- a/tests/ui/ReportActionsTest.tsx +++ b/tests/ui/ReportActionsTest.tsx @@ -149,6 +149,7 @@ describe('ReportActions (orchestrator)', () => { render(); + expect(screen.getByTestId('ReportActionsSkeletonCover')).toBeTruthy(); expect(screen.getByTestId('ReportActionsSkeletonView')).toBeTruthy(); expect(mockReportActionsListBody).not.toHaveBeenCalled(); expect(mockMoneyRequestList).not.toHaveBeenCalled(); @@ -159,6 +160,7 @@ describe('ReportActions (orchestrator)', () => { render(); + expect(screen.getByTestId('ReportActionsSkeletonCover')).toBeTruthy(); expect(screen.getByTestId('ReportActionsSkeletonView')).toBeTruthy(); expect(mockReportActionsListBody).not.toHaveBeenCalled(); expect(mockMoneyRequestList).not.toHaveBeenCalled(); diff --git a/tests/unit/ReportActionsSkeletonCoverTest.tsx b/tests/unit/ReportActionsSkeletonCoverTest.tsx new file mode 100644 index 000000000000..6392f50174b2 --- /dev/null +++ b/tests/unit/ReportActionsSkeletonCoverTest.tsx @@ -0,0 +1,41 @@ +import {render, screen} from '@testing-library/react-native'; + +import ReportActionsSkeletonCover from '@components/ReportActionsSkeletonCover'; +import ReportActionsSkeletonView from '@components/ReportActionsSkeletonView'; + +import React from 'react'; + +jest.mock('@components/ReportActionsSkeletonView', () => jest.fn(() => null)); + +const mockReportActionsSkeletonView = jest.mocked(ReportActionsSkeletonView); + +describe('ReportActionsSkeletonCover', () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('fills, clips, and bottom-aligns the report-actions skeleton', () => { + render(); + + expect(screen.getByTestId('ReportActionsSkeletonCover')).toHaveStyle({ + flex: 1, + overflow: 'hidden', + justifyContent: 'flex-end', + paddingBottom: 16, + }); + expect(mockReportActionsSkeletonView.mock.calls.at(-1)?.at(0)).toEqual(expect.objectContaining({shouldAnimate: false})); + }); + + it('fills the parent when covering already-mounted content', () => { + render(); + + expect(screen.getByTestId('ReportActionsSkeletonCover')).toHaveStyle({ + position: 'absolute', + top: 0, + right: 0, + bottom: 0, + left: 0, + zIndex: 10, + }); + }); +}); From 3a782f9f3f4d2c7279578837dc903e59aa8944dc Mon Sep 17 00:00:00 2001 From: chrispader Date: Thu, 10 Sep 2026 19:13:51 +0200 Subject: [PATCH 04/61] test: model content growth in pagination test --- tests/ui/PaginationTest.tsx | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/ui/PaginationTest.tsx b/tests/ui/PaginationTest.tsx index f393af39fb38..b569e00b5215 100644 --- a/tests/ui/PaginationTest.tsx +++ b/tests/ui/PaginationTest.tsx @@ -44,6 +44,11 @@ const LIST_CONTENT_SIZE = { height: 600, }; const LIST_END_OFFSET = LIST_CONTENT_SIZE.height - LIST_SIZE.height; +const PAGINATED_LIST_CONTENT_SIZE = { + ...LIST_CONTENT_SIZE, + height: LIST_CONTENT_SIZE.height * 2, +}; +const PAGINATED_LIST_END_OFFSET = PAGINATED_LIST_CONTENT_SIZE.height - LIST_SIZE.height; const TEN_MINUTES_AGO = subMinutes(new Date(), 10); const REPORT_ID = '1'; @@ -59,14 +64,14 @@ function getReportScreen(reportID = REPORT_ID) { return screen.getByTestId(`report-screen-${reportID}`); } -function scrollToOffset(offset: number) { +function scrollToOffset(offset: number, contentSize = LIST_CONTENT_SIZE) { const hintText = TestHelper.translateLocal('sidebarScreen.listOfChatMessages'); fireEvent.scroll(within(getReportScreen()).getByLabelText(hintText), { nativeEvent: { contentOffset: { y: offset, }, - contentSize: LIST_CONTENT_SIZE, + contentSize, layoutMeasurement: LIST_SIZE, }, }); @@ -394,9 +399,9 @@ describe('Pagination', () => { TestHelper.expectAPICommandToHaveBeenCalledWith('GetNewerActions', 0, {reportID: REPORT_ID, reportActionID: '5'}); // Simulate the maintainVisibleContentPosition scroll adjustment, so it is now possible to scroll down more. - scrollToOffset(0); + scrollToOffset(0, PAGINATED_LIST_CONTENT_SIZE); await waitForBatchedUpdatesWithAct(); - scrollToOffset(LIST_END_OFFSET); + scrollToOffset(PAGINATED_LIST_END_OFFSET, PAGINATED_LIST_CONTENT_SIZE); await waitForBatchedUpdatesWithAct(); // We now have 10 messages. 5 from the initial OpenReport and 5 from the GetNewerActions call. @@ -406,9 +411,9 @@ describe('Pagination', () => { TestHelper.expectAPICommandToHaveBeenCalled('GetOlderActions', 0); TestHelper.expectAPICommandToHaveBeenCalled('GetNewerActions', 2); - scrollToOffset(0); + scrollToOffset(0, PAGINATED_LIST_CONTENT_SIZE); await waitForBatchedUpdatesWithAct(); - scrollToOffset(LIST_END_OFFSET); + scrollToOffset(PAGINATED_LIST_END_OFFSET, PAGINATED_LIST_CONTENT_SIZE); await waitForBatchedUpdatesWithAct(); // When there are no newer actions, we don't want to trigger GetNewerActions again. From fa5194f7dd0afb68b1fd8a043078ecc07b756697 Mon Sep 17 00:00:00 2001 From: chrispader Date: Thu, 10 Sep 2026 21:25:06 +0200 Subject: [PATCH 05/61] fix: stabilize report actions loading behavior --- .../MoneyRequestReportView.tsx | 7 ++++- src/components/ReportActionsSkeletonCover.tsx | 24 +++++++--------- src/hooks/useReportActionsListModel.ts | 2 -- src/pages/inbox/report/ReportActionsList.tsx | 17 ++--------- .../report/ReportActionsLoadingSkeleton.tsx | 7 ++++- tests/ui/ReportActionsListTest.tsx | 28 +++++++++++++++++-- tests/unit/ReportActionsSkeletonCoverTest.tsx | 10 +++++-- 7 files changed, 56 insertions(+), 39 deletions(-) diff --git a/src/components/MoneyRequestReportView/MoneyRequestReportView.tsx b/src/components/MoneyRequestReportView/MoneyRequestReportView.tsx index 088194236dc0..066762507a64 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestReportView.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestReportView.tsx @@ -4,6 +4,7 @@ import MoneyRequestHeader from '@components/MoneyRequestHeader'; import OfflineWithFeedback from '@components/OfflineWithFeedback'; import MoneyRequestReceiptView from '@components/ReportActionItem/MoneyRequestReceiptView'; import ReportActionsSkeletonCover from '@components/ReportActionsSkeletonCover'; +import ReportActionsSkeletonView from '@components/ReportActionsSkeletonView'; import ReportHeaderSkeletonView from '@components/ReportHeaderSkeletonView'; import {useIsAppLoadPending, useIsReportLoadPending} from '@hooks/useInFlightRequests'; @@ -217,7 +218,11 @@ function MoneyRequestReportView({report, reportIDFromRoute, reportLoadingState, } if (shouldShowEmptyActionsSkeleton) { - return ; + return ( + + + + ); } if (!report) { diff --git a/src/components/ReportActionsSkeletonCover.tsx b/src/components/ReportActionsSkeletonCover.tsx index 9390dcc5721b..9e3a4de0986e 100644 --- a/src/components/ReportActionsSkeletonCover.tsx +++ b/src/components/ReportActionsSkeletonCover.tsx @@ -1,36 +1,32 @@ import useThemeStyles from '@hooks/useThemeStyles'; +import type {ReactNode} from 'react'; +import type {StyleProp, ViewStyle} from 'react-native'; + import React from 'react'; import {View} from 'react-native'; import ReportActionsSkeletonView from './ReportActionsSkeletonView'; type ReportActionsSkeletonCoverProps = { - /** Whether the skeleton rows animate */ - shouldAnimate?: boolean; + /** The skeleton content to place at the bottom of the report viewport */ + children?: ReactNode; - /** Whether the cover should be positioned over already-mounted content */ - shouldOverlay?: boolean; + /** Additional styles for the cover */ + style?: StyleProp; }; /** Fills the report-actions viewport with a consistently positioned loading skeleton. */ -function ReportActionsSkeletonCover({shouldAnimate = true, shouldOverlay = false}: ReportActionsSkeletonCoverProps) { +function ReportActionsSkeletonCover({children, style}: ReportActionsSkeletonCoverProps) { const styles = useThemeStyles(); return ( - + {children ?? } ); } diff --git a/src/hooks/useReportActionsListModel.ts b/src/hooks/useReportActionsListModel.ts index 0a2a9989d38b..988354e83518 100644 --- a/src/hooks/useReportActionsListModel.ts +++ b/src/hooks/useReportActionsListModel.ts @@ -150,8 +150,6 @@ function useReportActionsListModel(reportID: string, isReportLoadPending: boolea isInitialReportLoadPending, hasOlderActions, hasNewerActions, - isLoadingOlderReportActions, - hasLoadingOlderReportActionsError, oldestReportActionID: currentReportOldestActionID, sortedAllReportActions, oldestUnreadReportAction, diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index c5b652899539..fb5225af3373 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -166,8 +166,6 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct isInitialReportLoadPending, hasOlderActions, hasNewerActions, - isLoadingOlderReportActions, - hasLoadingOlderReportActionsError, oldestReportActionID, sortedAllReportActions, oldestUnreadReportAction, @@ -193,18 +191,6 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct const lastRequestedOldestActionIDRef = useRef(undefined); const emitComposerScrollEvents = useEmitComposerScrollEvents({enabled: true}); - useEffect(() => { - didLayout.current = false; - lastRequestedOldestActionIDRef.current = undefined; - }, [reportID]); - - useEffect(() => { - if (isLoadingOlderReportActions && !hasLoadingOlderReportActionsError) { - return; - } - lastRequestedOldestActionIDRef.current = undefined; - }, [isLoadingOlderReportActions, hasLoadingOlderReportActionsError]); - useLinkedMessageOfflineLoading({reportID: report?.reportID ?? reportID, reportActionIDFromRoute}); // Owned here rather than by the callout, which unmounts as the layout and composer change size. @@ -639,7 +625,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct onLoad={handleListLoad} onContentSizeChange={() => trackVerticalScrolling(undefined)} /> - {shouldShowInitialViewportSkeleton && } + {shouldShowInitialViewportSkeleton && } ); @@ -653,6 +639,7 @@ function ReportActionsList({reportID, conciergeChat, onLayout}: ReportActionsLis return ( ; + return ( + + + + ); } ReportActionsLoadingSkeleton.displayName = 'ReportActionsLoadingSkeleton'; diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 4b54308c3c83..9aeb898162af 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -85,9 +85,9 @@ const mockUseConciergeDraftActions = useConciergeDraftActions as jest.MockedFunc const mockUseConciergeSessionState = useConciergeSessionState as jest.MockedFunction; const mockUseConciergeSessionActions = useConciergeSessionActions as jest.MockedFunction; -function getMockReportLoadingState(selector: unknown, hasOnceLoadedReportActions = true, isLoadingInitialReportActions = false) { +function getMockReportLoadingState(selector: unknown, hasOnceLoadedReportActions = true, isLoadingInitialReportActions = false, isLoadingOlderReportActions = false) { return selector === reportActionsListLoadingStateSelector - ? {hasOnceLoadedReportActions, isLoadingInitialReportActions, isLoadingOlderReportActions: false, hasLoadingOlderReportActionsError: false} + ? {hasOnceLoadedReportActions, isLoadingInitialReportActions, isLoadingOlderReportActions, hasLoadingOlderReportActionsError: false} : undefined; } @@ -234,6 +234,7 @@ const mockUseReportActionsScroll: jest.Mock = jest.requireMock('@hooks/useReport const mockMarkOpenReportEnd: jest.Mock = jest.requireMock('@libs/telemetry/markOpenReportEnd'); let mockHasOnceLoadedReportActions = true; let mockIsLoadingInitialReportActions = false; +let mockIsLoadingOlderReportActions = false; jest.mock('@libs/actions/Report', () => ({ updateLoadingInitialReportAction: jest.fn(), @@ -310,6 +311,7 @@ describe('ReportActionsList (body)', () => { jest.clearAllMocks(); mockHasOnceLoadedReportActions = true; mockIsLoadingInitialReportActions = false; + mockIsLoadingOlderReportActions = false; mockShouldCallLegendListOnLoad = true; mockUseIsReportLoadPending.mockReturnValue(false); @@ -373,7 +375,7 @@ describe('ReportActionsList (body)', () => { return [false, {status: 'loaded'}]; } if (key.includes('reportLoadingState')) { - return [getMockReportLoadingState(options?.selector, mockHasOnceLoadedReportActions, mockIsLoadingInitialReportActions), {status: 'loaded'}]; + return [getMockReportLoadingState(options?.selector, mockHasOnceLoadedReportActions, mockIsLoadingInitialReportActions, mockIsLoadingOlderReportActions), {status: 'loaded'}]; } if (key.includes('reportActions')) { return [[], {status: 'loaded'}]; @@ -659,6 +661,26 @@ describe('ReportActionsList (body)', () => { }); expect(mockLoadOlderChats).toHaveBeenCalledTimes(1); + mockIsLoadingOlderReportActions = true; + view.rerender( + , + ); + mockIsLoadingOlderReportActions = false; + view.rerender( + , + ); + + act(() => { + getCapturedListProps()?.onScroll?.(createScrollEvent(0)); + }); + expect(mockLoadOlderChats).toHaveBeenCalledTimes(1); + mockUsePaginatedReportActions.mockReturnValue({ ...defaultPaginatedReportActionsResult, reportActions: [...mockReportActions, olderMockReportAction], diff --git a/tests/unit/ReportActionsSkeletonCoverTest.tsx b/tests/unit/ReportActionsSkeletonCoverTest.tsx index 6392f50174b2..b33a729a9ea8 100644 --- a/tests/unit/ReportActionsSkeletonCoverTest.tsx +++ b/tests/unit/ReportActionsSkeletonCoverTest.tsx @@ -15,7 +15,11 @@ describe('ReportActionsSkeletonCover', () => { }); it('fills, clips, and bottom-aligns the report-actions skeleton', () => { - render(); + render( + + + , + ); expect(screen.getByTestId('ReportActionsSkeletonCover')).toHaveStyle({ flex: 1, @@ -26,8 +30,8 @@ describe('ReportActionsSkeletonCover', () => { expect(mockReportActionsSkeletonView.mock.calls.at(-1)?.at(0)).toEqual(expect.objectContaining({shouldAnimate: false})); }); - it('fills the parent when covering already-mounted content', () => { - render(); + it('accepts additional presentation styles', () => { + render(); expect(screen.getByTestId('ReportActionsSkeletonCover')).toHaveStyle({ position: 'absolute', From 52df1e9fb652bb340607c463a9e59d20c478152b Mon Sep 17 00:00:00 2001 From: chrispader Date: Fri, 11 Sep 2026 12:55:02 +0200 Subject: [PATCH 06/61] refactor: remove compiled report memoization --- .../FlatList/FlatList/index.ios.tsx | 35 +++----- .../MoneyRequestReportView.tsx | 79 ++++++++----------- .../useReportActionsNewActionLiveTail.ts | 6 +- 3 files changed, 48 insertions(+), 72 deletions(-) diff --git a/src/components/FlatList/FlatList/index.ios.tsx b/src/components/FlatList/FlatList/index.ios.tsx index 7892a044825c..05bfe439c045 100644 --- a/src/components/FlatList/FlatList/index.ios.tsx +++ b/src/components/FlatList/FlatList/index.ios.tsx @@ -7,7 +7,7 @@ import useThemeStyles from '@hooks/useThemeStyles'; import type {NativeScrollEvent, NativeSyntheticEvent} from 'react-native'; -import React, {useCallback, useRef, useState} from 'react'; +import {useRef, useState} from 'react'; import {FlatList} from 'react-native'; import type {CustomFlatListProps} from './types'; @@ -26,30 +26,21 @@ function CustomFlatList({ }: CustomFlatListProps) { const [isScrolling, setIsScrolling] = useState(false); const styles = useThemeStyles(); - const handleScrollBegin = useCallback( - (event: NativeSyntheticEvent) => { - onMomentumScrollBegin?.(event); - setIsScrolling(true); - }, - [onMomentumScrollBegin], - ); + const handleScrollBegin = (event: NativeSyntheticEvent) => { + onMomentumScrollBegin?.(event); + setIsScrolling(true); + }; - const handleScrollEnd = useCallback( - (event: NativeSyntheticEvent) => { - onMomentumScrollEnd?.(event); - setIsScrolling(false); - }, - [onMomentumScrollEnd], - ); + const handleScrollEnd = (event: NativeSyntheticEvent) => { + onMomentumScrollEnd?.(event); + setIsScrolling(false); + }; const emitComposerScrollEvents = useEmitComposerScrollEvents({enabled: !enableAnimatedKeyboardDismissal && !!restProps.inverted}); - const handleScroll = useCallback( - (e: NativeSyntheticEvent) => { - onScrollProp?.(e); - emitComposerScrollEvents(); - }, - [emitComposerScrollEvents, onScrollProp], - ); + const handleScroll = (e: NativeSyntheticEvent) => { + onScrollProp?.(e); + emitComposerScrollEvents(); + }; const listRef = useRef | null>(null); useFlatListHandle({ diff --git a/src/components/MoneyRequestReportView/MoneyRequestReportView.tsx b/src/components/MoneyRequestReportView/MoneyRequestReportView.tsx index 066762507a64..232f71657bc4 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestReportView.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestReportView.tsx @@ -45,7 +45,7 @@ import type {LayoutChangeEvent} from 'react-native'; import type {OnyxEntry} from 'react-native-onyx'; import {PortalHost} from '@gorhom/portal'; -import React, {useCallback, useEffect, useMemo} from 'react'; +import {useEffect} from 'react'; // We use Animated for all functionality related to wide RHP to make it easier // to interact with react-navigation components (e.g., CardContainer, interpolator), which also use Animated. // eslint-disable-next-line no-restricted-imports @@ -127,34 +127,23 @@ function MoneyRequestReportView({report, reportIDFromRoute, reportLoadingState, const {reportActions: unfilteredReportActions} = usePaginatedReportActions(reportID); - const reportActions = useMemo(() => { - return getFilteredReportActionsForReportView(unfilteredReportActions); - }, [unfilteredReportActions]); + const reportActions = getFilteredReportActionsForReportView(unfilteredReportActions); const reportTransactions = useReportTransactionsCollection(reportID); - const transactions = useMemo(() => getAllNonDeletedTransactions(reportTransactions, reportActions, isOffline, true), [reportTransactions, reportActions, isOffline]); + const transactions = getAllNonDeletedTransactions(reportTransactions, reportActions, isOffline, true); - const visibleTransactions = useMemo(() => { - if (isOffline) { - return transactions; - } - - // When there are no pending delete transactions, which is most of the time, we can return the same transactions keeping the same reference avoiding extra work - const hasPendingDelete = transactions.some((transaction) => transaction.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE); - if (!hasPendingDelete) { - return transactions; - } - - return transactions.filter((transaction) => transaction.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE); - }, [transactions, isOffline]); + // When there are no pending delete transactions, which is most of the time, return the same transactions to keep the same reference and avoid extra work. + const hasPendingDelete = transactions.some((transaction) => transaction.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE); + const visibleTransactions = + isOffline || !hasPendingDelete ? transactions : transactions.filter((transaction) => transaction.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE); const reportErrors = visibleTransactions.length === 1 && visibleTransactions.at(0)?.errors ? undefined : allReportErrors; const reportTransactionIDs = visibleTransactions.map((transaction) => transaction.transactionID); const transactionThreadReportID = getOneTransactionThreadReportID(report, chatReport, reportActions ?? [], isOffline, reportTransactionIDs); const isReportLoadPending = useIsReportLoadPending(reportID); - const dismissReportCreationError = useCallback(() => { + const dismissReportCreationError = () => { goBackFromSearchMoneyRequest({afterTransition: () => removeFailedReport(reportID)}); - }, [reportID]); + }; // Special case handling a report that is a transaction thread // If true we will use the standard `ReportActionsList` to display report data and a special header, anything else is handled via `MoneyRequestReportActionsList` @@ -172,33 +161,29 @@ function MoneyRequestReportView({report, reportIDFromRoute, reportLoadingState, const [transactionThreadReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`); const shouldShowWideRHPReceipt = visibleTransactions.length === 1 && !isSmallScreenWidth && !!transactionThreadReport; - const reportHeaderView = useMemo( - () => - isTransactionThreadView ? ( - { - if (!backToRoute) { - goBackFromSearchMoneyRequest(); - return; - } - Navigation.goBack(backToRoute); - }} - /> - ) : ( - { - if (!backToRoute) { - goBackFromSearchMoneyRequest(); - return; - } - Navigation.goBack(backToRoute); - }} - /> - ), - [backToRoute, isTransactionThreadView, report?.reportID], + const reportHeaderView = isTransactionThreadView ? ( + { + if (!backToRoute) { + goBackFromSearchMoneyRequest(); + return; + } + Navigation.goBack(backToRoute); + }} + /> + ) : ( + { + if (!backToRoute) { + goBackFromSearchMoneyRequest(); + return; + } + Navigation.goBack(backToRoute); + }} + /> ); // We need to cancel telemetry span when user leaves the screen before full report data is loaded diff --git a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts index bb150ca6d99a..02b8d8665a76 100644 --- a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts +++ b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts @@ -19,7 +19,7 @@ import type * as OnyxTypes from '@src/types/onyx'; import type {OnyxEntry} from 'react-native-onyx'; import {useNavigation} from '@react-navigation/native'; -import {useCallback, useEffect, useEffectEvent, useRef, useState} from 'react'; +import {useEffect, useEffectEvent, useRef, useState} from 'react'; // In the component we are subscribing to the arrival of new actions. // As there is the possibility that there are multiple instances of a ReportScreen @@ -153,14 +153,14 @@ function useReportActionsNewActionLiveTail({ }); }); - const completeLiveTailPruneAfterScrollToBottom = useCallback(() => { + const completeLiveTailPruneAfterScrollToBottom = () => { if (liveTailJumpRef.current.stage !== 'await_prune') { return; } pruneReportActionPagesToNewestWindow(reportID, sortedAllReportActionsForPagination, reportActionPages); setTreatAsNoPaginationAnchor(false); liveTailJumpRef.current = {stage: 'idle'}; - }, [reportID, sortedAllReportActionsForPagination, reportActionPages, setTreatAsNoPaginationAnchor]); + }; useEffect(() => { liveTailJumpRef.current = {stage: 'idle'}; From c80ea5a0392b772e5f3713e203fd4b41ed0faa6c Mon Sep 17 00:00:00 2001 From: chrispader Date: Mon, 14 Sep 2026 11:29:20 +0200 Subject: [PATCH 07/61] fix: allow report action menus to overflow list rows --- .../LegendList/allowLegendListItemOverflow.ts | 22 ++++++++++++ src/pages/inbox/report/ReportActionsList.tsx | 15 ++++++-- tests/ui/ReportActionsListTest.tsx | 34 +++++++++++++++++++ tests/unit/LegendListItemOverflowTest.ts | 31 +++++++++++++++++ 4 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 src/components/LegendList/allowLegendListItemOverflow.ts create mode 100644 tests/unit/LegendListItemOverflowTest.ts diff --git a/src/components/LegendList/allowLegendListItemOverflow.ts b/src/components/LegendList/allowLegendListItemOverflow.ts new file mode 100644 index 000000000000..5907b06815e1 --- /dev/null +++ b/src/components/LegendList/allowLegendListItemOverflow.ts @@ -0,0 +1,22 @@ +import type {LegendListRef} from '@legendapp/list/react-native'; + +/** + * LegendList uses paint containment for web item containers, which clips descendants that render + * outside their row. Remove only paint containment so inline menus can overflow while layout and + * style containment keep the rest of the row isolated. + */ +function allowLegendListItemOverflow(list: LegendListRef | null, index: number): boolean { + const itemContainer: unknown = list?.getState().elementAtIndex(index); + if (!isRecord(itemContainer) || !isRecord(itemContainer.style) || typeof itemContainer.style.contain !== 'string') { + return false; + } + + itemContainer.style.contain = 'layout style'; + return true; +} + +function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null; +} + +export default allowLegendListItemOverflow; diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index fb5225af3373..2497bc2283a6 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -1,4 +1,5 @@ import {renderScrollComponent as renderActionSheetAwareScrollView} from '@components/ActionSheetAwareScrollView'; +import allowLegendListItemOverflow from '@components/LegendList/allowLegendListItemOverflow'; import MerchantRuleSuggestionBanner from '@components/MerchantRuleSuggestionBanner'; import ReportActionsSkeletonCover from '@components/ReportActionsSkeletonCover'; import ReportActionsSkeletonView from '@components/ReportActionsSkeletonView'; @@ -58,7 +59,7 @@ import type SCREENS from '@src/SCREENS'; import {getStableReportSelector} from '@src/selectors/Report'; import type * as OnyxTypes from '@src/types/onyx'; -import type {LegendListRef, LegendListRenderItemProps} from '@legendapp/list/react-native'; +import type {LegendListRef, LegendListRenderItemProps, OnViewableItemsChangedInfo} from '@legendapp/list/react-native'; import type {LayoutChangeEvent, NativeScrollEvent, NativeSyntheticEvent} from 'react-native'; import type {OnyxEntry} from 'react-native-onyx'; @@ -390,6 +391,16 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct const [loadedInitialViewportListID, setLoadedInitialViewportListID] = useState(); const shouldShowInitialViewportSkeleton = !isOffline && (isInitialReportLoadPending || loadedInitialViewportListID !== listID); + const handleViewableItemsChanged = (info: OnViewableItemsChangedInfo) => { + onViewableItemsChanged(info); + for (const item of info.changed) { + if (!item.isViewable) { + continue; + } + allowLegendListItemOverflow(legendListRef.current, item.index); + } + }; + const handleListLoad = () => { onLoad(); setLoadedInitialViewportListID(listID); @@ -610,7 +621,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct onScroll={trackScrollPositionAndThreshold} onStartReached={loadOlderChatsOnStartReached} onStartReachedThreshold={PAGINATION_THRESHOLD} - onViewableItemsChanged={onViewableItemsChanged} + onViewableItemsChanged={handleViewableItemsChanged} extraData={extraData} key={listID} getItemType={getItemType} diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 9aeb898162af..d01d3942bcaf 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -1,5 +1,7 @@ import {act, render, screen} from '@testing-library/react-native'; +import allowLegendListItemOverflow from '@components/LegendList/allowLegendListItemOverflow'; + import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; import {useIsReportLoadPending} from '@hooks/useInFlightRequests'; /* eslint-disable @typescript-eslint/no-unsafe-type-assertion */ @@ -26,6 +28,7 @@ import ONYXKEYS from '@src/ONYXKEYS'; import {reportActionsListLoadingStateSelector} from '@src/selectors/ReportMetaData'; import type * as OnyxTypes from '@src/types/onyx'; +import type {OnViewableItemsChangedInfo} from '@legendapp/list/react-native'; import type * as ReactNavigation from '@react-navigation/native'; import React from 'react'; @@ -61,6 +64,7 @@ jest.mock('@hooks/useParentReportAction', () => jest.fn()); jest.mock('@hooks/useIsInSidePanel', () => jest.fn()); jest.mock('@hooks/useSidePanelState', () => jest.fn()); jest.mock('@hooks/useReportTransactionsCollection', () => jest.fn()); +jest.mock('@components/LegendList/allowLegendListItemOverflow', () => jest.fn()); jest.mock('@pages/inbox/ConciergeSessionContext', () => ({ useConciergeSessionState: jest.fn(), useConciergeSessionActions: jest.fn(), @@ -84,6 +88,7 @@ const mockUseConciergeDraft = useConciergeDraft as jest.MockedFunction; const mockUseConciergeSessionState = useConciergeSessionState as jest.MockedFunction; const mockUseConciergeSessionActions = useConciergeSessionActions as jest.MockedFunction; +const mockAllowLegendListItemOverflow = allowLegendListItemOverflow as jest.MockedFunction; function getMockReportLoadingState(selector: unknown, hasOnceLoadedReportActions = true, isLoadingInitialReportActions = false, isLoadingOlderReportActions = false) { return selector === reportActionsListLoadingStateSelector @@ -191,6 +196,8 @@ type MockLegendListProps = { maintainVisibleContentPosition?: boolean; onLoad?: () => void; onStartReachedThreshold?: number; + onContentSizeChange?: (width: number, height: number) => void; + onViewableItemsChanged?: (info: OnViewableItemsChangedInfo) => void; recycleItems?: boolean; renderItem?: (info: {item: OnyxTypes.ReportAction; index: number}) => React.ReactElement | null; onStartReached?: () => void; @@ -584,6 +591,33 @@ describe('ReportActionsList (body)', () => { expect(listProps?.experimental_hideItemsUntilMeasured).toBe(true); }); + it('allows visible report action menus to overflow their LegendList item containers', () => { + mockUseNetwork.mockReturnValue({isOffline: false}); + renderReportActionsList(); + const hiddenAction = mockReportActions.at(0); + const visibleAction = mockReportActions.at(1); + if (!hiddenAction || !visibleAction) { + throw new Error('Expected report action fixtures'); + } + + act(() => { + getCapturedListProps()?.onViewableItemsChanged?.({ + changed: [ + {containerId: 1, index: 1, isViewable: true, item: visibleAction, key: '1'}, + {containerId: 0, index: 0, isViewable: false, item: hiddenAction, key: '0'}, + ], + end: 1, + endBuffered: 1, + start: 0, + startBuffered: 0, + viewableItems: [], + }); + }); + + expect(mockAllowLegendListItemOverflow).toHaveBeenCalledTimes(1); + expect(mockAllowLegendListItemOverflow).toHaveBeenCalledWith(null, 1); + }); + it('groups comments by layout characteristics for measurement estimates', () => { mockUseNetwork.mockReturnValue({isOffline: false}); renderReportActionsList(); diff --git a/tests/unit/LegendListItemOverflowTest.ts b/tests/unit/LegendListItemOverflowTest.ts new file mode 100644 index 000000000000..4cbf62a78432 --- /dev/null +++ b/tests/unit/LegendListItemOverflowTest.ts @@ -0,0 +1,31 @@ +import allowLegendListItemOverflow from '@components/LegendList/allowLegendListItemOverflow'; + +import type {LegendListRef} from '@legendapp/list/react-native'; + +import createMock from '../utils/createMock'; + +describe('allowLegendListItemOverflow', () => { + it('removes paint containment from a web item container', () => { + const style = {contain: 'paint layout style'}; + const list = createMock({ + getState: () => + createMock>({ + elementAtIndex: () => ({style}), + }), + }); + + expect(allowLegendListItemOverflow(list, 3)).toBe(true); + expect(style.contain).toBe('layout style'); + }); + + it('does nothing for a native item container', () => { + const list = createMock({ + getState: () => + createMock>({ + elementAtIndex: () => ({setNativeProps: jest.fn()}), + }), + }); + + expect(allowLegendListItemOverflow(list, 3)).toBe(false); + }); +}); From 62a7c0d1e89d2d6474520d0f2390cc1ef92c95eb Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 17 Sep 2026 22:29:20 +0200 Subject: [PATCH 08/61] fix: supply report name in modified expense tests --- tests/unit/ModifiedExpenseMessageTest.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/unit/ModifiedExpenseMessageTest.ts b/tests/unit/ModifiedExpenseMessageTest.ts index e5fc178f70d8..b9d5fc7b9732 100644 --- a/tests/unit/ModifiedExpenseMessageTest.ts +++ b/tests/unit/ModifiedExpenseMessageTest.ts @@ -465,6 +465,7 @@ describe('ModifiedExpenseMessage', () => { policyTags: undefined, currentUserAccountID: CURRENT_USER_ACCOUNT_ID, currentUserLogin: CURRENT_USER_LOGIN, + movedFromReportName: undefined, }); expect(result).toEqual(expectedResult); @@ -496,6 +497,7 @@ describe('ModifiedExpenseMessage', () => { policyTags: undefined, currentUserAccountID: CURRENT_USER_ACCOUNT_ID, currentUserLogin: CURRENT_USER_LOGIN, + movedFromReportName: undefined, }); expect(result).toEqual(expectedResult); From 09057d53cf34c7e24e12295594100be27a54ff0b Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Fri, 18 Sep 2026 13:55:30 +0200 Subject: [PATCH 09/61] fix: address report actions list review feedback --- patches/react-native/details.md | 2 +- src/components/FlashList/types.ts | 3 --- .../LegendList/allowLegendListItemOverflow.ts | 11 ++++------- .../MoneyRequestReportTransactionList.tsx | 5 +++-- .../MoneyRequestReportUnifiedList.tsx | 7 ++++--- .../inbox/report/ReportActionIndexContext.tsx | 1 + src/pages/inbox/report/ReportActionsList.tsx | 15 +++++---------- src/setup/telemetry/debugTransport.ts | 6 ++---- tests/unit/ActionListContextProviderTest.tsx | 7 +++---- tests/unit/LegendListItemOverflowTest.ts | 10 +++++++--- tests/unit/useReportScrollManagerTest.tsx | 9 ++++----- 11 files changed, 34 insertions(+), 42 deletions(-) delete mode 100644 src/components/FlashList/types.ts diff --git a/patches/react-native/details.md b/patches/react-native/details.md index 7b69f3793020..135aa21f25fb 100644 --- a/patches/react-native/details.md +++ b/patches/react-native/details.md @@ -13,7 +13,7 @@ 1. Changes when the first visible view is calculated - now happens on scroll events instead of during Fabric's willMountItems lifecycle, which was causing incorrect updates 2. Improves first visible view selection logic to handle Fabric's z-index-based view reordering by finding the view with the smallest position that's still greater than the scroll position 3. Preserves a positioned, zero-sized first child as a scroll anchor. LegendList moves this anchor to compensate for item measurements. Selecting its surrounding container or rejecting its empty frame prevents native scroll compensation and makes the chat jump as estimated rows shrink. -- Upstream PR/issue: https://github.com/facebook/react-native/pull/46247 +- Upstream PR/issue: https://github.com/facebook/react-native/pull/46247 (closed without merging; the zero-sized LegendList anchor fix is not covered there) - E/App issue: 🛑 - PR Introducing Patch: https://github.com/Expensify/App/pull/46315 (introduced), https://github.com/Expensify/App/pull/45289 (refactored) diff --git a/src/components/FlashList/types.ts b/src/components/FlashList/types.ts deleted file mode 100644 index 8563ec9959a4..000000000000 --- a/src/components/FlashList/types.ts +++ /dev/null @@ -1,3 +0,0 @@ -import type ActionListRefType from '@pages/inbox/ActionListTypes'; - -export default ActionListRefType; diff --git a/src/components/LegendList/allowLegendListItemOverflow.ts b/src/components/LegendList/allowLegendListItemOverflow.ts index 5907b06815e1..a222d6d81055 100644 --- a/src/components/LegendList/allowLegendListItemOverflow.ts +++ b/src/components/LegendList/allowLegendListItemOverflow.ts @@ -1,3 +1,5 @@ +import {isRecord} from '@libs/ObjectUtils'; + import type {LegendListRef} from '@legendapp/list/react-native'; /** @@ -5,18 +7,13 @@ import type {LegendListRef} from '@legendapp/list/react-native'; * outside their row. Remove only paint containment so inline menus can overflow while layout and * style containment keep the rest of the row isolated. */ -function allowLegendListItemOverflow(list: LegendListRef | null, index: number): boolean { +function allowLegendListItemOverflow(list: LegendListRef | null, index: number): void { const itemContainer: unknown = list?.getState().elementAtIndex(index); if (!isRecord(itemContainer) || !isRecord(itemContainer.style) || typeof itemContainer.style.contain !== 'string') { - return false; + return; } itemContainer.style.contain = 'layout style'; - return true; -} - -function isRecord(value: unknown): value is Record { - return typeof value === 'object' && value !== null; } export default allowLegendListItemOverflow; diff --git a/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx b/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx index 2ce48fa2bba1..9eadc6fbc552 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx @@ -1,5 +1,4 @@ import LinkButton from '@components/Button/composed/LinkButton'; -import type FlatListRefType from '@components/FlashList/types'; import {useSearchSelectionActions, useSearchSelectionContext} from '@components/Search/SearchContext'; import type {SearchCustomColumnIds, SortOrder} from '@components/Search/types'; @@ -46,6 +45,8 @@ import shouldShowTransactionYear from '@libs/TransactionUtils/shouldShowTransact import isReportOpenInSuperWideRHP from '@navigation/helpers/isReportOpenInSuperWideRHP'; import Navigation from '@navigation/Navigation'; +import type ActionListRefType from '@pages/inbox/ActionListTypes'; + import CONST from '@src/CONST'; import NAVIGATORS from '@src/NAVIGATORS'; import ONYXKEYS from '@src/ONYXKEYS'; @@ -184,7 +185,7 @@ type MoneyRequestReportTransactionListProps = { linkedReportActionID: string | undefined; /** Ref forwarded to the underlying FlashList. */ - listRef: FlatListRefType; + listRef: ActionListRefType; /** Reports the unified list's last item index so the parent can jump to the bottom via scrollToIndex. */ onLastItemIndexChange?: (index: number) => void; diff --git a/src/components/MoneyRequestReportView/MoneyRequestReportUnifiedList.tsx b/src/components/MoneyRequestReportView/MoneyRequestReportUnifiedList.tsx index 81368bb1bada..da5999234ce7 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestReportUnifiedList.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestReportUnifiedList.tsx @@ -1,8 +1,9 @@ import FlashList from '@components/FlashList'; -import type FlatListRefType from '@components/FlashList/types'; import useWindowDimensions from '@hooks/useWindowDimensions'; +import type ActionListRefType from '@pages/inbox/ActionListTypes'; + import variables from '@styles/variables'; import type * as OnyxTypes from '@src/types/onyx'; @@ -46,7 +47,7 @@ function unifiedListItemType(item: UnifiedListItem) { type MoneyRequestReportFlashListProps = FlashListProps & { /** Ref to the underlying list, shared via the ActionList context (typed for the legacy FlatList). */ - ref: FlatListRefType; + ref: ActionListRefType; }; /** @@ -90,7 +91,7 @@ type MoneyRequestReportUnifiedListProps = { newTransactionID?: string; /** Ref to the underlying list, shared via the ActionList context. */ - listRef: FlatListRefType; + listRef: ActionListRefType; accessibilityLabel: string; diff --git a/src/pages/inbox/report/ReportActionIndexContext.tsx b/src/pages/inbox/report/ReportActionIndexContext.tsx index ae2fc03aa9a5..546d1cc5da83 100644 --- a/src/pages/inbox/report/ReportActionIndexContext.tsx +++ b/src/pages/inbox/report/ReportActionIndexContext.tsx @@ -6,6 +6,7 @@ import {createContext, useContext, useState} from 'react'; type ReportActionPosition = { index: number; isNewest: boolean; + /** LegendList reuses mounted rows for different actions; their item state must reset when the row is recycled. */ isRecycling?: boolean; }; diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index d7ab0f26678e..30a1f7024bd0 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -7,6 +7,7 @@ import ReportActionsSkeletonView from '@components/ReportActionsSkeletonView'; import useConciergeSessionStartTime from '@hooks/useConciergeSessionStartTime'; import useEmitComposerScrollEvents from '@hooks/useEmitComposerScrollEvents'; import useEnvironment from '@hooks/useEnvironment'; +import useInitial from '@hooks/useInitial'; import useLinkedMessageOfflineLoading from '@hooks/useLinkedMessageOfflineLoading'; import useLocalize from '@hooks/useLocalize'; import useMarkAsRead from '@hooks/useMarkAsRead'; @@ -317,17 +318,11 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct return visibleReportActionsWithDraft; })(); - const [initialReportActionsSnapshot, setInitialReportActionsSnapshot] = useState<{reportActions: OnyxTypes.ReportAction[]; reportID: string}>(); - const hasInitialReportActionsSnapshot = initialReportActionsSnapshot?.reportID === reportID; - // OpenReport starts with a tiny cached page before replacing it with the hydrated page. Keep that // already-visible page mounted until hydration finishes instead of exposing intermediate estimated // layouts. The hydrated list then mounts from scratch using the full dataset. - if (!hasOnceLoadedReportActions && !hasInitialReportActionsSnapshot && renderedVisibleReportActions.length > 0) { - setInitialReportActionsSnapshot({reportActions: renderedVisibleReportActions, reportID}); - } - - const reportActionsToRender = !hasOnceLoadedReportActions && hasInitialReportActionsSnapshot ? initialReportActionsSnapshot.reportActions : renderedVisibleReportActions; + const initialReportActionsSnapshot = useInitial(renderedVisibleReportActions.length > 0 ? renderedVisibleReportActions : undefined); + const reportActionsToRender = hasOnceLoadedReportActions ? renderedVisibleReportActions : (initialReportActionsSnapshot ?? renderedVisibleReportActions); // Report actions are stored newest-first. LegendList intentionally has no inverted mode, so // give it chronological data and use its normal start/end and scrolling semantics. @@ -392,7 +387,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct const [loadedInitialViewportListID, setLoadedInitialViewportListID] = useState(); const shouldShowInitialViewportSkeleton = !isOffline && (isInitialReportLoadPending || loadedInitialViewportListID !== listID); - const handleViewableItemsChanged = (info: OnViewableItemsChangedInfo) => { + const updateVisibleItemOverflow = (info: OnViewableItemsChangedInfo) => { onViewableItemsChanged(info); for (const item of info.changed) { if (!item.isViewable) { @@ -626,7 +621,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct onScroll={trackScrollPositionAndThreshold} onStartReached={loadOlderChatsOnStartReached} onStartReachedThreshold={PAGINATION_THRESHOLD} - onViewableItemsChanged={handleViewableItemsChanged} + onViewableItemsChanged={updateVisibleItemOverflow} extraData={extraData} key={listID} getItemType={getItemType} diff --git a/src/setup/telemetry/debugTransport.ts b/src/setup/telemetry/debugTransport.ts index 3dfd00f51a85..08e51d397c05 100644 --- a/src/setup/telemetry/debugTransport.ts +++ b/src/setup/telemetry/debugTransport.ts @@ -1,3 +1,5 @@ +import {isRecord} from '@libs/ObjectUtils'; + import ONYXKEYS from '@src/ONYXKEYS'; import type {BaseTransportOptions, Envelope, Transport} from '@sentry/core'; @@ -43,10 +45,6 @@ function formatLogPrefix(category: string, op?: string): string { return `${SENTRY_LOG_PREFIX}[${category}]`; } -function isRecord(value: unknown): value is Record { - return typeof value === 'object' && value !== null && !Array.isArray(value); -} - function isString(value: unknown): value is string { return typeof value === 'string'; } diff --git a/tests/unit/ActionListContextProviderTest.tsx b/tests/unit/ActionListContextProviderTest.tsx index 37033b6ad9e5..d255306e5cdc 100644 --- a/tests/unit/ActionListContextProviderTest.tsx +++ b/tests/unit/ActionListContextProviderTest.tsx @@ -1,8 +1,7 @@ import {act, renderHook} from '@testing-library/react-native'; -import type FlatListRefType from '@components/FlashList/types'; - import {ActionListContextProvider, useActionListContext} from '@pages/inbox/ActionListContext'; +import type ActionListRefType from '@pages/inbox/ActionListTypes'; /** * `ActionListContextProvider` owns a private holder and exposes `registerListRef(ref)` / `getListRef()` @@ -18,7 +17,7 @@ describe('ActionListContextProvider', () => { it('getListRef() returns the same ref object after registerListRef(ref)', () => { const {result} = renderHook(() => useActionListContext(), {wrapper: ActionListContextProvider}); - const listRef: FlatListRefType = {current: null}; + const listRef: ActionListRefType = {current: null}; act(() => { result.current.registerListRef(listRef); @@ -29,7 +28,7 @@ describe('ActionListContextProvider', () => { it('getListRef() returns null again after registerListRef(null) (unmount path)', () => { const {result} = renderHook(() => useActionListContext(), {wrapper: ActionListContextProvider}); - const listRef: FlatListRefType = {current: null}; + const listRef: ActionListRefType = {current: null}; act(() => { result.current.registerListRef(listRef); diff --git a/tests/unit/LegendListItemOverflowTest.ts b/tests/unit/LegendListItemOverflowTest.ts index 4cbf62a78432..d6559318af66 100644 --- a/tests/unit/LegendListItemOverflowTest.ts +++ b/tests/unit/LegendListItemOverflowTest.ts @@ -14,18 +14,22 @@ describe('allowLegendListItemOverflow', () => { }), }); - expect(allowLegendListItemOverflow(list, 3)).toBe(true); + allowLegendListItemOverflow(list, 3); expect(style.contain).toBe('layout style'); }); it('does nothing for a native item container', () => { + const setNativeProps = jest.fn(); + const itemContainer = {setNativeProps}; const list = createMock({ getState: () => createMock>({ - elementAtIndex: () => ({setNativeProps: jest.fn()}), + elementAtIndex: () => itemContainer, }), }); - expect(allowLegendListItemOverflow(list, 3)).toBe(false); + allowLegendListItemOverflow(list, 3); + expect(itemContainer).toEqual({setNativeProps}); + expect(setNativeProps).not.toHaveBeenCalled(); }); }); diff --git a/tests/unit/useReportScrollManagerTest.tsx b/tests/unit/useReportScrollManagerTest.tsx index 6fcd7cb5c64a..6435a8dea5e7 100644 --- a/tests/unit/useReportScrollManagerTest.tsx +++ b/tests/unit/useReportScrollManagerTest.tsx @@ -1,10 +1,9 @@ import {act, renderHook} from '@testing-library/react-native'; -import type FlatListRefType from '@components/FlashList/types'; - import useReportScrollManager from '@hooks/useReportScrollManager'; import {ActionListContext, useActionListContext} from '@pages/inbox/ActionListContext'; +import type ActionListRefType from '@pages/inbox/ActionListTypes'; import type {ReactNode} from 'react'; @@ -24,17 +23,17 @@ function buildMockListRef() { getNativeScrollRef: jest.fn(() => undefined), }; // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion - const ref = {current: methods} as unknown as FlatListRefType; + const ref = {current: methods} as unknown as ActionListRefType; return {ref, methods}; } // Context value backed by a closure holder so registering a ref is visible to the manager's getListRef(). function buildContextValue() { - let held: FlatListRefType = null; + let held: ActionListRefType = null; return { scrollOffsetRef: {current: 0}, getScrollOffset: () => 0, - registerListRef: (ref: FlatListRefType) => { + registerListRef: (ref: ActionListRefType) => { held = ref; }, getListRef: () => held, From ffa18ae1b0a609e2b992b2fc6bae186b5a0a6373 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Fri, 18 Sep 2026 14:49:04 +0200 Subject: [PATCH 10/61] docs: explain report action recycling state --- src/pages/inbox/report/ReportActionIndexContext.tsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/pages/inbox/report/ReportActionIndexContext.tsx b/src/pages/inbox/report/ReportActionIndexContext.tsx index 546d1cc5da83..df37daebe0f3 100644 --- a/src/pages/inbox/report/ReportActionIndexContext.tsx +++ b/src/pages/inbox/report/ReportActionIndexContext.tsx @@ -6,7 +6,6 @@ import {createContext, useContext, useState} from 'react'; type ReportActionPosition = { index: number; isNewest: boolean; - /** LegendList reuses mounted rows for different actions; their item state must reset when the row is recycled. */ isRecycling?: boolean; }; @@ -23,6 +22,12 @@ const ReportActionIndexContext = createContext({index: 0, /** Lets shared list implementations provide their own reliable way to reach the newest action. */ const ReportActionScrollToNewestContext = createContext<(() => void) | undefined>(undefined); +/** + * Each list passes the index from its row renderer and marks the last action as newest. The + * LegendList caller also sets isRecycling because it can reuse a mounted row for another action. + * Descendants use that flag to reset transient state (such as open menus or PDF load errors) + * with useRecyclingState instead of carrying it over to the next action in that row. + */ function ReportActionPositionContextProvider({children, index, isNewest, isRecycling}: PropsWithChildren) { return {children}; } From f12b575e2308d26cf321451dd413904b28185590 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Tue, 22 Sep 2026 14:09:08 +0200 Subject: [PATCH 11/61] fix: remove duplicate report list spacing --- src/pages/inbox/report/ReportActionsList.tsx | 2 +- tests/ui/ReportActionsListTest.tsx | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index afe11d56aa18..4f465e0903ca 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -620,7 +620,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct recycleItems experimental_hideItemsUntilMeasured renderScrollComponent={renderActionSheetAwareScrollView} - contentContainerStyle={styles.chatContentScrollView} + contentContainerStyle={[styles.chatContentScrollView, styles.pb0]} onEndReached={loadNewerChatsAfterTransitions} onEndReachedThreshold={PAGINATION_THRESHOLD} ListHeaderComponent={listFooterComponent} diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 72718bd0b8dd..e6a49ee00bf6 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -30,8 +30,10 @@ import type * as OnyxTypes from '@src/types/onyx'; import type {OnViewableItemsChangedInfo} from '@legendapp/list/react-native'; import type * as ReactNavigation from '@react-navigation/native'; +import type {StyleProp, ViewStyle} from 'react-native'; import React from 'react'; +import {StyleSheet} from 'react-native'; import Onyx from 'react-native-onyx'; import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; @@ -184,6 +186,7 @@ jest.mock('@pages/inbox/report/ReportActionItemCreated', () => jest.fn(() => nul type MockLegendListProps = { alignItemsAtEnd?: boolean; + contentContainerStyle?: StyleProp; data?: OnyxTypes.ReportAction[]; drawDistance?: number; // eslint-disable-next-line @typescript-eslint/naming-convention @@ -421,6 +424,13 @@ describe('ReportActionsList (body)', () => { expect(getCapturedListProps()?.maintainVisibleContentPosition).toBe(true); }); + it('does not duplicate the composer spacing inside the chronological list', () => { + mockUseNetwork.mockReturnValue({isOffline: false}); + renderReportActionsList(); + + expect(StyleSheet.flatten(getCapturedListProps()?.contentContainerStyle)?.paddingBottom).toBe(0); + }); + it('initially aligns the seed page to the end', () => { mockUseNetwork.mockReturnValue({isOffline: false}); mockHasOnceLoadedReportActions = false; From 34b345d4c529ad17ab7c92612c6bd59784d7a8c6 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Tue, 22 Sep 2026 14:10:59 +0200 Subject: [PATCH 12/61] fix: synchronize sent comment scrolling --- .../useReportActionsNewActionLiveTail.ts | 27 ++++++++++++++----- .../useReportActionsNewActionLiveTailTest.ts | 26 +++++++++++++++--- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts index 02b8d8665a76..cf01b300b280 100644 --- a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts +++ b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts @@ -19,7 +19,7 @@ import type * as OnyxTypes from '@src/types/onyx'; import type {OnyxEntry} from 'react-native-onyx'; import {useNavigation} from '@react-navigation/native'; -import {useEffect, useEffectEvent, useRef, useState} from 'react'; +import {useCallback, useEffect, useEffectEvent, useRef, useState} from 'react'; // In the component we are subscribing to the arrival of new actions. // As there is the possibility that there are multiple instances of a ReportScreen @@ -93,6 +93,16 @@ function useReportActionsNewActionLiveTail({ const {accountID: currentUserAccountID} = useCurrentUserPersonalDetails(); const liveTailJumpRef = useRef<{stage: LiveTailJumpStage}>({stage: 'idle'}); const [isScrollToBottomEnabled, setIsScrollToBottomEnabled] = useState(false); + const [pendingScrollActionID, setPendingScrollActionID] = useState(); + + const setShouldScrollToBottom = useCallback((enabled: boolean) => { + setIsScrollToBottomEnabled(enabled); + if (!enabled) { + setPendingScrollActionID(undefined); + } + }, []); + + const isPendingScrollActionRendered = !!pendingScrollActionID && renderedVisibleReportActions.some((item) => item.reportActionID === pendingScrollActionID); const scrollToBottomForCurrentUserAction = useEffectEvent((isFromCurrentUser: boolean, action?: OnyxTypes.ReportAction) => { TransitionTracker.runAfterTransitions({ @@ -102,6 +112,11 @@ function useReportActionsNewActionLiveTail({ if (!isFromCurrentUser || (!isReportTopmostSplitNavigator() && !Navigation.getReportRHPActiveRoute())) { return; } + // Pusher echoes omit the action payload and can represent edits or deletions. Local sends + // notify with the optimistic action, which lets us wait until that exact row is rendered. + if (!action) { + return; + } if (!hasNewestReportAction && !isFromCurrentUser) { if (Navigation.getReportRHPActiveRoute()) { return; @@ -147,7 +162,7 @@ function useReportActionsNewActionLiveTail({ } } else { setIsFloatingMessageCounterVisible(false); - setIsScrollToBottomEnabled(true); + setPendingScrollActionID(action.reportActionID); } }, }); @@ -205,9 +220,9 @@ function useReportActionsNewActionLiveTail({ setIsFloatingMessageCounterVisible(false); // Defer so this effect does not synchronously chain a second render from setState (eslint react-hooks/set-state-in-effect). queueMicrotask(() => { - setIsScrollToBottomEnabled(true); + setShouldScrollToBottom(true); }); - }, [hasNewestReportAction, treatAsNoPaginationAnchor, setIsFloatingMessageCounterVisible]); + }, [hasNewestReportAction, treatAsNoPaginationAnchor, setIsFloatingMessageCounterVisible, setShouldScrollToBottom]); useEffect(() => { // Why are we doing this, when in the cleanup of the useEffect we are already calling the unsubscribe function? @@ -237,8 +252,8 @@ function useReportActionsNewActionLiveTail({ }, [reportID]); return { - isScrollToBottomEnabled, - setIsScrollToBottomEnabled, + isScrollToBottomEnabled: isScrollToBottomEnabled || isPendingScrollActionRendered, + setIsScrollToBottomEnabled: setShouldScrollToBottom, completeLiveTailPruneAfterScrollToBottom, }; } diff --git a/tests/unit/useReportActionsNewActionLiveTailTest.ts b/tests/unit/useReportActionsNewActionLiveTailTest.ts index cf372cf3122c..635d82eddb92 100644 --- a/tests/unit/useReportActionsNewActionLiveTailTest.ts +++ b/tests/unit/useReportActionsNewActionLiveTailTest.ts @@ -120,15 +120,33 @@ describe('useReportActionsNewActionLiveTail', () => { mockIsInSidePanel = false; }); - it('requests one post-render scroll for a sent comment instead of also scrolling immediately', () => { - const {result} = renderHook(() => useReportActionsNewActionLiveTail(buildParams({hasNewerActions: false, hasNewestReportAction: true}))); + it('requests one post-render scroll once the sent comment is rendered', () => { + const action = getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT}); + const {result, rerender} = renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), { + initialProps: buildParams({hasNewerActions: false, hasNewestReportAction: true}), + }); act(() => { - newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT})); + newActionHandler?.(true, action); }); - expect(result.current.isScrollToBottomEnabled).toBe(true); + expect(result.current.isScrollToBottomEnabled).toBe(false); expect(reportScrollManager.scrollToBottom).not.toHaveBeenCalled(); + + rerender(buildParams({hasNewerActions: false, hasNewestReportAction: true, renderedVisibleReportActions: [action]})); + + expect(result.current.isScrollToBottomEnabled).toBe(true); + }); + + it('does not scroll for a payload-less realtime echo', () => { + const {result} = renderHook(() => useReportActionsNewActionLiveTail(buildParams({hasNewerActions: false, hasNewestReportAction: true}))); + + act(() => { + newActionHandler?.(true, undefined); + }); + + expect(result.current.isScrollToBottomEnabled).toBe(false); + expect(mockOpenReport).not.toHaveBeenCalled(); }); it('does not queue a bottom scroll that would compete with a report-preview target', () => { From da3b443d6bfc4905a14cf31a1ee902f470a3bab8 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Tue, 22 Sep 2026 14:12:25 +0200 Subject: [PATCH 13/61] fix: preserve hydrated report list state --- src/hooks/useReportActionsListModel.ts | 5 ++++- src/pages/inbox/report/ReportActionsList.tsx | 5 ++++- tests/ui/ReportActionsListTest.tsx | 23 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/hooks/useReportActionsListModel.ts b/src/hooks/useReportActionsListModel.ts index 156fb84db12e..ef0c8d9b26cd 100644 --- a/src/hooks/useReportActionsListModel.ts +++ b/src/hooks/useReportActionsListModel.ts @@ -12,6 +12,7 @@ import {reportActionsListLoadingStateSelector} from '@src/selectors/ReportMetaDa import {useRoute} from '@react-navigation/native'; import {useIsAppLoadPending} from './useInFlightRequests'; +import useInitial from './useInitial'; import useLoadReportActions from './useLoadReportActions'; import useNetworkWithOfflineStatus from './useNetworkWithOfflineStatus'; import useOnyx from './useOnyx'; @@ -54,7 +55,9 @@ function useReportActionsListModel(reportID: string, isReportLoadPending: boolea const [reportLoadingState] = useOnyx(`${ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE}${reportID}`, { selector: reportActionsListLoadingStateSelector, }); - const hasOnceLoadedReportActions = reportLoadingState?.hasOnceLoadedReportActions; + // This flag is monotonic for one report visit. Keep it true if the RAM-only entry is briefly + // absent while optimistic writes are merged, so the hydrated list never returns to its loading state. + const hasOnceLoadedReportActions = useInitial(reportLoadingState?.hasOnceLoadedReportActions ? true : undefined); const isLoadingInitialReportActions = reportLoadingState?.isLoadingInitialReportActions; const isInitialReportLoadPending = !hasOnceLoadedReportActions && (isReportLoadPending || isLoadingInitialReportActions !== false); const isLoadingOlderReportActions = reportLoadingState?.isLoadingOlderReportActions; diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index 4f465e0903ca..aeb493bf939d 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -665,7 +665,10 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct */ function ReportActionsList({reportID, conciergeChat, onLayout}: ReportActionsListProps) { return ( - + { expect(getCapturedVisibleActions()?.some((action) => action.reportActionID === olderMockReportAction.reportActionID)).toBe(true); }); + it('keeps a hydrated list mounted when its RAM-only loading entry briefly regresses', async () => { + mockUseNetwork.mockReturnValue({isOffline: false}); + const view = renderReportActionsList(); + + expect(mockLegendListMount).toHaveBeenCalledTimes(1); + expect(screen.queryByTestId('ReportActionsSkeletonView')).toBeNull(); + + mockHasOnceLoadedReportActions = false; + mockIsLoadingInitialReportActions = true; + view.rerender( + , + ); + await waitForBatchedUpdatesWithAct(); + + expect(mockLegendListMount).toHaveBeenCalledTimes(1); + expect(mockLegendListUnmount).not.toHaveBeenCalled(); + expect(screen.queryByTestId('ReportActionsSkeletonView')).toBeNull(); + }); + it('limits the render buffer and enables item recycling', () => { mockUseNetwork.mockReturnValue({isOffline: false}); renderReportActionsList(); From 55cb72a5d4646ae1ee1af1b325b33cd022a73488 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Tue, 22 Sep 2026 14:13:56 +0200 Subject: [PATCH 14/61] fix: refresh recycled unread marker rows --- src/pages/inbox/report/ReportActionsList.tsx | 4 +--- tests/ui/ReportActionsListTest.tsx | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index aeb493bf939d..2dae9cc9a702 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -14,7 +14,6 @@ import useMarkAsRead from '@hooks/useMarkAsRead'; import useNetwork from '@hooks/useNetwork'; import useOnyx from '@hooks/useOnyx'; import useReportActionsScroll from '@hooks/useReportActionsScroll'; -import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useRetireMerchantRuleSuggestionOnLeave from '@hooks/useRetireMerchantRuleSuggestionOnLeave'; import useThemeStyles from '@hooks/useThemeStyles'; import useUnreadMarker from '@hooks/useUnreadMarker'; @@ -161,7 +160,6 @@ function keyExtractor(item: OnyxTypes.ReportAction): string { function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportActionsListContentProps) { const styles = useThemeStyles(); const {translate} = useLocalize(); - const {shouldUseNarrowLayout} = useResponsiveLayout(); const {isProduction} = useEnvironment(); const { @@ -538,7 +536,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct // Native mobile does not render updates flatlist the changes even though component did update called. // To notify there something changes we can use extraData prop to flatlist const extraData = [ - shouldUseNarrowLayout ? unreadMarkerReportActionID : undefined, + unreadMarkerReportActionID, isArchivedNonExpenseReport(report, isReportArchived), draftReportActionID, draftMessageHTML, diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index d87bcec39a02..0d6cf09b37f8 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -223,7 +223,7 @@ const getCapturedListProps = (): MockLegendListProps | undefined => mockLegendLi const getRenderedReportActionsListItemProps = ( reportAction: OnyxTypes.ReportAction, index = 0, -): {shouldDisableContextMenuForConciergeDraft?: boolean; isLatestConciergeFeedbackAction?: boolean} => { +): {shouldDisableContextMenuForConciergeDraft?: boolean; isLatestConciergeFeedbackAction?: boolean; shouldDisplayNewMarker?: boolean} => { const renderedItem = getCapturedListProps()?.renderItem?.({item: reportAction, index}); if (!React.isValidElement<{children: React.ReactNode}>(renderedItem)) { @@ -244,6 +244,7 @@ const getRenderedReportActionsListItemProps = ( const mockUseMarkAsRead: jest.Mock = jest.requireMock('@hooks/useMarkAsRead'); const mockUseReportActionsScroll: jest.Mock = jest.requireMock('@hooks/useReportActionsScroll'); +const mockUseUnreadMarker: jest.Mock = jest.requireMock('@hooks/useUnreadMarker'); const mockMarkOpenReportEnd: jest.Mock = jest.requireMock('@libs/telemetry/markOpenReportEnd'); let mockHasOnceLoadedReportActions = true; let mockIsLoadingInitialReportActions = false; @@ -352,6 +353,7 @@ describe('ReportActionsList (body)', () => { mockIsLoadingInitialReportActions = false; mockIsLoadingOlderReportActions = false; mockShouldCallLegendListOnLoad = true; + mockUseUnreadMarker.mockReturnValue({unreadMarkerReportActionID: null, unreadMarkerReportActionIndex: -1}); mockUseIsReportLoadPending.mockReturnValue(false); mockUseCurrentUserPersonalDetails.mockReturnValue({ @@ -431,6 +433,19 @@ describe('ReportActionsList (body)', () => { expect(StyleSheet.flatten(getCapturedListProps()?.contentContainerStyle)?.paddingBottom).toBe(0); }); + it('invalidates a recycled row when the unread marker changes on a wide layout', () => { + mockUseNetwork.mockReturnValue({isOffline: false}); + mockUseUnreadMarker.mockReturnValue({unreadMarkerReportActionID: '2', unreadMarkerReportActionIndex: 0}); + renderReportActionsList(); + + expect(getCapturedListProps()?.extraData).toEqual(expect.arrayContaining(['2'])); + const markerAction = getCapturedVisibleActions()?.find((action) => action.reportActionID === '2'); + if (!markerAction) { + throw new Error('Expected unread marker action fixture'); + } + expect(getRenderedReportActionsListItemProps(markerAction, 1).shouldDisplayNewMarker).toBe(true); + }); + it('initially aligns the seed page to the end', () => { mockUseNetwork.mockReturnValue({isOffline: false}); mockHasOnceLoadedReportActions = false; From 8188df1a366d2ae10ebea53b29f8fa9795e67cbc Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 24 Sep 2026 14:27:46 +0200 Subject: [PATCH 15/61] fix: keep report actions live across incomplete loads --- src/pages/inbox/report/ReportActionsList.tsx | 19 +++--- tests/ui/ReportActionsListTest.tsx | 67 ++++++++++++++++++++ 2 files changed, 78 insertions(+), 8 deletions(-) diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index 2dae9cc9a702..8353a1599357 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -322,7 +322,8 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct // already-visible page mounted until hydration finishes instead of exposing intermediate estimated // layouts. The hydrated list then mounts from scratch using the full dataset. const initialReportActionsSnapshot = useInitial(renderedVisibleReportActions.length > 0 ? renderedVisibleReportActions : undefined); - const reportActionsToRender = hasOnceLoadedReportActions ? renderedVisibleReportActions : (initialReportActionsSnapshot ?? renderedVisibleReportActions); + const shouldKeepInitialSnapshot = isInitialReportLoadPending && !isOffline && !showFullHistory; + const reportActionsToRender = shouldKeepInitialSnapshot ? (initialReportActionsSnapshot ?? renderedVisibleReportActions) : renderedVisibleReportActions; // Report actions are stored newest-first. LegendList intentionally has no inverted mode, so // give it chronological data and use its normal start/end and scrolling semantics. @@ -385,7 +386,10 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct }); const [loadedInitialViewportListID, setLoadedInitialViewportListID] = useState(); - const shouldShowInitialViewportSkeleton = !isOffline && (isInitialReportLoadPending || loadedInitialViewportListID !== listID); + // A warm report already has its complete action page. Only an initial visit needs a cover + // while the first page finishes loading and LegendList positions it. + const [shouldCoverInitialViewport] = useState(!hasOnceLoadedReportActions); + const shouldShowInitialViewportSkeleton = !isOffline && shouldCoverInitialViewport && (isInitialReportLoadPending || loadedInitialViewportListID !== listID); const updateVisibleItemOverflow = (info: OnViewableItemsChangedInfo) => { onViewableItemsChanged(info); @@ -533,8 +537,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct ); }; - // Native mobile does not render updates flatlist the changes even though component did update called. - // To notify there something changes we can use extraData prop to flatlist + // Ensure row presentation updates when these values change without changing the action data. const extraData = [ unreadMarkerReportActionID, isArchivedNonExpenseReport(report, isReportArchived), @@ -544,7 +547,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct latestConciergeFeedbackActionID, ]; - const listHeaderComponent = ( + const latestActionsFooter = ( action.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED); - const listFooterComponent = shouldShowOfflineSkeleton ? : undefined; + const offlineHistoryHeader = shouldShowOfflineSkeleton ? : undefined; const shouldShowMarkAsDoneCopy = shouldShowMarkAsDone({ policy, @@ -621,8 +624,8 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct contentContainerStyle={[styles.chatContentScrollView, styles.pb0]} onEndReached={loadNewerChatsAfterTransitions} onEndReachedThreshold={PAGINATION_THRESHOLD} - ListHeaderComponent={listFooterComponent} - ListFooterComponent={listHeaderComponent} + ListHeaderComponent={offlineHistoryHeader} + ListFooterComponent={latestActionsFooter} ListFooterComponentStyle={shouldBeAlignedToTop ? styles.flex1 : undefined} keyboardShouldPersistTaps="handled" onLayout={recordTimeToMeasureItemLayout} diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 0569681d2e57..5148b4c108af 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -527,6 +527,33 @@ describe('ReportActionsList (body)', () => { expect(screen.queryByTestId('ReportActionsSkeletonView')).toBeNull(); }); + it('forwards the list load event so initial scroll tracking can resume', () => { + // Given a report with its action list ready to mount. + mockShouldCallLegendListOnLoad = false; + renderReportActionsList(); + const onLoad = mockUseReportActionsScroll.mock.results.at(-1)?.value.onLoad; + expect(onLoad).not.toHaveBeenCalled(); + + // When LegendList reports that its first layout is complete. + act(() => getCapturedListProps()?.onLoad?.()); + + // Then the scroll hook receives that event to finish initial positioning. + expect(onLoad).toHaveBeenCalledTimes(1); + }); + + it('shows a previously hydrated report without replaying the loading cover', () => { + // Given a report that was already loaded before this list mounted + mockUseNetwork.mockReturnValue({isOffline: false}); + mockShouldCallLegendListOnLoad = false; + + // When the user opens the report again + renderReportActionsList(); + + // Then cached actions stay visible while LegendList completes its layout + expect(getCapturedVisibleActions()).toHaveLength(mockReportActions.length); + expect(screen.queryByTestId('ReportActionsSkeletonCover')).toBeNull(); + }); + it('releases the initial viewport cover after a terminal OpenReport failure', () => { mockUseNetwork.mockReturnValue({isOffline: false}); mockHasOnceLoadedReportActions = false; @@ -571,6 +598,7 @@ describe('ReportActionsList (body)', () => { it('keeps the initial actions visible until the hydrated page is complete', async () => { mockUseNetwork.mockReturnValue({isOffline: false}); mockHasOnceLoadedReportActions = false; + mockIsLoadingInitialReportActions = true; const groupingSpy = jest.spyOn(ReportActionsUtils, 'isConsecutiveActionMadeByPreviousActor'); const view = renderReportActionsList(); @@ -615,6 +643,45 @@ describe('ReportActionsList (body)', () => { expect(getCapturedVisibleActions()?.some((action) => action.reportActionID === olderMockReportAction.reportActionID)).toBe(true); }); + it.each([ + {isOffline: true, isLoadingInitialReportActions: true}, + {isOffline: false, isLoadingInitialReportActions: false}, + ])('keeps actions live after a first load cannot complete: %p', async ({isOffline, isLoadingInitialReportActions}) => { + // Given cached actions with no successful OpenReport response + mockUseNetwork.mockReturnValue({isOffline}); + mockHasOnceLoadedReportActions = false; + mockIsLoadingInitialReportActions = isLoadingInitialReportActions; + const view = renderReportActionsList(); + + // When an optimistic action is added while offline or after a terminal load failure + const existingAction = mockReportActions.at(1); + if (!existingAction) { + throw new Error('Expected a cached report action'); + } + mockUsePaginatedReportActions.mockReturnValue({ + ...defaultPaginatedReportActionsResult, + reportActions: [ + { + ...existingAction, + reportActionID: 'new-comment', + created: '2023-01-03', + }, + ...mockReportActions, + ], + }); + view.rerender( + , + ); + await waitForBatchedUpdatesWithAct(); + + // Then the new action appears without waiting for hydration + expect(getCapturedVisibleActions()?.at(-1)?.reportActionID).toBe('new-comment'); + }); + it('keeps a hydrated list mounted when its RAM-only loading entry briefly regresses', async () => { mockUseNetwork.mockReturnValue({isOffline: false}); const view = renderReportActionsList(); From 9bd382a51488769267b48df5b44a5fa5e9a1b00f Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 24 Sep 2026 14:28:15 +0200 Subject: [PATCH 16/61] fix: preserve local action scrolling without realtime echo jumps --- src/libs/actions/Report/index.ts | 2 +- .../actions/Report/reportActionSubscribers.ts | 8 ++- .../useReportActionsNewActionLiveTail.ts | 27 +++++++-- .../unit/libs/reportActionSubscribersTest.ts | 15 ++++- .../useReportActionsNewActionLiveTailTest.ts | 55 +++++++++++++++---- 5 files changed, 87 insertions(+), 20 deletions(-) diff --git a/src/libs/actions/Report/index.ts b/src/libs/actions/Report/index.ts index 59ffb84762ba..68c0fd0ed708 100644 --- a/src/libs/actions/Report/index.ts +++ b/src/libs/actions/Report/index.ts @@ -5294,7 +5294,7 @@ function showReportActionNotification( LocalNotification.showCommentNotification(report, reportAction, onClick, derivedReportName); } - notifyNewAction(reportID, undefined, reportAction.actorAccountID === currentUserAccountID); + notifyNewAction(reportID, undefined, reportAction.actorAccountID === currentUserAccountID, 'realtime'); } /** Clear the errors associated with the IOUs of a given report. */ diff --git a/src/libs/actions/Report/reportActionSubscribers.ts b/src/libs/actions/Report/reportActionSubscribers.ts index 8fc4644ac0eb..66b78ef98872 100644 --- a/src/libs/actions/Report/reportActionSubscribers.ts +++ b/src/libs/actions/Report/reportActionSubscribers.ts @@ -3,7 +3,8 @@ */ import type ReportAction from '@src/types/onyx/ReportAction'; -type SubscriberCallback = (isFromCurrentUser: boolean, reportAction: ReportAction | undefined) => void; +type ActionEventSource = 'local' | 'realtime'; +type SubscriberCallback = (isFromCurrentUser: boolean, reportAction: ReportAction | undefined, source: ActionEventSource) => void; type ActionSubscriber = { reportID: string; @@ -26,7 +27,7 @@ function subscribeToNewActionEvent(reportID: string, callback: SubscriberCallbac } /** Notify the ReportActionsList that a new comment has arrived */ -function notifyNewAction(reportID: string | string[] | undefined, reportAction: ReportAction | undefined, isFromCurrentUser: boolean) { +function notifyNewAction(reportID: string | string[] | undefined, reportAction: ReportAction | undefined, isFromCurrentUser: boolean, source: ActionEventSource = 'local') { if (!reportID) { return; } @@ -34,9 +35,10 @@ function notifyNewAction(reportID: string | string[] | undefined, reportAction: for (const id of ids) { const actionSubscriber = newActionSubscribers.find((subscriber) => subscriber.reportID === id); if (actionSubscriber) { - actionSubscriber.callback(isFromCurrentUser, reportAction); + actionSubscriber.callback(isFromCurrentUser, reportAction, source); } } } export {subscribeToNewActionEvent, notifyNewAction}; +export type {ActionEventSource}; diff --git a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts index c6288f0532dd..fb76130ce1e9 100644 --- a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts +++ b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts @@ -5,6 +5,7 @@ import type useReportScrollManager from '@hooks/useReportScrollManager'; import type {OpenReportActionParams} from '@libs/actions/Report'; import {openReport, pruneReportActionPagesToNewestWindow} from '@libs/actions/Report'; import {subscribeToNewActionEvent} from '@libs/actions/Report/reportActionSubscribers'; +import type {ActionEventSource} from '@libs/actions/Report/reportActionSubscribers'; import isReportTopmostSplitNavigator from '@libs/Navigation/helpers/isReportTopmostSplitNavigator'; import Navigation from '@libs/Navigation/Navigation'; import type {PlatformStackNavigationProp} from '@libs/Navigation/PlatformStackNavigation/types'; @@ -60,6 +61,7 @@ type UseReportActionsNewActionLiveTailParams = { }; type LiveTailJumpStage = 'idle' | 'open_report' | 'await_scroll' | 'await_prune'; +const PENDING_SENT_ACTION_TIMEOUT_MS = 5000; /** * Owns subscribe-to-new-action scrolling, live-tail jump (openReport → scroll → prune), and the @@ -105,7 +107,18 @@ function useReportActionsNewActionLiveTail({ const isPendingScrollActionRendered = !!pendingScrollActionID && renderedVisibleReportActions.some((item) => item.reportActionID === pendingScrollActionID); - const scrollToBottomForCurrentUserAction = useEffectEvent((isFromCurrentUser: boolean, action?: OnyxTypes.ReportAction) => { + useEffect(() => { + if (!pendingScrollActionID) { + return; + } + + // The optimistic row normally arrives immediately. A filtered or failed row must not + // scroll the reader much later when pagination or reconnecting makes it visible. + const timeout = setTimeout(() => setPendingScrollActionID(undefined), PENDING_SENT_ACTION_TIMEOUT_MS); + return () => clearTimeout(timeout); + }, [pendingScrollActionID]); + + const scrollToBottomForCurrentUserAction = useEffectEvent((isFromCurrentUser: boolean, action: OnyxTypes.ReportAction | undefined, source: ActionEventSource) => { TransitionTracker.runAfterTransitions({ callback: () => { // If a new comment is added and it's from the current user scroll to the bottom otherwise leave the user positioned where @@ -113,9 +126,9 @@ function useReportActionsNewActionLiveTail({ if (!isFromCurrentUser || (!isReportTopmostSplitNavigator() && !Navigation.getReportRHPActiveRoute())) { return; } - // Pusher echoes omit the action payload and can represent edits or deletions. Local sends - // notify with the optimistic action, which lets us wait until that exact row is rendered. - if (!action) { + // A realtime echo can represent an edit or deletion, whereas local payload-less + // notifications from money requests still need the usual live-tail behavior. + if (!action && source === 'realtime') { return; } if (!hasNewestReportAction && !isFromCurrentUser) { @@ -163,7 +176,11 @@ function useReportActionsNewActionLiveTail({ } } else { setIsFloatingMessageCounterVisible(false); - setPendingScrollActionID(action.reportActionID); + if (action?.reportActionID) { + setPendingScrollActionID(action.reportActionID); + } else { + setShouldScrollToBottom(true); + } } }, }); diff --git a/tests/unit/libs/reportActionSubscribersTest.ts b/tests/unit/libs/reportActionSubscribersTest.ts index 262557d19a03..da2401146bda 100644 --- a/tests/unit/libs/reportActionSubscribersTest.ts +++ b/tests/unit/libs/reportActionSubscribersTest.ts @@ -7,7 +7,7 @@ describe('subscribeToNewActionEvent', () => { notifyNewAction('1', undefined, true); - expect(callback).toHaveBeenCalledWith(true, undefined); + expect(callback).toHaveBeenCalledWith(true, undefined, 'local'); unsubscribe(); }); @@ -56,4 +56,17 @@ describe('subscribeToNewActionEvent', () => { expect(callback).not.toHaveBeenCalled(); }); + + test('identifies realtime notifications separately from local actions', () => { + // Given a subscriber listening to the current report + const callback = jest.fn(); + const unsubscribe = subscribeToNewActionEvent('1', callback); + + // When a realtime echo arrives without an action payload + notifyNewAction('1', undefined, true, 'realtime'); + + // Then the subscriber can distinguish it from a local money request + expect(callback).toHaveBeenCalledWith(true, undefined, 'realtime'); + unsubscribe(); + }); }); diff --git a/tests/unit/useReportActionsNewActionLiveTailTest.ts b/tests/unit/useReportActionsNewActionLiveTailTest.ts index a3257b4292d3..7930bd25809d 100644 --- a/tests/unit/useReportActionsNewActionLiveTailTest.ts +++ b/tests/unit/useReportActionsNewActionLiveTailTest.ts @@ -13,7 +13,7 @@ const mockNavigationSetParams = jest.fn(); const mockGlobalSetParams = jest.fn(); const mockOpenReport = jest.fn(); const mockLogAlert = jest.fn(); -let newActionHandler: ((isFromCurrentUser: boolean, action?: ReportAction) => void) | undefined; +let newActionHandler: ((isFromCurrentUser: boolean, action: ReportAction | undefined, source: 'local' | 'realtime') => void) | undefined; jest.mock('@libs/Log', () => ({ __esModule: true, @@ -73,7 +73,7 @@ jest.mock('@libs/actions/Report', () => ({ })); jest.mock('@libs/actions/Report/reportActionSubscribers', () => ({ - subscribeToNewActionEvent: (_reportID: string, callback: (isFromCurrentUser: boolean, action?: ReportAction) => void) => { + subscribeToNewActionEvent: (_reportID: string, callback: (isFromCurrentUser: boolean, action: ReportAction | undefined, source: 'local' | 'realtime') => void) => { newActionHandler = callback; return jest.fn(); }, @@ -130,7 +130,7 @@ describe('useReportActionsNewActionLiveTail', () => { }); act(() => { - newActionHandler?.(true, action); + newActionHandler?.(true, action, 'local'); }); expect(result.current.isScrollToBottomEnabled).toBe(false); @@ -145,19 +145,54 @@ describe('useReportActionsNewActionLiveTail', () => { const {result} = renderHook(() => useReportActionsNewActionLiveTail(buildParams({hasNewerActions: false, hasNewestReportAction: true}))); act(() => { - newActionHandler?.(true, undefined); + newActionHandler?.(true, undefined, 'realtime'); }); expect(result.current.isScrollToBottomEnabled).toBe(false); expect(mockOpenReport).not.toHaveBeenCalled(); }); + it('keeps the live-tail jump for a local action without a report action payload', () => { + // Given a report showing an older page + renderHook(() => useReportActionsNewActionLiveTail(buildParams())); + + // When a local money request sends a payload-less notification + act(() => { + newActionHandler?.(true, undefined, 'local'); + }); + + // Then the report opens its latest page + expect(mockOpenReport).toHaveBeenCalledTimes(1); + }); + + it('expires a sent-comment scroll request when the action never enters the list', () => { + // Given a sent action that is filtered out or fails to appear + jest.useFakeTimers(); + const action = getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT}); + const {result, rerender} = renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), { + initialProps: buildParams({hasNewerActions: false, hasNewestReportAction: true}), + }); + act(() => { + newActionHandler?.(true, action, 'local'); + }); + + // When the action finally appears well after the send attempt + act(() => { + jest.advanceTimersByTime(10000); + }); + rerender(buildParams({hasNewerActions: false, hasNewestReportAction: true, renderedVisibleReportActions: [action]})); + + // Then the stale request does not pull the reader to the bottom + expect(result.current.isScrollToBottomEnabled).toBe(false); + jest.useRealTimers(); + }); + it('does not queue a bottom scroll that would compete with a report-preview target', () => { const preview = getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW}); const {result} = renderHook(() => useReportActionsNewActionLiveTail(buildParams({hasNewerActions: false, hasNewestReportAction: true, renderedVisibleReportActions: [preview]}))); act(() => { - newActionHandler?.(true, preview); + newActionHandler?.(true, preview, 'local'); }); expect(reportScrollManager.scrollToBottom).toHaveBeenCalledTimes(1); @@ -169,7 +204,7 @@ describe('useReportActionsNewActionLiveTail', () => { renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), {initialProps: buildParams({conciergeChat})}); act(() => { - newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT})); + newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT}), 'local'); }); expect(mockOpenReport).toHaveBeenCalledWith(expect.objectContaining({conciergeChat})); @@ -179,7 +214,7 @@ describe('useReportActionsNewActionLiveTail', () => { renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), {initialProps: buildParams({isSelfTourViewed: true, hasCompletedGuidedSetupFlow: false})}); act(() => { - newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT})); + newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT}), 'local'); }); // The onboarding flags are threaded straight through so guided-setup optimistic data is derived from real Onyx @@ -191,7 +226,7 @@ describe('useReportActionsNewActionLiveTail', () => { const {rerender} = renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), {initialProps: buildParams()}); act(() => { - newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT})); + newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT}), 'local'); }); expect(mockOpenReport).toHaveBeenCalledTimes(1); @@ -214,7 +249,7 @@ describe('useReportActionsNewActionLiveTail', () => { const {rerender} = renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), {initialProps: buildParams({setTreatAsNoPaginationAnchor})}); act(() => { - newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT})); + newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT}), 'local'); }); expect(() => @@ -236,7 +271,7 @@ describe('useReportActionsNewActionLiveTail', () => { const {rerender} = renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), {initialProps: buildParams({setTreatAsNoPaginationAnchor})}); act(() => { - newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT})); + newActionHandler?.(true, getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT}), 'local'); }); rerender( From f0b046a56553c2af10dc9a7e200d0f361ec0e3cc Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 24 Sep 2026 14:28:36 +0200 Subject: [PATCH 17/61] fix: key recycled media state by action and image --- src/components/ThumbnailImage.tsx | 6 +- src/pages/inbox/report/ReportActionItem.tsx | 13 ++--- tests/unit/ThumbnailImageTest.tsx | 61 +++++++++++++++++++++ 3 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 tests/unit/ThumbnailImageTest.tsx diff --git a/src/components/ThumbnailImage.tsx b/src/components/ThumbnailImage.tsx index 96fccc69e533..2471ac4ad8af 100644 --- a/src/components/ThumbnailImage.tsx +++ b/src/components/ThumbnailImage.tsx @@ -101,7 +101,9 @@ function ThumbnailImage({ const failedToLoad = failedLoadKey !== null && failedLoadKey.url === previewSourceURL && failedLoadKey.isOffline === isOffline; const cachedDimensions = shouldDynamicallyResize && typeof previewSourceURL === 'string' ? thumbnailDimensionsCache.get(previewSourceURL) : null; - const [imageDimensions, setImageDimensions] = useState({width: cachedDimensions?.width ?? imageWidth, height: cachedDimensions?.height ?? imageHeight}); + const [measuredDimensions, setMeasuredDimensions] = useState<{url: string | ImageSourcePropType; width: number; height: number} | null>(null); + const imageDimensions = + measuredDimensions?.url === previewSourceURL ? measuredDimensions : {width: cachedDimensions?.width ?? imageWidth, height: cachedDimensions?.height ?? imageHeight}; const {thumbnailDimensionsStyles} = useThumbnailDimensions(imageDimensions.width, imageDimensions.height); const StyleUtils = useStyleUtils(); @@ -114,7 +116,7 @@ function ThumbnailImage({ thumbnailDimensionsCache.set(previewSourceURL, {width, height}); } - setImageDimensions({width, height}); + setMeasuredDimensions({url: previewSourceURL, width, height}); }; const sizeStyles = shouldDynamicallyResize ? [thumbnailDimensionsStyles] : [styles.w100, styles.h100]; diff --git a/src/pages/inbox/report/ReportActionItem.tsx b/src/pages/inbox/report/ReportActionItem.tsx index c031179b2a36..df94cd75d2e7 100644 --- a/src/pages/inbox/report/ReportActionItem.tsx +++ b/src/pages/inbox/report/ReportActionItem.tsx @@ -80,7 +80,6 @@ import {isEmptyObject, isEmptyValueObject} from '@src/types/utils/EmptyObject'; import type {GestureResponderEvent, TextInput} from 'react-native'; import type {OnyxEntry} from 'react-native-onyx'; -import {useRecyclingEffect} from '@legendapp/list/react-native'; import {useNavigation} from '@react-navigation/native'; import {isTrackIntentUserSelector} from '@selectors/Onboarding'; import {personalDetailsDisplayNameSelector} from '@selectors/PersonalDetails'; @@ -219,10 +218,7 @@ function ReportActionItem({ const {isActiveReportAction: isActiveReactionListReportAction, hideReactionList} = useContext(ReactionListContext); const {updateHiddenAttachments} = useContext(AttachmentModalContext); const popoverAnchorRef = useRef>(null); - const downloadedPreviews = useRef([]); - useRecyclingEffect(() => { - downloadedPreviews.current = []; - }); + const downloadedPreviews = useRef<{reportActionID: string; urls: string[]}>({reportActionID: action.reportActionID, urls: []}); const isReportActionLinked = linkedReportActionID && action.reportActionID && linkedReportActionID === action.reportActionID; const [isReportActionActive, setIsReportActionActive] = useReportActionItemState(!!isReportActionLinked); @@ -342,11 +338,14 @@ function ReportActionItem({ } const urls = extractLinksFromMessageHtml(action); - if (deepEqual(downloadedPreviews.current, urls) || action.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) { + if ( + (downloadedPreviews.current.reportActionID === action.reportActionID && deepEqual(downloadedPreviews.current.urls, urls)) || + action.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE + ) { return; } - downloadedPreviews.current = urls; + downloadedPreviews.current = {reportActionID: action.reportActionID, urls}; expandURLPreview(reportID, action.reportActionID); }, [action, reportID]); diff --git a/tests/unit/ThumbnailImageTest.tsx b/tests/unit/ThumbnailImageTest.tsx new file mode 100644 index 000000000000..e98a1b0284b4 --- /dev/null +++ b/tests/unit/ThumbnailImageTest.tsx @@ -0,0 +1,61 @@ +import {act, render} from '@testing-library/react-native'; + +import ThumbnailImage from '@components/ThumbnailImage'; + +import React from 'react'; + +const mockUseThumbnailDimensions = jest.fn((width: number, height: number) => ({thumbnailDimensionsStyles: {width, height}})); +let mockOnMeasure: ((dimensions: {width: number; height: number}) => void) | undefined; + +jest.mock('@hooks/useLazyAsset', () => ({useMemoizedLazyExpensifyIcons: () => ({})})); +jest.mock('@hooks/useNetwork', () => () => ({isOffline: false})); +jest.mock('@hooks/useStyleUtils', () => () => ({})); +jest.mock('@hooks/useTheme', () => () => ({border: '#000'})); +jest.mock('@hooks/useThemeStyles', () => () => ({})); +jest.mock('@hooks/useThumbnailDimensions', () => (width: number, height: number) => mockUseThumbnailDimensions(width, height)); +jest.mock('@components/ImageWithSizeCalculation', () => ({ + __esModule: true, + default: ({onMeasure}: {onMeasure: typeof mockOnMeasure}) => { + mockOnMeasure = onMeasure; + return null; + }, +})); + +describe('ThumbnailImage', () => { + beforeEach(() => { + mockUseThumbnailDimensions.mockClear(); + mockOnMeasure = undefined; + }); + + it('uses dimensions for the current URL when a recycled row shows another image', () => { + // Given a measured thumbnail for the first image. + const view = render( + , + ); + act(() => mockOnMeasure?.({width: 400, height: 100})); + expect(mockUseThumbnailDimensions).toHaveBeenLastCalledWith(400, 100); + + // When the same component instance is reused for a second image. + view.rerender( + , + ); + + // Then the second image starts with its own dimensions, rather than the first image's. + expect(mockUseThumbnailDimensions).toHaveBeenLastCalledWith(200, 200); + + // When the first image returns, its measured dimensions are restored from the URL cache. + view.rerender( + , + ); + expect(mockUseThumbnailDimensions).toHaveBeenLastCalledWith(400, 100); + }); +}); From a8fc4fad1e164ab663364a587844d99d215c4e73 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 24 Sep 2026 14:28:54 +0200 Subject: [PATCH 18/61] fix: keep highlighted final action in view --- src/hooks/useReportActionsScroll.ts | 5 ++++- tests/unit/useReportActionsScrollTest.tsx | 26 ++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/hooks/useReportActionsScroll.ts b/src/hooks/useReportActionsScroll.ts index 3f5ce23335be..5417ac6e6aa9 100644 --- a/src/hooks/useReportActionsScroll.ts +++ b/src/hooks/useReportActionsScroll.ts @@ -345,7 +345,10 @@ function useReportActionsScroll({ let initialScrollIndexParams: {viewPosition?: number; viewOffset?: number} | undefined; if (targetIndex >= 0) { initialScrollIndex = targetIndex; - initialScrollIndexParams = {viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}; + // The final action can be taller than the viewport. Aligning its top can leave the + // highlighted content below the screen; at the tail, show the action's end instead. + initialScrollIndexParams = + targetIndex === renderedVisibleReportActions.length - 1 ? {viewPosition: 1, viewOffset: 0} : {viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}; } else if (shouldFocusToTopOnMount) { initialScrollIndex = 0; } diff --git a/tests/unit/useReportActionsScrollTest.tsx b/tests/unit/useReportActionsScrollTest.tsx index 118361255128..f73c3d40d140 100644 --- a/tests/unit/useReportActionsScrollTest.tsx +++ b/tests/unit/useReportActionsScrollTest.tsx @@ -278,12 +278,25 @@ describe('useReportActionsScroll', () => { expect(result.current.shouldBeAlignedToTop).toBe(true); }); - it('positions a linked report action at chronological index zero', async () => { + it('bottom-aligns a linked report action when it is the final item', async () => { mockRouteParams = {reportActionID: LINKED_ACTION_ID}; const linkedAction = makeAction(LINKED_ACTION_ID); const {result} = await renderScroll({sortedVisibleReportActions: [linkedAction], renderedVisibleReportActions: [linkedAction]}); + expect(result.current.initialScrollIndex).toBe(0); + expect(result.current.initialScrollIndexParams).toEqual({viewPosition: 1, viewOffset: 0}); + }); + + it('top-aligns a linked report action when later actions follow it', async () => { + // Given a linked action followed by another action in chronological order. + mockRouteParams = {reportActionID: LINKED_ACTION_ID}; + const actions = [makeAction(LINKED_ACTION_ID), makeAction('999')]; + + // When the list chooses its initial position. + const {result} = await renderScroll({sortedVisibleReportActions: actions.toReversed(), renderedVisibleReportActions: actions}); + + // Then the linked action is aligned below the header. expect(result.current.initialScrollIndex).toBe(0); expect(result.current.initialScrollIndexParams).toEqual({viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}); }); @@ -330,6 +343,17 @@ describe('useReportActionsScroll', () => { expect(result.current.shouldBeAlignedToTop).toBe(true); }); + + it('does not clear the latest route parameter when it was never set', async () => { + // Given an ordinary report open without the X Replies flag. + mockRouteParams = {}; + + // When its scroll hook mounts. + await renderScroll(); + + // Then it leaves unrelated route parameters alone. + expect(mockSetParams).not.toHaveBeenCalledWith({shouldScrollToLatest: undefined}); + }); }); describe('scrollToBottomAndMarkReportAsRead', () => { From 6e7f0455b710029c42fbbf99d9c8d36a7fe29858 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 24 Sep 2026 14:29:14 +0200 Subject: [PATCH 19/61] test: verify unread and read thresholds from saved offset --- tests/unit/ReportActionsListThresholdTest.tsx | 47 +++++++++++-------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/tests/unit/ReportActionsListThresholdTest.tsx b/tests/unit/ReportActionsListThresholdTest.tsx index 61c2508361be..4408d9f440cb 100644 --- a/tests/unit/ReportActionsListThresholdTest.tsx +++ b/tests/unit/ReportActionsListThresholdTest.tsx @@ -38,19 +38,14 @@ type ScrollEvent = { }; }; type CapturedListProps = { - maintainVisibleContentPosition?: boolean | {data: boolean}; onScroll?: (event: ScrollEvent) => void; }; -// Capture the props the list is rendered with so we can verify data anchoring remains enabled while the -// list crosses the visible-action threshold. let capturedListProps: CapturedListProps = {}; - -// Whether the captured LegendList configuration enables data-based maintain-visible-content-position. -function isMvcpEnabled() { - const config = capturedListProps.maintainVisibleContentPosition; - return config === true || (typeof config === 'object' && config.data); -} +jest.mock('@hooks/useUnreadMarker', () => jest.fn(() => ({unreadMarkerReportActionID: null}))); +jest.mock('@hooks/useMarkAsRead', () => jest.fn(() => ({markNewestActionAsRead: jest.fn(), completeSkippedMarkAsRead: jest.fn()}))); +const mockUseUnreadMarker: jest.Mock = jest.requireMock('@hooks/useUnreadMarker'); +const mockUseMarkAsRead: jest.Mock = jest.requireMock('@hooks/useMarkAsRead'); jest.mock('@legendapp/list/react-native', () => { const {forwardRef} = jest.requireActual('react'); @@ -128,12 +123,14 @@ async function renderList(initialOffset: number) { , ); - await waitFor(() => expect(capturedListProps.maintainVisibleContentPosition).toBeDefined()); + await waitFor(() => expect(capturedListProps.onScroll).toBeDefined()); return utils; } beforeEach(async () => { capturedListProps = {}; + mockUseUnreadMarker.mockClear(); + mockUseMarkAsRead.mockClear(); setHasRadio(true); wrapOnyxWithWaitForBatchedUpdates(Onyx); await act(async () => { @@ -158,31 +155,43 @@ afterEach(async () => { await waitForBatchedUpdates(); }); -describe('ReportActionsList maintainVisibleContentPosition', () => { - it('enables data anchoring on first render when mounted while scrolled past the threshold', async () => { +describe('ReportActionsList visible-action threshold', () => { + it('seeds unread marker and mark-as-read state from a saved offset past the threshold', async () => { + // Given a report restored away from the newest action. await renderList(THRESHOLD + 50); - expect(isMvcpEnabled()).toBe(true); + // Then neither consumer treats the report as scrolled to the end before onScroll fires. + expect(mockUseUnreadMarker).toHaveBeenLastCalledWith(expect.objectContaining({isScrolledOverThreshold: true})); + expect(mockUseMarkAsRead).toHaveBeenLastCalledWith(expect.objectContaining({isScrolledToEnd: false})); }); - it('enables data anchoring at the bottom so initial hydration preserves the visible tail', async () => { + it('seeds both consumers as being at the newest action when the saved offset is zero', async () => { + // Given a report restored at its newest action. await renderList(0); - expect(isMvcpEnabled()).toBe(true); + // Then both consumers receive the bottom state immediately. + expect(mockUseUnreadMarker).toHaveBeenLastCalledWith(expect.objectContaining({isScrolledOverThreshold: false})); + expect(mockUseMarkAsRead).toHaveBeenLastCalledWith(expect.objectContaining({isScrolledToEnd: true})); }); - it('keeps data anchoring enabled as the user scrolls across the threshold', async () => { + it('updates both consumers when scrolling across the threshold', async () => { + // Given a report initially at the newest action. await renderList(0); - expect(isMvcpEnabled()).toBe(true); + // When the reader scrolls away from the newest action. act(() => { capturedListProps.onScroll?.(createScrollEvent(THRESHOLD + 50)); }); - expect(isMvcpEnabled()).toBe(true); + // Then the unread marker sees the reader as away and mark-as-read pauses. + expect(mockUseUnreadMarker).toHaveBeenLastCalledWith(expect.objectContaining({isScrolledOverThreshold: true})); + expect(mockUseMarkAsRead).toHaveBeenLastCalledWith(expect.objectContaining({isScrolledToEnd: false})); + // When the reader returns to the newest action. act(() => { capturedListProps.onScroll?.(createScrollEvent(0)); }); - expect(isMvcpEnabled()).toBe(true); + // Then both consumers see the bottom state again. + expect(mockUseUnreadMarker).toHaveBeenLastCalledWith(expect.objectContaining({isScrolledOverThreshold: false})); + expect(mockUseMarkAsRead).toHaveBeenLastCalledWith(expect.objectContaining({isScrolledToEnd: true})); }); }); From ca78b551b28106208b500850fdee8ba0e371e210 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 24 Sep 2026 14:29:36 +0200 Subject: [PATCH 20/61] refactor: share web report scroll-to-end implementation --- src/hooks/useReportScrollManager/index.ts | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/hooks/useReportScrollManager/index.ts b/src/hooks/useReportScrollManager/index.ts index 09ce64a5687c..9eb1ff0e1650 100644 --- a/src/hooks/useReportScrollManager/index.ts +++ b/src/hooks/useReportScrollManager/index.ts @@ -28,17 +28,7 @@ function useReportScrollManager(): ReportScrollManagerData { listRef.current.scrollToEnd({animated: false}); }; - /** - * Scroll to the end of the FlatList. - */ - const scrollToEnd = () => { - const listRef = getListRef(); - if (!listRef?.current) { - return; - } - - listRef.current.scrollToEnd({animated: false}); - }; + const scrollToEnd = scrollToBottom; const scrollToOffset = (offset: number) => { const listRef = getListRef(); From 59a539746a196b4cf34d0874c4b36577e5787780 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 24 Sep 2026 14:29:59 +0200 Subject: [PATCH 21/61] docs: note Android visible-position patch scope --- patches/react-native/details.md | 1 + 1 file changed, 1 insertion(+) diff --git a/patches/react-native/details.md b/patches/react-native/details.md index b475827ad5f6..63f477cb5fc9 100644 --- a/patches/react-native/details.md +++ b/patches/react-native/details.md @@ -13,6 +13,7 @@ 1. Changes when the first visible view is calculated - now happens on scroll events instead of during Fabric's willMountItems lifecycle, which was causing incorrect updates 2. Improves first visible view selection logic to handle Fabric's z-index-based view reordering by finding the view with the smallest position that's still greater than the scroll position 3. Preserves a positioned, zero-sized first child as a scroll anchor. LegendList moves this anchor to compensate for item measurements. Selecting its surrounding container or rejecting its empty frame prevents native scroll compensation and makes the chat jump as estimated rows shrink. +- Scope: This changes Android `MaintainVisibleContentPosition` for every React Native scroll view that enables it, not only the report-action list. Keep this in mind when updating React Native or investigating anchoring in other scroll views. - Upstream PR/issue: https://github.com/facebook/react-native/pull/46247 (closed without merging; the zero-sized LegendList anchor fix is not covered there) - E/App issue: 🛑 - PR Introducing Patch: https://github.com/Expensify/App/pull/46315 (introduced), https://github.com/Expensify/App/pull/45289 (refactored) From d2f2cc89eef435493614f6a5da202def20deda53 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 24 Sep 2026 14:35:19 +0200 Subject: [PATCH 22/61] fix: scroll after edited action layout settles --- .../ReportActionCompose/useEditMessage.ts | 16 +++++---- tests/unit/hooks/useEditMessage.test.ts | 33 +++++++++++++++++++ 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/pages/inbox/report/ReportActionCompose/useEditMessage.ts b/src/pages/inbox/report/ReportActionCompose/useEditMessage.ts index a799717b18d4..691d3e68322f 100644 --- a/src/pages/inbox/report/ReportActionCompose/useEditMessage.ts +++ b/src/pages/inbox/report/ReportActionCompose/useEditMessage.ts @@ -60,17 +60,19 @@ function useEditMessage({ clearAllReportActionDrafts(); - // Scroll to the last comment after editing to make sure the whole comment is clearly visible in the report. + // Wait for the editor to close and the restored action row to lay out before scrolling. + // Scrolling against the editor's old height can leave the final action below the viewport. if (!shouldScrollToLastMessage) { return; } - if (scrollToLastMessage) { - scrollToLastMessage(); - return; - } - - reportScrollManager.scrollToBottom(); + requestAnimationFrame(() => { + if (scrollToLastMessage) { + scrollToLastMessage(); + return; + } + reportScrollManager.scrollToBottom(); + }); } /** diff --git a/tests/unit/hooks/useEditMessage.test.ts b/tests/unit/hooks/useEditMessage.test.ts index 0e27b60b99f4..10b043ba23b5 100644 --- a/tests/unit/hooks/useEditMessage.test.ts +++ b/tests/unit/hooks/useEditMessage.test.ts @@ -90,17 +90,32 @@ function makeDebouncedValidator({flushResult}: {flushResult: boolean}): Debounce } describe('useEditMessage', () => { + let pendingAnimationFrame: FrameRequestCallback | undefined; + beforeAll(() => { Onyx.init({keys: ONYXKEYS}); }); + beforeEach(() => { + pendingAnimationFrame = undefined; + jest.spyOn(global, 'requestAnimationFrame').mockImplementation((callback) => { + pendingAnimationFrame = callback; + return 1; + }); + }); + afterEach(async () => { await act(async () => { await Onyx.clear(); }); jest.clearAllMocks(); + jest.restoreAllMocks(); }); + function finishActionLayout() { + act(() => pendingAnimationFrame?.(0)); + } + function renderUseEditMessage(overrides?: Partial) { const report = LHNTestUtils.getFakeReport(); const reportAction = LHNTestUtils.getFakeReportAction(); @@ -154,6 +169,8 @@ describe('useEditMessage', () => { mockShowDeleteModal.mock.calls.at(0)?.[3]?.(); }); + expect(mockScrollToBottom).not.toHaveBeenCalled(); + finishActionLayout(); expect(mockScrollToBottom).toHaveBeenCalledTimes(1); }); @@ -168,10 +185,26 @@ describe('useEditMessage', () => { mockShowDeleteModal.mock.calls.at(0)?.[3]?.(); }); + expect(scrollToLastMessage).not.toHaveBeenCalled(); + finishActionLayout(); expect(scrollToLastMessage).toHaveBeenCalledTimes(1); expect(mockScrollToBottom).not.toHaveBeenCalled(); }); + it('scrolls to the final saved action after the editor layout has been replaced', () => { + // Given a draft on the newest action. + const {hook} = renderUseEditMessage({shouldScrollToLastMessage: true}); + + // When the edit is submitted, its optimistic update is queued before layout settles. + act(() => hook.result.current.publishDraft('Updated message')); + expect(mockEditReportComment).toHaveBeenCalledTimes(1); + expect(mockScrollToBottom).not.toHaveBeenCalled(); + + // Then scrolling runs after the restored action row gets a layout frame. + finishActionLayout(); + expect(mockScrollToBottom).toHaveBeenCalledTimes(1); + }); + it('does not scroll after deleting a non-newest message draft', () => { const scrollToLastMessage = jest.fn(); const {hook} = renderUseEditMessage({shouldScrollToLastMessage: false, scrollToLastMessage}); From 08e1f69196d938bac6d625a1241681f84975468d Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 24 Sep 2026 14:35:39 +0200 Subject: [PATCH 23/61] test: type captured list load callback --- tests/ui/ReportActionsListTest.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 5148b4c108af..e1c0381e0e5e 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -531,7 +531,8 @@ describe('ReportActionsList (body)', () => { // Given a report with its action list ready to mount. mockShouldCallLegendListOnLoad = false; renderReportActionsList(); - const onLoad = mockUseReportActionsScroll.mock.results.at(-1)?.value.onLoad; + const scrollHookResult = mockUseReportActionsScroll.mock.results.at(-1)?.value as {onLoad?: jest.Mock} | undefined; + const onLoad = scrollHookResult?.onLoad; expect(onLoad).not.toHaveBeenCalled(); // When LegendList reports that its first layout is complete. From 84b0f1913322e67109be6f9cc01818fe6c7a091d Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 24 Sep 2026 14:46:42 +0200 Subject: [PATCH 24/61] fix: preserve Concierge tail when revealing history --- src/pages/inbox/report/ReportActionsList.tsx | 19 +++- tests/ui/ReportActionsListTest.tsx | 91 +++++++++++++++++--- 2 files changed, 96 insertions(+), 14 deletions(-) diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index 8353a1599357..df6ac95356c2 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -260,9 +260,15 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct const {clearDraft, revealDraftFromReportAction} = useConciergeDraftActions(); const showHiddenHistory = isConciergeHiddenHistory && !showFullHistory; - const onShowPreviousMessages = handleShowPreviousMessages; const [hasScrolledOverThreshold, setHasScrolledOverThreshold] = useState(() => getScrollOffset() >= CONST.REPORT.ACTIONS.ACTION_VISIBLE_THRESHOLD); + const shouldRestoreTailAfterHistoryRevealRef = useRef(false); + const onShowPreviousMessages = () => { + // A short Concierge session has no scrollable range. Revealing its older actions makes + // the list scrollable, so preserve the tail the reader was looking at before the reveal. + shouldRestoreTailAfterHistoryRevealRef.current = !hasScrolledOverThreshold && !hasNewerActions; + handleShowPreviousMessages(); + }; const {unreadMarkerReportActionID} = useUnreadMarker({ reportID, @@ -329,6 +335,17 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct // give it chronological data and use its normal start/end and scrolling semantics. const listData = reportActionsToRender.toReversed(); + useEffect(() => { + if (!showFullHistory || !shouldRestoreTailAfterHistoryRevealRef.current) { + return; + } + const animationFrame = requestAnimationFrame(() => { + legendListRef.current?.scrollToEnd({animated: false}); + shouldRestoreTailAfterHistoryRevealRef.current = false; + }); + return () => cancelAnimationFrame(animationFrame); + }, [showFullHistory, listData.length]); + const draftMessageHTML = draftReportAction ? getReportActionMessage(draftReportAction)?.html : undefined; const draftReportActionID = draftReportAction?.reportActionID; const isSyntheticDraftVisible = !!draftReportAction && renderedVisibleReportActions !== sortedVisibleReportActions; diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index e1c0381e0e5e..1877cab4398f 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -141,22 +141,25 @@ const mockUseCurrentUserPersonalDetails = useCurrentUserPersonalDetails as jest. // content is never mounted, so useMarkAsRead/useReportActionsScroll are never called. const mockLegendListMount = jest.fn(); const mockLegendListUnmount = jest.fn(); +const mockLegendScrollToEnd = jest.fn(); let mockShouldCallLegendListOnLoad = true; jest.mock('@legendapp/list/react-native', () => { const reactModule = jest.requireActual('react'); + const MockLegendListContent = reactModule.forwardRef<{scrollToEnd: typeof mockLegendScrollToEnd}, {onLoad?: () => void}>(({onLoad}, ref) => { + reactModule.useImperativeHandle(ref, () => ({scrollToEnd: mockLegendScrollToEnd}), []); + reactModule.useEffect(() => { + mockLegendListMount(); + if (mockShouldCallLegendListOnLoad) { + onLoad?.(); + } + return () => { + mockLegendListUnmount(); + }; + }, []); + return null; + }); return { - LegendList: jest.fn(({onLoad}: {onLoad?: () => void}) => { - reactModule.useEffect(() => { - mockLegendListMount(); - if (mockShouldCallLegendListOnLoad) { - onLoad?.(); - } - return () => { - mockLegendListUnmount(); - }; - }, []); - return null; - }), + LegendList: jest.fn((props: {onLoad?: () => void; ref?: React.Ref<{scrollToEnd: typeof mockLegendScrollToEnd}>}) => reactModule.createElement(MockLegendListContent, props)), }; }); jest.mock('@hooks/useUnreadMarker', () => jest.fn(() => ({unreadMarkerReportActionID: null, unreadMarkerReportActionIndex: -1}))); @@ -242,6 +245,17 @@ const getRenderedReportActionsListItemProps = ( return child.props; }; +const getShowHistoryPress = (action: OnyxTypes.ReportAction, index: number): (() => void) | undefined => { + const renderedItem = getCapturedListProps()?.renderItem?.({item: action, index}); + if (!React.isValidElement<{children: React.ReactNode}>(renderedItem)) { + return undefined; + } + const historyButton = React.Children.toArray(renderedItem.props.children).find( + (child): child is React.ReactElement<{onPress: () => void}> => React.isValidElement<{onPress?: () => void}>(child) && typeof child.props.onPress === 'function', + ); + return historyButton?.props.onPress; +}; + const mockUseMarkAsRead: jest.Mock = jest.requireMock('@hooks/useMarkAsRead'); const mockUseReportActionsScroll: jest.Mock = jest.requireMock('@hooks/useReportActionsScroll'); const mockUseUnreadMarker: jest.Mock = jest.requireMock('@hooks/useUnreadMarker'); @@ -741,7 +755,7 @@ describe('ReportActionsList (body)', () => { }); expect(mockAllowLegendListItemOverflow).toHaveBeenCalledTimes(1); - expect(mockAllowLegendListItemOverflow).toHaveBeenCalledWith(null, 1); + expect(mockAllowLegendListItemOverflow).toHaveBeenCalledWith(expect.objectContaining({scrollToEnd: mockLegendScrollToEnd}), 1); }); it('groups comments by layout characteristics for measurement estimates', () => { @@ -1587,6 +1601,57 @@ describe('ReportActionsList (body)', () => { expect(passedActions?.some((a) => a.reportActionID === 'old-concierge-msg')).toBe(true); }); + it('keeps a short Concierge session at its visible tail when Show History reveals older actions', () => { + // Given a short session at its latest action, with history filtered out. + setupMainDMConciergeMocks(); + let isShowingFullHistory = false; + mockUseConciergeSessionState.mockImplementation(() => ({sessionStartTime: SESSION_START, showFullHistory: isShowingFullHistory, hadMessagesAtSessionStart: false})); + mockUseConciergeSessionActions.mockReturnValue({ + startSession: jest.fn(), + setShowFullHistory: (show: boolean) => { + isShowingFullHistory = show; + }, + setHadMessagesAtSessionStart: jest.fn(), + }); + mockUsePaginatedReportActions.mockReturnValue({...defaultPaginatedReportActionsResult, reportActions: oldReportActions, hasOlderActions: false}); + const animationFrames: FrameRequestCallback[] = []; + const requestAnimationFrameSpy = jest.spyOn(global, 'requestAnimationFrame').mockImplementation((callback) => { + animationFrames.push(callback); + return animationFrames.length; + }); + + try { + const view = renderReportActionsList({reportID: CONCIERGE_REPORT_ID}); + const actions = getCapturedVisibleActions(); + const createdIndex = actions?.findIndex((action) => action.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED) ?? -1; + const createdAction = actions?.at(createdIndex); + if (!createdAction) { + throw new Error('Expected the Concierge created action'); + } + + // When the reader reveals the older history. + act(() => getShowHistoryPress(createdAction, createdIndex)?.()); + view.rerender( + , + ); + act(() => { + for (const callback of animationFrames) { + callback(0); + } + }); + + // Then all actions are available while the latest session stays in view. + expect(getCapturedVisibleActions()?.some((action) => action.reportActionID === 'old-user-msg')).toBe(true); + expect(mockLegendScrollToEnd).toHaveBeenCalledWith({animated: false}); + } finally { + requestAnimationFrameSpy.mockRestore(); + } + }); + it('should show all actions unfiltered when user sends a message in current session', () => { setupMainDMConciergeMocks(); From a1e0d25134114f765d908ee066b6389e901219aa Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 24 Sep 2026 14:52:07 +0200 Subject: [PATCH 25/61] test: remove nonvirtualized report list benchmark --- .../perf-test/ReportActionsList.perf-test.tsx | 131 ------------------ 1 file changed, 131 deletions(-) delete mode 100644 tests/perf-test/ReportActionsList.perf-test.tsx diff --git a/tests/perf-test/ReportActionsList.perf-test.tsx b/tests/perf-test/ReportActionsList.perf-test.tsx deleted file mode 100644 index a1ab104f6a2b..000000000000 --- a/tests/perf-test/ReportActionsList.perf-test.tsx +++ /dev/null @@ -1,131 +0,0 @@ -import {act, screen} from '@testing-library/react-native'; - -import OnyxListItemProvider from '@components/OnyxListItemProvider'; - -import type Navigation from '@libs/Navigation/Navigation'; -import navigationRef from '@libs/Navigation/navigationRef'; -import {setHasRadio} from '@libs/NetworkState'; - -import {ActionListContext} from '@pages/inbox/ActionListContext'; -import {ReactionListContext} from '@pages/inbox/ReactionListContext'; -import ReportActionsList from '@pages/inbox/report/ReportActionsList'; -import {AttachmentModalContextProvider} from '@pages/media/AttachmentModalScreen/AttachmentModalContext'; - -import ComposeProviders from '@src/components/ComposeProviders'; -import {LocaleContextProvider} from '@src/components/LocaleContextProvider'; -import CONST from '@src/CONST'; -import ONYXKEYS from '@src/ONYXKEYS'; -import type {ReportAction, ReportActions} from '@src/types/onyx'; - -import {NavigationContainer} from '@react-navigation/native'; -import Onyx from 'react-native-onyx'; -import {measureRenders} from 'reassure'; - -import * as ReportTestUtils from '../utils/ReportTestUtils'; -import * as TestHelper from '../utils/TestHelper'; -import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; -import wrapOnyxWithWaitForBatchedUpdates from '../utils/wrapOnyxWithWaitForBatchedUpdates'; - -const mockedNavigate = jest.fn(); - -jest.mock('@react-navigation/native', () => { - const actualNav = jest.requireActual('@react-navigation/native'); - return { - ...actualNav, - useRoute: () => mockedNavigate, - useIsFocused: () => true, - }; -}); - -beforeAll(() => - Onyx.init({ - keys: ONYXKEYS, - evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS], - }), -); - -const mockOnLayout = jest.fn(); -// Built via a function so the value isn't an inline literal the context-split lint rule would flag; these are all refs/accessors with no re-render concern. -function buildActionListContextValue() { - return {scrollOffsetRef: {current: 0}, getScrollOffset: () => 0, registerListRef: () => {}, getListRef: () => null}; -} -const actionListContextValue = buildActionListContextValue(); -const mockReactionListContextValue = { - showReactionList: () => {}, - hideReactionList: () => {}, - isActiveReportAction: () => false, -}; - -const TEST_USER_ACCOUNT_ID = 1; -const TEST_USER_LOGIN = 'test@test.com'; -const REPORT_ID = '1'; - -const signUpWithTestUser = () => { - TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN); -}; - -const sortedReportActions = ReportTestUtils.getMockedSortedReportActions(500); -const reportActions: ReportActions = Object.fromEntries(sortedReportActions.map((action: ReportAction) => [action.reportActionID, action])); -const report = ReportTestUtils.createMockReport({reportID: REPORT_ID, lastVisibleActionCreated: sortedReportActions.at(0)?.created}); - -beforeEach(async () => { - // Initialize the network key for OfflineWithFeedback - setHasRadio(true); - wrapOnyxWithWaitForBatchedUpdates(Onyx); - // Pre-seed the locale so LocaleContextProvider's mount effect is a no-op (setLocale early-returns), - // avoiding post-mount Onyx writes that would re-render outside act(). - await act(async () => { - signUpWithTestUser(); - await Onyx.merge(ONYXKEYS.NVP_PREFERRED_LOCALE, CONST.LOCALES.DEFAULT); - - // Seed the report under test: the report, its 500 actions, and a settled loading state. - await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report); - await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, reportActions); - await Onyx.set(`${ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE}${REPORT_ID}`, { - isLoadingInitialReportActions: false, - hasOnceLoadedReportActions: true, - isLoadingOlderReportActions: false, - hasLoadingOlderReportActionsError: false, - isLoadingNewerReportActions: false, - hasLoadingNewerReportActionsError: false, - }); - await waitForBatchedUpdates(); - }); -}); - -afterEach(async () => { - // Await the clear so its broadcasts settle in teardown instead of leaking into the next test. - await Onyx.clear(); - await waitForBatchedUpdates(); -}); - -// Mirror the production data flow (usePaginatedReportActions): the body reads REPORT_ACTIONS from Onyx and -// sorts inside a useOnyx selector, then runs it through the real continuous-chain pagination and the -// visibility filter. -function ReportActionsListWrapper() { - return ( - - - - - - - - - - ); -} - -// LegendList's behavior mock eagerly renders the supplied data, while the previous FlashList test used -// its virtualized implementation. Start a renderer-specific baseline instead of comparing those harnesses. -test('[ReportActionsList] should render LegendList with 500 reportActions stored', async () => { - const scenario = async () => { - await screen.findByTestId('report-actions-list'); - }; - await waitForBatchedUpdates(); - await measureRenders(, {scenario}); -}); From e37edb4ddb51e0bf29c06de403c80f00e7bfc512 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 24 Sep 2026 14:56:57 +0200 Subject: [PATCH 26/61] test: mock edited-action draft clearing --- tests/unit/hooks/useEditMessage.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/hooks/useEditMessage.test.ts b/tests/unit/hooks/useEditMessage.test.ts index 10b043ba23b5..01e2081db571 100644 --- a/tests/unit/hooks/useEditMessage.test.ts +++ b/tests/unit/hooks/useEditMessage.test.ts @@ -21,7 +21,7 @@ jest.mock('@libs/actions/Report', () => { return { ...actual, editReportComment: jest.fn(), - clearReportActionDrafts: jest.fn(), + clearAllReportActionDrafts: jest.fn(), }; }); From e904b9988930140ad2678688ed965bca0f0ff825 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 24 Sep 2026 15:53:10 +0200 Subject: [PATCH 27/61] test: wait for edited action scroll frame --- tests/ui/ReportActionItemMessageEditTest.tsx | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/ui/ReportActionItemMessageEditTest.tsx b/tests/ui/ReportActionItemMessageEditTest.tsx index e7468c15412d..2d92a8f4d0ac 100644 --- a/tests/ui/ReportActionItemMessageEditTest.tsx +++ b/tests/ui/ReportActionItemMessageEditTest.tsx @@ -1,4 +1,4 @@ -import {act, fireEvent, render, screen} from '@testing-library/react-native'; +import {act, fireEvent, render, screen, waitFor} from '@testing-library/react-native'; import ComposeProviders from '@components/ComposeProviders'; import {LocaleContextProvider} from '@components/LocaleContextProvider'; @@ -219,14 +219,17 @@ describe('ReportActionCompose Integration Tests', () => { expect(videoAttributeCache?.[videoSource]).toContain('data-expensify-width'); }); - it('should use the list-specific scroll after saving the newest message', () => { + it('should use the list-specific scroll after saving the newest message', async () => { + // Given the newest message is being edited in a report list const scrollToNewestAction = jest.fn(); renderReportActionItemMessageEdit(undefined, {index: 9, isNewest: true, scrollToNewestAction}); + // When the edit is saved and the restored message row has time to lay out fireEvent.changeText(screen.getByTestId('composer'), 'Edited message'); fireEvent.press(screen.getByLabelText('common.saveChanges')); - expect(scrollToNewestAction).toHaveBeenCalledTimes(1); + // Then the list scrolls to the newest action after the scheduled frame + await waitFor(() => expect(scrollToNewestAction).toHaveBeenCalledTimes(1)); }); it('should not scroll after saving a non-newest message at index zero', () => { From 40f77197ba69984ff495e641c012e18ec296a7f9 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 24 Sep 2026 16:18:45 +0200 Subject: [PATCH 28/61] chore: update LegendList to 3.4.0 --- package-lock.json | 8 ++++---- package.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index b2c6deb5b564..f84e7ac909ce 100644 --- a/package-lock.json +++ b/package-lock.json @@ -33,7 +33,7 @@ "@fullstory/react-native": "^1.10.2", "@gorhom/portal": "^1.0.14", "@invertase/react-native-apple-authentication": "^2.5.0", - "@legendapp/list": "^3.3.10", + "@legendapp/list": "^3.4.0", "@lottiefiles/dotlottie-react": "0.13.5", "@onfido/react-native-sdk": "15.1.0", "@pusher/pusher-websocket-react-native": "^1.3.1", @@ -12109,9 +12109,9 @@ "license": "MIT" }, "node_modules/@legendapp/list": { - "version": "3.3.10", - "resolved": "https://registry.npmjs.org/@legendapp/list/-/list-3.3.10.tgz", - "integrity": "sha512-S8cwV11oJJD2m47JoRnCmlgbUrnY/f48TPL1zl1C3wwyrvru53Zf1NtYFlMtAU6lUKh/cU9RIffLI7unbf3kmg==", + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/@legendapp/list/-/list-3.4.0.tgz", + "integrity": "sha512-Tlx7Uf9pRP/JbrekuWdKf1+42C9MTfl1wGkP8Lvmg2E2T9rCWQphYOs8aOkcvgbAkJ2S+WR+RTXHS2+6U+nROw==", "license": "MIT", "dependencies": { "use-sync-external-store": "^1.5.0" diff --git a/package.json b/package.json index da86ff47f9b6..0221d715f35f 100644 --- a/package.json +++ b/package.json @@ -109,7 +109,7 @@ "@fullstory/react-native": "^1.10.2", "@gorhom/portal": "^1.0.14", "@invertase/react-native-apple-authentication": "^2.5.0", - "@legendapp/list": "^3.3.10", + "@legendapp/list": "^3.4.0", "@lottiefiles/dotlottie-react": "0.13.5", "@onfido/react-native-sdk": "15.1.0", "@pusher/pusher-websocket-react-native": "^1.3.1", From a68ee3c024232addfd13a5054f4e1376753a5c7e Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 24 Sep 2026 16:30:08 +0200 Subject: [PATCH 29/61] fix: cover warm report actions during initial layout --- src/pages/inbox/report/ReportActionsList.tsx | 6 ++---- tests/ui/ReportActionsListTest.tsx | 7 +++++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index df6ac95356c2..b26181da4258 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -403,10 +403,8 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct }); const [loadedInitialViewportListID, setLoadedInitialViewportListID] = useState(); - // A warm report already has its complete action page. Only an initial visit needs a cover - // while the first page finishes loading and LegendList positions it. - const [shouldCoverInitialViewport] = useState(!hasOnceLoadedReportActions); - const shouldShowInitialViewportSkeleton = !isOffline && shouldCoverInitialViewport && (isInitialReportLoadPending || loadedInitialViewportListID !== listID); + // Cover every initial LegendList layout, including cached chats, until the rows are positioned. + const shouldShowInitialViewportSkeleton = !isOffline && (isInitialReportLoadPending || loadedInitialViewportListID !== listID); const updateVisibleItemOverflow = (info: OnViewableItemsChangedInfo) => { onViewableItemsChanged(info); diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 1877cab4398f..32580971cb8c 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -556,7 +556,7 @@ describe('ReportActionsList (body)', () => { expect(onLoad).toHaveBeenCalledTimes(1); }); - it('shows a previously hydrated report without replaying the loading cover', () => { + it('covers a previously hydrated report until LegendList finishes its initial layout', () => { // Given a report that was already loaded before this list mounted mockUseNetwork.mockReturnValue({isOffline: false}); mockShouldCallLegendListOnLoad = false; @@ -564,8 +564,11 @@ describe('ReportActionsList (body)', () => { // When the user opens the report again renderReportActionsList(); - // Then cached actions stay visible while LegendList completes its layout + // Then the cover hides LegendList's empty initial viewport until its actions are ready expect(getCapturedVisibleActions()).toHaveLength(mockReportActions.length); + expect(screen.getByTestId('ReportActionsSkeletonCover')).toBeTruthy(); + + act(() => getCapturedListProps()?.onLoad?.()); expect(screen.queryByTestId('ReportActionsSkeletonCover')).toBeNull(); }); From e117ffcbe639bd68a8ff3c49ebf80c84c92afb7f Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Fri, 25 Sep 2026 16:27:06 +0200 Subject: [PATCH 30/61] fix: render Concierge notification banner before chat scrolls to end --- .../notificationPermission/index.native.ts | 1 + .../Notification/notificationPermission/index.ts | 13 +++++++++---- .../Notification/notificationPermission/types.ts | 1 + .../useShouldShowEnableNotificationsBanner.ts | 3 ++- .../useShouldShowEnableNotificationsBanner.test.ts | 12 ++++++++++++ 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/libs/Notification/notificationPermission/index.native.ts b/src/libs/Notification/notificationPermission/index.native.ts index 29123ad37089..bd0fe625309e 100644 --- a/src/libs/Notification/notificationPermission/index.native.ts +++ b/src/libs/Notification/notificationPermission/index.native.ts @@ -1,6 +1,7 @@ import type NotificationPermissionModule from './types'; const NotificationPermissionNative: NotificationPermissionModule = { + getStatusSync: () => 'denied', getStatus: () => Promise.resolve('denied'), request: () => Promise.resolve('denied'), }; diff --git a/src/libs/Notification/notificationPermission/index.ts b/src/libs/Notification/notificationPermission/index.ts index 62f333a376f4..333fecf4bf54 100644 --- a/src/libs/Notification/notificationPermission/index.ts +++ b/src/libs/Notification/notificationPermission/index.ts @@ -10,12 +10,17 @@ function toStatus(permission: NotificationPermission): NotificationPermissionSta return 'default'; } +function getStatusSync(): NotificationPermissionStatus { + if (typeof window === 'undefined' || !window.Notification) { + return 'denied'; + } + return toStatus(Notification.permission); +} + const NotificationPermission: NotificationPermissionModule = { + getStatusSync, getStatus(): Promise { - if (typeof window === 'undefined' || !window.Notification) { - return Promise.resolve('denied'); - } - return Promise.resolve(toStatus(Notification.permission)); + return Promise.resolve(getStatusSync()); }, request(): Promise { diff --git a/src/libs/Notification/notificationPermission/types.ts b/src/libs/Notification/notificationPermission/types.ts index 9ffca38417ae..b6c834f2ed36 100644 --- a/src/libs/Notification/notificationPermission/types.ts +++ b/src/libs/Notification/notificationPermission/types.ts @@ -1,6 +1,7 @@ type NotificationPermissionStatus = 'granted' | 'denied' | 'default'; type NotificationPermissionModule = { + getStatusSync: () => NotificationPermissionStatus; getStatus: () => Promise; request: () => Promise; }; diff --git a/src/pages/inbox/report/useShouldShowEnableNotificationsBanner.ts b/src/pages/inbox/report/useShouldShowEnableNotificationsBanner.ts index 82a875706485..c60c604a7497 100644 --- a/src/pages/inbox/report/useShouldShowEnableNotificationsBanner.ts +++ b/src/pages/inbox/report/useShouldShowEnableNotificationsBanner.ts @@ -14,7 +14,8 @@ import {useEffect, useState} from 'react'; function useShouldShowEnableNotificationsBanner(report: OnyxEntry): boolean { const [conciergeReportID] = useOnyx(ONYXKEYS.CONCIERGE_REPORT_ID); const [hasDismissed] = useOnyx(ONYXKEYS.RAM_ONLY_HAS_DISMISSED_CONCIERGE_NOTIFICATION_BANNER); - const [permissionStatus, setPermissionStatus] = useState(); + // Reserve the banner's space before the report list makes its initial scroll-to-end measurement. + const [permissionStatus, setPermissionStatus] = useState(() => NotificationPermission.getStatusSync()); const isConcierge = isConciergeChatReport(report, conciergeReportID); diff --git a/tests/unit/hooks/useShouldShowEnableNotificationsBanner.test.ts b/tests/unit/hooks/useShouldShowEnableNotificationsBanner.test.ts index 38e5c20713bb..4263e110fcf5 100644 --- a/tests/unit/hooks/useShouldShowEnableNotificationsBanner.test.ts +++ b/tests/unit/hooks/useShouldShowEnableNotificationsBanner.test.ts @@ -20,6 +20,7 @@ let mockCurrentPermission: NotificationPermissionStatus = 'default'; jest.mock('@libs/Notification/notificationPermission', () => ({ __esModule: true, default: { + getStatusSync: () => mockCurrentPermission, getStatus: () => Promise.resolve(mockCurrentPermission), request: () => Promise.resolve(mockCurrentPermission), }, @@ -69,4 +70,15 @@ describe('useShouldShowEnableNotificationsBanner', () => { const {result} = renderHook(() => useShouldShowEnableNotificationsBanner(conciergeReport)); await waitFor(() => expect(result.current).toBe(true)); }); + + it('shows the Concierge banner on the initial render so the chat opens with its final viewport height', () => { + // Given notification permission has not been decided and the Concierge report is already known. + mockCurrentPermission = 'default'; + + // When Concierge opens, the list and footer measure their initial layout together. + const {result} = renderHook(() => useShouldShowEnableNotificationsBanner(conciergeReport)); + + // Then the banner must occupy space before the list positions itself at the latest message. + expect(result.current).toBe(true); + }); }); From 4adbdaabeb64b5d8958d33c46504103a6a59de30 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Fri, 25 Sep 2026 16:37:52 +0200 Subject: [PATCH 31/61] fix: keep latest chat action visible after list measurement --- src/pages/inbox/report/ReportActionsList.tsx | 66 ++++++++++++++- tests/ui/ReportActionsListTest.tsx | 88 +++++++++++++++++++- 2 files changed, 149 insertions(+), 5 deletions(-) diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index b26181da4258..bc725e2c709d 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -91,6 +91,7 @@ type ReportActionsListContentProps = { type ReportActionsListProps = ReportActionsListContentProps; const PAGINATION_THRESHOLD = 0.75; +const MAINTAIN_SCROLL_AT_END_THRESHOLD = 0.01; const REPORT_ACTIONS_DRAW_DISTANCE = 1500; const REPORT_ACTION_COMMENT_SIZE = { @@ -405,6 +406,39 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct const [loadedInitialViewportListID, setLoadedInitialViewportListID] = useState(); // Cover every initial LegendList layout, including cached chats, until the rows are positioned. const shouldShowInitialViewportSkeleton = !isOffline && (isInitialReportLoadPending || loadedInitialViewportListID !== listID); + const readyEndListIDRef = useRef(undefined); + const shouldFollowEndOnResizeRef = useRef(false); + const lastScrollMetricsRef = useRef<{offset: number; contentHeight: number; viewportHeight: number} | undefined>(undefined); + const endCorrectionFrameRef = useRef(undefined); + + const scheduleEndCorrection = () => { + if ( + readyEndListIDRef.current !== listID || + !shouldFollowEndOnResizeRef.current || + hasNewerActions || + initialScrollIndex !== undefined || + endCorrectionFrameRef.current !== undefined + ) { + return; + } + endCorrectionFrameRef.current = requestAnimationFrame(() => { + endCorrectionFrameRef.current = undefined; + if (readyEndListIDRef.current === listID && shouldFollowEndOnResizeRef.current) { + legendListRef.current?.scrollToEnd({animated: false}); + } + }); + }; + + useEffect( + () => () => { + if (endCorrectionFrameRef.current === undefined) { + return; + } + cancelAnimationFrame(endCorrectionFrameRef.current); + endCorrectionFrameRef.current = undefined; + }, + [listID], + ); const updateVisibleItemOverflow = (info: OnViewableItemsChangedInfo) => { onViewableItemsChanged(info); @@ -421,6 +455,14 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct setLoadedInitialViewportListID(listID); }; + const handleListReady = () => { + readyEndListIDRef.current = listID; + shouldFollowEndOnResizeRef.current = initialScrollIndex === undefined && !hasNewerActions; + lastScrollMetricsRef.current = undefined; + // The final row can grow after LegendList's initial end estimate (for example, Concierge feedback). + scheduleEndCorrection(); + }; + const loadOlderChatsOnStartReached = () => { if (showHiddenHistory || isOffline || !hasOlderActions || !oldestReportActionID || lastRequestedOldestActionIDRef.current === oldestReportActionID) { return; @@ -434,6 +476,21 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct const {contentOffset, contentSize, layoutMeasurement} = event.nativeEvent; const distanceFromBottom = Math.max(0, contentSize.height - layoutMeasurement.height - contentOffset.y); const isNearStart = contentOffset.y <= layoutMeasurement.height * PAGINATION_THRESHOLD; + const previousMetrics = lastScrollMetricsRef.current; + if (readyEndListIDRef.current === listID && !hasNewerActions) { + if (distanceFromBottom <= layoutMeasurement.height * MAINTAIN_SCROLL_AT_END_THRESHOLD) { + shouldFollowEndOnResizeRef.current = true; + } else if ( + (previousMetrics && contentOffset.y < previousMetrics.offset - 1) || + (previousMetrics && + previousMetrics.contentHeight === contentSize.height && + previousMetrics.viewportHeight === layoutMeasurement.height && + endCorrectionFrameRef.current === undefined) + ) { + shouldFollowEndOnResizeRef.current = false; + } + } + lastScrollMetricsRef.current = {offset: contentOffset.y, contentHeight: contentSize.height, viewportHeight: layoutMeasurement.height}; if (isNearStart) { loadOlderChatsOnStartReached(); @@ -585,6 +642,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct */ const recordTimeToMeasureItemLayout = (event: LayoutChangeEvent) => { onLayout?.(event); + scheduleEndCorrection(); if (didLayout.current) { return; } @@ -657,10 +715,14 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct // Only follow the real latest page. Older/linked windows must retain their visible anchor. maintainScrollAtEnd={!hasNewerActions && {animated: false}} // Leave the end-follow region as soon as the user starts reading older messages. - maintainScrollAtEndThreshold={0.01} + maintainScrollAtEndThreshold={MAINTAIN_SCROLL_AT_END_THRESHOLD} maintainVisibleContentPosition onLoad={handleListLoad} - onContentSizeChange={() => trackVerticalScrolling(undefined)} + onReady={handleListReady} + onContentSizeChange={() => { + trackVerticalScrolling(undefined); + scheduleEndCorrection(); + }} /> {shouldShowInitialViewportSkeleton && ( { const reactModule = jest.requireActual('react'); - const MockLegendListContent = reactModule.forwardRef<{scrollToEnd: typeof mockLegendScrollToEnd}, {onLoad?: () => void}>(({onLoad}, ref) => { + const MockLegendListContent = reactModule.forwardRef<{scrollToEnd: typeof mockLegendScrollToEnd}, {onLoad?: () => void; onReady?: () => void}>(({onLoad, onReady}, ref) => { reactModule.useImperativeHandle(ref, () => ({scrollToEnd: mockLegendScrollToEnd}), []); reactModule.useEffect(() => { mockLegendListMount(); if (mockShouldCallLegendListOnLoad) { onLoad?.(); + onReady?.(); } return () => { mockLegendListUnmount(); @@ -159,7 +160,9 @@ jest.mock('@legendapp/list/react-native', () => { return null; }); return { - LegendList: jest.fn((props: {onLoad?: () => void; ref?: React.Ref<{scrollToEnd: typeof mockLegendScrollToEnd}>}) => reactModule.createElement(MockLegendListContent, props)), + LegendList: jest.fn((props: {onLoad?: () => void; onReady?: () => void; ref?: React.Ref<{scrollToEnd: typeof mockLegendScrollToEnd}>}) => + reactModule.createElement(MockLegendListContent, props), + ), }; }); jest.mock('@hooks/useUnreadMarker', () => jest.fn(() => ({unreadMarkerReportActionID: null, unreadMarkerReportActionIndex: -1}))); @@ -201,6 +204,7 @@ type MockLegendListProps = { maintainScrollAtEndThreshold?: number; maintainVisibleContentPosition?: boolean; onLoad?: () => void; + onReady?: () => void; onStartReachedThreshold?: number; onContentSizeChange?: (width: number, height: number) => void; onViewableItemsChanged?: (info: OnViewableItemsChangedInfo) => void; @@ -440,6 +444,84 @@ describe('ReportActionsList (body)', () => { expect(getCapturedListProps()?.maintainVisibleContentPosition).toBe(true); }); + it('corrects the initial end position after the latest row has been measured', async () => { + // Given a report opens at its latest action and the virtualized list finishes its initial placement. + mockUseNetwork.mockReturnValue({isOffline: false}); + renderReportActionsList(); + + // When the last row is taller than the estimate, its feedback controls must not remain below the viewport. + // Then the list makes one final end scroll after the layout is ready. + await waitFor(() => expect(mockLegendScrollToEnd).toHaveBeenCalledWith({animated: false})); + }); + + it('keeps the latest message visible when its measured height grows after opening', async () => { + // Given the chat has opened at the end with a measured viewport. + mockUseNetwork.mockReturnValue({isOffline: false}); + renderReportActionsList(); + await waitFor(() => expect(mockLegendScrollToEnd).toHaveBeenCalledWith({animated: false})); + mockLegendScrollToEnd.mockClear(); + const listProps = getCapturedListProps(); + act(() => { + listProps?.onScroll?.({ + nativeEvent: { + contentOffset: {x: 0, y: 500}, + contentSize: {height: 1000, width: 300}, + layoutMeasurement: {height: 500, width: 300}, + }, + }); + }); + + // When a late feedback row makes the content taller without a user scroll. + act(() => { + listProps?.onScroll?.({ + nativeEvent: { + contentOffset: {x: 0, y: 500}, + contentSize: {height: 1025, width: 300}, + layoutMeasurement: {height: 500, width: 300}, + }, + }); + listProps?.onContentSizeChange?.(300, 1025); + }); + + // Then the last 25 pixels are brought into view. + await waitFor(() => expect(mockLegendScrollToEnd).toHaveBeenCalledWith({animated: false})); + }); + + it('does not pull the chat back down when the reader has scrolled away', async () => { + // Given the chat opened at the end and the reader scrolled upward. + mockUseNetwork.mockReturnValue({isOffline: false}); + renderReportActionsList(); + await waitFor(() => expect(mockLegendScrollToEnd).toHaveBeenCalledWith({animated: false})); + mockLegendScrollToEnd.mockClear(); + const listProps = getCapturedListProps(); + act(() => { + listProps?.onScroll?.({ + nativeEvent: { + contentOffset: {x: 0, y: 500}, + contentSize: {height: 1000, width: 300}, + layoutMeasurement: {height: 500, width: 300}, + }, + }); + listProps?.onScroll?.({ + nativeEvent: { + contentOffset: {x: 0, y: 400}, + contentSize: {height: 1000, width: 300}, + layoutMeasurement: {height: 500, width: 300}, + }, + }); + listProps?.onContentSizeChange?.(300, 1025); + }); + + // When content grows, the reader's position stays where they left it. + await act( + async () => + new Promise((resolve) => { + setTimeout(resolve, 30); + }), + ); + expect(mockLegendScrollToEnd).not.toHaveBeenCalled(); + }); + it('does not duplicate the composer spacing inside the chronological list', () => { mockUseNetwork.mockReturnValue({isOffline: false}); renderReportActionsList(); From c92613d5d5f5cf37cded40cf918b637125376089 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Fri, 25 Sep 2026 17:03:07 +0200 Subject: [PATCH 32/61] fix: preserve Concierge scroll position when showing history --- src/hooks/useReportActionsListModel.ts | 2 + src/pages/inbox/report/ReportActionsList.tsx | 33 +++-- tests/ui/ReportActionsListTest.tsx | 142 +++++++++++++++++++ 3 files changed, 169 insertions(+), 8 deletions(-) diff --git a/src/hooks/useReportActionsListModel.ts b/src/hooks/useReportActionsListModel.ts index d8dce8b79da0..4b7331487bf1 100644 --- a/src/hooks/useReportActionsListModel.ts +++ b/src/hooks/useReportActionsListModel.ts @@ -154,6 +154,8 @@ function useReportActionsListModel(reportID: string, isReportLoadPending: boolea report, hasOnceLoadedReportActions, isInitialReportLoadPending, + isLoadingOlderReportActions, + hasLoadingOlderReportActionsError, hasOlderActions, hasNewerActions, oldestReportActionID: currentReportOldestActionID, diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index bc725e2c709d..a3ab06fe21e3 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -167,6 +167,8 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct report, hasOnceLoadedReportActions, isInitialReportLoadPending, + isLoadingOlderReportActions, + hasLoadingOlderReportActionsError, hasOlderActions, hasNewerActions, oldestReportActionID, @@ -264,10 +266,18 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct const [hasScrolledOverThreshold, setHasScrolledOverThreshold] = useState(() => getScrollOffset() >= CONST.REPORT.ACTIONS.ACTION_VISIBLE_THRESHOLD); const shouldRestoreTailAfterHistoryRevealRef = useRef(false); + const oldestLoadedActionID = allReportActionIDs.at(-1); + const oldestLoadedActionIDAtHistoryRevealRef = useRef(undefined); + const distanceFromBottomRef = useRef(getScrollOffset()); + const shouldFollowEndOnResizeRef = useRef(false); const onShowPreviousMessages = () => { - // A short Concierge session has no scrollable range. Revealing its older actions makes - // the list scrollable, so preserve the tail the reader was looking at before the reveal. - shouldRestoreTailAfterHistoryRevealRef.current = !hasScrolledOverThreshold && !hasNewerActions; + // Revealing older actions can make a short session scrollable before the requested history page arrives. + // Follow the tail only when the reader was actually there; the action-visible threshold is much wider. + shouldRestoreTailAfterHistoryRevealRef.current = distanceFromBottomRef.current <= 1 && !hasNewerActions; + oldestLoadedActionIDAtHistoryRevealRef.current = shouldRestoreTailAfterHistoryRevealRef.current && hasOlderActions ? oldestLoadedActionID : undefined; + if (shouldRestoreTailAfterHistoryRevealRef.current) { + shouldFollowEndOnResizeRef.current = true; + } handleShowPreviousMessages(); }; @@ -337,15 +347,22 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct const listData = reportActionsToRender.toReversed(); useEffect(() => { - if (!showFullHistory || !shouldRestoreTailAfterHistoryRevealRef.current) { + if (!showFullHistory) { + shouldRestoreTailAfterHistoryRevealRef.current = false; + return; + } + if (!shouldRestoreTailAfterHistoryRevealRef.current) { return; } const animationFrame = requestAnimationFrame(() => { legendListRef.current?.scrollToEnd({animated: false}); - shouldRestoreTailAfterHistoryRevealRef.current = false; + const hasReceivedHistoryPage = oldestLoadedActionID !== oldestLoadedActionIDAtHistoryRevealRef.current || !hasOlderActions; + if (!oldestLoadedActionIDAtHistoryRevealRef.current || (!isLoadingOlderReportActions && (hasReceivedHistoryPage || hasLoadingOlderReportActionsError))) { + shouldRestoreTailAfterHistoryRevealRef.current = false; + } }); return () => cancelAnimationFrame(animationFrame); - }, [showFullHistory, listData.length]); + }, [showFullHistory, listData.length, oldestLoadedActionID, hasOlderActions, isLoadingOlderReportActions, hasLoadingOlderReportActionsError]); const draftMessageHTML = draftReportAction ? getReportActionMessage(draftReportAction)?.html : undefined; const draftReportActionID = draftReportAction?.reportActionID; @@ -407,7 +424,6 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct // Cover every initial LegendList layout, including cached chats, until the rows are positioned. const shouldShowInitialViewportSkeleton = !isOffline && (isInitialReportLoadPending || loadedInitialViewportListID !== listID); const readyEndListIDRef = useRef(undefined); - const shouldFollowEndOnResizeRef = useRef(false); const lastScrollMetricsRef = useRef<{offset: number; contentHeight: number; viewportHeight: number} | undefined>(undefined); const endCorrectionFrameRef = useRef(undefined); @@ -475,10 +491,11 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct const trackScrollPositionAndThreshold = (event: NativeSyntheticEvent) => { const {contentOffset, contentSize, layoutMeasurement} = event.nativeEvent; const distanceFromBottom = Math.max(0, contentSize.height - layoutMeasurement.height - contentOffset.y); + distanceFromBottomRef.current = distanceFromBottom; const isNearStart = contentOffset.y <= layoutMeasurement.height * PAGINATION_THRESHOLD; const previousMetrics = lastScrollMetricsRef.current; if (readyEndListIDRef.current === listID && !hasNewerActions) { - if (distanceFromBottom <= layoutMeasurement.height * MAINTAIN_SCROLL_AT_END_THRESHOLD) { + if (shouldRestoreTailAfterHistoryRevealRef.current || distanceFromBottom <= layoutMeasurement.height * MAINTAIN_SCROLL_AT_END_THRESHOLD) { shouldFollowEndOnResizeRef.current = true; } else if ( (previousMetrics && contentOffset.y < previousMetrics.offset - 1) || diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 6517a913962d..2042e3486785 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -1737,6 +1737,148 @@ describe('ReportActionsList (body)', () => { } }); + it('keeps the latest Concierge messages visible when an older history page arrives after Show History', () => { + // Given a Concierge session with more history to load after the initially revealed actions. + setupMainDMConciergeMocks(); + let isShowingFullHistory = false; + mockUseConciergeSessionState.mockImplementation(() => ({sessionStartTime: SESSION_START, showFullHistory: isShowingFullHistory, hadMessagesAtSessionStart: false})); + mockUseConciergeSessionActions.mockReturnValue({ + startSession: jest.fn(), + setShowFullHistory: (show: boolean) => { + isShowingFullHistory = show; + }, + setHadMessagesAtSessionStart: jest.fn(), + }); + mockUsePaginatedReportActions.mockReturnValue({...defaultPaginatedReportActionsResult, reportActions: oldReportActions, hasOlderActions: true}); + const animationFrames: FrameRequestCallback[] = []; + const requestAnimationFrameSpy = jest.spyOn(global, 'requestAnimationFrame').mockImplementation((callback) => { + animationFrames.push(callback); + return animationFrames.length; + }); + const flushAnimationFrames = () => { + act(() => { + for (const callback of animationFrames.splice(0)) { + callback(0); + } + }); + }; + + try { + const view = renderReportActionsList({reportID: CONCIERGE_REPORT_ID}); + const actions = getCapturedVisibleActions(); + const createdIndex = actions?.findIndex((action) => action.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED) ?? -1; + const createdAction = actions?.at(createdIndex); + if (!createdAction) { + throw new Error('Expected the Concierge created action'); + } + + // When Show History reveals the loaded actions and an older page arrives afterward. + act(() => getShowHistoryPress(createdAction, createdIndex)?.()); + view.rerender( + , + ); + flushAnimationFrames(); + mockLegendScrollToEnd.mockClear(); + + const oldUserAction = oldReportActions.at(1); + if (!oldUserAction) { + throw new Error('Expected the older Concierge action'); + } + const olderAction = {...oldUserAction, reportActionID: 'older-user-msg', created: '2022-06-15 10:00:00.000'}; + mockUsePaginatedReportActions.mockReturnValue({...defaultPaginatedReportActionsResult, reportActions: [...oldReportActions, olderAction], hasOlderActions: false}); + view.rerender( + , + ); + flushAnimationFrames(); + + // Then the added page remains available without moving the reader away from the latest messages. + expect(getCapturedVisibleActions()?.some((action) => action.reportActionID === 'older-user-msg')).toBe(true); + expect(mockLegendScrollToEnd).toHaveBeenCalledWith({animated: false}); + } finally { + requestAnimationFrameSpy.mockRestore(); + } + }); + + it('does not pull a slightly scrolled Concierge chat to the bottom when Show History is pressed', () => { + // Given the reader is above the latest message but still within the generic action-visible threshold. + setupMainDMConciergeMocks(); + let isShowingFullHistory = false; + mockUseConciergeSessionState.mockImplementation(() => ({sessionStartTime: SESSION_START, showFullHistory: isShowingFullHistory, hadMessagesAtSessionStart: false})); + mockUseConciergeSessionActions.mockReturnValue({ + startSession: jest.fn(), + setShowFullHistory: (show: boolean) => { + isShowingFullHistory = show; + }, + setHadMessagesAtSessionStart: jest.fn(), + }); + mockUsePaginatedReportActions.mockReturnValue({...defaultPaginatedReportActionsResult, reportActions: oldReportActions, hasOlderActions: false}); + const animationFrames: FrameRequestCallback[] = []; + const requestAnimationFrameSpy = jest.spyOn(global, 'requestAnimationFrame').mockImplementation((callback) => { + animationFrames.push(callback); + return animationFrames.length; + }); + + try { + const view = renderReportActionsList({reportID: CONCIERGE_REPORT_ID}); + act(() => { + for (const callback of animationFrames.splice(0)) { + callback(0); + } + }); + mockLegendScrollToEnd.mockClear(); + act(() => { + getCapturedListProps()?.onScroll?.({ + nativeEvent: { + contentOffset: {x: 0, y: 500}, + contentSize: {height: 1000, width: 300}, + layoutMeasurement: {height: 500, width: 300}, + }, + }); + getCapturedListProps()?.onScroll?.({ + nativeEvent: { + contentOffset: {x: 0, y: 400}, + contentSize: {height: 1000, width: 300}, + layoutMeasurement: {height: 500, width: 300}, + }, + }); + }); + const actions = getCapturedVisibleActions(); + const createdIndex = actions?.findIndex((action) => action.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED) ?? -1; + const createdAction = actions?.at(createdIndex); + if (!createdAction) { + throw new Error('Expected the Concierge created action'); + } + + // When the reader reveals older history. + act(() => getShowHistoryPress(createdAction, createdIndex)?.()); + view.rerender( + , + ); + act(() => { + for (const callback of animationFrames.splice(0)) { + callback(0); + } + }); + + // Then the reader's offset is left to LegendList's visible-content preservation. + expect(mockLegendScrollToEnd).not.toHaveBeenCalled(); + } finally { + requestAnimationFrameSpy.mockRestore(); + } + }); + it('should show all actions unfiltered when user sends a message in current session', () => { setupMainDMConciergeMocks(); From 7096033845a3aa7a91a197084bb29849d22c9205 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Tue, 29 Sep 2026 13:40:04 +0200 Subject: [PATCH 33/61] fix: preserve unread message alignment at the latest action --- src/hooks/useReportActionsScroll.ts | 9 ++++++--- tests/unit/useReportActionsScrollTest.tsx | 6 +++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/hooks/useReportActionsScroll.ts b/src/hooks/useReportActionsScroll.ts index bfe62b432f7f..cb52d7f01150 100644 --- a/src/hooks/useReportActionsScroll.ts +++ b/src/hooks/useReportActionsScroll.ts @@ -343,10 +343,13 @@ function useReportActionsScroll({ let initialScrollIndexParams: {viewPosition?: number; viewOffset?: number} | undefined; if (targetIndex >= 0) { initialScrollIndex = targetIndex; - // The final action can be taller than the viewport. Aligning its top can leave the - // highlighted content below the screen; at the tail, show the action's end instead. + // A linked final action can be taller than the viewport, so show its highlighted end. + // Unread actions still start at the New marker. Omit viewOffset for bottom alignment so + // LegendList includes the footer (for example, Concierge's thinking indicator). initialScrollIndexParams = - targetIndex === renderedVisibleReportActions.length - 1 ? {viewPosition: 1, viewOffset: 0} : {viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}; + initialScrollKey === linkedReportActionID && targetIndex === renderedVisibleReportActions.length - 1 + ? {viewPosition: 1} + : {viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}; } else if (shouldFocusToTopOnMount) { initialScrollIndex = 0; } diff --git a/tests/unit/useReportActionsScrollTest.tsx b/tests/unit/useReportActionsScrollTest.tsx index f73c3d40d140..5cd100198bda 100644 --- a/tests/unit/useReportActionsScrollTest.tsx +++ b/tests/unit/useReportActionsScrollTest.tsx @@ -285,7 +285,7 @@ describe('useReportActionsScroll', () => { const {result} = await renderScroll({sortedVisibleReportActions: [linkedAction], renderedVisibleReportActions: [linkedAction]}); expect(result.current.initialScrollIndex).toBe(0); - expect(result.current.initialScrollIndexParams).toEqual({viewPosition: 1, viewOffset: 0}); + expect(result.current.initialScrollIndexParams).toEqual({viewPosition: 1}); }); it('top-aligns a linked report action when later actions follow it', async () => { @@ -302,14 +302,18 @@ describe('useReportActionsScroll', () => { }); it('positions an unread marker at chronological index zero', async () => { + // Given the newest action is unread and can be taller than the viewport. const unreadAction = makeAction(UNREAD_ACTION_ID); + // When opening the report at its unread boundary. const {result} = await renderScroll({ unreadMarkerReportActionID: UNREAD_ACTION_ID, sortedVisibleReportActions: [unreadAction], renderedVisibleReportActions: [unreadAction], }); + // Then the start of the action and its New marker remain visible. expect(result.current.initialScrollIndex).toBe(0); + expect(result.current.initialScrollIndexParams).toEqual({viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}); }); it('suppresses the initial scroll key for an aligned-to-top CREATED anchor action', async () => { From c0751dd1b9880cd6d5b1fc0edb21c91d9f47c1b3 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Tue, 29 Sep 2026 13:41:33 +0200 Subject: [PATCH 34/61] fix: cancel report end corrections when the reader scrolls --- src/pages/inbox/report/ReportActionsList.tsx | 27 ++++++-- tests/ui/ReportActionsListTest.tsx | 69 +++++++++++++++++--- 2 files changed, 83 insertions(+), 13 deletions(-) diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index a3ab06fe21e3..60d4d747833d 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -355,6 +355,9 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct return; } const animationFrame = requestAnimationFrame(() => { + if (!shouldRestoreTailAfterHistoryRevealRef.current || !shouldFollowEndOnResizeRef.current) { + return; + } legendListRef.current?.scrollToEnd({animated: false}); const hasReceivedHistoryPage = oldestLoadedActionID !== oldestLoadedActionIDAtHistoryRevealRef.current || !hasOlderActions; if (!oldestLoadedActionIDAtHistoryRevealRef.current || (!isLoadingOlderReportActions && (hasReceivedHistoryPage || hasLoadingOlderReportActionsError))) { @@ -426,6 +429,18 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct const readyEndListIDRef = useRef(undefined); const lastScrollMetricsRef = useRef<{offset: number; contentHeight: number; viewportHeight: number} | undefined>(undefined); const endCorrectionFrameRef = useRef(undefined); + const userScrolledListIDRef = useRef(undefined); + + const stopFollowingEnd = () => { + userScrolledListIDRef.current = listID; + shouldFollowEndOnResizeRef.current = false; + shouldRestoreTailAfterHistoryRevealRef.current = false; + if (endCorrectionFrameRef.current === undefined) { + return; + } + cancelAnimationFrame(endCorrectionFrameRef.current); + endCorrectionFrameRef.current = undefined; + }; const scheduleEndCorrection = () => { if ( @@ -473,7 +488,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct const handleListReady = () => { readyEndListIDRef.current = listID; - shouldFollowEndOnResizeRef.current = initialScrollIndex === undefined && !hasNewerActions; + shouldFollowEndOnResizeRef.current = initialScrollIndex === undefined && !hasNewerActions && userScrolledListIDRef.current !== listID; lastScrollMetricsRef.current = undefined; // The final row can grow after LegendList's initial end estimate (for example, Concierge feedback). scheduleEndCorrection(); @@ -495,7 +510,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct const isNearStart = contentOffset.y <= layoutMeasurement.height * PAGINATION_THRESHOLD; const previousMetrics = lastScrollMetricsRef.current; if (readyEndListIDRef.current === listID && !hasNewerActions) { - if (shouldRestoreTailAfterHistoryRevealRef.current || distanceFromBottom <= layoutMeasurement.height * MAINTAIN_SCROLL_AT_END_THRESHOLD) { + if (distanceFromBottom <= layoutMeasurement.height * MAINTAIN_SCROLL_AT_END_THRESHOLD) { shouldFollowEndOnResizeRef.current = true; } else if ( (previousMetrics && contentOffset.y < previousMetrics.offset - 1) || @@ -504,7 +519,8 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct previousMetrics.viewportHeight === layoutMeasurement.height && endCorrectionFrameRef.current === undefined) ) { - shouldFollowEndOnResizeRef.current = false; + // A pending history request must not reclaim the end after the reader has moved away. + stopFollowingEnd(); } } lastScrollMetricsRef.current = {offset: contentOffset.y, contentHeight: contentSize.height, viewportHeight: layoutMeasurement.height}; @@ -720,6 +736,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct keyboardShouldPersistTaps="handled" onLayout={recordTimeToMeasureItemLayout} onScroll={trackScrollPositionAndThreshold} + onScrollBeginDrag={stopFollowingEnd} onStartReached={loadOlderChatsOnStartReached} onStartReachedThreshold={PAGINATION_THRESHOLD} onViewableItemsChanged={updateVisibleItemOverflow} @@ -729,8 +746,8 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct initialScrollAtEnd={initialScrollIndex === undefined} initialScrollIndex={initialScrollIndex === undefined ? undefined : {index: initialScrollIndex, ...initialScrollIndexParams}} alignItemsAtEnd={!shouldBeAlignedToTop} - // Only follow the real latest page. Older/linked windows must retain their visible anchor. - maintainScrollAtEnd={!hasNewerActions && {animated: false}} + // Follow new actions on the latest page. Layout changes use the cancellable correction above. + maintainScrollAtEnd={!hasNewerActions && {animated: false, on: {dataChange: true}}} // Leave the end-follow region as soon as the user starts reading older messages. maintainScrollAtEndThreshold={MAINTAIN_SCROLL_AT_END_THRESHOLD} maintainVisibleContentPosition diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 2042e3486785..8ac9fb16fd48 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -200,11 +200,12 @@ type MockLegendListProps = { extraData?: unknown; getItemType?: (item: OnyxTypes.ReportAction) => string; initialScrollAtEnd?: boolean; - maintainScrollAtEnd?: {animated: boolean} | false; + maintainScrollAtEnd?: {animated: boolean; on: {dataChange: boolean}} | false; maintainScrollAtEndThreshold?: number; maintainVisibleContentPosition?: boolean; onLoad?: () => void; onReady?: () => void; + onScrollBeginDrag?: () => void; onStartReachedThreshold?: number; onContentSizeChange?: (width: number, height: number) => void; onViewableItemsChanged?: (info: OnViewableItemsChangedInfo) => void; @@ -435,11 +436,11 @@ describe('ReportActionsList (body)', () => { await Onyx.clear(); }); - it('delegates end following and size corrections to LegendList without a full-viewport threshold', () => { + it('delegates new-action following to LegendList without a full-viewport threshold', () => { mockUseNetwork.mockReturnValue({isOffline: false}); renderReportActionsList(); - expect(getCapturedListProps()?.maintainScrollAtEnd).toEqual({animated: false}); + expect(getCapturedListProps()?.maintainScrollAtEnd).toEqual({animated: false, on: {dataChange: true}}); expect(getCapturedListProps()?.maintainScrollAtEndThreshold).toBe(0.01); expect(getCapturedListProps()?.maintainVisibleContentPosition).toBe(true); }); @@ -522,6 +523,35 @@ describe('ReportActionsList (body)', () => { expect(mockLegendScrollToEnd).not.toHaveBeenCalled(); }); + it('cancels a queued end correction as soon as the reader starts dragging', async () => { + // Given removing the latest message has queued a layout correction at the end. + mockUseNetwork.mockReturnValue({isOffline: false}); + renderReportActionsList(); + await waitFor(() => expect(mockLegendScrollToEnd).toHaveBeenCalled()); + mockLegendScrollToEnd.mockClear(); + const animationFrames: FrameRequestCallback[] = []; + const requestAnimationFrameSpy = jest.spyOn(global, 'requestAnimationFrame').mockImplementation((callback) => { + animationFrames.push(callback); + return animationFrames.length; + }); + try { + const listProps = getCapturedListProps(); + // When the user touches the list before the next scroll metrics arrive. + act(() => { + listProps?.onContentSizeChange?.(300, 900); + listProps?.onScrollBeginDrag?.(); + for (const callback of animationFrames) { + callback(0); + } + }); + + // Then even a callback already dispatched by the frame scheduler respects the gesture. + expect(mockLegendScrollToEnd).not.toHaveBeenCalled(); + } finally { + requestAnimationFrameSpy.mockRestore(); + } + }); + it('does not duplicate the composer spacing inside the chronological list', () => { mockUseNetwork.mockReturnValue({isOffline: false}); renderReportActionsList(); @@ -549,7 +579,7 @@ describe('ReportActionsList (body)', () => { expect(getCapturedListProps()?.initialScrollAtEnd).toBe(true); expect(getCapturedListProps()?.alignItemsAtEnd).toBe(true); - expect(getCapturedListProps()?.maintainScrollAtEnd).toEqual({animated: false}); + expect(getCapturedListProps()?.maintainScrollAtEnd).toEqual({animated: false, on: {dataChange: true}}); }); it('does not follow the end of a page that still has newer actions to load', () => { @@ -588,7 +618,7 @@ describe('ReportActionsList (body)', () => { expect(mockLegendListMount).toHaveBeenCalledTimes(2); expect(mockLegendListUnmount).toHaveBeenCalledTimes(1); expect(getCapturedListProps()?.initialScrollAtEnd).toBe(true); - expect(getCapturedListProps()?.maintainScrollAtEnd).toEqual({animated: false}); + expect(getCapturedListProps()?.maintainScrollAtEnd).toEqual({animated: false, on: {dataChange: true}}); }); it('keeps the initial viewport covered until the hydrated LegendList finishes rendering it', async () => { @@ -1737,7 +1767,7 @@ describe('ReportActionsList (body)', () => { } }); - it('keeps the latest Concierge messages visible when an older history page arrives after Show History', () => { + it.each([false, true])('only follows a delayed Show History page while the reader stays at the end (scrolls away: %s)', (scrollsAway) => { // Given a Concierge session with more history to load after the initially revealed actions. setupMainDMConciergeMocks(); let isShowingFullHistory = false; @@ -1784,6 +1814,25 @@ describe('ReportActionsList (body)', () => { flushAnimationFrames(); mockLegendScrollToEnd.mockClear(); + if (scrollsAway) { + act(() => { + getCapturedListProps()?.onScroll?.({ + nativeEvent: { + contentOffset: {x: 0, y: 500}, + contentSize: {height: 1000, width: 300}, + layoutMeasurement: {height: 500, width: 300}, + }, + }); + getCapturedListProps()?.onScroll?.({ + nativeEvent: { + contentOffset: {x: 0, y: 200}, + contentSize: {height: 1000, width: 300}, + layoutMeasurement: {height: 500, width: 300}, + }, + }); + }); + } + const oldUserAction = oldReportActions.at(1); if (!oldUserAction) { throw new Error('Expected the older Concierge action'); @@ -1799,9 +1848,13 @@ describe('ReportActionsList (body)', () => { ); flushAnimationFrames(); - // Then the added page remains available without moving the reader away from the latest messages. + // Then loading the page respects the reader's choice to stay at the end or read history. expect(getCapturedVisibleActions()?.some((action) => action.reportActionID === 'older-user-msg')).toBe(true); - expect(mockLegendScrollToEnd).toHaveBeenCalledWith({animated: false}); + if (scrollsAway) { + expect(mockLegendScrollToEnd).not.toHaveBeenCalled(); + } else { + expect(mockLegendScrollToEnd).toHaveBeenCalledWith({animated: false}); + } } finally { requestAnimationFrameSpy.mockRestore(); } From 0cb9d7be3841cf870726b71d87ec0302534a8e5c Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Tue, 29 Sep 2026 13:43:55 +0200 Subject: [PATCH 35/61] fix: keep report scroll events flowing on web --- src/pages/inbox/report/ReportActionsList.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index 60d4d747833d..5f6e1d29adaf 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -1,4 +1,5 @@ import {renderScrollComponent as renderActionSheetAwareScrollView} from '@components/ActionSheetAwareScrollView'; +import type {ActionSheetAwareScrollViewProps} from '@components/ActionSheetAwareScrollView/types'; import allowLegendListItemOverflow from '@components/LegendList/allowLegendListItemOverflow'; import MerchantRuleSuggestionBanner from '@components/MerchantRuleSuggestionBanner'; import {ReportActionsAnimatedSkeletonCover} from '@components/ReportActionsSkeletonCover'; @@ -726,7 +727,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct drawDistance={REPORT_ACTIONS_DRAW_DISTANCE} recycleItems experimental_hideItemsUntilMeasured - renderScrollComponent={renderActionSheetAwareScrollView} + renderScrollComponent={renderReportActionsScrollView} contentContainerStyle={[styles.chatContentScrollView, styles.pb0]} onEndReached={loadNewerChatsAfterTransitions} onEndReachedThreshold={PAGINATION_THRESHOLD} @@ -792,3 +793,9 @@ function ReportActionsList({reportID, conciergeChat, onLayout}: ReportActionsLis } export default ReportActionsList; + +function renderReportActionsScrollView(props: ActionSheetAwareScrollViewProps) { + // LegendList passes 0 to custom scroll views. On web that only emits the first and last event, + // allowing the hover-suppression timer to expire during a continuous scroll. + return renderActionSheetAwareScrollView({...props, scrollEventThrottle: 16}); +} From 0f53485b08c51ceab379a46b7c99461eeab25e4f Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Tue, 29 Sep 2026 13:53:33 +0200 Subject: [PATCH 36/61] fix: keep chat skeleton visible until initial scrolling settles --- src/pages/inbox/report/ReportActionsList.tsx | 7 ++----- tests/ui/ReportActionsListTest.tsx | 17 ++++++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index 5f6e1d29adaf..36663bb2e0dc 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -482,12 +482,10 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct } }; - const handleListLoad = () => { + const handleListReady = () => { + // onLoad only means rows rendered; keep their estimated positions covered until initial scrolling settles. onLoad(); setLoadedInitialViewportListID(listID); - }; - - const handleListReady = () => { readyEndListIDRef.current = listID; shouldFollowEndOnResizeRef.current = initialScrollIndex === undefined && !hasNewerActions && userScrolledListIDRef.current !== listID; lastScrollMetricsRef.current = undefined; @@ -752,7 +750,6 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct // Leave the end-follow region as soon as the user starts reading older messages. maintainScrollAtEndThreshold={MAINTAIN_SCROLL_AT_END_THRESHOLD} maintainVisibleContentPosition - onLoad={handleListLoad} onReady={handleListReady} onContentSizeChange={() => { trackVerticalScrolling(undefined); diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 8ac9fb16fd48..58a36fd1b4a1 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -631,7 +631,7 @@ describe('ReportActionsList (body)', () => { expect(screen.getByTestId('ReportActionsSkeletonView')).toBeTruthy(); act(() => { - getCapturedListProps()?.onLoad?.(); + getCapturedListProps()?.onReady?.(); }); expect(screen.getByTestId('ReportActionsSkeletonView')).toBeTruthy(); @@ -648,12 +648,12 @@ describe('ReportActionsList (body)', () => { expect(screen.getByTestId('ReportActionsSkeletonView')).toBeTruthy(); act(() => { - getCapturedListProps()?.onLoad?.(); + getCapturedListProps()?.onReady?.(); }); expect(screen.queryByTestId('ReportActionsSkeletonView')).toBeNull(); }); - it('forwards the list load event so initial scroll tracking can resume', () => { + it('resumes initial scroll tracking after layout and scrolling are ready', () => { // Given a report with its action list ready to mount. mockShouldCallLegendListOnLoad = false; renderReportActionsList(); @@ -661,8 +661,8 @@ describe('ReportActionsList (body)', () => { const onLoad = scrollHookResult?.onLoad; expect(onLoad).not.toHaveBeenCalled(); - // When LegendList reports that its first layout is complete. - act(() => getCapturedListProps()?.onLoad?.()); + // When LegendList reports that layout and initial scrolling are complete. + act(() => getCapturedListProps()?.onReady?.()); // Then the scroll hook receives that event to finish initial positioning. expect(onLoad).toHaveBeenCalledTimes(1); @@ -681,6 +681,9 @@ describe('ReportActionsList (body)', () => { expect(screen.getByTestId('ReportActionsSkeletonCover')).toBeTruthy(); act(() => getCapturedListProps()?.onLoad?.()); + expect(screen.getByTestId('ReportActionsSkeletonCover')).toBeTruthy(); + + act(() => getCapturedListProps()?.onReady?.()); expect(screen.queryByTestId('ReportActionsSkeletonCover')).toBeNull(); }); @@ -694,7 +697,7 @@ describe('ReportActionsList (body)', () => { expect(screen.getByTestId('ReportActionsSkeletonCover')).toBeTruthy(); act(() => { - getCapturedListProps()?.onLoad?.(); + getCapturedListProps()?.onReady?.(); }); expect(screen.queryByTestId('ReportActionsSkeletonCover')).toBeNull(); @@ -709,7 +712,7 @@ describe('ReportActionsList (body)', () => { const view = renderReportActionsList(); act(() => { - getCapturedListProps()?.onLoad?.(); + getCapturedListProps()?.onReady?.(); }); expect(screen.getByTestId('ReportActionsSkeletonCover')).toBeTruthy(); From b611f3838000c157310c054f46b2d32bc155dbee Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Tue, 29 Sep 2026 13:59:54 +0200 Subject: [PATCH 37/61] fix: resume end following after scrolling past a linked message --- src/pages/inbox/report/ReportActionsList.tsx | 8 +---- tests/ui/ReportActionsListTest.tsx | 31 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index 36663bb2e0dc..fefda07db672 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -444,13 +444,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct }; const scheduleEndCorrection = () => { - if ( - readyEndListIDRef.current !== listID || - !shouldFollowEndOnResizeRef.current || - hasNewerActions || - initialScrollIndex !== undefined || - endCorrectionFrameRef.current !== undefined - ) { + if (readyEndListIDRef.current !== listID || !shouldFollowEndOnResizeRef.current || hasNewerActions || endCorrectionFrameRef.current !== undefined) { return; } endCorrectionFrameRef.current = requestAnimationFrame(() => { diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 58a36fd1b4a1..ebf8b374cbf2 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -11,6 +11,7 @@ import useNetwork from '@hooks/useNetwork'; import useOnyx from '@hooks/useOnyx'; import usePaginatedReportActions from '@hooks/usePaginatedReportActions'; import useParentReportAction from '@hooks/useParentReportAction'; +import type useReportActionsScroll from '@hooks/useReportActionsScroll'; import useReportTransactionsCollection from '@hooks/useReportTransactionsCollection'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useSidePanelState from '@hooks/useSidePanelState'; @@ -523,6 +524,36 @@ describe('ReportActionsList (body)', () => { expect(mockLegendScrollToEnd).not.toHaveBeenCalled(); }); + it('resumes resize following after scrolling from a linked action to the end', async () => { + // Given the chat opens at a linked action, which must keep its initial position. + mockUseNetwork.mockReturnValue({isOffline: false}); + const originalImplementation = mockUseReportActionsScroll.getMockImplementation(); + const defaultScrollState = mockUseReportActionsScroll() as ReturnType; + mockUseReportActionsScroll.mockReturnValue({...defaultScrollState, initialScrollIndex: 0}); + try { + renderReportActionsList(); + expect(mockLegendScrollToEnd).not.toHaveBeenCalled(); + + // When the reader reaches the end and the latest row grows afterward. + act(() => { + const listProps = getCapturedListProps(); + listProps?.onScroll?.({ + nativeEvent: { + contentOffset: {x: 0, y: 500}, + contentSize: {height: 1000, width: 300}, + layoutMeasurement: {height: 500, width: 300}, + }, + }); + listProps?.onContentSizeChange?.(300, 1025); + }); + + // Then the latest content remains visible even though the route still contains a linked action. + await waitFor(() => expect(mockLegendScrollToEnd).toHaveBeenCalledWith({animated: false})); + } finally { + mockUseReportActionsScroll.mockImplementation(originalImplementation); + } + }); + it('cancels a queued end correction as soon as the reader starts dragging', async () => { // Given removing the latest message has queued a layout correction at the end. mockUseNetwork.mockReturnValue({isOffline: false}); From 779eaacf57bdbe2b3395f9cad9459669da761fd6 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 1 Oct 2026 15:47:56 +0200 Subject: [PATCH 38/61] fix: preserve chat content when clearing linked messages Clearing the linked-action route after sending remounted the report content and LegendList, briefly restoring the loading skeleton and resetting composer state. Keep the report content mounted while resetting only the link checks, and remount the list only when opening a linked action or completing initial hydration. --- src/pages/inbox/LinkedActionNotFoundGuard.tsx | 23 +++-- src/pages/inbox/report/ReportActionsList.tsx | 12 ++- .../LinkedActionNotFoundGuardTest.tsx | 88 +++++++++++++++---- tests/ui/ReportActionsListTest.tsx | 63 +++++++++++++ 4 files changed, 155 insertions(+), 31 deletions(-) diff --git a/src/pages/inbox/LinkedActionNotFoundGuard.tsx b/src/pages/inbox/LinkedActionNotFoundGuard.tsx index d77c311a4068..697064fe7fa7 100644 --- a/src/pages/inbox/LinkedActionNotFoundGuard.tsx +++ b/src/pages/inbox/LinkedActionNotFoundGuard.tsx @@ -30,26 +30,25 @@ function LinkedActionNotFoundGuard({children}: LinkedActionNotFoundGuardProps) { const routeParams = route.params as {reportActionID?: string} | undefined; const reportActionIDFromRoute = routeParams?.reportActionID; - if (!reportActionIDFromRoute) { - return children; - } - return ( - + <> + {/* Reset the link checks without remounting the report list and composer. */} + {!!reportActionIDFromRoute && ( + + )} {children} - + ); } type LinkedActionNotFoundGateProps = { reportActionIDFromRoute: string; - children: ReactNode; }; -function LinkedActionNotFoundGate({reportActionIDFromRoute, children}: LinkedActionNotFoundGateProps) { +function LinkedActionNotFoundGate({reportActionIDFromRoute}: LinkedActionNotFoundGateProps) { const route = useRoute(); const navigation = useNavigation(); const navigatorKey = navigation.getState()?.key; @@ -147,7 +146,7 @@ function LinkedActionNotFoundGate({reportActionIDFromRoute, children}: LinkedAct useAutoNavigateForDeletedLinkedAction(isLinkedActionUnavailable, navigateToEndOfReport); - return children; + return null; } LinkedActionNotFoundGuard.displayName = 'LinkedActionNotFoundGuard'; diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index fefda07db672..ab0d49e5bf7b 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -203,11 +203,21 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct // Owned here rather than by the callout, which unmounts as the layout and composer change size. useRetireMerchantRuleSuggestionOnLeave(reportID); + // Clearing a link after sending returns to the live tail of the same list. Only opening a link + // needs fresh initial positioning, including reopening the same action after its link was cleared. + const [linkedActionListState, setLinkedActionListState] = useState({reportActionID: reportActionIDFromRoute, version: 0}); + if (linkedActionListState.reportActionID !== reportActionIDFromRoute) { + setLinkedActionListState({ + reportActionID: reportActionIDFromRoute, + version: linkedActionListState.version + (reportActionIDFromRoute ? 1 : 0), + }); + } + // OpenReport can first provide a tiny cached page and then replace it with the hydrated page. Remounting // gives the complete dataset a fresh initial layout so initialScrollAtEnd targets its actual end. const listID = [ reportID, - reportActionIDFromRoute, + linkedActionListState.version, hasOnceLoadedReportActions ? 'hydrated' : 'initial', hasOnceLoadedReportActions ? undefined : oldestUnreadReportAction?.reportActionID, ].join(':'); diff --git a/tests/navigation/LinkedActionNotFoundGuardTest.tsx b/tests/navigation/LinkedActionNotFoundGuardTest.tsx index b464add1f92b..c1cda0fd16d5 100644 --- a/tests/navigation/LinkedActionNotFoundGuardTest.tsx +++ b/tests/navigation/LinkedActionNotFoundGuardTest.tsx @@ -16,6 +16,8 @@ const NAVIGATOR_KEY = 'test-navigator-key'; const mockSetParams = jest.fn(); const mockCleanStaleBackToParam = jest.fn(); const mockIsNavigationReady = jest.fn(() => Promise.resolve()); +const mockChildrenMount = jest.fn(); +const mockChildrenUnmount = jest.fn(); jest.mock('@libs/Navigation/Navigation', () => ({ __esModule: true, @@ -43,7 +45,7 @@ jest.mock('@react-navigation/native', () => { useRoute: () => ({ key: ROUTE_KEY, name: 'Report', - params: mockRouteParams, + params: {...mockRouteParams}, }), useNavigation: () => ({ getState: () => ({key: NAVIGATOR_KEY}), @@ -83,23 +85,33 @@ jest.mock('@libs/ReportUtils', () => ({ type UseOnyxReturn = [unknown, {status: string}]; let mockLinkedAction: ReportAction | null | undefined; let mockIsLoadingInitialReportActions: boolean; - -jest.mock('@hooks/useOnyx', () => ({ - __esModule: true, - default: (key: string): UseOnyxReturn => { - if (key.startsWith('reportActions_')) { - return [mockLinkedAction, {status: 'loaded'}]; - } - if (key.startsWith('reportLoadingState_')) { - return [mockIsLoadingInitialReportActions, {status: 'loaded'}]; - } - if (key.startsWith('report_')) { - return [{reportID: '12345', type: 'chat'}, {status: 'loaded'}]; - } - // DERIVED.VISIBLE_REPORT_ACTIONS - return [undefined, {status: 'loaded'}]; - }, -})); +let mockOnyxVersion = 0; +const mockOnyxListeners = new Set<() => void>(); + +jest.mock('@hooks/useOnyx', () => { + const {useSyncExternalStore} = jest.requireActual('react'); + const subscribe = (listener: () => void) => { + mockOnyxListeners.add(listener); + return () => mockOnyxListeners.delete(listener); + }; + return { + __esModule: true, + default: function useMockOnyx(key: string): UseOnyxReturn { + useSyncExternalStore(subscribe, () => mockOnyxVersion); + if (key.startsWith('reportActions_')) { + return [mockLinkedAction, {status: 'loaded'}]; + } + if (key.startsWith('reportLoadingState_')) { + return [mockIsLoadingInitialReportActions, {status: 'loaded'}]; + } + if (key.startsWith('report_')) { + return [{reportID: '12345', type: 'chat'}, {status: 'loaded'}]; + } + // DERIVED.VISIBLE_REPORT_ACTIONS + return [undefined, {status: 'loaded'}]; + }, + }; +}); function createReportAction(overrides: Partial = {}): ReportAction { return { @@ -114,6 +126,10 @@ function createReportAction(overrides: Partial = {}): ReportAction } function TestChildren() { + React.useEffect(() => { + mockChildrenMount(); + return () => mockChildrenUnmount(); + }, []); return ; } @@ -122,6 +138,8 @@ describe('LinkedActionNotFoundGuard', () => { mockSetParams.mockClear(); mockCleanStaleBackToParam.mockClear(); mockIsNavigationReady.mockClear(); + mockChildrenMount.mockClear(); + mockChildrenUnmount.mockClear(); mockRouteParams.reportID = REPORT_ID; mockRouteParams.reportActionID = REPORT_ACTION_ID; mockLinkedAction = createReportAction(); @@ -129,6 +147,29 @@ describe('LinkedActionNotFoundGuard', () => { mockIsReportActionVisible = true; }); + it('preserves the report content when a link is cleared or a different link is opened', () => { + // Given a linked report has mounted its list and composer. + const view = render( + + + , + ); + + // When sending clears the link, and the reader subsequently opens another link. + for (const reportActionID of ['', 'another-action', undefined, REPORT_ACTION_ID]) { + mockRouteParams.reportActionID = reportActionID; + view.rerender( + + + , + ); + + // Then the report keeps its mounted list, editor, and keyboard state. + expect(mockChildrenMount).toHaveBeenCalledTimes(1); + expect(mockChildrenUnmount).not.toHaveBeenCalled(); + } + }); + it('renders children when linked action exists', () => { render( @@ -165,6 +206,7 @@ describe('LinkedActionNotFoundGuard', () => { act(() => { mockIsReportActionVisible = false; + notifyMockOnyxChanges(); }); rerender( @@ -191,6 +233,7 @@ describe('LinkedActionNotFoundGuard', () => { // Now remove the action (simulating REPORT_PREVIEW being nulled when moving IOU to workspace) act(() => { mockLinkedAction = null; + notifyMockOnyxChanges(); }); rerender( @@ -229,6 +272,7 @@ describe('LinkedActionNotFoundGuard', () => { // Remove the action act(() => { mockLinkedAction = null; + notifyMockOnyxChanges(); }); rerender( @@ -266,6 +310,7 @@ describe('LinkedActionNotFoundGuard', () => { act(() => { mockLinkedAction = null; mockIsLoadingInitialReportActions = true; + notifyMockOnyxChanges(); }); rerender( @@ -278,3 +323,10 @@ describe('LinkedActionNotFoundGuard', () => { expect(mockSetParams).not.toHaveBeenCalled(); }); }); + +function notifyMockOnyxChanges() { + mockOnyxVersion += 1; + for (const listener of mockOnyxListeners) { + listener(); + } +} diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index ebf8b374cbf2..d90daaae49a9 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -33,6 +33,7 @@ import type {OnViewableItemsChangedInfo} from '@legendapp/list/react-native'; import type * as ReactNavigation from '@react-navigation/native'; import type {StyleProp, ViewStyle} from 'react-native'; +import {useRoute} from '@react-navigation/native'; import React from 'react'; import {StyleSheet} from 'react-native'; import Onyx from 'react-native-onyx'; @@ -78,6 +79,7 @@ jest.mock('@pages/inbox/ConciergeDraftContext', () => ({ })); const mockUseNetwork = useNetwork as jest.MockedFunction; +const mockUseRoute = useRoute as jest.MockedFunction; const mockUseIsReportLoadPending = useIsReportLoadPending as jest.MockedFunction; const mockUseOnyx = useOnyx as jest.MockedFunction; const mockUseResponsiveLayout = useResponsiveLayout as jest.MockedFunction; @@ -375,6 +377,7 @@ describe('ReportActionsList (body)', () => { mockShouldCallLegendListOnLoad = true; mockUseUnreadMarker.mockReturnValue({unreadMarkerReportActionID: null, unreadMarkerReportActionIndex: -1}); mockUseIsReportLoadPending.mockReturnValue(false); + mockUseRoute.mockReturnValue({key: 'report', name: 'Report', params: {}}); mockUseCurrentUserPersonalDetails.mockReturnValue({ accountID: 100, @@ -718,6 +721,66 @@ describe('ReportActionsList (body)', () => { expect(screen.queryByTestId('ReportActionsSkeletonCover')).toBeNull(); }); + it('keeps the positioned list mounted when sending a message clears the linked action', async () => { + // Given a linked message has finished its initial positioning. + mockUseNetwork.mockReturnValue({isOffline: false}); + mockUseRoute.mockReturnValue({key: 'report', name: 'Report', params: {reportActionID: '2'}}); + const view = renderReportActionsList(); + expect(screen.queryByTestId('ReportActionsSkeletonCover')).toBeNull(); + + // When the live-tail jump clears the link, a new mount would have to position again. + mockShouldCallLegendListOnLoad = false; + mockUseRoute.mockReturnValue({key: 'report', name: 'Report', params: {reportActionID: ''}}); + view.rerender( + , + ); + await waitForBatchedUpdatesWithAct(); + + // Then the existing list stays visible without another initial skeleton. + expect(mockLegendListMount).toHaveBeenCalledTimes(1); + expect(mockLegendListUnmount).not.toHaveBeenCalled(); + expect(screen.queryByTestId('ReportActionsSkeletonCover')).toBeNull(); + }); + + it.each(['2', 'another-action'])('positions a newly opened link after the previous link was cleared: %s', async (reportActionID) => { + // Given a positioned linked list is kept mounted when returning to its latest messages. + mockUseNetwork.mockReturnValue({isOffline: false}); + mockUseRoute.mockReturnValue({key: 'report', name: 'Report', params: {reportActionID: '2'}}); + const view = renderReportActionsList(); + mockUseRoute.mockReturnValue({key: 'report', name: 'Report', params: {reportActionID: ''}}); + view.rerender( + , + ); + await waitForBatchedUpdatesWithAct(); + expect(mockLegendListMount).toHaveBeenCalledTimes(1); + + // When the reader opens a link, even if it targets the same action again. + mockShouldCallLegendListOnLoad = false; + mockUseRoute.mockReturnValue({key: 'report', name: 'Report', params: {reportActionID}}); + view.rerender( + , + ); + await waitForBatchedUpdatesWithAct(); + + // Then fresh positioning is covered until the new list is ready. + expect(mockLegendListMount).toHaveBeenCalledTimes(2); + expect(screen.getByTestId('ReportActionsSkeletonCover')).toBeTruthy(); + act(() => getCapturedListProps()?.onReady?.()); + expect(screen.queryByTestId('ReportActionsSkeletonCover')).toBeNull(); + }); + it('releases the initial viewport cover after a terminal OpenReport failure', () => { mockUseNetwork.mockReturnValue({isOffline: false}); mockHasOnceLoadedReportActions = false; From 519cb5b40353cff3c010433e4008e1c9e974d4f3 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 8 Oct 2026 13:58:53 +0100 Subject: [PATCH 39/61] fix: prevent Android LegendList viewport flashes --- package-lock.json | 8 +-- package.json | 2 +- ...+001+synchronize-fabric-content-size.patch | 52 +++++++++++++++++++ patches/@legendapp/list/details.md | 10 ++++ 4 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 patches/@legendapp/list/@legendapp+list+3.6.0+001+synchronize-fabric-content-size.patch create mode 100644 patches/@legendapp/list/details.md diff --git a/package-lock.json b/package-lock.json index b1348ef8538a..8d51c8123255 100644 --- a/package-lock.json +++ b/package-lock.json @@ -33,7 +33,7 @@ "@fullstory/react-native": "^1.10.2", "@gorhom/portal": "^1.0.14", "@invertase/react-native-apple-authentication": "^2.5.0", - "@legendapp/list": "^3.4.0", + "@legendapp/list": "^3.6.0", "@lottiefiles/dotlottie-react": "0.13.5", "@onfido/react-native-sdk": "15.1.0", "@pusher/pusher-websocket-react-native": "^1.3.1", @@ -12091,9 +12091,9 @@ "license": "MIT" }, "node_modules/@legendapp/list": { - "version": "3.4.0", - "resolved": "https://registry.npmjs.org/@legendapp/list/-/list-3.4.0.tgz", - "integrity": "sha512-Tlx7Uf9pRP/JbrekuWdKf1+42C9MTfl1wGkP8Lvmg2E2T9rCWQphYOs8aOkcvgbAkJ2S+WR+RTXHS2+6U+nROw==", + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/@legendapp/list/-/list-3.6.0.tgz", + "integrity": "sha512-ndGQjSYdA32LWErJYbXEm4+eta+4bo0NxQO7eA3Xcvh0rXSb1mTGPbUtUkpea5w4MYciLKaE+0hoPxlMcaMtrw==", "license": "MIT", "dependencies": { "use-sync-external-store": "^1.5.0" diff --git a/package.json b/package.json index 6b2e5987a617..9da7345e8d86 100644 --- a/package.json +++ b/package.json @@ -109,7 +109,7 @@ "@fullstory/react-native": "^1.10.2", "@gorhom/portal": "^1.0.14", "@invertase/react-native-apple-authentication": "^2.5.0", - "@legendapp/list": "^3.4.0", + "@legendapp/list": "^3.6.0", "@lottiefiles/dotlottie-react": "0.13.5", "@onfido/react-native-sdk": "15.1.0", "@pusher/pusher-websocket-react-native": "^1.3.1", diff --git a/patches/@legendapp/list/@legendapp+list+3.6.0+001+synchronize-fabric-content-size.patch b/patches/@legendapp/list/@legendapp+list+3.6.0+001+synchronize-fabric-content-size.patch new file mode 100644 index 000000000000..c3d167d4c3a2 --- /dev/null +++ b/patches/@legendapp/list/@legendapp+list+3.6.0+001+synchronize-fabric-content-size.patch @@ -0,0 +1,52 @@ +diff --git a/node_modules/@legendapp/list/react-native.js b/node_modules/@legendapp/list/react-native.js +index 0b730ec..9e14f1d 100644 +--- a/node_modules/@legendapp/list/react-native.js ++++ b/node_modules/@legendapp/list/react-native.js +@@ -6097,4 +6097,12 @@ function useFreshDataTransitionVisibility(readyToRender, transitionEpoch) { + // src/components/Containers.native.tsx ++// Fabric positions and the MVCP anchor update through React. Commit the content ++// size with them so an imperative Animated update cannot clamp the old viewport. ++var useContainersSize = IsNewArchitecture ? function useContainersSizeState() { ++ const [size = 0] = useArr$(["totalSize"]); ++ return size; ++} : function useContainersSizeAnimated() { ++ return useValue$("totalSize"); ++}; + var ContainersLayer = typedMemo(function ContainersLayer2({ + children, + freshDataTransitionEpoch, +@@ -6102,7 +6110,7 @@ var ContainersLayer = typedMemo(function ContainersLayer2({ + }) { + const ctx = useStateContext(); + const columnWrapperStyle = ctx.columnWrapperStyle; +- const animSize = useValue$("totalSize"); ++ const animSize = useContainersSize(); + const [readyToRender, numColumns, otherAxisSize = 0] = useArr$(["readyToRender", "numColumns", "otherAxisSize"]); + const isVisible = useFreshDataTransitionVisibility(!!readyToRender, freshDataTransitionEpoch); + const style = horizontal ? { +diff --git a/node_modules/@legendapp/list/react-native.mjs b/node_modules/@legendapp/list/react-native.mjs +index a3d8d9a..1f2bcf9 100644 +--- a/node_modules/@legendapp/list/react-native.mjs ++++ b/node_modules/@legendapp/list/react-native.mjs +@@ -6076,4 +6076,12 @@ function useFreshDataTransitionVisibility(readyToRender, transitionEpoch) { + // src/components/Containers.native.tsx ++// Fabric positions and the MVCP anchor update through React. Commit the content ++// size with them so an imperative Animated update cannot clamp the old viewport. ++var useContainersSize = IsNewArchitecture ? function useContainersSizeState() { ++ const [size = 0] = useArr$(["totalSize"]); ++ return size; ++} : function useContainersSizeAnimated() { ++ return useValue$("totalSize"); ++}; + var ContainersLayer = typedMemo(function ContainersLayer2({ + children, + freshDataTransitionEpoch, +@@ -6081,7 +6089,7 @@ var ContainersLayer = typedMemo(function ContainersLayer2({ + }) { + const ctx = useStateContext(); + const columnWrapperStyle = ctx.columnWrapperStyle; +- const animSize = useValue$("totalSize"); ++ const animSize = useContainersSize(); + const [readyToRender, numColumns, otherAxisSize = 0] = useArr$(["readyToRender", "numColumns", "otherAxisSize"]); + const isVisible = useFreshDataTransitionVisibility(!!readyToRender, freshDataTransitionEpoch); + const style = horizontal ? { diff --git a/patches/@legendapp/list/details.md b/patches/@legendapp/list/details.md new file mode 100644 index 000000000000..7629edac0980 --- /dev/null +++ b/patches/@legendapp/list/details.md @@ -0,0 +1,10 @@ +# `@legendapp/list` patches + +### [@legendapp+list+3.6.0+001+synchronize-fabric-content-size.patch](@legendapp+list+3.6.0+001+synchronize-fabric-content-size.patch) + +- Reason: On Android, the entire report-action list can briefly jump to older rows while item measurements settle. LegendList updates its content size through an imperative `Animated.Value`, while Fabric row positions and the scroll-preservation anchor update through React. An early content-size update can clamp the old viewport before the matching positions mount. Read the content size through the same React store on the New Architecture so these layout changes commit together. +- Scope: Both native module formats use the synchronized size on the New Architecture. The legacy architecture retains its animated size updates, and the web entries are unchanged. Native scroll preservation and the initial loading skeleton remain enabled. +- Upstream PR/issue: Pending submission after local validation. This patch is being developed locally as part of the LegendList migration. +- E/App issue: https://github.com/Expensify/App/pull/100733#issuecomment-5886543887 +- PR introducing patch: https://github.com/Expensify/App/pull/100733 +- Reproduction: Open an expense chat containing a report preview and messages with different heights. Wait for the actual rows to replace the skeleton. The highlighted preview should stay visible while measurements settle, without a frame displaying older rows. Also scroll through older history and return to the latest messages to check scroll preservation. From 8229bcedc1fef3592f6dfbb15da2a01716aea43d Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 8 Oct 2026 14:19:59 +0100 Subject: [PATCH 40/61] fix: keep linked messages visible after navigation and layout changes --- src/hooks/useLinkedReportActionScroll.ts | 120 +++++++++++++ src/hooks/useReportActionsScroll.ts | 66 ++++--- src/pages/inbox/ActionListTypes.ts | 1 + src/pages/inbox/report/ReportActionsList.tsx | 11 +- tests/ui/ReportActionsListTest.tsx | 29 +++ tests/unit/useReportActionsScrollTest.tsx | 180 ++++++++++++++++++- 6 files changed, 382 insertions(+), 25 deletions(-) create mode 100644 src/hooks/useLinkedReportActionScroll.ts diff --git a/src/hooks/useLinkedReportActionScroll.ts b/src/hooks/useLinkedReportActionScroll.ts new file mode 100644 index 000000000000..dca1024f4b22 --- /dev/null +++ b/src/hooks/useLinkedReportActionScroll.ts @@ -0,0 +1,120 @@ +import TransitionTracker from '@libs/Navigation/TransitionTracker'; + +import {useActionListContext} from '@pages/inbox/ActionListContext'; + +import CONST from '@src/CONST'; + +import {useIsFocused} from '@react-navigation/native'; +import {useEffect, useEffectEvent, useRef, useState} from 'react'; +import {Platform} from 'react-native'; + +import useReportScrollManager from './useReportScrollManager'; + +type LinkedReportActionScrollParams = { + listID: string; + linkedReportActionID: string | undefined; + targetIndex: number; + isLastAction: boolean; +}; + +/** Keeps a linked action positioned as its measured layout settles, until the reader takes over scrolling. */ +function useLinkedReportActionScroll({listID, linkedReportActionID, targetIndex, isLastAction}: LinkedReportActionScrollParams) { + const isFocused = useIsFocused(); + const {getListRef} = useActionListContext(); + const reportScrollManager = useReportScrollManager(); + const [readyListID, setReadyListID] = useState(); + const [measurementVersion, setMeasurementVersion] = useState(0); + const shouldPositionRef = useRef(false); + const wasLastActionRef = useRef(false); + const cancelPositionRef = useRef<(() => void) | undefined>(undefined); + + const stopPositioning = () => { + shouldPositionRef.current = false; + cancelPositionRef.current?.(); + }; + + useEffect(() => { + shouldPositionRef.current = isFocused && !!linkedReportActionID; + wasLastActionRef.current = false; + return () => { + shouldPositionRef.current = false; + cancelPositionRef.current?.(); + }; + }, [isFocused, linkedReportActionID, listID]); + + const positionLinkedAction = useEffectEvent(() => { + if (!shouldPositionRef.current || targetIndex < 0) { + return; + } + // Once a reply follows a linked final action, normal live-tail following owns the viewport. + if (wasLastActionRef.current && !isLastAction) { + shouldPositionRef.current = false; + return; + } + wasLastActionRef.current = isLastAction; + reportScrollManager.scrollToIndex( + targetIndex, + isLastAction ? {animated: false, viewPosition: 1} : {animated: false, viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}, + ); + }); + + useEffect(() => { + if (!isFocused || !linkedReportActionID || readyListID !== listID || targetIndex < 0 || !shouldPositionRef.current) { + return; + } + + let frame: number | undefined; + // Row size notifications precede LegendList's position recalculation. Wait for that layout + // and the navigation transition before using the measured target position. + const transition = TransitionTracker.runAfterTransitions({ + callback: () => { + frame = requestAnimationFrame(positionLinkedAction); + }, + }); + const cancel = () => { + transition.cancel(); + if (frame !== undefined) { + cancelAnimationFrame(frame); + } + }; + cancelPositionRef.current = cancel; + return cancel; + }, [isFocused, linkedReportActionID, listID, readyListID, targetIndex, isLastAction, measurementVersion]); + + const stopPositioningFromWebInput = useEffectEvent(stopPositioning); + useEffect(() => { + if (Platform.OS !== 'web' || readyListID !== listID) { + return; + } + const scrollElement = getListRef()?.current?.getScrollableNode?.(); + if (!(scrollElement instanceof HTMLElement)) { + return; + } + const onScrollKey = (event: KeyboardEvent) => { + if (!['ArrowUp', 'ArrowDown', 'PageUp', 'PageDown', 'Home', 'End', ' '].includes(event.key)) { + return; + } + stopPositioningFromWebInput(); + }; + scrollElement.addEventListener('wheel', stopPositioningFromWebInput, {passive: true}); + scrollElement.addEventListener('pointerdown', stopPositioningFromWebInput, {passive: true}); + scrollElement.addEventListener('keydown', onScrollKey); + return () => { + scrollElement.removeEventListener('wheel', stopPositioningFromWebInput); + scrollElement.removeEventListener('pointerdown', stopPositioningFromWebInput); + scrollElement.removeEventListener('keydown', onScrollKey); + }; + }, [getListRef, listID, readyListID]); + + const onReady = () => setReadyListID(listID); + const onItemSizeChanged = ({index, size, previous}: {index: number; size: number; previous: number}) => { + if (!shouldPositionRef.current || readyListID !== listID || index > targetIndex || size === previous) { + return; + } + setMeasurementVersion((version) => version + 1); + }; + + return {onReady, onItemSizeChanged, stopPositioning}; +} + +export default useLinkedReportActionScroll; diff --git a/src/hooks/useReportActionsScroll.ts b/src/hooks/useReportActionsScroll.ts index 497cb7844f58..30857b271f9e 100644 --- a/src/hooks/useReportActionsScroll.ts +++ b/src/hooks/useReportActionsScroll.ts @@ -31,12 +31,16 @@ import {guidedSetupAndTourStatusSelector} from '@selectors/Onboarding'; import {useEffect, useEffectEvent, useState} from 'react'; import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails'; +import useLinkedReportActionScroll from './useLinkedReportActionScroll'; import useNetworkWithOfflineStatus from './useNetworkWithOfflineStatus'; import useOnyx from './useOnyx'; import usePrevious from './usePrevious'; import useReportScrollManager from './useReportScrollManager'; type UseReportActionsScrollParams = { + /** Identifies the mounted list, including linked-message and hydration remounts. */ + listID: string; + /** The Concierge chat report */ conciergeChat: OnyxEntry; @@ -117,9 +121,14 @@ type UseReportActionsScrollResult = { /** onLoad handler that enables pill tracking after initial positioning settles */ onLoad: () => void; + + onItemSizeChanged: (info: {index: number; size: number; previous: number}) => void; + + stopLinkedMessagePositioning: () => void; }; function useReportActionsScroll({ + listID, conciergeChat, reportID, report, @@ -292,7 +301,38 @@ function useReportActionsScroll({ return () => handle?.cancel(); }, [lastAction]); + // Decide where the list should be positioned on mount. + // 1. If we're opening a linked or unread message, find that action in the chronological list. + // 2. Otherwise, aligned-to-top reports start at the first action. + const targetIndex = initialScrollKey ? renderedVisibleReportActions.findIndex((item) => keyExtractor(item) === initialScrollKey) : -1; + let initialScrollIndex: number | undefined; + let initialScrollIndexParams: {viewPosition?: number; viewOffset?: number} | undefined; + if (targetIndex >= 0) { + initialScrollIndex = targetIndex; + // A linked final action can be taller than the viewport, so show its highlighted end. + // Unread actions still start at the New marker. Omit viewOffset for bottom alignment so + // LegendList includes the footer (for example, Concierge's thinking indicator). + initialScrollIndexParams = + initialScrollKey === linkedReportActionID && targetIndex === renderedVisibleReportActions.length - 1 + ? {viewPosition: 1} + : {viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}; + } else if (shouldFocusToTopOnMount) { + initialScrollIndex = 0; + } + + const { + onReady, + onItemSizeChanged, + stopPositioning: stopLinkedMessagePositioning, + } = useLinkedReportActionScroll({ + listID, + linkedReportActionID: initialScrollKey === linkedReportActionID ? linkedReportActionID : undefined, + targetIndex, + isLastAction: targetIndex === renderedVisibleReportActions.length - 1, + }); + const scrollToBottomAndMarkReportAsRead = () => { + stopLinkedMessagePositioning(); setIsFloatingMessageCounterVisible(false); if (unreadMarkerReportActionIndex >= 0) { @@ -327,42 +367,22 @@ function useReportActionsScroll({ if (actionBadgeTargetIndex < 0) { return; } + stopLinkedMessagePositioning(); // `animated` is explicit because native defaults to an instant jump, which would teleport the list and lose the user's place. reportScrollManager.scrollToIndex(actionBadgeTargetIndex, {animated: true, viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}); }; - // Data is ready when LegendList finishes its first render. const onLoad = () => { + onReady(); if (!shouldDisablePillTracking) { return; } - - // Wait one frame so the initial positioning can settle, then disable it. requestAnimationFrame(() => { setShouldDisablePillTracking(false); updatePillVisibility(); }); }; - // Decide where the list should be positioned on mount. - // 1. If we're opening a linked or unread message, find that action in the chronological list. - // 2. Otherwise, aligned-to-top reports start at the first action. - const targetIndex = initialScrollKey ? renderedVisibleReportActions.findIndex((item) => keyExtractor(item) === initialScrollKey) : -1; - let initialScrollIndex: number | undefined; - let initialScrollIndexParams: {viewPosition?: number; viewOffset?: number} | undefined; - if (targetIndex >= 0) { - initialScrollIndex = targetIndex; - // A linked final action can be taller than the viewport, so show its highlighted end. - // Unread actions still start at the New marker. Omit viewOffset for bottom alignment so - // LegendList includes the footer (for example, Concierge's thinking indicator). - initialScrollIndexParams = - initialScrollKey === linkedReportActionID && targetIndex === renderedVisibleReportActions.length - 1 - ? {viewPosition: 1} - : {viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}; - } else if (shouldFocusToTopOnMount) { - initialScrollIndex = 0; - } - return { trackVerticalScrolling, onViewableItemsChanged, @@ -374,6 +394,8 @@ function useReportActionsScroll({ initialScrollIndex, initialScrollIndexParams, onLoad, + onItemSizeChanged, + stopLinkedMessagePositioning, }; } diff --git a/src/pages/inbox/ActionListTypes.ts b/src/pages/inbox/ActionListTypes.ts index d429168c46cb..30be95af4a9c 100644 --- a/src/pages/inbox/ActionListTypes.ts +++ b/src/pages/inbox/ActionListTypes.ts @@ -22,6 +22,7 @@ type ActionListRef = { scrollToOffset: (params: ScrollToOffsetParams) => void; scrollToEnd: (params?: ScrollToEndParams) => void; getNativeScrollRef?: () => unknown; + getScrollableNode?: () => unknown; }; /** Ref to the underlying list instance attached via `ref={}`. */ diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index 6adc81e8ad3e..442e5b995ff0 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -439,7 +439,10 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct initialScrollIndex, initialScrollIndexParams, onLoad, + onItemSizeChanged, + stopLinkedMessagePositioning, } = useReportActionsScroll({ + listID, reportID, conciergeChat, report, @@ -478,6 +481,11 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct endCorrectionFrameRef.current = undefined; }; + const handleScrollBeginDrag = () => { + stopLinkedMessagePositioning(); + stopFollowingEnd(); + }; + const scheduleEndCorrection = () => { if (readyEndListIDRef.current !== listID || !shouldFollowEndOnResizeRef.current || hasNewerActions || endCorrectionFrameRef.current !== undefined) { return; @@ -764,7 +772,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct keyboardShouldPersistTaps="handled" onLayout={recordTimeToMeasureItemLayout} onScroll={trackScrollPositionAndThreshold} - onScrollBeginDrag={stopFollowingEnd} + onScrollBeginDrag={handleScrollBeginDrag} onStartReached={loadOlderChatsOnStartReached} onStartReachedThreshold={PAGINATION_THRESHOLD} onViewableItemsChanged={updateVisibleItemOverflow} @@ -780,6 +788,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct maintainScrollAtEndThreshold={MAINTAIN_SCROLL_AT_END_THRESHOLD} maintainVisibleContentPosition onReady={handleListReady} + onItemSizeChanged={onItemSizeChanged} onContentSizeChange={() => { trackVerticalScrolling(undefined); scheduleEndCorrection(); diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 742bd6f26c5b..9ef02417ed37 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -183,6 +183,8 @@ jest.mock('@hooks/useReportActionsScroll', () => initialScrollIndex: undefined, initialScrollIndexParams: undefined, onLoad: jest.fn(), + onItemSizeChanged: jest.fn(), + stopLinkedMessagePositioning: jest.fn(), })), ); jest.mock('@pages/inbox/report/FloatingMessageCounter', () => jest.fn(() => null)); @@ -586,6 +588,33 @@ describe('ReportActionsList (body)', () => { } }); + it('keeps linked positioning active through automatic scroll metrics until the reader drags', () => { + // Given a measured linked target can cause automatic scroll compensation before its preview settles. + mockUseNetwork.mockReturnValue({isOffline: false}); + renderReportActionsList(); + const scrollHookResult = mockUseReportActionsScroll.mock.results.at(-1)?.value as ReturnType; + const stopLinkedPositioning = scrollHookResult.stopLinkedMessagePositioning; + const listProps = getCapturedListProps(); + + // When layout compensation moves the list without a user gesture. + act(() => { + for (const offset of [200, 100]) { + listProps?.onScroll?.({ + nativeEvent: { + contentOffset: {x: 0, y: offset}, + contentSize: {height: 2000, width: 300}, + layoutMeasurement: {height: 100, width: 300}, + }, + }); + } + }); + + // Then later preview measurements can still correct the target, until the reader takes over. + expect(stopLinkedPositioning).not.toHaveBeenCalled(); + act(() => listProps?.onScrollBeginDrag?.()); + expect(stopLinkedPositioning).toHaveBeenCalledTimes(1); + }); + it('does not duplicate the composer spacing inside the chronological list', () => { mockUseNetwork.mockReturnValue({isOffline: false}); renderReportActionsList(); diff --git a/tests/unit/useReportActionsScrollTest.tsx b/tests/unit/useReportActionsScrollTest.tsx index 1316c153b8be..81350e93c826 100644 --- a/tests/unit/useReportActionsScrollTest.tsx +++ b/tests/unit/useReportActionsScrollTest.tsx @@ -82,7 +82,15 @@ jest.mock('@libs/Navigation/TransitionTracker', () => ({ default: { runAfterTransitions: jest.fn(({callback}: {callback: () => void}) => { mockTransitionCallbacks.push(callback); - return {cancel: jest.fn()}; + return { + cancel: jest.fn(() => { + const index = mockTransitionCallbacks.indexOf(callback); + if (index < 0) { + return; + } + mockTransitionCallbacks.splice(index, 1); + }), + }; }), }, })); @@ -115,11 +123,13 @@ jest.mock('@userActions/Report', () => ({ // --- react-navigation route --- let mockRouteParams: {reportActionID?: string; backTo?: string; shouldScrollToLatest?: string} = {}; +let mockIsFocused = true; jest.mock('@react-navigation/native', () => { const actualNav = jest.requireActual('@react-navigation/native'); return { ...actualNav, useRoute: () => ({params: mockRouteParams}), + useIsFocused: () => mockIsFocused, }; }); @@ -171,6 +181,7 @@ function makeAction(reportActionID: string, overrides: Partial = { function buildParams(overrides: Partial = {}): ScrollParams { return { + listID: REPORT_ID, reportID: REPORT_ID, conciergeChat: undefined, report: createMockReport({reportID: REPORT_ID}), @@ -209,7 +220,7 @@ async function renderScroll(overrides: Partial = {}) { function flushTransitions() { act(() => { - for (const callback of mockTransitionCallbacks) { + for (const callback of mockTransitionCallbacks.splice(0)) { callback(); } }); @@ -226,6 +237,7 @@ describe('useReportActionsScroll', () => { await waitForBatchedUpdates(); mockTransitionCallbacks.length = 0; mockRouteParams = {}; + mockIsFocused = true; mockReportRHPActiveRoute = undefined; mockIsFloatingMessageCounterVisible = false; mockIsActionBadgeAboveViewport = false; @@ -360,6 +372,170 @@ describe('useReportActionsScroll', () => { }); }); + describe('linked message positioning', () => { + it('repositions the linked parent message when returning from a thread', async () => { + // Given the linked parent report stays mounted while its thread is open. + mockRouteParams = {reportActionID: LINKED_ACTION_ID}; + const actions = [makeAction(LINKED_ACTION_ID), makeAction('999')]; + const params = buildParams({sortedVisibleReportActions: actions.toReversed(), renderedVisibleReportActions: actions}); + const {result, rerender} = await renderScroll(params); + act(() => result.current.onLoad()); + flushTransitions(); + mockIsFocused = false; + rerender(params); + mockScrollToIndex.mockClear(); + + // When the parent link returns to that same report and the navigation transition finishes. + mockIsFocused = true; + rerender(params); + flushTransitions(); + + // Then the linked parent is positioned again instead of keeping the thread's stale offset. + expect(mockScrollToIndex).toHaveBeenCalledWith(0, { + animated: false, + viewPosition: 0, + viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET, + }); + }); + + it('repositions a linked PDF after its preview or preceding rows change height', async () => { + // Given the linked PDF is initially positioned using estimated row heights. + mockRouteParams = {reportActionID: LINKED_ACTION_ID}; + const actions = [makeAction('123'), makeAction(LINKED_ACTION_ID), makeAction('999')]; + const {result} = await renderScroll(buildParams({renderedVisibleReportActions: actions})); + act(() => result.current.onLoad()); + flushTransitions(); + mockScrollToIndex.mockClear(); + + // When the preview above it finishes measuring, then the PDF itself expands. + act(() => result.current.onItemSizeChanged({index: 0, previous: 80, size: 350})); + flushTransitions(); + expect(mockScrollToIndex).toHaveBeenCalledWith(1, {animated: false, viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}); + mockScrollToIndex.mockClear(); + act(() => result.current.onItemSizeChanged({index: 1, previous: 80, size: 350})); + flushTransitions(); + + // Then the measured target is positioned again, keeping its highlight in view. + expect(mockScrollToIndex).toHaveBeenCalledWith(1, {animated: false, viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}); + }); + + it('lets the reader scroll away before a pending preview correction runs', async () => { + // Given a preview resize has scheduled a linked-message correction. + mockRouteParams = {reportActionID: LINKED_ACTION_ID}; + const {result, rerender} = await renderScroll(buildParams({renderedVisibleReportActions: [makeAction(LINKED_ACTION_ID)]})); + act(() => result.current.onLoad()); + flushTransitions(); + mockScrollToIndex.mockClear(); + act(() => result.current.onItemSizeChanged({index: 0, previous: 80, size: 350})); + + // When the reader takes over scrolling before the navigation/layout callback runs. + act(() => result.current.stopLinkedMessagePositioning()); + flushTransitions(); + act(() => result.current.onItemSizeChanged({index: 0, previous: 350, size: 500})); + rerender(buildParams({renderedVisibleReportActions: [makeAction(LINKED_ACTION_ID)]})); + flushTransitions(); + + // Then later measurements and ordinary rerenders do not reclaim the linked position. + expect(mockScrollToIndex).not.toHaveBeenCalled(); + }); + + it.each(['latest', 'badge'] as const)('stops linked corrections after explicitly jumping to the %s target', async (destination) => { + // Given a linked message still has late preview measurements to process. + mockRouteParams = {reportActionID: LINKED_ACTION_ID}; + const {result} = await renderScroll(buildParams({renderedVisibleReportActions: [makeAction(LINKED_ACTION_ID), makeAction('999')], actionBadgeTargetIndex: 1})); + act(() => result.current.onLoad()); + flushTransitions(); + + // When the reader selects another scroll destination. + act(() => { + if (destination === 'latest') { + result.current.scrollToBottomAndMarkReportAsRead(); + return; + } + result.current.scrollToActionBadgeTarget(); + }); + mockScrollToIndex.mockClear(); + act(() => result.current.onItemSizeChanged({index: 0, previous: 80, size: 350})); + flushTransitions(); + + // Then a later measurement does not pull the list back to the old linked message. + expect(mockScrollToIndex).not.toHaveBeenCalled(); + }); + + it('does not reposition for measurements below the linked message', async () => { + // Given the linked message is already positioned. + mockRouteParams = {reportActionID: LINKED_ACTION_ID}; + const {result} = await renderScroll(buildParams({renderedVisibleReportActions: [makeAction(LINKED_ACTION_ID), makeAction('999')]})); + act(() => result.current.onLoad()); + flushTransitions(); + mockScrollToIndex.mockClear(); + + // When only a later message changes height. + act(() => result.current.onItemSizeChanged({index: 1, previous: 80, size: 350})); + flushTransitions(); + + // Then the reader's viewport is left in place. + expect(mockScrollToIndex).not.toHaveBeenCalled(); + }); + + it('leaves following a new reply to the list after positioning a linked final action', async () => { + // Given the linked final action has been positioned at the bottom. + mockRouteParams = {reportActionID: LINKED_ACTION_ID}; + const linkedAction = makeAction(LINKED_ACTION_ID); + const {result, rerender} = await renderScroll(buildParams({renderedVisibleReportActions: [linkedAction]})); + act(() => result.current.onLoad()); + flushTransitions(); + expect(mockScrollToIndex).toHaveBeenCalledWith(0, {animated: false, viewPosition: 1}); + mockScrollToIndex.mockClear(); + + // When a new reply arrives while normal end-following is active. + rerender(buildParams({renderedVisibleReportActions: [linkedAction, makeAction('999')]})); + flushTransitions(); + act(() => result.current.onItemSizeChanged({index: 0, previous: 80, size: 350})); + flushTransitions(); + + // Then the linked action does not reclaim the viewport from the new reply. + expect(mockScrollToIndex).not.toHaveBeenCalled(); + }); + + it('cancels pending positioning when the report loses focus', async () => { + // Given a linked action has a correction waiting for navigation to finish. + mockRouteParams = {reportActionID: LINKED_ACTION_ID}; + const params = buildParams({renderedVisibleReportActions: [makeAction(LINKED_ACTION_ID)]}); + const {result, rerender} = await renderScroll(params); + act(() => result.current.onLoad()); + + // When another report takes focus before the callback runs. + mockIsFocused = false; + rerender(params); + flushTransitions(); + + // Then the background list is not scrolled by a stale navigation callback. + expect(mockScrollToIndex).not.toHaveBeenCalled(); + }); + + it('waits for the replacement list to be ready after hydration', async () => { + // Given the cached list is ready and its linked message has been positioned. + mockRouteParams = {reportActionID: LINKED_ACTION_ID}; + const params = buildParams({renderedVisibleReportActions: [makeAction(LINKED_ACTION_ID)]}); + const {result, rerender} = await renderScroll(params); + act(() => result.current.onLoad()); + flushTransitions(); + mockScrollToIndex.mockClear(); + + // When hydration replaces that list with a newly mounted instance. + rerender({...params, listID: `${REPORT_ID}:hydrated`}); + act(() => result.current.onItemSizeChanged({index: 0, previous: 80, size: 350})); + flushTransitions(); + expect(mockScrollToIndex).not.toHaveBeenCalled(); + act(() => result.current.onLoad()); + flushTransitions(); + + // Then the correction uses the replacement list only after it is ready. + expect(mockScrollToIndex).toHaveBeenCalledWith(0, {animated: false, viewPosition: 1}); + }); + }); + describe('scrollToBottomAndMarkReportAsRead', () => { it('scrolls to bottom and marks as read when the newest action is present', async () => { // Default created === lastVisibleActionCreated → newest present. From ed4e31af1b125ba4de0d0fd29784489f378a78f4 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 8 Oct 2026 16:21:30 +0100 Subject: [PATCH 41/61] fix: emit web scroll events for chronological lists Suppress hover throughout scrolling in the shared list wrappers after the chronological migration. Restore report-list render benchmarks and exercise the production start-reached pagination callback. --- src/components/FlashList/index.tsx | 2 +- src/components/FlatList/FlatList/index.tsx | 2 +- .../KeyboardDismissibleFlatList/index.tsx | 4 +- .../MoneyRequestReportActionsList.tsx | 2 +- .../MoneyRequestReportUnifiedList.tsx | 8 +- .../perf-test/ReportActionsList.perf-test.tsx | 166 ++++++++++++++++++ tests/ui/PaginationTest.tsx | 4 +- tests/ui/ReportActionsListTest.tsx | 23 +-- tests/unit/ComposerScrollEventsTest.tsx | 117 ++++++++++++ 9 files changed, 295 insertions(+), 33 deletions(-) create mode 100644 tests/perf-test/ReportActionsList.perf-test.tsx create mode 100644 tests/unit/ComposerScrollEventsTest.tsx diff --git a/src/components/FlashList/index.tsx b/src/components/FlashList/index.tsx index f9a7b9528735..59291cdb3fe2 100644 --- a/src/components/FlashList/index.tsx +++ b/src/components/FlashList/index.tsx @@ -7,7 +7,7 @@ import {FlashList as ShopifyFlashList} from '@shopify/flash-list'; import React from 'react'; function FlashList({onScroll: onScrollProp, inverted, ...restProps}: FlashListProps) { - const emitComposerScrollEvents = useEmitComposerScrollEvents({enabled: !!inverted}); + const emitComposerScrollEvents = useEmitComposerScrollEvents(); const handleScroll = (e: NativeSyntheticEvent) => { onScrollProp?.(e); diff --git a/src/components/FlatList/FlatList/index.tsx b/src/components/FlatList/FlatList/index.tsx index c50c521eee01..682d40174bd9 100644 --- a/src/components/FlatList/FlatList/index.tsx +++ b/src/components/FlatList/FlatList/index.tsx @@ -223,7 +223,7 @@ function MVCPFlatList({ }; }, []); - const emitComposerScrollEvents = useEmitComposerScrollEvents({enabled: !!restProps.inverted}); + const emitComposerScrollEvents = useEmitComposerScrollEvents(); const handleScroll = useCallback( (e: NativeSyntheticEvent) => { onScrollProp?.(e); diff --git a/src/components/KeyboardDismissibleFlatList/index.tsx b/src/components/KeyboardDismissibleFlatList/index.tsx index 3e8109d2d0e6..d48e9f7b96e6 100644 --- a/src/components/KeyboardDismissibleFlatList/index.tsx +++ b/src/components/KeyboardDismissibleFlatList/index.tsx @@ -8,10 +8,10 @@ import {useAnimatedScrollHandler, useComposedEventHandler} from 'react-native-re import {useKeyboardDismissibleFlatListActions} from './KeyboardDismissibleFlatListContext'; -function KeyboardDismissibleFlatList({onScroll: onScrollProp, inverted, ref, ...restProps}: AnimatedFlatListWithCellRendererProps) { +function KeyboardDismissibleFlatList({onScroll: onScrollProp, ref, ...restProps}: AnimatedFlatListWithCellRendererProps) { const {onScroll: onScrollHandleKeyboard} = useKeyboardDismissibleFlatListActions(); - const emitComposerScrollEvents = useEmitComposerScrollEvents({enabled: !!inverted}); + const emitComposerScrollEvents = useEmitComposerScrollEvents(); const additionalOnScroll = useAnimatedScrollHandler({ onScroll: emitComposerScrollEvents, diff --git a/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx b/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx index 5344b559e761..63fb56f6ccff 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx @@ -346,7 +346,7 @@ function MoneyRequestReportActionsListContent({reportIDFromRoute, onLayout}: Mon contentContainerStyle={[shouldUseNarrowLayout ? styles.pt4 : styles.pt3, shouldReserveBulkActionBarSpace && styles.bulkActionBarListSpacing]} isLoadingInitialActions={isInitialReportLoadPending} /* This list is not inverted, so the footer is the bottom of the message feed — - the same position the indicator occupies in the inverted ReportActionsList. */ + the same position the indicator occupies in ReportActionsList. */ listFooterComponent={} /> )} diff --git a/src/components/MoneyRequestReportView/MoneyRequestReportUnifiedList.tsx b/src/components/MoneyRequestReportView/MoneyRequestReportUnifiedList.tsx index e17420395d31..c4fe72bf25f2 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestReportUnifiedList.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestReportUnifiedList.tsx @@ -48,15 +48,13 @@ function unifiedListItemType(item: UnifiedListItem) { } type MoneyRequestReportFlashListProps = FlashListProps & { - /** Ref to the underlying list, shared via the ActionList context (typed for the legacy FlatList). */ + /** Ref to the underlying list, shared via the common ActionList imperative API. */ ref: ActionListRefType; }; /** - * Forwards the shared ActionList context ref to the underlying FlashList. That context slot predates this FlashList-based - * list and is still shared with the legacy report list, so it is typed for a FlatList. Mirroring InvertedFlashList, the - * ref is forwarded through @components/FlashList — which receives it as an untyped runtime prop — so no type assertion is - * needed. The scroll manager relies on the FlashList registering into this slot. + * Forwards the shared ActionList ref through @components/FlashList to the underlying list. + * FlashList implements the common imperative API consumed by the report scroll manager. */ function MoneyRequestReportFlashList(props: MoneyRequestReportFlashListProps) { return ( diff --git a/tests/perf-test/ReportActionsList.perf-test.tsx b/tests/perf-test/ReportActionsList.perf-test.tsx new file mode 100644 index 000000000000..78ba39d7e827 --- /dev/null +++ b/tests/perf-test/ReportActionsList.perf-test.tsx @@ -0,0 +1,166 @@ +import {act, screen} from '@testing-library/react-native'; + +import OnyxListItemProvider from '@components/OnyxListItemProvider'; + +import navigationRef from '@libs/Navigation/navigationRef'; +import {setHasRadio} from '@libs/NetworkState'; + +import {ActionListContext} from '@pages/inbox/ActionListContext'; +import {ReactionListContext} from '@pages/inbox/ReactionListContext'; +import ReportActionsList from '@pages/inbox/report/ReportActionsList'; +import {AttachmentModalContextProvider} from '@pages/media/AttachmentModalScreen/AttachmentModalContext'; + +import ComposeProviders from '@src/components/ComposeProviders'; +import {LocaleContextProvider} from '@src/components/LocaleContextProvider'; +import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {ReportActions} from '@src/types/onyx'; + +import type * as ReactNavigation from '@react-navigation/native'; + +import {NavigationContainer} from '@react-navigation/native'; +import Onyx from 'react-native-onyx'; +import {measureRenders} from 'reassure'; + +import * as ReportTestUtils from '../utils/ReportTestUtils'; +import * as TestHelper from '../utils/TestHelper'; +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; +import wrapOnyxWithWaitForBatchedUpdates from '../utils/wrapOnyxWithWaitForBatchedUpdates'; + +jest.mock('@react-navigation/native', () => { + const actualNav = jest.requireActual('@react-navigation/native'); + return { + ...actualNav, + useRoute: () => ({key: 'report', name: 'Report', params: {reportID: '1'}}), + useIsFocused: () => true, + }; +}); + +beforeAll(() => + Onyx.init({ + keys: ONYXKEYS, + evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS], + }), +); + +const mockOnLayout = jest.fn(); +// Built via a function so the value isn't an inline literal the context-split lint rule would flag; these are all refs/accessors with no re-render concern. +function buildActionListContextValue() { + return {scrollOffsetRef: {current: 0}, getScrollOffset: () => 0, registerListRef: () => {}, getListRef: () => null}; +} +const actionListContextValue = buildActionListContextValue(); +const mockReactionListContextValue = { + showReactionList: () => {}, + hideReactionList: () => {}, + isActiveReportAction: () => false, +}; + +const TEST_USER_ACCOUNT_ID = 1; +const TEST_USER_LOGIN = 'test@test.com'; +const REPORT_ID = '1'; + +const signUpWithTestUser = () => { + TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN); +}; + +function buildComment(index: number) { + const text = `Benchmark message ${index}`; + return ReportTestUtils.getFakeReportAction(index, { + actionName: index === 1 ? CONST.REPORT.ACTIONS.TYPE.CREATED : CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, + actorAccountID: TEST_USER_ACCOUNT_ID, + created: new Date(Date.UTC(2026, 0, 1, 0, 0, index)).toISOString().replace('T', ' ').replace('Z', ''), + isAttachmentOnly: false, + message: [{type: 'COMMENT', html: text, text}], + }); +} + +const actions = Array.from({length: 500}, (_unused, index) => buildComment(index + 1)); +const reportActions: ReportActions = Object.fromEntries(actions.map((action) => [action.reportActionID, action])); +const report = ReportTestUtils.createMockReport({reportID: REPORT_ID, lastVisibleActionCreated: actions.at(-1)?.created}); + +beforeEach(async () => { + // Initialize the network key for OfflineWithFeedback + setHasRadio(true); + wrapOnyxWithWaitForBatchedUpdates(Onyx); + // Pre-seed the locale so LocaleContextProvider's mount effect is a no-op (setLocale early-returns), + // avoiding post-mount Onyx writes that would re-render outside act(). + await act(async () => { + signUpWithTestUser(); + await Onyx.merge(ONYXKEYS.NVP_PREFERRED_LOCALE, CONST.LOCALES.DEFAULT); + + // Seed the report under test: the report, its 500 actions, and a settled loading state. + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, reportActions); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES}${REPORT_ID}`, [ + [CONST.PAGINATION_START_ID, ...actions.map((action) => action.reportActionID).reverse(), CONST.PAGINATION_END_ID], + ]); + await Onyx.set(`${ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE}${REPORT_ID}`, { + isLoadingInitialReportActions: false, + hasOnceLoadedReportActions: true, + isLoadingOlderReportActions: false, + hasLoadingOlderReportActionsError: false, + isLoadingNewerReportActions: false, + hasLoadingNewerReportActionsError: false, + }); + await waitForBatchedUpdates(); + }); +}); + +afterEach(async () => { + // Await the clear so its broadcasts settle in teardown instead of leaking into the next test. + await Onyx.clear(); + await waitForBatchedUpdates(); +}); + +// Mirror the production data flow (usePaginatedReportActions): the body reads REPORT_ACTIONS from Onyx and +// sorts inside a useOnyx selector, then runs it through the real continuous-chain pagination and the +// visibility filter. +function ReportActionsListWrapper() { + return ( + + + + + + + + + + ); +} + +// LegendList's behavior mock eagerly renders the supplied data, while the previous FlashList test used +// its virtualized implementation. Start a renderer-specific baseline instead of comparing those harnesses. +test('[ReportActionsList] should render LegendList with 500 reportActions stored', async () => { + // Given a settled report whose complete 500-action page is stored in Onyx. + const scenario = async () => { + // When the report list mounts, then its final comment must actually render. + await screen.findByTestId('report-actions-list'); + await screen.findByText('Benchmark message 500'); + }; + await waitForBatchedUpdates(); + await measureRenders(, {scenario}); +}); + +test('[ReportActionsList] should render a new comment in a report with 500 stored actions', async () => { + // Given a mounted report with 500 stored actions. + let run = 0; + const scenario = async () => { + await screen.findByTestId('report-actions-list'); + // When a new comment arrives, update the real Onyx collection and the report's latest timestamp. + // Reassure repeats this scenario, so every run must append a new action to the continuous chain. + const action = buildComment(501 + run++); + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, {[action.reportActionID]: action}); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, {lastVisibleActionCreated: action.created}); + await waitForBatchedUpdates(); + }); + // Then the new comment renders before this measurement completes. + await screen.findByText(`Benchmark message ${action.reportActionID}`); + }; + await measureRenders(, {scenario}); +}); diff --git a/tests/ui/PaginationTest.tsx b/tests/ui/PaginationTest.tsx index e28f34f653ba..8331a9ee7247 100644 --- a/tests/ui/PaginationTest.tsx +++ b/tests/ui/PaginationTest.tsx @@ -344,8 +344,8 @@ describe('Pagination', () => { TestHelper.expectAPICommandToHaveBeenCalled('GetOlderActions', 0); TestHelper.expectAPICommandToHaveBeenCalled('GetNewerActions', 0); - // Scrolling here should trigger a new network request. - scrollToOffset(0); + // Reaching the list start must load older history without relying on the onScroll fallback. + fireEvent(within(getReportScreen()).getByLabelText(TestHelper.translateLocal('sidebarScreen.listOfChatMessages')), 'onStartReached'); await waitForBatchedUpdatesWithAct(); TestHelper.expectAPICommandToHaveBeenCalled('OpenReport', 1); diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 9ef02417ed37..3dfbb59608e4 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -442,15 +442,6 @@ describe('ReportActionsList (body)', () => { await Onyx.clear(); }); - it('delegates new-action following to LegendList without a full-viewport threshold', () => { - mockUseNetwork.mockReturnValue({isOffline: false}); - renderReportActionsList(); - - expect(getCapturedListProps()?.maintainScrollAtEnd).toEqual({animated: false, on: {dataChange: true}}); - expect(getCapturedListProps()?.maintainScrollAtEndThreshold).toBe(0.01); - expect(getCapturedListProps()?.maintainVisibleContentPosition).toBe(true); - }); - it('corrects the initial end position after the latest row has been measured', async () => { // Given a report opens at its latest action and the virtualized list finishes its initial placement. mockUseNetwork.mockReturnValue({isOffline: false}); @@ -635,16 +626,6 @@ describe('ReportActionsList (body)', () => { expect(getRenderedReportActionsListItemProps(markerAction, 1).shouldDisplayNewMarker).toBe(true); }); - it('initially aligns the seed page to the end', () => { - mockUseNetwork.mockReturnValue({isOffline: false}); - mockHasOnceLoadedReportActions = false; - renderReportActionsList(); - - expect(getCapturedListProps()?.initialScrollAtEnd).toBe(true); - expect(getCapturedListProps()?.alignItemsAtEnd).toBe(true); - expect(getCapturedListProps()?.maintainScrollAtEnd).toEqual({animated: false, on: {dataChange: true}}); - }); - it('does not follow the end of a page that still has newer actions to load', () => { mockUseNetwork.mockReturnValue({isOffline: false}); mockUsePaginatedReportActions.mockReturnValue({ @@ -665,6 +646,7 @@ describe('ReportActionsList (body)', () => { expect(mockLegendListMount).toHaveBeenCalledTimes(1); expect(mockLegendListUnmount).not.toHaveBeenCalled(); + mockLegendScrollToEnd.mockClear(); mockHasOnceLoadedReportActions = true; // The mocked Onyx hook does not own state, so changing its return value cannot schedule the @@ -680,8 +662,7 @@ describe('ReportActionsList (body)', () => { expect(mockLegendListMount).toHaveBeenCalledTimes(2); expect(mockLegendListUnmount).toHaveBeenCalledTimes(1); - expect(getCapturedListProps()?.initialScrollAtEnd).toBe(true); - expect(getCapturedListProps()?.maintainScrollAtEnd).toEqual({animated: false, on: {dataChange: true}}); + await waitFor(() => expect(mockLegendScrollToEnd).toHaveBeenCalledWith({animated: false})); }); it('keeps the initial viewport covered until the hydrated LegendList finishes rendering it', async () => { diff --git a/tests/unit/ComposerScrollEventsTest.tsx b/tests/unit/ComposerScrollEventsTest.tsx new file mode 100644 index 000000000000..6ee1540209a4 --- /dev/null +++ b/tests/unit/ComposerScrollEventsTest.tsx @@ -0,0 +1,117 @@ +import {act, fireEvent, render, screen} from '@testing-library/react-native'; + +import FlashList from '@components/FlashList'; +import MVCPFlatList from '@components/FlatList/FlatList'; +import type * as MVCPList from '@components/FlatList/FlatList'; +import ActiveHoverable from '@components/Hoverable/ActiveHoverable'; +import type * as KeyboardList from '@components/KeyboardDismissibleFlatList'; +import KeyboardDismissibleFlatList from '@components/KeyboardDismissibleFlatList/index'; + +import type * as ComposerScrollEvents from '@hooks/useEmitComposerScrollEvents'; + +import CONST from '@src/CONST'; + +import type {NativeScrollEvent, NativeSyntheticEvent, ScrollViewProps, ViewProps} from 'react-native'; +import type * as Reanimated from 'react-native-reanimated'; + +import React from 'react'; +import {DeviceEventEmitter, View} from 'react-native'; + +jest.mock('@hooks/useEmitComposerScrollEvents', () => jest.requireActual('@hooks/useEmitComposerScrollEvents/index.ts')); +jest.mock('@components/KeyboardDismissibleFlatList/index', () => jest.requireActual('@components/KeyboardDismissibleFlatList/index.tsx')); +jest.mock('@components/FlatList/FlatList', () => jest.requireActual('@components/FlatList/FlatList/index.tsx')); +jest.mock('@hooks/useThemeStyles', () => () => ({})); +jest.mock('@shopify/flash-list', () => ({ + FlashList: (props: ScrollViewProps) => { + const {ScrollView} = jest.requireActual<{ScrollView: React.ComponentType}>('react-native'); + return ; + }, +})); +jest.mock('@components/AnimatedFlatListWithCellRenderer', () => (props: ScrollViewProps) => { + const {ScrollView} = jest.requireActual<{ScrollView: React.ComponentType}>('react-native'); + return ; +}); +jest.mock('react-native-reanimated', () => ({ + ...jest.requireActual('react-native-reanimated/mock'), + useAnimatedScrollHandler: ({onScroll}: {onScroll: (event: NativeSyntheticEvent) => void}) => onScroll, + useComposedEventHandler: (handlers: Array<((event: NativeSyntheticEvent) => void) | null>) => (event: NativeSyntheticEvent) => { + for (const handler of handlers) { + handler?.(event); + } + }, +})); + +const scrollEvent = { + nativeEvent: { + contentOffset: {x: 0, y: 100}, + contentSize: {width: 300, height: 1000}, + layoutMeasurement: {width: 300, height: 500}, + }, +}; + +function HoverTarget({ref, ...props}: ViewProps & {ref?: React.Ref}) { + // Supply the DOM ref used by web hover handling while rendering through the native test renderer. + React.useImperativeHandle(ref, () => document.createElement('div'), []); + return ; +} + +describe('Composer scroll events on chronological lists', () => { + beforeEach(() => jest.useFakeTimers()); + afterEach(() => { + jest.clearAllTimers(); + jest.useRealTimers(); + jest.restoreAllMocks(); + }); + + it.each([ + ['FlashList', FlashList], + ['MVCP FlatList', MVCPFlatList], + ['keyboard dismissible FlatList', KeyboardDismissibleFlatList], + ] as const)('%s suppresses hover throughout scrolling and forwards the scroll event', (_name, List) => { + // Given a chronological list, as used by expense chats after the migration. + const emit = jest.spyOn(DeviceEventEmitter, 'emit'); + const onScroll = jest.fn(); + render( + <> + } + inverted={false} + onScroll={onScroll} + /> + + {(isHovered) => ( + + )} + + , + ); + + // When scrolling continues, hover must remain suppressed until the last event settles. + fireEvent(screen.getByTestId('hoverable-message'), 'mouseEnter'); + expect(screen.getByTestId('hoverable-message').props.accessibilityLabel).toBe('hovered'); + fireEvent.scroll(screen.getByTestId('chronological-list'), scrollEvent); + expect(onScroll).toHaveBeenCalledWith(scrollEvent); + expect(emit).toHaveBeenCalledWith(CONST.EVENTS.SCROLLING, true); + expect(screen.getByTestId('hoverable-message').props.accessibilityLabel).toBe('idle'); + act(() => jest.advanceTimersByTime(200)); + fireEvent.scroll(screen.getByTestId('chronological-list'), scrollEvent); + fireEvent(screen.getByTestId('hoverable-message'), 'mouseEnter'); + expect(screen.getByTestId('hoverable-message').props.accessibilityLabel).toBe('idle'); + act(() => jest.advanceTimersByTime(200)); + expect(emit).not.toHaveBeenCalledWith(CONST.EVENTS.SCROLLING, false); + + // Then hover resumes once, after scrolling ends. + act(() => jest.advanceTimersByTime(50)); + expect(emit.mock.calls.filter(([event]) => event === CONST.EVENTS.SCROLLING)).toEqual([ + [CONST.EVENTS.SCROLLING, true], + [CONST.EVENTS.SCROLLING, false], + ]); + fireEvent(screen.getByTestId('hoverable-message'), 'mouseEnter'); + expect(screen.getByTestId('hoverable-message').props.accessibilityLabel).toBe('hovered'); + }); +}); From d25f5b663d5c6be6eaf95ad9c9f922a7a20573cb Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Fri, 9 Oct 2026 14:29:30 +0100 Subject: [PATCH 42/61] fix: preserve linked-message position through late layout --- patches/react-native/details.md | 2 +- src/hooks/useLinkedReportActionScroll.ts | 20 +++++- src/hooks/useReportActionsScroll.ts | 4 ++ src/pages/inbox/ActionListTypes.ts | 2 + src/pages/inbox/report/ReportActionsList.tsx | 10 ++- .../useReportActionsNewActionLiveTail.ts | 6 ++ tests/ui/ReportActionsListTest.tsx | 72 ++++++++++++++++++- .../useReportActionsNewActionLiveTailTest.ts | 15 ++++ tests/unit/useReportActionsScrollTest.tsx | 44 +++++++++++- 9 files changed, 170 insertions(+), 5 deletions(-) diff --git a/patches/react-native/details.md b/patches/react-native/details.md index 40fe8cf38219..bea8a0402cf5 100644 --- a/patches/react-native/details.md +++ b/patches/react-native/details.md @@ -12,7 +12,7 @@ - Reason: Fixes content jumping issues with `MaintainVisibleContentPosition` on Android, particularly in bidirectional pagination scenarios. The patch: 1. Changes when the first visible view is calculated - now happens on scroll events instead of during Fabric's willMountItems lifecycle, which was causing incorrect updates 2. Improves first visible view selection logic to handle Fabric's z-index-based view reordering by finding the view with the smallest position that's still greater than the scroll position - 3. Preserves a positioned, zero-sized first child as a scroll anchor. LegendList moves this anchor to compensate for item measurements. Selecting its surrounding container or rejecting its empty frame prevents native scroll compensation and makes the chat jump as estimated rows shrink. + 3. Preserves a positioned, zero-sized first child as a scroll anchor. LegendList moves this anchor to compensate for item measurements. Selecting its surrounding container or rejecting its empty frame prevents native scroll compensation and makes the chat jump as estimated rows shrink. LegendList 3.6 places the anchor at its adjustment offset plus 10,000,000 pixels, beyond the viewport. The strict visibility check also prevents a fixed, zero-sized spacer at or above the scroll offset in another scroll view from taking over its visible-content anchor. - Scope: This changes Android `MaintainVisibleContentPosition` for every React Native scroll view that enables it, not only the report-action list. Keep this in mind when updating React Native or investigating anchoring in other scroll views. - Upstream PR/issue: https://github.com/facebook/react-native/pull/46247 (closed without merging; the zero-sized LegendList anchor fix is not covered there) - E/App issue: 🛑 diff --git a/src/hooks/useLinkedReportActionScroll.ts b/src/hooks/useLinkedReportActionScroll.ts index dca1024f4b22..f1c89d41e997 100644 --- a/src/hooks/useLinkedReportActionScroll.ts +++ b/src/hooks/useLinkedReportActionScroll.ts @@ -4,6 +4,8 @@ import {useActionListContext} from '@pages/inbox/ActionListContext'; import CONST from '@src/CONST'; +import type {NativeScrollEvent, NativeSyntheticEvent} from 'react-native'; + import {useIsFocused} from '@react-navigation/native'; import {useEffect, useEffectEvent, useRef, useState} from 'react'; import {Platform} from 'react-native'; @@ -107,6 +109,22 @@ function useLinkedReportActionScroll({listID, linkedReportActionID, targetIndex, }, [getListRef, listID, readyListID]); const onReady = () => setReadyListID(listID); + const onScroll = (event: NativeSyntheticEvent) => { + if (!shouldPositionRef.current || readyListID !== listID || !linkedReportActionID || isLastAction) { + return; + } + const position = getListRef()?.current?.getState?.()?.positionByKey(linkedReportActionID); + if (position === undefined) { + return; + } + const {contentOffset, contentSize, layoutMeasurement} = event.nativeEvent; + const targetOffset = Math.max(0, Math.min(position - CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET, contentSize.height - layoutMeasurement.height)); + // Native anchoring can commit after the row-size correction. Check the resulting viewport + // as well, so that late adjustment cannot move a successfully positioned link out of view. + if (Math.abs(targetOffset - contentOffset.y) > 1) { + setMeasurementVersion((version) => version + 1); + } + }; const onItemSizeChanged = ({index, size, previous}: {index: number; size: number; previous: number}) => { if (!shouldPositionRef.current || readyListID !== listID || index > targetIndex || size === previous) { return; @@ -114,7 +132,7 @@ function useLinkedReportActionScroll({listID, linkedReportActionID, targetIndex, setMeasurementVersion((version) => version + 1); }; - return {onReady, onItemSizeChanged, stopPositioning}; + return {onReady, onItemSizeChanged, onScroll, stopPositioning}; } export default useLinkedReportActionScroll; diff --git a/src/hooks/useReportActionsScroll.ts b/src/hooks/useReportActionsScroll.ts index 30857b271f9e..5555ee471793 100644 --- a/src/hooks/useReportActionsScroll.ts +++ b/src/hooks/useReportActionsScroll.ts @@ -124,6 +124,8 @@ type UseReportActionsScrollResult = { onItemSizeChanged: (info: {index: number; size: number; previous: number}) => void; + trackLinkedMessageScroll: (event: NativeSyntheticEvent) => void; + stopLinkedMessagePositioning: () => void; }; @@ -323,6 +325,7 @@ function useReportActionsScroll({ const { onReady, onItemSizeChanged, + onScroll: trackLinkedMessageScroll, stopPositioning: stopLinkedMessagePositioning, } = useLinkedReportActionScroll({ listID, @@ -395,6 +398,7 @@ function useReportActionsScroll({ initialScrollIndexParams, onLoad, onItemSizeChanged, + trackLinkedMessageScroll, stopLinkedMessagePositioning, }; } diff --git a/src/pages/inbox/ActionListTypes.ts b/src/pages/inbox/ActionListTypes.ts index 30be95af4a9c..b366b0686c43 100644 --- a/src/pages/inbox/ActionListTypes.ts +++ b/src/pages/inbox/ActionListTypes.ts @@ -23,6 +23,8 @@ type ActionListRef = { scrollToEnd: (params?: ScrollToEndParams) => void; getNativeScrollRef?: () => unknown; getScrollableNode?: () => unknown; + /** Measured item positions supplied by LegendList. Other list implementations can omit this. */ + getState?: () => {positionByKey: (key: string) => number | undefined} | undefined; }; /** Ref to the underlying list instance attached via `ref={}`. */ diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index 442e5b995ff0..4098ced5dfee 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -258,6 +258,8 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct listRef, (): ActionListRef => ({ getNativeScrollRef: () => legendListRef.current?.getNativeScrollRef(), + getScrollableNode: (): unknown => legendListRef.current?.getScrollableNode(), + getState: () => legendListRef.current?.getState(), scrollToEnd: (options) => { legendListRef.current?.scrollToEnd(options); }, @@ -355,7 +357,11 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct // already-visible page mounted until hydration finishes instead of exposing intermediate estimated // layouts. The hydrated list then mounts from scratch using the full dataset. const initialReportActionsSnapshot = useInitial(renderedVisibleReportActions.length > 0 ? renderedVisibleReportActions : undefined); - const shouldKeepInitialSnapshot = isInitialReportLoadPending && !isOffline && !showFullHistory; + const [hasReleasedInitialSnapshot, setHasReleasedInitialSnapshot] = useState(isOffline || !isInitialReportLoadPending || showFullHistory); + if (!hasReleasedInitialSnapshot && (isOffline || !isInitialReportLoadPending || showFullHistory)) { + setHasReleasedInitialSnapshot(true); + } + const shouldKeepInitialSnapshot = !hasReleasedInitialSnapshot; const reportActionsToRender = shouldKeepInitialSnapshot ? (initialReportActionsSnapshot ?? renderedVisibleReportActions) : renderedVisibleReportActions; // Report actions are stored newest-first. LegendList intentionally has no inverted mode, so @@ -440,6 +446,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct initialScrollIndexParams, onLoad, onItemSizeChanged, + trackLinkedMessageScroll, stopLinkedMessagePositioning, } = useReportActionsScroll({ listID, @@ -541,6 +548,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct }; const trackScrollPositionAndThreshold = (event: NativeSyntheticEvent) => { + trackLinkedMessageScroll(event); const {contentOffset, contentSize, layoutMeasurement} = event.nativeEvent; const distanceFromBottom = Math.max(0, contentSize.height - layoutMeasurement.height - contentOffset.y); distanceFromBottomRef.current = distanceFromBottom; diff --git a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts index 5b58bb160caa..695da263499f 100644 --- a/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts +++ b/src/pages/inbox/report/useReportActionsNewActionLiveTail.ts @@ -95,6 +95,12 @@ function useReportActionsNewActionLiveTail({ const liveTailJumpRef = useRef<{stage: LiveTailJumpStage}>({stage: 'idle'}); const [isScrollToBottomEnabled, setIsScrollToBottomEnabled] = useState(false); const [pendingScrollActionID, setPendingScrollActionID] = useState(); + const [scrollRequestReportID, setScrollRequestReportID] = useState(reportID); + if (scrollRequestReportID !== reportID) { + setScrollRequestReportID(reportID); + setIsScrollToBottomEnabled(false); + setPendingScrollActionID(undefined); + } const setShouldScrollToBottom = useCallback((enabled: boolean) => { setIsScrollToBottomEnabled(enabled); diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 3dfbb59608e4..f4b6d54b6cd0 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -20,6 +20,8 @@ import useTransactionsAndViolationsForReport from '@hooks/useTransactionsAndViol import DateUtils from '@libs/DateUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; +import {ActionListContext} from '@pages/inbox/ActionListContext'; +import type ActionListRefType from '@pages/inbox/ActionListTypes'; import {useConciergeDraft, useConciergeDraftActions} from '@pages/inbox/ConciergeDraftContext'; import {useConciergeSessionActions, useConciergeSessionState} from '@pages/inbox/ConciergeSessionContext'; import ReportActionsList from '@pages/inbox/report/ReportActionsList'; @@ -145,11 +147,14 @@ const mockUseCurrentUserPersonalDetails = useCurrentUserPersonalDetails as jest. const mockLegendListMount = jest.fn(); const mockLegendListUnmount = jest.fn(); const mockLegendScrollToEnd = jest.fn(); +const mockLegendPositionByKey = jest.fn(() => 1500); +const mockLegendGetState = jest.fn(() => ({positionByKey: mockLegendPositionByKey})); +const mockLegendGetScrollableNode = jest.fn(() => ({scrollTop: 0})); let mockShouldCallLegendListOnLoad = true; jest.mock('@legendapp/list/react-native', () => { const reactModule = jest.requireActual('react'); const MockLegendListContent = reactModule.forwardRef<{scrollToEnd: typeof mockLegendScrollToEnd}, {onLoad?: () => void; onReady?: () => void}>(({onLoad, onReady}, ref) => { - reactModule.useImperativeHandle(ref, () => ({scrollToEnd: mockLegendScrollToEnd}), []); + reactModule.useImperativeHandle(ref, () => ({scrollToEnd: mockLegendScrollToEnd, getState: mockLegendGetState, getScrollableNode: mockLegendGetScrollableNode}), []); reactModule.useEffect(() => { mockLegendListMount(); if (mockShouldCallLegendListOnLoad) { @@ -173,6 +178,7 @@ jest.mock('@hooks/useMarkAsRead', () => jest.fn(() => ({markNewestActionAsRead: jest.mock('@hooks/useReportActionsScroll', () => jest.fn(() => ({ listRef: {current: null}, + trackLinkedMessageScroll: jest.fn(), trackVerticalScrolling: jest.fn(), onViewableItemsChanged: jest.fn(), isFloatingMessageCounterVisible: false, @@ -442,6 +448,34 @@ describe('ReportActionsList (body)', () => { await Onyx.clear(); }); + it('publishes measured positions and the scroll element through the action-list adapter', () => { + // Given a mounted LegendList with measured rows and its own scroll element. + mockUseNetwork.mockReturnValue({isOffline: false}); + const registerListRef = jest.fn(); + const contextValue = {scrollOffsetRef: {current: 0}, getScrollOffset: () => 0, getListRef: () => null, registerListRef}; + render( + // The shared context intentionally combines stable refs with their accessors. + // eslint-disable-next-line rulesdir/context-provider-split-values + + + , + ); + + // When linked positioning reads the registered adapter rather than the raw LegendList ref. + const adapter = registerListRef.mock.calls.at(-1)?.at(0)?.current; + const position = adapter?.getState?.()?.positionByKey('linked-action'); + const scrollElement = adapter?.getScrollableNode?.(); + + // Then measurement recovery and web input cancellation can reach the real list. + expect(position).toBe(1500); + expect(mockLegendPositionByKey).toHaveBeenCalledWith('linked-action'); + expect(scrollElement).toEqual({scrollTop: 0}); + expect(mockLegendGetScrollableNode).toHaveBeenCalledTimes(1); + }); + it('corrects the initial end position after the latest row has been measured', async () => { // Given a report opens at its latest action and the virtualized list finishes its initial placement. mockUseNetwork.mockReturnValue({isOffline: false}); @@ -919,6 +953,42 @@ describe('ReportActionsList (body)', () => { expect(getCapturedVisibleActions()?.at(-1)?.reportActionID).toBe('new-comment'); }); + it('keeps offline additions visible when reconnecting during the initial load', async () => { + // Given a cached report whose initial request is still pending. + mockUseNetwork.mockReturnValue({isOffline: false}); + mockHasOnceLoadedReportActions = false; + mockIsLoadingInitialReportActions = true; + const view = renderReportActionsList(); + const existingAction = mockReportActions.at(1); + if (!existingAction) { + throw new Error('Expected a cached report action'); + } + const renderList = () => + view.rerender( + , + ); + + // When an action is added offline and the connection returns before hydration. + mockUseNetwork.mockReturnValue({isOffline: true}); + mockUsePaginatedReportActions.mockReturnValue({ + ...defaultPaginatedReportActionsResult, + reportActions: [{...existingAction, reportActionID: 'offline-comment', created: '2023-01-03'}, ...mockReportActions], + }); + renderList(); + await waitForBatchedUpdatesWithAct(); + expect(getCapturedVisibleActions()?.at(-1)?.reportActionID).toBe('offline-comment'); + mockUseNetwork.mockReturnValue({isOffline: false}); + renderList(); + await waitForBatchedUpdatesWithAct(); + + // Then the list does not revert to the earlier cached snapshot. + expect(getCapturedVisibleActions()?.at(-1)?.reportActionID).toBe('offline-comment'); + }); + it('keeps a hydrated list mounted when its RAM-only loading entry briefly regresses', async () => { mockUseNetwork.mockReturnValue({isOffline: false}); const view = renderReportActionsList(); diff --git a/tests/unit/useReportActionsNewActionLiveTailTest.ts b/tests/unit/useReportActionsNewActionLiveTailTest.ts index 641b4dd2caef..df4ecb40108a 100644 --- a/tests/unit/useReportActionsNewActionLiveTailTest.ts +++ b/tests/unit/useReportActionsNewActionLiveTailTest.ts @@ -140,6 +140,21 @@ describe('useReportActionsNewActionLiveTail', () => { expect(result.current.isScrollToBottomEnabled).toBe(true); }); + it('clears a pending sent-comment scroll when switching reports', () => { + // Given a sent action that has not entered the rendered page yet. + const action = getFakeReportAction(1, {actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT}); + const params = buildParams({hasNewerActions: false, hasNewestReportAction: true}); + const {result, rerender} = renderHook((props: HookParams) => useReportActionsNewActionLiveTail(props), {initialProps: params}); + act(() => newActionHandler?.(true, action, 'local')); + + // When the reader switches reports and returns before the timeout, with the row now loaded. + rerender({...params, reportID: '2'}); + rerender({...params, renderedVisibleReportActions: [action]}); + + // Then the old send request does not reclaim the viewport. + expect(result.current.isScrollToBottomEnabled).toBe(false); + }); + it('does not scroll for a payload-less realtime echo', () => { const {result} = renderHook(() => useReportActionsNewActionLiveTail(buildParams({hasNewerActions: false, hasNewestReportAction: true}))); diff --git a/tests/unit/useReportActionsScrollTest.tsx b/tests/unit/useReportActionsScrollTest.tsx index 81350e93c826..c87290a648d4 100644 --- a/tests/unit/useReportActionsScrollTest.tsx +++ b/tests/unit/useReportActionsScrollTest.tsx @@ -15,6 +15,7 @@ import type {ReactNode} from 'react'; import React from 'react'; import Onyx from 'react-native-onyx'; +import createMock from '../utils/createMock'; import {createMockReport, getFakeReportAction} from '../utils/ReportTestUtils'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; @@ -33,6 +34,7 @@ jest.spyOn(global, 'requestAnimationFrame').mockImplementation((callback: FrameR // --- useReportScrollManager --- const mockScrollToBottom = jest.fn(); const mockScrollToIndex = jest.fn(); +let mockLinkedActionPosition = 40; jest.mock('@hooks/useReportScrollManager', () => ({ __esModule: true, default: () => ({ @@ -205,7 +207,19 @@ function buildParams(overrides: Partial = {}): ScrollParams { // Built via a function so the value isn't an inline literal the context-split lint rule would flag; these are all refs/accessors with no re-render concern. function buildActionListContextValue() { - return {scrollOffsetRef: mockScrollOffsetRef, getScrollOffset: () => mockScrollOffsetRef.current, registerListRef: () => {}, getListRef: () => null}; + return { + scrollOffsetRef: mockScrollOffsetRef, + getScrollOffset: () => mockScrollOffsetRef.current, + registerListRef: () => {}, + getListRef: () => ({ + current: { + scrollToIndex: mockScrollToIndex, + scrollToEnd: mockScrollToBottom, + scrollToOffset: jest.fn(), + getState: () => ({positionByKey: () => mockLinkedActionPosition}), + }, + }), + }; } function wrapper({children}: {children: ReactNode}) { @@ -237,6 +251,7 @@ describe('useReportActionsScroll', () => { await waitForBatchedUpdates(); mockTransitionCallbacks.length = 0; mockRouteParams = {}; + mockLinkedActionPosition = 40; mockIsFocused = true; mockReportRHPActiveRoute = undefined; mockIsFloatingMessageCounterVisible = false; @@ -373,6 +388,33 @@ describe('useReportActionsScroll', () => { }); describe('linked message positioning', () => { + it('restores a linked message moved out of view by a late native scroll adjustment', async () => { + // Given the linked message has already landed below a measured long predecessor. + mockRouteParams = {reportActionID: LINKED_ACTION_ID}; + const {result} = await renderScroll({renderedVisibleReportActions: [makeAction(LINKED_ACTION_ID), makeAction('999')]}); + act(() => result.current.onLoad()); + flushTransitions(); + mockScrollToIndex.mockClear(); + + // When a native layout adjustment moves its measured position beyond the viewport. + mockLinkedActionPosition = 1500; + const scrollEvent = createMock[0]>({ + nativeEvent: {contentOffset: {x: 0, y: 0}, contentSize: {width: 300, height: 2000}, layoutMeasurement: {width: 300, height: 600}}, + }); + act(() => result.current.trackLinkedMessageScroll(scrollEvent)); + flushTransitions(); + + // Then the linked position is restored even without another row size notification. + expect(mockScrollToIndex).toHaveBeenCalledWith(0, {animated: false, viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}); + mockScrollToIndex.mockClear(); + act(() => { + result.current.stopLinkedMessagePositioning(); + result.current.trackLinkedMessageScroll(scrollEvent); + }); + flushTransitions(); + expect(mockScrollToIndex).not.toHaveBeenCalled(); + }); + it('repositions the linked parent message when returning from a thread', async () => { // Given the linked parent report stays mounted while its thread is open. mockRouteParams = {reportActionID: LINKED_ACTION_ID}; From b6c6df2fa0d5db6a90199ac4dd18ed84102f8415 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Tue, 13 Oct 2026 18:17:37 +0100 Subject: [PATCH 43/61] fix: preserve LegendList web scroll completion --- src/pages/inbox/report/ReportActionsList.tsx | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index b14846fa30c9..8f5ffafd50a8 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -71,7 +71,7 @@ import {LegendList} from '@legendapp/list/react-native'; import {useRoute} from '@react-navigation/native'; import {isTrackIntentUserSelector} from '@selectors/Onboarding'; import React, {useEffect, useImperativeHandle, useRef, useState} from 'react'; -import {View} from 'react-native'; +import {Platform, View} from 'react-native'; import FloatingMessageCounter from './FloatingMessageCounter'; import {ReportActionPositionContextProvider} from './ReportActionIndexContext'; @@ -792,9 +792,11 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct isReportArchived={isReportArchived} > Date: Tue, 13 Oct 2026 18:17:57 +0100 Subject: [PATCH 44/61] fix: resolve the current composer after focus delays --- src/libs/focusComposerWithDelay/index.ts | 30 ++++++------- .../ComposerWithSuggestions.tsx | 2 +- tests/unit/focusComposerWithDelayTest.ts | 44 +++++++++++++++++++ 3 files changed, 59 insertions(+), 17 deletions(-) diff --git a/src/libs/focusComposerWithDelay/index.ts b/src/libs/focusComposerWithDelay/index.ts index bcefb4bdc200..08e9b6ca7566 100644 --- a/src/libs/focusComposerWithDelay/index.ts +++ b/src/libs/focusComposerWithDelay/index.ts @@ -12,16 +12,14 @@ import setTextInputSelection from './setTextInputSelection'; /** * Create a function that focuses the composer. + * A getter resolves the current input after the delay, since editing can replace it during layout. * @param [canFocusAfterDelay] Re-checked when the delay is up, so a composer that lost its focus claim meanwhile does not take focus back */ -function focusComposerWithDelay(textInput: InputType | null, delay: number = CONST.COMPOSER_FOCUS_DELAY, canFocusAfterDelay?: () => boolean): FocusComposerWithDelay { - function getIsFocused() { - if (textInput && 'isFocused' in textInput) { - return textInput.isFocused(); - } - return false; - } - +function focusComposerWithDelay( + textInput: InputType | null | (() => InputType | null), + delay: number = CONST.COMPOSER_FOCUS_DELAY, + canFocusAfterDelay?: () => boolean, +): FocusComposerWithDelay { /** * Focus the text input * @param [shouldDelay] Impose delay before focusing the text input @@ -35,8 +33,12 @@ function focusComposerWithDelay(textInput: InputType | null, delay: number = CON return; } - function focusAndUpdateSelection(input: InputType) { - if (getIsFocused()) { + function focusAndUpdateSelection() { + const input = typeof textInput === 'function' ? textInput() : textInput; + if (!input) { + return; + } + if ('isFocused' in input && input.isFocused()) { if (forceKeyboardIfAlreadyFocused) { requestKeyboardForFocusedComposer(input, forcedSelectionRange); } @@ -50,23 +52,19 @@ function focusComposerWithDelay(textInput: InputType | null, delay: number = CON } if (!shouldDelay) { - focusAndUpdateSelection(textInput); + focusAndUpdateSelection(); return; } await Promise.all([ComposerFocusManager.isReadyToFocus(), isWindowReadyToFocus()]); - if (!textInput) { - return; - } - // When the closing modal has a focused text input focus() needs a delay to properly work. // Setting 150ms here is a temporary workaround for the Android HybridApp. It should be reverted once we identify the real root cause of this issue: https://github.com/Expensify/App/issues/56311. setTimeout(() => { if (canFocusAfterDelay && !canFocusAfterDelay()) { return; } - focusAndUpdateSelection(textInput); + focusAndUpdateSelection(); }, delay); }; } diff --git a/src/pages/inbox/report/ReportActionCompose/ComposerWithSuggestions.tsx b/src/pages/inbox/report/ReportActionCompose/ComposerWithSuggestions.tsx index 128ef3727bc5..0c815b764ee1 100644 --- a/src/pages/inbox/report/ReportActionCompose/ComposerWithSuggestions.tsx +++ b/src/pages/inbox/report/ReportActionCompose/ComposerWithSuggestions.tsx @@ -315,7 +315,7 @@ function ComposerWithSuggestions({ const delay = shouldDelayAutoFocusRef.current ? CONST.ANIMATED_TRANSITION : CONST.COMPOSER_FOCUS_DELAY; // Another composer taking focus releases the claim, so it has to be read when the focus lands rather than now. const canFocusAfterDelay = isInSidePanelRef.current ? () => !!ReportActionComposeFocusManager.sidePanelComposerRef.current : undefined; - focusComposerWithDelay(composerRef.current, delay, canFocusAfterDelay)(shouldDelay, forcedSelectionRange, forceKeyboardIfAlreadyFocused).catch(() => {}); + focusComposerWithDelay(() => composerRef.current, delay, canFocusAfterDelay)(shouldDelay, forcedSelectionRange, forceKeyboardIfAlreadyFocused).catch(() => {}); }, []); const shouldIgnoreEditSelectionResetRef = useRef(false); diff --git a/tests/unit/focusComposerWithDelayTest.ts b/tests/unit/focusComposerWithDelayTest.ts index e086cd81b168..eb65adbed324 100644 --- a/tests/unit/focusComposerWithDelayTest.ts +++ b/tests/unit/focusComposerWithDelayTest.ts @@ -41,4 +41,48 @@ describe('focusComposerWithDelay', () => { expect(focus).toHaveBeenCalled(); }); + + it('focuses an editor that mounts after the delayed focus request', async () => { + // Given an edit transition that requests focus before the input ref is attached. + const {input, focus} = createInput(); + let currentInput: HTMLTextAreaElement | null = null; + + // When the editor mounts before the focus delay ends. + await focusComposerWithDelay(() => currentInput)(true); + currentInput = input; + jest.runAllTimers(); + + // Then the mounted editor receives focus instead of losing the request. + expect(focus).toHaveBeenCalledTimes(1); + }); + + it('does not focus an editor that unmounts before the delay ends', async () => { + // Given a focus request for an editor that is still mounted. + const {input, focus} = createInput(); + let currentInput: HTMLTextAreaElement | null = input; + await focusComposerWithDelay(() => currentInput)(true); + + // When navigation removes the editor before the delayed request runs. + currentInput = null; + jest.runAllTimers(); + + // Then the stale input does not take focus back. + expect(focus).not.toHaveBeenCalled(); + }); + + it('focuses the replacement editor instead of the old input', async () => { + // Given a pending focus request while the composer is changing its layout. + const original = createInput(); + const replacement = createInput(); + let currentInput = original.input; + await focusComposerWithDelay(() => currentInput)(true); + + // When the input ref changes before the focus delay ends. + currentInput = replacement.input; + jest.runAllTimers(); + + // Then only the current editor receives focus. + expect(original.focus).not.toHaveBeenCalled(); + expect(replacement.focus).toHaveBeenCalledTimes(1); + }); }); From 49ab56dabf4124ad34124e2c9e7e3ce8d19fd9dc Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Tue, 13 Oct 2026 18:18:11 +0100 Subject: [PATCH 45/61] fix: avoid duplicate Android linked-message adjustments --- src/hooks/useLinkedReportActionScroll.ts | 12 +++++++ src/pages/inbox/ActionListTypes.ts | 2 +- tests/unit/useReportActionsScrollTest.tsx | 39 ++++++++++++++++++++++- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/hooks/useLinkedReportActionScroll.ts b/src/hooks/useLinkedReportActionScroll.ts index f1c89d41e997..f2a4fb238de4 100644 --- a/src/hooks/useLinkedReportActionScroll.ts +++ b/src/hooks/useLinkedReportActionScroll.ts @@ -54,6 +54,18 @@ function useLinkedReportActionScroll({listID, linkedReportActionID, targetIndex, return; } wasLastActionRef.current = isLastAction; + if (Platform.OS === 'android' && !isLastAction && linkedReportActionID) { + const state = getListRef()?.current?.getState?.(); + const position = state?.positionByKey(linkedReportActionID); + if (position !== undefined && state?.scroll !== undefined && state.contentLength !== undefined && state.scrollLength !== undefined) { + const targetOffset = Math.max(0, Math.min(position - CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET, state.contentLength - state.scrollLength)); + // LegendList includes pending native anchoring in its scroll offset. An imperative + // scroll to that same offset would apply the height correction twice on Android. + if (Math.abs(targetOffset - state.scroll) <= 1) { + return; + } + } + } reportScrollManager.scrollToIndex( targetIndex, isLastAction ? {animated: false, viewPosition: 1} : {animated: false, viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}, diff --git a/src/pages/inbox/ActionListTypes.ts b/src/pages/inbox/ActionListTypes.ts index b366b0686c43..a890d2947e28 100644 --- a/src/pages/inbox/ActionListTypes.ts +++ b/src/pages/inbox/ActionListTypes.ts @@ -24,7 +24,7 @@ type ActionListRef = { getNativeScrollRef?: () => unknown; getScrollableNode?: () => unknown; /** Measured item positions supplied by LegendList. Other list implementations can omit this. */ - getState?: () => {positionByKey: (key: string) => number | undefined} | undefined; + getState?: () => {positionByKey: (key: string) => number | undefined; scroll?: number; contentLength?: number; scrollLength?: number} | undefined; }; /** Ref to the underlying list instance attached via `ref={}`. */ diff --git a/tests/unit/useReportActionsScrollTest.tsx b/tests/unit/useReportActionsScrollTest.tsx index e8529f8af019..11f53c03fe4b 100644 --- a/tests/unit/useReportActionsScrollTest.tsx +++ b/tests/unit/useReportActionsScrollTest.tsx @@ -13,6 +13,7 @@ import type {ReportAction} from '@src/types/onyx'; import type {ReactNode} from 'react'; import React from 'react'; +import {Platform} from 'react-native'; import Onyx from 'react-native-onyx'; import createMock from '../utils/createMock'; @@ -35,6 +36,7 @@ jest.spyOn(global, 'requestAnimationFrame').mockImplementation((callback: FrameR const mockScrollToBottom = jest.fn(); const mockScrollToIndex = jest.fn(); let mockLinkedActionPosition = 40; +let mockListScroll: number | undefined; jest.mock('@hooks/useReportScrollManager', () => ({ __esModule: true, default: () => ({ @@ -217,7 +219,7 @@ function buildActionListContextValue() { scrollToIndex: mockScrollToIndex, scrollToEnd: mockScrollToBottom, scrollToOffset: jest.fn(), - getState: () => ({positionByKey: () => mockLinkedActionPosition}), + getState: () => ({positionByKey: () => mockLinkedActionPosition, scroll: mockListScroll, contentLength: 5000, scrollLength: 600}), }, }), }; @@ -253,6 +255,7 @@ describe('useReportActionsScroll', () => { mockTransitionCallbacks.length = 0; mockRouteParams = {}; mockLinkedActionPosition = 40; + mockListScroll = undefined; mockIsFocused = true; mockReportRHPActiveRoute = undefined; mockIsFloatingMessageCounterVisible = false; @@ -389,6 +392,40 @@ describe('useReportActionsScroll', () => { }); describe('linked message positioning', () => { + it('lets Android preserve an already aligned linked message when a preceding row changes height', async () => { + // Given Android has already positioned the link and is preserving it during measurement. + const platform = jest.replaceProperty(Platform, 'OS', 'android'); + mockRouteParams = {reportActionID: LINKED_ACTION_ID}; + mockLinkedActionPosition = 2690; + mockListScroll = 2650; + const {result} = await renderScroll({renderedVisibleReportActions: [makeAction('123'), makeAction(LINKED_ACTION_ID), makeAction('999')]}); + act(() => result.current.onLoad()); + flushTransitions(); + mockScrollToIndex.mockClear(); + + // When LegendList updates both the measured position and its pending native scroll adjustment. + mockLinkedActionPosition = 2460; + mockListScroll = 2420; + act(() => result.current.onItemSizeChanged({index: 0, previous: 500, size: 270})); + flushTransitions(); + + // Then another imperative scroll does not apply the same correction a second time. + expect(mockScrollToIndex).not.toHaveBeenCalled(); + + // A later scroll event that really moves the link still receives a correction. + mockListScroll = 2190; + act(() => + result.current.trackLinkedMessageScroll( + createMock[0]>({ + nativeEvent: {contentOffset: {x: 0, y: 2190}, contentSize: {width: 300, height: 5000}, layoutMeasurement: {width: 300, height: 600}}, + }), + ), + ); + flushTransitions(); + expect(mockScrollToIndex).toHaveBeenCalledWith(1, {animated: false, viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}); + platform.restore(); + }); + it('restores a linked message moved out of view by a late native scroll adjustment', async () => { // Given the linked message has already landed below a measured long predecessor. mockRouteParams = {reportActionID: LINKED_ACTION_ID}; From 1de8343d1e67a06887b7afca5296834d503d9feb Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Mon, 19 Oct 2026 21:04:02 +0100 Subject: [PATCH 46/61] fix: keyboard focus --- src/components/Composer/implementation/index.native.tsx | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/components/Composer/implementation/index.native.tsx b/src/components/Composer/implementation/index.native.tsx index 31653968860b..7859cce9992a 100644 --- a/src/components/Composer/implementation/index.native.tsx +++ b/src/components/Composer/implementation/index.native.tsx @@ -2,7 +2,6 @@ import type {ComposerProps, ComposerRef} from '@components/Composer/types'; import type {AnimatedMarkdownTextInputRef} from '@components/RNMarkdownTextInput'; import RNMarkdownTextInput from '@components/RNMarkdownTextInput'; -import useBlurOnKeyboardHide from '@hooks/useBlurOnKeyboardHide'; import useIsInLandscapeMode from '@hooks/useIsInLandscapeMode'; import useMarkdownStyle from '@hooks/useMarkdownStyle'; import useStyleUtils from '@hooks/useStyleUtils'; @@ -59,9 +58,6 @@ function Composer({ const StyleUtils = useStyleUtils(); const isInLandscapeMode = useIsInLandscapeMode(); - // Android keeps the input focused when the keyboard is dismissed with the back button/gesture. - useBlurOnKeyboardHide(textInputRef); - useEffect(() => { if (!textInputRef.current?.setSelection || !selection || isComposerFullSize) { return; From bd9ce7cbdb5d6b96a344241883a055612716b027 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Tue, 20 Oct 2026 14:48:09 +0100 Subject: [PATCH 47/61] fix: restore Android composer blur on keyboard dismissal --- src/components/Composer/implementation/index.native.tsx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/components/Composer/implementation/index.native.tsx b/src/components/Composer/implementation/index.native.tsx index 7859cce9992a..31653968860b 100644 --- a/src/components/Composer/implementation/index.native.tsx +++ b/src/components/Composer/implementation/index.native.tsx @@ -2,6 +2,7 @@ import type {ComposerProps, ComposerRef} from '@components/Composer/types'; import type {AnimatedMarkdownTextInputRef} from '@components/RNMarkdownTextInput'; import RNMarkdownTextInput from '@components/RNMarkdownTextInput'; +import useBlurOnKeyboardHide from '@hooks/useBlurOnKeyboardHide'; import useIsInLandscapeMode from '@hooks/useIsInLandscapeMode'; import useMarkdownStyle from '@hooks/useMarkdownStyle'; import useStyleUtils from '@hooks/useStyleUtils'; @@ -58,6 +59,9 @@ function Composer({ const StyleUtils = useStyleUtils(); const isInLandscapeMode = useIsInLandscapeMode(); + // Android keeps the input focused when the keyboard is dismissed with the back button/gesture. + useBlurOnKeyboardHide(textInputRef); + useEffect(() => { if (!textInputRef.current?.setSelection || !selection || isComposerFullSize) { return; From 1c04141c57af8dab0a3d1379cf76870d8b7497d4 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Wed, 21 Oct 2026 16:52:52 +0100 Subject: [PATCH 48/61] fix: fill the available height in report action lists --- src/pages/inbox/report/ReportActionsList.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index ee6d6c9b3600..79742bcc4d97 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -810,7 +810,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct data-testid={Platform.OS === 'web' ? 'report-actions-list' : undefined} testID={Platform.OS === 'web' ? undefined : 'report-actions-list'} ref={legendListRef} - style={styles.overscrollBehaviorContain} + style={[styles.flex1, styles.overscrollBehaviorContain]} data={listData} renderItem={renderItem} keyExtractor={keyExtractor} From c12c90c814174c60b903fef486749247160ab9f3 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Wed, 21 Oct 2026 18:47:18 +0100 Subject: [PATCH 49/61] fix: keep the reader's position when returning to a linked chat Returning from a thread or attachment re-enabled linked-message positioning, so the parent chat jumped back to the old highlighted message. Positioning now stops when the screen loses focus and only restarts for a newly opened link. Co-Authored-By: Claude Opus 5.5 --- src/hooks/useLinkedReportActionScroll.ts | 16 +++++- tests/unit/useReportActionsScrollTest.tsx | 70 +++++++++++++++++++++-- 2 files changed, 78 insertions(+), 8 deletions(-) diff --git a/src/hooks/useLinkedReportActionScroll.ts b/src/hooks/useLinkedReportActionScroll.ts index f2a4fb238de4..57295a37ea33 100644 --- a/src/hooks/useLinkedReportActionScroll.ts +++ b/src/hooks/useLinkedReportActionScroll.ts @@ -36,13 +36,25 @@ function useLinkedReportActionScroll({listID, linkedReportActionID, targetIndex, }; useEffect(() => { - shouldPositionRef.current = isFocused && !!linkedReportActionID; + shouldPositionRef.current = !!linkedReportActionID; wasLastActionRef.current = false; return () => { shouldPositionRef.current = false; cancelPositionRef.current?.(); }; - }, [isFocused, linkedReportActionID, listID]); + }, [linkedReportActionID, listID]); + + useEffect(() => { + if (!isFocused) { + return; + } + + // Returning from a child screen must keep the reader's viewport, not reopen the old link. + return () => { + shouldPositionRef.current = false; + cancelPositionRef.current?.(); + }; + }, [isFocused]); const positionLinkedAction = useEffectEvent(() => { if (!shouldPositionRef.current || targetIndex < 0) { diff --git a/tests/unit/useReportActionsScrollTest.tsx b/tests/unit/useReportActionsScrollTest.tsx index c4ead60b3ac4..857f385cc371 100644 --- a/tests/unit/useReportActionsScrollTest.tsx +++ b/tests/unit/useReportActionsScrollTest.tsx @@ -459,24 +459,58 @@ describe('useReportActionsScroll', () => { expect(mockScrollToIndex).not.toHaveBeenCalled(); }); - it('repositions the linked parent message when returning from a thread', async () => { - // Given the linked parent report stays mounted while its thread is open. + it.each([false, true])('preserves the previous viewport after returning from child screens, reader scrolled away: %s', async (didScrollAway) => { + // Given a linked parent stays mounted after its initial positioning, with or without a later manual scroll. mockRouteParams = {reportActionID: LINKED_ACTION_ID}; const actions = [makeAction(LINKED_ACTION_ID), makeAction('999')]; const params = buildParams({sortedVisibleReportActions: actions.toReversed(), renderedVisibleReportActions: actions}); const {result, rerender} = await renderScroll(params); act(() => result.current.onLoad()); flushTransitions(); - mockIsFocused = false; - rerender(params); + expect(mockScrollToIndex).toHaveBeenCalled(); + if (didScrollAway) { + act(() => result.current.stopLinkedMessagePositioning()); + } mockScrollToIndex.mockClear(); - // When the parent link returns to that same report and the navigation transition finishes. + // When the reader opens a thread, returns, then opens an attachment and returns again. + for (let visit = 0; visit < 2; visit++) { + mockIsFocused = false; + rerender(params); + mockIsFocused = true; + rerender(params); + flushTransitions(); + act(() => { + result.current.onItemSizeChanged({index: 0, previous: 80, size: 350}); + result.current.trackLinkedMessageScroll( + createMock[0]>({ + nativeEvent: {contentOffset: {x: 0, y: 900}, contentSize: {width: 300, height: 2000}, layoutMeasurement: {width: 300, height: 600}}, + }), + ); + }); + flushTransitions(); + + // Then neither focus nor late measurements reclaim the viewport from the reader. + expect(mockScrollToIndex).not.toHaveBeenCalled(); + } + }); + + it('positions a new link when an initially unfocused report first becomes active', async () => { + // Given a linked report has loaded behind another screen without ever taking focus. + mockIsFocused = false; + mockRouteParams = {reportActionID: LINKED_ACTION_ID}; + const params = buildParams({renderedVisibleReportActions: [makeAction(LINKED_ACTION_ID), makeAction('999')]}); + const {result, rerender} = await renderScroll(params); + act(() => result.current.onLoad()); + flushTransitions(); + expect(mockScrollToIndex).not.toHaveBeenCalled(); + + // When the report becomes visible for the first time. mockIsFocused = true; rerender(params); flushTransitions(); - // Then the linked parent is positioned again instead of keeping the thread's stale offset. + // Then the initial link still receives its first positioning pass. expect(mockScrollToIndex).toHaveBeenCalledWith(0, { animated: false, viewPosition: 0, @@ -484,6 +518,30 @@ describe('useReportActionsScroll', () => { }); }); + it('positions a different link opened while the report is behind a child screen', async () => { + // Given a previously positioned linked report has lost focus to a child screen. + mockRouteParams = {reportActionID: LINKED_ACTION_ID}; + const params = buildParams({renderedVisibleReportActions: [makeAction(LINKED_ACTION_ID), makeAction(UNREAD_ACTION_ID), makeAction('999')]}); + const {result, rerender} = await renderScroll(params); + act(() => result.current.onLoad()); + flushTransitions(); + mockIsFocused = false; + rerender(params); + mockScrollToIndex.mockClear(); + + // When another message link targets the same report before it regains focus. + mockRouteParams = {reportActionID: UNREAD_ACTION_ID}; + rerender(params); + flushTransitions(); + expect(mockScrollToIndex).not.toHaveBeenCalled(); + mockIsFocused = true; + rerender(params); + flushTransitions(); + + // Then the new navigation request still positions its own highlighted message. + expect(mockScrollToIndex).toHaveBeenCalledWith(1, {animated: false, viewPosition: 0, viewOffset: CONST.REPORT.ACTIONS.LINKED_MESSAGE_OFFSET}); + }); + it('repositions a linked PDF after its preview or preceding rows change height', async () => { // Given the linked PDF is initially positioned using estimated row heights. mockRouteParams = {reportActionID: LINKED_ACTION_ID}; From 89ef8b220889037f2a26855bc1831145fd039f3b Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Wed, 21 Oct 2026 18:47:21 +0100 Subject: [PATCH 50/61] fix: keep unread chats at the newest message while older rows load Opening a chat whose unread marker is near the newest message lands at the end, but end following was only enabled for lists without an initial index. When rows above the viewport were measured after OpenReport, the list moved to older messages. Follow the end whenever the unread position lands there. Co-Authored-By: Claude Opus 5.5 --- src/pages/inbox/report/ReportActionsList.tsx | 6 +++- tests/ui/ReportActionsListTest.tsx | 38 +++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index 79742bcc4d97..4704a0cb2fb5 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -566,7 +566,11 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct onLoad(); setLoadedInitialViewportListID(listID); readyEndListIDRef.current = listID; - shouldFollowEndOnResizeRef.current = initialScrollIndex === undefined && !hasNewerActions && userScrolledListIDRef.current !== listID; + // An unread marker near the newest action lands at the end. Rows above it are still measured after + // onReady, so keep that end in view instead of exposing older messages. + const isUnreadInitialScroll = initialScrollIndex !== undefined && !reportActionIDFromRoute && !shouldBeAlignedToTop; + const didLandAtEnd = initialScrollIndex === undefined || (isUnreadInitialScroll && !!legendListRef.current?.getState()?.isAtEnd); + shouldFollowEndOnResizeRef.current = didLandAtEnd && !hasNewerActions && userScrolledListIDRef.current !== listID; lastScrollMetricsRef.current = undefined; // The final row can grow after LegendList's initial end estimate (for example, Concierge feedback). scheduleEndCorrection(); diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 7da4f50acb9c..996fb2cf16db 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -148,7 +148,7 @@ const mockLegendListMount = jest.fn(); const mockLegendListUnmount = jest.fn(); const mockLegendScrollToEnd = jest.fn(); const mockLegendPositionByKey = jest.fn(() => 1500); -const mockLegendGetState = jest.fn(() => ({positionByKey: mockLegendPositionByKey})); +const mockLegendGetState = jest.fn(() => ({positionByKey: mockLegendPositionByKey, isAtEnd: false})); const mockLegendGetScrollableNode = jest.fn(() => ({scrollTop: 0})); let mockShouldCallLegendListOnLoad = true; jest.mock('@legendapp/list/react-native', () => { @@ -584,6 +584,42 @@ describe('ReportActionsList (body)', () => { } }); + it.each([ + {isAtEnd: true, shouldFollowEnd: true}, + {isAtEnd: false, shouldFollowEnd: false}, + ])('follows late row growth after opening at an unread marker only when it lands at the end ($isAtEnd)', async ({isAtEnd, shouldFollowEnd}) => { + // Given the chat opens at an unread marker, which LegendList clamps to the end when the marker is near the newest action. + mockUseNetwork.mockReturnValue({isOffline: false}); + const originalImplementation = mockUseReportActionsScroll.getMockImplementation(); + const defaultScrollState = mockUseReportActionsScroll() as ReturnType; + mockUseReportActionsScroll.mockReturnValue({...defaultScrollState, initialScrollIndex: 1}); + mockLegendGetState.mockReturnValue({positionByKey: mockLegendPositionByKey, isAtEnd}); + try { + renderReportActionsList(); + + // When rows above the viewport are measured after the list is ready and change the content height. + act(() => { + getCapturedListProps()?.onContentSizeChange?.(300, 1025); + }); + await act( + async () => + new Promise((resolve) => { + setTimeout(resolve, 30); + }), + ); + + // Then a list that landed at the end keeps the newest messages in view, and one showing older unread messages stays put. + if (shouldFollowEnd) { + expect(mockLegendScrollToEnd).toHaveBeenCalledWith({animated: false}); + } else { + expect(mockLegendScrollToEnd).not.toHaveBeenCalled(); + } + } finally { + mockUseReportActionsScroll.mockImplementation(originalImplementation); + mockLegendGetState.mockReturnValue({positionByKey: mockLegendPositionByKey, isAtEnd: false}); + } + }); + it('cancels a queued end correction as soon as the reader starts dragging', async () => { // Given removing the latest message has queued a layout correction at the end. mockUseNetwork.mockReturnValue({isOffline: false}); From eb148d32a02d1d6c7a16acf004cbfee99f0a2d21 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Wed, 21 Oct 2026 21:11:21 +0100 Subject: [PATCH 51/61] fix: stop Android chats from flashing older messages at the end When rows above the viewport get smaller, Android clamps the scroll offset to the new end, and LegendList's scroll-preservation anchor then moves the offset by the same amount again. A chat opened at its newest messages showed older messages for a few frames until the end correction ran. Patch LegendList to account for the clamp and only move the anchor by what the clamp leaves. Co-Authored-By: Claude Opus 5.5 --- ....0+002+account-for-android-end-clamp.patch | 68 +++++++++++++++++++ patches/@legendapp/list/details.md | 11 ++- 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 patches/@legendapp/list/@legendapp+list+3.6.0+002+account-for-android-end-clamp.patch diff --git a/patches/@legendapp/list/@legendapp+list+3.6.0+002+account-for-android-end-clamp.patch b/patches/@legendapp/list/@legendapp+list+3.6.0+002+account-for-android-end-clamp.patch new file mode 100644 index 000000000000..329f3102e456 --- /dev/null +++ b/patches/@legendapp/list/@legendapp+list+3.6.0+002+account-for-android-end-clamp.patch @@ -0,0 +1,68 @@ +diff --git a/node_modules/@legendapp/list/react-native.js b/node_modules/@legendapp/list/react-native.js +index 9e14f1d..0822107 100644 +--- a/node_modules/@legendapp/list/react-native.js ++++ b/node_modules/@legendapp/list/react-native.js +@@ -1544,6 +1544,19 @@ function getPredictedNativeClamp(state, unresolvedAmount, totalSize) { + } + return 0; + } ++// Android clamps the offset to the new end while mounting a content shrink, then native MVCP adds the ++// anchor delta on top. Sync JS state to that clamp and only request the rest of the movement. ++function getAndroidNativeClampedAdjust(ctx, positionDiff) { ++ const state = ctx.state; ++ const maxScroll = Math.max(0, getContentSize(ctx) - state.scrollLength); ++ if (state.scroll <= maxScroll + MVCP_POSITION_EPSILON) { ++ return positionDiff; ++ } ++ const targetScroll = Math.min(state.scroll + positionDiff, maxScroll); ++ state.scroll = maxScroll; ++ state.scrollPending = maxScroll; ++ return targetScroll - maxScroll; ++} + function getProgressTowardAmount(targetDelta, nativeDelta) { + return targetDelta < 0 ? -nativeDelta : nativeDelta; + } +@@ -1770,6 +1783,9 @@ function prepareMVCP(ctx, dataChanged, adjustmentSource) { + now, + positionDiff + }); ++ if (Platform.OS === "android" && !dataChanged && scrollTarget === void 0) { ++ positionDiff = getAndroidNativeClampedAdjust(ctx, positionDiff); ++ } + if (shouldQueueNativeMVCPAdjust(dataChanged, state, positionDiff, prevTotalSize, prevScroll, scrollTarget)) { + state.pendingNativeMVCPAdjust = { + amount: positionDiff, +diff --git a/node_modules/@legendapp/list/react-native.mjs b/node_modules/@legendapp/list/react-native.mjs +index 1f2bcf9..5cc805b 100644 +--- a/node_modules/@legendapp/list/react-native.mjs ++++ b/node_modules/@legendapp/list/react-native.mjs +@@ -1523,6 +1523,19 @@ function getPredictedNativeClamp(state, unresolvedAmount, totalSize) { + } + return 0; + } ++// Android clamps the offset to the new end while mounting a content shrink, then native MVCP adds the ++// anchor delta on top. Sync JS state to that clamp and only request the rest of the movement. ++function getAndroidNativeClampedAdjust(ctx, positionDiff) { ++ const state = ctx.state; ++ const maxScroll = Math.max(0, getContentSize(ctx) - state.scrollLength); ++ if (state.scroll <= maxScroll + MVCP_POSITION_EPSILON) { ++ return positionDiff; ++ } ++ const targetScroll = Math.min(state.scroll + positionDiff, maxScroll); ++ state.scroll = maxScroll; ++ state.scrollPending = maxScroll; ++ return targetScroll - maxScroll; ++} + function getProgressTowardAmount(targetDelta, nativeDelta) { + return targetDelta < 0 ? -nativeDelta : nativeDelta; + } +@@ -1749,6 +1762,9 @@ function prepareMVCP(ctx, dataChanged, adjustmentSource) { + now, + positionDiff + }); ++ if (Platform.OS === "android" && !dataChanged && scrollTarget === void 0) { ++ positionDiff = getAndroidNativeClampedAdjust(ctx, positionDiff); ++ } + if (shouldQueueNativeMVCPAdjust(dataChanged, state, positionDiff, prevTotalSize, prevScroll, scrollTarget)) { + state.pendingNativeMVCPAdjust = { + amount: positionDiff, diff --git a/patches/@legendapp/list/details.md b/patches/@legendapp/list/details.md index 7629edac0980..299029c6711f 100644 --- a/patches/@legendapp/list/details.md +++ b/patches/@legendapp/list/details.md @@ -4,7 +4,16 @@ - Reason: On Android, the entire report-action list can briefly jump to older rows while item measurements settle. LegendList updates its content size through an imperative `Animated.Value`, while Fabric row positions and the scroll-preservation anchor update through React. An early content-size update can clamp the old viewport before the matching positions mount. Read the content size through the same React store on the New Architecture so these layout changes commit together. - Scope: Both native module formats use the synchronized size on the New Architecture. The legacy architecture retains its animated size updates, and the web entries are unchanged. Native scroll preservation and the initial loading skeleton remain enabled. -- Upstream PR/issue: Pending submission after local validation. This patch is being developed locally as part of the LegendList migration. +- Upstream PR/issue: Fixed upstream in https://github.com/LegendApp/legend-list/commit/a2d20a9b, which isn't in a release yet. - E/App issue: https://github.com/Expensify/App/pull/100733#issuecomment-5886543887 - PR introducing patch: https://github.com/Expensify/App/pull/100733 - Reproduction: Open an expense chat containing a report preview and messages with different heights. Wait for the actual rows to replace the skeleton. The highlighted preview should stay visible while measurements settle, without a frame displaying older rows. Also scroll through older history and return to the latest messages to check scroll preservation. + +### [@legendapp+list+3.6.0+002+account-for-android-end-clamp.patch](@legendapp+list+3.6.0+002+account-for-android-end-clamp.patch) + +- Reason: On Android, a chat that opens at its newest messages can briefly show older messages before it snaps back to the end. When rows above the viewport get their real, smaller heights, the content shrinks. Android clamps the scroll offset to the new end, and LegendList's scroll-preservation anchor then moves the offset by the full shrink again. The list stays above the end by that amount until the next scroll to the end. Account for Android's clamp and only move the anchor by what the clamp doesn't cover. +- Scope: Only item-size scroll preservation on Android, and only when the content shrinks below the current offset. Data changes and active scroll targets already handle the clamp and are unchanged. iOS and web are unchanged. +- Upstream PR/issue: Not submitted yet. https://github.com/LegendApp/legend-list/pull/481 handles the same clamp for lists with a bottom inset. +- E/App issue: https://github.com/Expensify/App/pull/100733#issuecomment-6287555197 +- PR introducing patch: https://github.com/Expensify/App/pull/100733 +- Reproduction: In a DM, mark the third-newest message as unread and go back to the inbox. Open the DM again. It should open at the newest messages without a frame showing older messages. Also scroll through older history and send a new message to check scroll preservation and end following. From c71756d8aeb1d0f07db6d4dcba874d622f991bd3 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Wed, 21 Oct 2026 21:32:53 +0100 Subject: [PATCH 52/61] ci: retrigger unit tests Retry CI after Jest workers were killed and full-app tests timed out. Co-authored-by: Codex From 9b1bf78de3d76cf8d6805b8ff2dbb51637b8d8e7 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 22 Oct 2026 13:21:32 +0100 Subject: [PATCH 53/61] refactor: consolidate report scroll-to-bottom commands Co-authored-by: Codex --- .../useMoneyRequestReportScroll.ts | 4 +-- .../useReportScrollManager/index.native.ts | 25 +------------------ src/hooks/useReportScrollManager/index.ts | 4 +-- src/hooks/useReportScrollManager/types.ts | 1 - .../unit/useMoneyRequestReportScrollTest.tsx | 17 ++++++------- .../useReportActionsNewActionLiveTailTest.ts | 1 - tests/unit/useReportActionsScrollTest.tsx | 1 - tests/unit/useReportScrollManagerTest.tsx | 16 +++++++----- 8 files changed, 22 insertions(+), 47 deletions(-) diff --git a/src/components/MoneyRequestReportView/useMoneyRequestReportScroll.ts b/src/components/MoneyRequestReportView/useMoneyRequestReportScroll.ts index a8e996a9d037..828bbbd2a695 100644 --- a/src/components/MoneyRequestReportView/useMoneyRequestReportScroll.ts +++ b/src/components/MoneyRequestReportView/useMoneyRequestReportScroll.ts @@ -193,7 +193,7 @@ function useMoneyRequestReportScroll({ resetKey, }); - // The indicator renders in the list footer, below the row scrollToBottom targets, so only scrollToEnd reveals it + // The indicator is below the last row, so use the manager's full-list scroll to include the footer. const {candidateAgentIDs} = useAgentZeroStatus(); const isThinkingIndicatorVisible = candidateAgentIDs.length > 0; // Scroll once per appearance, because the label changes many times per run and re-firing would yank the viewport @@ -210,7 +210,7 @@ function useMoneyRequestReportScroll({ // Wait for the footer to lay out, otherwise there is nothing to scroll to yet const timeoutID = setTimeout(() => { - reportScrollManager.scrollToEnd(); + reportScrollManager.scrollToBottom(); }, DELAY_FOR_SCROLLING_TO_END); return () => clearTimeout(timeoutID); diff --git a/src/hooks/useReportScrollManager/index.native.ts b/src/hooks/useReportScrollManager/index.native.ts index 43aa21afc658..8c9dea34397a 100644 --- a/src/hooks/useReportScrollManager/index.native.ts +++ b/src/hooks/useReportScrollManager/index.native.ts @@ -1,9 +1,5 @@ import {useActionListContext} from '@pages/inbox/ActionListContext'; -import type {ComponentRef} from 'react'; -// eslint-disable-next-line no-restricted-imports -import type {ScrollView} from 'react-native'; - import type {ReportScrollManagerData, ScrollToIndexOptions} from './types'; function useReportScrollManager(): ReportScrollManagerData { @@ -32,25 +28,6 @@ function useReportScrollManager(): ReportScrollManagerData { listRef.current.scrollToEnd({animated: false}); }; - /** - * Scroll to the end of the FlatList. - */ - const scrollToEnd = () => { - const listRef = getListRef(); - if (!listRef?.current) { - return; - } - - const scrollViewRef = listRef.current.getNativeScrollRef?.() as ComponentRef | undefined; - // Try to scroll on underlying scrollView if available, fallback to usual listRef - if (scrollViewRef && typeof scrollViewRef.scrollToEnd === 'function') { - scrollViewRef.scrollToEnd({animated: false}); - return; - } - - listRef.current.scrollToEnd({animated: false}); - }; - const scrollToOffset = (offset: number) => { const listRef = getListRef(); if (!listRef?.current) { @@ -59,7 +36,7 @@ function useReportScrollManager(): ReportScrollManagerData { listRef.current.scrollToOffset({offset, animated: false}); }; - return {scrollToIndex, scrollToBottom, scrollToEnd, scrollToOffset}; + return {scrollToIndex, scrollToBottom, scrollToOffset}; } export default useReportScrollManager; diff --git a/src/hooks/useReportScrollManager/index.ts b/src/hooks/useReportScrollManager/index.ts index 9eb1ff0e1650..9ff4bafbcc4e 100644 --- a/src/hooks/useReportScrollManager/index.ts +++ b/src/hooks/useReportScrollManager/index.ts @@ -28,8 +28,6 @@ function useReportScrollManager(): ReportScrollManagerData { listRef.current.scrollToEnd({animated: false}); }; - const scrollToEnd = scrollToBottom; - const scrollToOffset = (offset: number) => { const listRef = getListRef(); if (!listRef?.current) { @@ -39,7 +37,7 @@ function useReportScrollManager(): ReportScrollManagerData { listRef.current.scrollToOffset({animated: true, offset}); }; - return {scrollToIndex, scrollToBottom, scrollToEnd, scrollToOffset}; + return {scrollToIndex, scrollToBottom, scrollToOffset}; } export default useReportScrollManager; diff --git a/src/hooks/useReportScrollManager/types.ts b/src/hooks/useReportScrollManager/types.ts index c7649ecc334a..01587bf2f60e 100644 --- a/src/hooks/useReportScrollManager/types.ts +++ b/src/hooks/useReportScrollManager/types.ts @@ -18,7 +18,6 @@ type ReportScrollManagerData = { */ scrollToIndex: (index: number, options?: ScrollToIndexOptions) => void; scrollToBottom: () => void; - scrollToEnd: () => void; scrollToOffset: (offset: number) => void; }; diff --git a/tests/unit/useMoneyRequestReportScrollTest.tsx b/tests/unit/useMoneyRequestReportScrollTest.tsx index 8c2e0270e4e3..95cad9fd3073 100644 --- a/tests/unit/useMoneyRequestReportScrollTest.tsx +++ b/tests/unit/useMoneyRequestReportScrollTest.tsx @@ -31,13 +31,12 @@ const BOTTOM_OFFSET_AT_TOP = LIST_CONTENT_HEIGHT - LIST_LAYOUT_HEIGHT; const LAST_ITEM_INDEX = 4; const mockScrollToIndex = jest.fn(); -const mockScrollToEnd = jest.fn(); +const mockScrollToBottom = jest.fn(); jest.mock('@hooks/useReportScrollManager', () => ({ __esModule: true, default: () => ({ scrollToIndex: mockScrollToIndex, - scrollToEnd: mockScrollToEnd, - scrollToBottom: jest.fn(), + scrollToBottom: mockScrollToBottom, scrollToOffset: jest.fn(), }), })); @@ -559,7 +558,7 @@ describe('useMoneyRequestReportScroll', () => { }); // Then the list reveals the indicator, which lives in the footer below the row a last-item jump targets - expect(mockScrollToEnd).toHaveBeenCalledTimes(1); + expect(mockScrollToBottom).toHaveBeenCalledTimes(1); }); it('should not scroll for the thinking indicator while the list is away from the bottom', async () => { @@ -576,7 +575,7 @@ describe('useMoneyRequestReportScroll', () => { }); // Then the reading position is untouched - expect(mockScrollToEnd).not.toHaveBeenCalled(); + expect(mockScrollToBottom).not.toHaveBeenCalled(); expect(result.current.isFloatingMessageCounterVisible).toBe(false); }); @@ -589,7 +588,7 @@ describe('useMoneyRequestReportScroll', () => { act(() => { jest.advanceTimersByTime(100); }); - expect(mockScrollToEnd).toHaveBeenCalledTimes(1); + expect(mockScrollToBottom).toHaveBeenCalledTimes(1); // When the thinking label changes, which re-renders the list many times per run rerender(buildParams()); @@ -600,7 +599,7 @@ describe('useMoneyRequestReportScroll', () => { }); // Then the viewport is not yanked down again - expect(mockScrollToEnd).toHaveBeenCalledTimes(1); + expect(mockScrollToBottom).toHaveBeenCalledTimes(1); }); it('should scroll again for a new thinking-indicator run', async () => { @@ -612,7 +611,7 @@ describe('useMoneyRequestReportScroll', () => { act(() => { jest.advanceTimersByTime(100); }); - expect(mockScrollToEnd).toHaveBeenCalledTimes(1); + expect(mockScrollToBottom).toHaveBeenCalledTimes(1); // When that run ends and another one starts mockCandidateAgentIDs = []; @@ -626,7 +625,7 @@ describe('useMoneyRequestReportScroll', () => { }); // Then the new run's indicator is revealed too - expect(mockScrollToEnd).toHaveBeenCalledTimes(2); + expect(mockScrollToBottom).toHaveBeenCalledTimes(2); }); }); diff --git a/tests/unit/useReportActionsNewActionLiveTailTest.ts b/tests/unit/useReportActionsNewActionLiveTailTest.ts index f41cba2ace3e..4350bbc1bad3 100644 --- a/tests/unit/useReportActionsNewActionLiveTailTest.ts +++ b/tests/unit/useReportActionsNewActionLiveTailTest.ts @@ -84,7 +84,6 @@ type HookParams = Parameters[0]; const reportScrollManager = { scrollToBottom: jest.fn(), scrollToIndex: jest.fn(), - scrollToEnd: jest.fn(), scrollToOffset: jest.fn(), }; diff --git a/tests/unit/useReportActionsScrollTest.tsx b/tests/unit/useReportActionsScrollTest.tsx index 857f385cc371..22bfca4d91e3 100644 --- a/tests/unit/useReportActionsScrollTest.tsx +++ b/tests/unit/useReportActionsScrollTest.tsx @@ -42,7 +42,6 @@ jest.mock('@hooks/useReportScrollManager', () => ({ default: () => ({ scrollToBottom: mockScrollToBottom, scrollToIndex: mockScrollToIndex, - scrollToEnd: jest.fn(), scrollToOffset: jest.fn(), }), })); diff --git a/tests/unit/useReportScrollManagerTest.tsx b/tests/unit/useReportScrollManagerTest.tsx index 6435a8dea5e7..5d9bdf09132b 100644 --- a/tests/unit/useReportScrollManagerTest.tsx +++ b/tests/unit/useReportScrollManagerTest.tsx @@ -20,7 +20,7 @@ function buildMockListRef() { scrollToIndex: jest.fn(), scrollToOffset: jest.fn(), scrollToEnd: jest.fn(), - getNativeScrollRef: jest.fn(() => undefined), + getNativeScrollRef: jest.fn(() => ({scrollToEnd: jest.fn()})), }; // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion const ref = {current: methods} as unknown as ActionListRefType; @@ -57,28 +57,32 @@ function renderManager() { describe('useReportScrollManager', () => { it('resolves the list ref at call time, not at init (register AFTER the manager is created)', () => { + // Given a manager created before its list ref is registered. const {result} = renderManager(); const {ref, methods} = buildMockListRef(); - // Register only after the manager hook has already run once. + // When the list registers and a scroll is requested afterward. act(() => { result.current.registerListRef(ref); }); act(() => { - result.current.manager.scrollToEnd(); + result.current.manager.scrollToBottom(); }); - // The ref was resolved lazily — the method on the later-registered ref fired. - expect(methods.scrollToEnd).toHaveBeenCalled(); + // Then the list handles the scroll so its virtualization and footer positioning stay active. + expect(methods.scrollToEnd).toHaveBeenCalledWith({animated: false}); + expect(methods.getNativeScrollRef).not.toHaveBeenCalled(); }); it('no-ops when no ref is registered', () => { + // Given a manager whose list has not mounted yet. const {result} = renderManager(); + // When any scroll command runs, then it tolerates the absent list. expect(() => { act(() => { - result.current.manager.scrollToEnd(); + result.current.manager.scrollToBottom(); result.current.manager.scrollToIndex(3); result.current.manager.scrollToOffset(100); result.current.manager.scrollToIndex(1, {animated: false}); From 311f777c097ca2a8016a4ebe1e66d4723aa2c350 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 22 Oct 2026 13:37:22 +0100 Subject: [PATCH 54/61] refactor: share linked-message positioning cleanup Co-authored-by: Codex --- src/hooks/useLinkedReportActionScroll.ts | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/hooks/useLinkedReportActionScroll.ts b/src/hooks/useLinkedReportActionScroll.ts index 57295a37ea33..b8a77f24856d 100644 --- a/src/hooks/useLinkedReportActionScroll.ts +++ b/src/hooks/useLinkedReportActionScroll.ts @@ -34,14 +34,12 @@ function useLinkedReportActionScroll({listID, linkedReportActionID, targetIndex, shouldPositionRef.current = false; cancelPositionRef.current?.(); }; + const stopPositioningFromEffect = useEffectEvent(stopPositioning); useEffect(() => { shouldPositionRef.current = !!linkedReportActionID; wasLastActionRef.current = false; - return () => { - shouldPositionRef.current = false; - cancelPositionRef.current?.(); - }; + return stopPositioningFromEffect; }, [linkedReportActionID, listID]); useEffect(() => { @@ -50,10 +48,7 @@ function useLinkedReportActionScroll({listID, linkedReportActionID, targetIndex, } // Returning from a child screen must keep the reader's viewport, not reopen the old link. - return () => { - shouldPositionRef.current = false; - cancelPositionRef.current?.(); - }; + return stopPositioningFromEffect; }, [isFocused]); const positionLinkedAction = useEffectEvent(() => { @@ -107,7 +102,6 @@ function useLinkedReportActionScroll({listID, linkedReportActionID, targetIndex, return cancel; }, [isFocused, linkedReportActionID, listID, readyListID, targetIndex, isLastAction, measurementVersion]); - const stopPositioningFromWebInput = useEffectEvent(stopPositioning); useEffect(() => { if (Platform.OS !== 'web' || readyListID !== listID) { return; @@ -120,14 +114,14 @@ function useLinkedReportActionScroll({listID, linkedReportActionID, targetIndex, if (!['ArrowUp', 'ArrowDown', 'PageUp', 'PageDown', 'Home', 'End', ' '].includes(event.key)) { return; } - stopPositioningFromWebInput(); + stopPositioningFromEffect(); }; - scrollElement.addEventListener('wheel', stopPositioningFromWebInput, {passive: true}); - scrollElement.addEventListener('pointerdown', stopPositioningFromWebInput, {passive: true}); + scrollElement.addEventListener('wheel', stopPositioningFromEffect, {passive: true}); + scrollElement.addEventListener('pointerdown', stopPositioningFromEffect, {passive: true}); scrollElement.addEventListener('keydown', onScrollKey); return () => { - scrollElement.removeEventListener('wheel', stopPositioningFromWebInput); - scrollElement.removeEventListener('pointerdown', stopPositioningFromWebInput); + scrollElement.removeEventListener('wheel', stopPositioningFromEffect); + scrollElement.removeEventListener('pointerdown', stopPositioningFromEffect); scrollElement.removeEventListener('keydown', onScrollKey); }; }, [getListRef, listID, readyListID]); From 5c905a665ca121165a299883b65df10a3c507dfc Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 22 Oct 2026 13:37:31 +0100 Subject: [PATCH 55/61] fix: preserve shared-list scroll event behavior Co-authored-by: Codex --- src/components/FlashList/index.tsx | 2 +- src/components/FlatList/FlatList/index.tsx | 2 +- .../KeyboardDismissibleFlatList/index.tsx | 2 +- tests/unit/ComposerScrollEventsTest.tsx | 45 +++++++++++++++++-- 4 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/components/FlashList/index.tsx b/src/components/FlashList/index.tsx index 59291cdb3fe2..f9a7b9528735 100644 --- a/src/components/FlashList/index.tsx +++ b/src/components/FlashList/index.tsx @@ -7,7 +7,7 @@ import {FlashList as ShopifyFlashList} from '@shopify/flash-list'; import React from 'react'; function FlashList({onScroll: onScrollProp, inverted, ...restProps}: FlashListProps) { - const emitComposerScrollEvents = useEmitComposerScrollEvents(); + const emitComposerScrollEvents = useEmitComposerScrollEvents({enabled: !!inverted}); const handleScroll = (e: NativeSyntheticEvent) => { onScrollProp?.(e); diff --git a/src/components/FlatList/FlatList/index.tsx b/src/components/FlatList/FlatList/index.tsx index 682d40174bd9..c50c521eee01 100644 --- a/src/components/FlatList/FlatList/index.tsx +++ b/src/components/FlatList/FlatList/index.tsx @@ -223,7 +223,7 @@ function MVCPFlatList({ }; }, []); - const emitComposerScrollEvents = useEmitComposerScrollEvents(); + const emitComposerScrollEvents = useEmitComposerScrollEvents({enabled: !!restProps.inverted}); const handleScroll = useCallback( (e: NativeSyntheticEvent) => { onScrollProp?.(e); diff --git a/src/components/KeyboardDismissibleFlatList/index.tsx b/src/components/KeyboardDismissibleFlatList/index.tsx index d48e9f7b96e6..effaefefceaa 100644 --- a/src/components/KeyboardDismissibleFlatList/index.tsx +++ b/src/components/KeyboardDismissibleFlatList/index.tsx @@ -11,7 +11,7 @@ import {useKeyboardDismissibleFlatListActions} from './KeyboardDismissibleFlatLi function KeyboardDismissibleFlatList({onScroll: onScrollProp, ref, ...restProps}: AnimatedFlatListWithCellRendererProps) { const {onScroll: onScrollHandleKeyboard} = useKeyboardDismissibleFlatListActions(); - const emitComposerScrollEvents = useEmitComposerScrollEvents(); + const emitComposerScrollEvents = useEmitComposerScrollEvents({enabled: !!restProps.inverted}); const additionalOnScroll = useAnimatedScrollHandler({ onScroll: emitComposerScrollEvents, diff --git a/tests/unit/ComposerScrollEventsTest.tsx b/tests/unit/ComposerScrollEventsTest.tsx index 6ee1540209a4..cced2c8752dc 100644 --- a/tests/unit/ComposerScrollEventsTest.tsx +++ b/tests/unit/ComposerScrollEventsTest.tsx @@ -55,7 +55,7 @@ function HoverTarget({ref, ...props}: ViewProps & {ref?: React.Ref} return ; } -describe('Composer scroll events on chronological lists', () => { +describe('Composer scroll events on shared lists', () => { beforeEach(() => jest.useFakeTimers()); afterEach(() => { jest.clearAllTimers(); @@ -68,7 +68,7 @@ describe('Composer scroll events on chronological lists', () => { ['MVCP FlatList', MVCPFlatList], ['keyboard dismissible FlatList', KeyboardDismissibleFlatList], ] as const)('%s suppresses hover throughout scrolling and forwards the scroll event', (_name, List) => { - // Given a chronological list, as used by expense chats after the migration. + // Given an inverted list that already emits composer scroll events. const emit = jest.spyOn(DeviceEventEmitter, 'emit'); const onScroll = jest.fn(); render( @@ -77,7 +77,7 @@ describe('Composer scroll events on chronological lists', () => { testID="chronological-list" data={['message']} renderItem={() => } - inverted={false} + inverted onScroll={onScroll} /> @@ -114,4 +114,43 @@ describe('Composer scroll events on chronological lists', () => { fireEvent(screen.getByTestId('hoverable-message'), 'mouseEnter'); expect(screen.getByTestId('hoverable-message').props.accessibilityLabel).toBe('hovered'); }); + + it.each([ + ['FlashList', FlashList], + ['MVCP FlatList', MVCPFlatList], + ['keyboard dismissible FlatList', KeyboardDismissibleFlatList], + ] as const)('%s forwards non-inverted scrolling without changing global hover state', (_name, List) => { + // Given a non-inverted shared list and an unrelated hovered message. + const emit = jest.spyOn(DeviceEventEmitter, 'emit'); + const onScroll = jest.fn(); + render( + <> + } + inverted={false} + onScroll={onScroll} + /> + + {(isHovered) => ( + + )} + + , + ); + fireEvent(screen.getByTestId('hoverable-message'), 'mouseEnter'); + + // When that list scrolls and its scroll-end timer would have settled. + fireEvent.scroll(screen.getByTestId('chronological-list'), scrollEvent); + act(() => jest.advanceTimersByTime(250)); + + // Then its callback still runs without broadcasting a report scroll or clearing hover. + expect(onScroll).toHaveBeenCalledWith(scrollEvent); + expect(emit).not.toHaveBeenCalledWith(CONST.EVENTS.SCROLLING, expect.anything()); + expect(screen.getByTestId('hoverable-message').props.accessibilityLabel).toBe('hovered'); + }); }); From e3b9d15b88b679716f9d114e0c31396711f06678 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 22 Oct 2026 13:37:43 +0100 Subject: [PATCH 56/61] docs: clarify edit-message scroll scheduling Co-authored-by: Codex --- src/pages/inbox/report/ReportActionCompose/useEditMessage.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/pages/inbox/report/ReportActionCompose/useEditMessage.ts b/src/pages/inbox/report/ReportActionCompose/useEditMessage.ts index f64d8b2062de..ce9e49df98fb 100644 --- a/src/pages/inbox/report/ReportActionCompose/useEditMessage.ts +++ b/src/pages/inbox/report/ReportActionCompose/useEditMessage.ts @@ -62,8 +62,9 @@ function useEditMessage({ clearAllReportActionDrafts(); - // Wait for the editor to close and the restored action row to lay out before scrolling. - // Scrolling against the editor's old height can leave the final action below the viewport. + // Give the edit-state update a frame before scrolling the newest action. + // Inline edits do not start a tracked transition, so TransitionTracker.runAfterTransitions + // cannot wait for their layout. This frame delay is not a layout-completion guarantee. if (!shouldScrollToLastMessage) { return; } From c8de6b3aa78e49ae5d4c335a83832697170593fb Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 22 Oct 2026 13:45:25 +0100 Subject: [PATCH 57/61] fix: preserve LegendList native scroll event cadence Co-authored-by: Codex --- src/pages/inbox/report/ReportActionsList.tsx | 7 +----- tests/ui/ReportActionsListTest.tsx | 25 +++++++++++++++++++- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index 4704a0cb2fb5..9a42e1b52d14 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -1,5 +1,4 @@ import {renderScrollComponent as renderActionSheetAwareScrollView} from '@components/ActionSheetAwareScrollView'; -import type {ActionSheetAwareScrollViewProps} from '@components/ActionSheetAwareScrollView/types'; import allowLegendListItemOverflow from '@components/LegendList/allowLegendListItemOverflow'; import MerchantRuleSuggestionBanner from '@components/MerchantRuleSuggestionBanner'; import {ReportActionsAnimatedSkeletonCover} from '@components/ReportActionsSkeletonCover'; @@ -821,7 +820,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct drawDistance={REPORT_ACTIONS_DRAW_DISTANCE} recycleItems experimental_hideItemsUntilMeasured - renderScrollComponent={Platform.OS === 'web' ? undefined : renderReportActionsScrollView} + renderScrollComponent={Platform.OS === 'web' ? undefined : renderActionSheetAwareScrollView} scrollEventThrottle={16} contentContainerStyle={[styles.chatContentScrollView, styles.pb0]} onEndReached={loadNewerChatsAfterTransitions} @@ -888,7 +887,3 @@ function ReportActionsList({reportID, conciergeChat, onLayout}: ReportActionsLis } export default ReportActionsList; - -function renderReportActionsScrollView(props: ActionSheetAwareScrollViewProps) { - return renderActionSheetAwareScrollView({...props, scrollEventThrottle: 16}); -} diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 996fb2cf16db..bfe3ceb794cd 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -1,5 +1,6 @@ import {act, render, screen, waitFor} from '@testing-library/react-native'; +import type {RenderActionSheetAwareScrollViewComponent} from '@components/ActionSheetAwareScrollView/types'; import allowLegendListItemOverflow from '@components/LegendList/allowLegendListItemOverflow'; import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; @@ -37,12 +38,13 @@ import type {StyleProp, ViewStyle} from 'react-native'; import {useRoute} from '@react-navigation/native'; import React from 'react'; -import {StyleSheet} from 'react-native'; +import {Platform, StyleSheet} from 'react-native'; import Onyx from 'react-native-onyx'; import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; const mockUseIsFocused = jest.fn().mockReturnValue(false); +jest.unmock('@components/ActionSheetAwareScrollView'); jest.mock('@react-navigation/native', () => { const actualNav = jest.requireActual('@react-navigation/native'); return { @@ -222,6 +224,8 @@ type MockLegendListProps = { onViewableItemsChanged?: (info: OnViewableItemsChangedInfo) => void; recycleItems?: boolean; renderItem?: (info: {item: OnyxTypes.ReportAction; index: number}) => React.ReactElement | null; + renderScrollComponent?: RenderActionSheetAwareScrollViewComponent; + scrollEventThrottle?: number; onStartReached?: () => void; onScroll?: (event: { nativeEvent: { @@ -486,6 +490,25 @@ describe('ReportActionsList (body)', () => { await waitFor(() => expect(mockLegendScrollToEnd).toHaveBeenCalledWith({animated: false})); }); + it('keeps the native scroll-view throttle supplied by LegendList', () => { + // Given a report list using the action-sheet-aware native scroller. + mockUseNetwork.mockReturnValue({isOffline: false}); + const platform = jest.replaceProperty(Platform, 'OS', 'android'); + try { + renderReportActionsList(); + const listProps = getCapturedListProps(); + + // When LegendList requests native scroll events on every frame. + const scrollView = listProps?.renderScrollComponent?.({scrollEventThrottle: 0}); + + // Then the wrapper preserves them while LegendList separately throttles the app callback. + expect(scrollView?.props.scrollEventThrottle).toBe(0); + expect(listProps?.scrollEventThrottle).toBe(16); + } finally { + platform.restore(); + } + }); + it('keeps the latest message visible when its measured height grows after opening', async () => { // Given the chat has opened at the end with a measured viewport. mockUseNetwork.mockReturnValue({isOffline: false}); From e466ab7b4c8adcfb846921e9479f3b5d19a4f2e7 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 22 Oct 2026 13:45:56 +0100 Subject: [PATCH 58/61] fix: retain the 250-pixel incoming-message follow range Co-authored-by: Codex --- src/hooks/useReportActionsScroll.ts | 15 ++- src/pages/inbox/report/ReportActionsList.tsx | 17 +-- tests/ui/ReportActionsListTest.tsx | 112 ++++++++++++++++++- tests/unit/useReportActionsScrollTest.tsx | 25 ++++- 4 files changed, 154 insertions(+), 15 deletions(-) diff --git a/src/hooks/useReportActionsScroll.ts b/src/hooks/useReportActionsScroll.ts index 7d6c7882e7ce..a254be3dc64d 100644 --- a/src/hooks/useReportActionsScroll.ts +++ b/src/hooks/useReportActionsScroll.ts @@ -36,6 +36,7 @@ import useNetworkWithOfflineStatus from './useNetworkWithOfflineStatus'; import useOnyx from './useOnyx'; import usePrevious from './usePrevious'; import useReportScrollManager from './useReportScrollManager'; +import useScrollToEndOnNewMessageReceived from './useScrollToEndOnNewMessageReceived'; type UseReportActionsScrollParams = { /** Identifies the mounted list, including linked-message and hydration remounts. */ @@ -257,7 +258,6 @@ function useReportActionsScroll({ // Consume an explicit live-tail request after the data render, not on an unrelated future // viewport layout (for example, opening the keyboard after the user has scrolled away). - // Incoming messages and streaming draft growth are followed by LegendList itself. useEffect(() => { if (!isScrollToBottomEnabled) { return; @@ -355,6 +355,19 @@ function useReportActionsScroll({ isLastAction: targetIndex === renderedVisibleReportActions.length - 1, }); + useScrollToEndOnNewMessageReceived({ + scrollOffsetRef, + lastActionID: lastAction?.reportActionID, + visibleActionsLength: sortedVisibleReportActions.length, + hasNewestReportAction, + setIsFloatingMessageCounterVisible, + scrollToEnd: () => { + stopLinkedMessagePositioning(); + reportScrollManager.scrollToBottom(); + }, + resetKey: `${reportID}:${linkedReportActionID}`, + }); + const scrollToBottomAndMarkReportAsRead = () => { stopLinkedMessagePositioning(); setIsFloatingMessageCounterVisible(false); diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index 9a42e1b52d14..28d9b1a34a04 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -99,7 +99,6 @@ type ReportActionsListContentProps = { type ReportActionsListProps = ReportActionsListContentProps; const PAGINATION_THRESHOLD = 0.75; -const MAINTAIN_SCROLL_AT_END_THRESHOLD = 0.01; const REPORT_ACTIONS_DRAW_DISTANCE = 1500; const REPORT_ACTION_COMMENT_SIZE = { @@ -257,7 +256,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct const linkedReportActionID = reportActionIDFromRoute; - const {getScrollOffset} = useActionListContext(); + const {getScrollOffset, scrollOffsetRef} = useActionListContext(); const listRef = useActionListRef(); const legendListRef = useRef(null); @@ -592,7 +591,7 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct const isNearStart = contentOffset.y <= layoutMeasurement.height * PAGINATION_THRESHOLD; const previousMetrics = lastScrollMetricsRef.current; if (readyEndListIDRef.current === listID && !hasNewerActions) { - if (distanceFromBottom <= layoutMeasurement.height * MAINTAIN_SCROLL_AT_END_THRESHOLD) { + if (distanceFromBottom < CONST.REPORT.ACTIONS.AUTOSCROLL_TO_TOP_THRESHOLD) { shouldFollowEndOnResizeRef.current = true; } else if ( (previousMetrics && contentOffset.y < previousMetrics.offset - 1) || @@ -767,6 +766,12 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct */ const recordTimeToMeasureItemLayout = (event: LayoutChangeEvent) => { onLayout?.(event); + const state = legendListRef.current?.getState(); + if (state && readyEndListIDRef.current === listID) { + const distanceFromBottom = Math.max(0, state.contentLength - state.scrollLength - state.scroll); + scrollOffsetRef.current = distanceFromBottom; + distanceFromBottomRef.current = distanceFromBottom; + } scheduleEndCorrection(); if (didLayout.current) { return; @@ -841,10 +846,8 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct initialScrollAtEnd={initialScrollIndex === undefined} initialScrollIndex={initialScrollIndex === undefined ? undefined : {index: initialScrollIndex, ...initialScrollIndexParams}} alignItemsAtEnd={!shouldBeAlignedToTop} - // Follow new actions on the latest page. Layout changes use the cancellable correction above. - maintainScrollAtEnd={!hasNewerActions && {animated: false, on: {dataChange: true}}} - // Leave the end-follow region as soon as the user starts reading older messages. - maintainScrollAtEndThreshold={MAINTAIN_SCROLL_AT_END_THRESHOLD} + // Keep the existing pixel-based new-message policy in useReportActionsScroll. + maintainScrollAtEnd={false} maintainVisibleContentPosition onReady={handleListReady} onItemSizeChanged={onItemSizeChanged} diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index bfe3ceb794cd..36eaaa129cca 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -34,13 +34,14 @@ import type * as OnyxTypes from '@src/types/onyx'; import type {OnViewableItemsChangedInfo} from '@legendapp/list/react-native'; import type * as ReactNavigation from '@react-navigation/native'; -import type {StyleProp, ViewStyle} from 'react-native'; +import type {LayoutChangeEvent, StyleProp, ViewStyle} from 'react-native'; import {useRoute} from '@react-navigation/native'; import React from 'react'; import {Platform, StyleSheet} from 'react-native'; import Onyx from 'react-native-onyx'; +import createMock from '../utils/createMock'; import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; const mockUseIsFocused = jest.fn().mockReturnValue(false); @@ -150,7 +151,13 @@ const mockLegendListMount = jest.fn(); const mockLegendListUnmount = jest.fn(); const mockLegendScrollToEnd = jest.fn(); const mockLegendPositionByKey = jest.fn(() => 1500); -const mockLegendGetState = jest.fn(() => ({positionByKey: mockLegendPositionByKey, isAtEnd: false})); +const mockLegendGetState = jest.fn(() => ({ + positionByKey: mockLegendPositionByKey, + isAtEnd: false, + contentLength: 1000, + scrollLength: 500, + scroll: 500, +})); const mockLegendGetScrollableNode = jest.fn(() => ({scrollTop: 0})); let mockShouldCallLegendListOnLoad = true; jest.mock('@legendapp/list/react-native', () => { @@ -216,6 +223,7 @@ type MockLegendListProps = { maintainScrollAtEnd?: {animated: boolean; on: {dataChange: boolean}} | false; maintainScrollAtEndThreshold?: number; maintainVisibleContentPosition?: boolean; + onLayout?: (event: LayoutChangeEvent) => void; onLoad?: () => void; onReady?: () => void; onScrollBeginDrag?: () => void; @@ -509,6 +517,75 @@ describe('ReportActionsList (body)', () => { } }); + it.each([800, 1000])('refreshes the newest-message offset without following successive viewport layouts at %i pixels', async (height) => { + // Given a reader 600 pixels from the end before the viewport grows. + mockUseNetwork.mockReturnValue({isOffline: false}); + const originalState = mockLegendGetState(); + const scrollOffsetRef = {current: 600}; + const contextValue = {scrollOffsetRef, getScrollOffset: () => scrollOffsetRef.current, getListRef: () => null, registerListRef: jest.fn()}; + render( + // The shared context intentionally combines stable refs with their accessors. + // eslint-disable-next-line rulesdir/context-provider-split-values + + + , + ); + await waitFor(() => expect(mockLegendScrollToEnd).toHaveBeenCalled()); + mockLegendScrollToEnd.mockClear(); + const frames: FrameRequestCallback[] = []; + const requestFrame = jest.spyOn(global, 'requestAnimationFrame').mockImplementation((callback) => frames.push(callback)); + try { + act(() => { + const listProps = getCapturedListProps(); + listProps?.onScrollBeginDrag?.(); + listProps?.onScroll?.({ + nativeEvent: { + contentOffset: {x: 0, y: 900}, + contentSize: {height: 2000, width: 300}, + layoutMeasurement: {height: 500, width: 300}, + }, + }); + }); + + // When successive layouts resize the viewport without new content or scroll events. + for (const viewportHeight of [height, height + 50]) { + mockLegendGetState.mockReturnValue({...originalState, contentLength: 2000, scrollLength: viewportHeight, scroll: 900}); + act(() => getCapturedListProps()?.onLayout?.(createMock({nativeEvent: {layout: {x: 0, y: 0, width: 300, height: viewportHeight}}}))); + + // Then a future incoming message uses the current pixel offset without resizing itself starting a scroll. + expect(scrollOffsetRef.current).toBe(2000 - viewportHeight - 900); + expect(frames).toHaveLength(0); + } + expect(mockLegendScrollToEnd).not.toHaveBeenCalled(); + expect(getCapturedListProps()?.maintainScrollAtEnd).toBe(false); + } finally { + requestFrame.mockRestore(); + mockLegendGetState.mockReturnValue(originalState); + } + }); + + it('keeps following the newest action when the keyboard shrinks the viewport', async () => { + // Given the reader is following the newest action before opening the keyboard. + mockUseNetwork.mockReturnValue({isOffline: false}); + const originalState = mockLegendGetState(); + renderReportActionsList(); + await waitFor(() => expect(mockLegendScrollToEnd).toHaveBeenCalled()); + mockLegendScrollToEnd.mockClear(); + try { + // When the smaller viewport temporarily leaves the newest action 300 pixels below it. + mockLegendGetState.mockReturnValue({...originalState, contentLength: 1000, scrollLength: 200, scroll: 500}); + act(() => getCapturedListProps()?.onLayout?.(createMock({nativeEvent: {layout: {x: 0, y: 0, width: 300, height: 200}}}))); + + // Then resizing keeps the existing end-following intent. + await waitFor(() => expect(mockLegendScrollToEnd).toHaveBeenCalledWith({animated: false})); + } finally { + mockLegendGetState.mockReturnValue(originalState); + } + }); + it('keeps the latest message visible when its measured height grows after opening', async () => { // Given the chat has opened at the end with a measured viewport. mockUseNetwork.mockReturnValue({isOffline: false}); @@ -542,6 +619,31 @@ describe('ReportActionsList (body)', () => { await waitFor(() => expect(mockLegendScrollToEnd).toHaveBeenCalledWith({animated: false})); }); + it('follows content growth when the reader is 50 pixels above the newest action', async () => { + // Given a reader still within the existing 250-pixel follow distance. + mockUseNetwork.mockReturnValue({isOffline: false}); + renderReportActionsList(); + await waitFor(() => expect(mockLegendScrollToEnd).toHaveBeenCalled()); + mockLegendScrollToEnd.mockClear(); + act(() => { + const listProps = getCapturedListProps(); + listProps?.onScrollBeginDrag?.(); + listProps?.onScroll?.({ + nativeEvent: { + contentOffset: {x: 0, y: 450}, + contentSize: {height: 1000, width: 300}, + layoutMeasurement: {height: 500, width: 300}, + }, + }); + }); + + // When a new message or streamed text increases the content height. + act(() => getCapturedListProps()?.onContentSizeChange?.(300, 1025)); + + // Then it follows the new content even though the reader is more than 1% from the end. + await waitFor(() => expect(mockLegendScrollToEnd).toHaveBeenCalledWith({animated: false})); + }); + it('does not pull the chat back down when the reader has scrolled away', async () => { // Given the chat opened at the end and the reader scrolled upward. mockUseNetwork.mockReturnValue({isOffline: false}); @@ -559,7 +661,7 @@ describe('ReportActionsList (body)', () => { }); listProps?.onScroll?.({ nativeEvent: { - contentOffset: {x: 0, y: 400}, + contentOffset: {x: 0, y: 200}, contentSize: {height: 1000, width: 300}, layoutMeasurement: {height: 500, width: 300}, }, @@ -616,7 +718,7 @@ describe('ReportActionsList (body)', () => { const originalImplementation = mockUseReportActionsScroll.getMockImplementation(); const defaultScrollState = mockUseReportActionsScroll() as ReturnType; mockUseReportActionsScroll.mockReturnValue({...defaultScrollState, initialScrollIndex: 1}); - mockLegendGetState.mockReturnValue({positionByKey: mockLegendPositionByKey, isAtEnd}); + mockLegendGetState.mockReturnValue({...mockLegendGetState(), isAtEnd}); try { renderReportActionsList(); @@ -639,7 +741,7 @@ describe('ReportActionsList (body)', () => { } } finally { mockUseReportActionsScroll.mockImplementation(originalImplementation); - mockLegendGetState.mockReturnValue({positionByKey: mockLegendPositionByKey, isAtEnd: false}); + mockLegendGetState.mockReturnValue({...mockLegendGetState(), isAtEnd: false}); } }); diff --git a/tests/unit/useReportActionsScrollTest.tsx b/tests/unit/useReportActionsScrollTest.tsx index 22bfca4d91e3..cbca996a25c5 100644 --- a/tests/unit/useReportActionsScrollTest.tsx +++ b/tests/unit/useReportActionsScrollTest.tsx @@ -679,6 +679,27 @@ describe('useReportActionsScroll', () => { }); }); + describe('new-message following', () => { + it.each([ + {distance: 50, shouldFollow: true}, + {distance: 250, shouldFollow: false}, + {distance: 300, shouldFollow: false}, + ])('preserves the pixel threshold at $distance pixels from the newest action', async ({distance, shouldFollow}) => { + // Given a loaded latest page and the reader's current bottom-relative offset. + const {rerender} = await renderScroll(); + mockScrollOffsetRef.current = distance; + mockScrollToBottom.mockClear(); + const newestAction = makeAction('2'); + + // When another user's message is appended to the chronological list. + rerender(buildParams({sortedVisibleReportActions: [newestAction, makeAction('1')], renderedVisibleReportActions: [makeAction('1'), newestAction]})); + await waitForBatchedUpdatesWithAct(); + + // Then it follows only inside the same 250-pixel range used before the migration. + expect(mockScrollToBottom).toHaveBeenCalledTimes(shouldFollow ? 1 : 0); + }); + }); + describe('scrollToBottomAndMarkReportAsRead', () => { it('scrolls to bottom and marks as read when the newest action is present', async () => { // Default created === lastVisibleActionCreated → newest present. @@ -839,7 +860,7 @@ describe('useReportActionsScroll', () => { }); describe('effects', () => { - it('leaves incoming-message following to LegendList', async () => { + it('follows incoming messages through the shared pixel-based policy', async () => { mockScrollOffsetRef.current = 0; const {rerender} = await renderScroll(); @@ -847,7 +868,7 @@ describe('useReportActionsScroll', () => { const actions = [makeAction('2'), makeAction('1')]; rerender(buildParams({sortedVisibleReportActions: actions, renderedVisibleReportActions: actions.toReversed()})); - expect(mockScrollToBottom).not.toHaveBeenCalled(); + expect(mockScrollToBottom).toHaveBeenCalledTimes(1); }); it('clears the X Replies flag once it has been applied', async () => { From 4b8a445c68c773db779a893c7e0ccf4d503035bc Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 22 Oct 2026 13:46:06 +0100 Subject: [PATCH 59/61] fix: end warm report timing when covered content is visible Co-authored-by: Codex --- src/pages/inbox/report/ReportActionsList.tsx | 21 ++++--- tests/ui/ReportActionsListTest.tsx | 64 ++++++++++++++++++++ 2 files changed, 76 insertions(+), 9 deletions(-) diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index 28d9b1a34a04..8569440e5ace 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -70,7 +70,7 @@ import type {LayoutChangeEvent, NativeScrollEvent, NativeSyntheticEvent} from 'r import type {OnyxEntry} from 'react-native-onyx'; import {LegendList} from '@legendapp/list/react-native'; -import {useRoute} from '@react-navigation/native'; +import {useIsFocused, useRoute} from '@react-navigation/native'; import {getCustomAgentAccountIDs} from '@selectors/AgentZeroChat'; import {isTrackIntentUserSelector} from '@selectors/Onboarding'; import {accountIDSelector} from '@selectors/Session'; @@ -166,6 +166,7 @@ function keyExtractor(item: OnyxTypes.ReportAction): string { * mounts it only once content is ready, so those hooks never run while a skeleton shows. */ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportActionsListContentProps) { + const isFocused = useIsFocused(); const styles = useThemeStyles(); const {translate} = useLocalize(); const {isProduction} = useEnvironment(); @@ -200,7 +201,6 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct const reportActionIDFromRoute = route?.params?.reportActionID; const sessionStartTime = useConciergeSessionStartTime(); - const didLayout = useRef(false); const lastRequestedOldestActionIDRef = useRef(undefined); const emitComposerScrollEvents = useEmitComposerScrollEvents({enabled: true}); @@ -504,6 +504,16 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct const [loadedInitialViewportListID, setLoadedInitialViewportListID] = useState(); // Cover every initial LegendList layout, including cached chats, until the rows are positioned. const shouldShowInitialViewportSkeleton = !isOffline && (isInitialReportLoadPending || loadedInitialViewportListID !== listID); + + useEffect(() => { + if (!isFocused || shouldShowInitialViewportSkeleton || loadedInitialViewportListID !== listID) { + return; + } + // onReady positions the list, but only this committed render has removed its cover. + // Refocus also completes new spans when the positioned list stays mounted. + markOpenReportEnd(reportID, report, {warm: true}); + }, [isFocused, listID, loadedInitialViewportListID, report, reportID, shouldShowInitialViewportSkeleton]); + const readyEndListIDRef = useRef(undefined); const lastScrollMetricsRef = useRef<{offset: number; contentHeight: number; viewportHeight: number} | undefined>(undefined); const endCorrectionFrameRef = useRef(undefined); @@ -773,13 +783,6 @@ function ReportActionsListContent({reportID, conciergeChat, onLayout}: ReportAct distanceFromBottomRef.current = distanceFromBottom; } scheduleEndCorrection(); - if (didLayout.current) { - return; - } - - didLayout.current = true; - - markOpenReportEnd(reportID, report, {warm: true}); }; // The guard only mounts this content when the report is loaded, so this is effectively unreachable. diff --git a/tests/ui/ReportActionsListTest.tsx b/tests/ui/ReportActionsListTest.tsx index 36eaaa129cca..191ef48ed8bd 100644 --- a/tests/ui/ReportActionsListTest.tsx +++ b/tests/ui/ReportActionsListTest.tsx @@ -391,6 +391,7 @@ describe('ReportActionsList (body)', () => { beforeEach(() => { jest.clearAllMocks(); + mockUseIsFocused.mockReturnValue(false); mockHasOnceLoadedReportActions = true; mockIsLoadingInitialReportActions = false; mockIsLoadingOlderReportActions = false; @@ -862,6 +863,7 @@ describe('ReportActionsList (body)', () => { it('keeps the initial viewport covered until the hydrated LegendList finishes rendering it', async () => { mockUseNetwork.mockReturnValue({isOffline: false}); + mockUseIsFocused.mockReturnValue(true); mockHasOnceLoadedReportActions = false; mockIsLoadingInitialReportActions = true; mockShouldCallLegendListOnLoad = false; @@ -873,6 +875,7 @@ describe('ReportActionsList (body)', () => { getCapturedListProps()?.onReady?.(); }); expect(screen.getByTestId('ReportActionsSkeletonView')).toBeTruthy(); + expect(mockMarkOpenReportEnd).not.toHaveBeenCalledWith(mockReport.reportID, mockReport, {warm: true}); mockHasOnceLoadedReportActions = true; view.rerender( @@ -890,6 +893,7 @@ describe('ReportActionsList (body)', () => { getCapturedListProps()?.onReady?.(); }); expect(screen.queryByTestId('ReportActionsSkeletonView')).toBeNull(); + expect(mockMarkOpenReportEnd).toHaveBeenCalledWith(mockReport.reportID, mockReport, {warm: true}); }); it('resumes initial scroll tracking after layout and scrolling are ready', () => { @@ -1837,6 +1841,66 @@ describe('ReportActionsList (body)', () => { }); describe('Open-report telemetry', () => { + beforeEach(() => mockUseIsFocused.mockReturnValue(true)); + + it('ends the warm span when the positioned content replaces its cover', () => { + // Given cached messages whose initial list layout is still covered. + mockUseNetwork.mockReturnValue({isOffline: false}); + mockShouldCallLegendListOnLoad = false; + renderReportActionsList(); + act(() => getCapturedListProps()?.onLayout?.(createMock({nativeEvent: {layout: {x: 0, y: 0, width: 300, height: 500}}}))); + expect(screen.getByTestId('ReportActionsSkeletonCover')).toBeTruthy(); + expect(mockMarkOpenReportEnd).not.toHaveBeenCalledWith(mockReport.reportID, mockReport, {warm: true}); + + // When LegendList finishes positioning and the cover is removed. + act(() => getCapturedListProps()?.onReady?.()); + + // Then telemetry records visible content once, rather than the earlier hidden layout. + expect(screen.queryByTestId('ReportActionsSkeletonCover')).toBeNull(); + expect(mockMarkOpenReportEnd).toHaveBeenCalledWith(mockReport.reportID, mockReport, {warm: true}); + act(() => getCapturedListProps()?.onLayout?.(createMock({nativeEvent: {layout: {x: 0, y: 0, width: 300, height: 400}}}))); + expect(mockMarkOpenReportEnd).toHaveBeenCalledTimes(1); + }); + + it('does not wait for a background OpenReport request when reopening a hydrated report', () => { + // Given a report loaded earlier in this app session with a refresh still in flight. + mockUseNetwork.mockReturnValue({isOffline: false}); + mockHasOnceLoadedReportActions = true; + mockIsLoadingInitialReportActions = true; + mockUseIsReportLoadPending.mockReturnValue(true); + mockShouldCallLegendListOnLoad = false; + renderReportActionsList(); + + // When the cached list finishes positioning, before that request completes. + act(() => getCapturedListProps()?.onReady?.()); + + // Then the cover clears and visible-content telemetry ends immediately. + expect(screen.queryByTestId('ReportActionsSkeletonCover')).toBeNull(); + expect(mockMarkOpenReportEnd).toHaveBeenCalledWith(mockReport.reportID, mockReport, {warm: true}); + }); + + it('ends a new warm span when an already-positioned report regains focus', () => { + // Given a positioned report that remains mounted while another screen has focus. + mockUseNetwork.mockReturnValue({isOffline: false}); + mockUseIsFocused.mockReturnValue(false); + const view = renderReportActionsList(); + expect(mockMarkOpenReportEnd).not.toHaveBeenCalled(); + + // When navigation returns without another list layout or OpenReport response. + mockUseIsFocused.mockReturnValue(true); + view.rerender( + , + ); + + // Then the visible cached content completes the new navigation span. + expect(mockLegendListMount).toHaveBeenCalledTimes(1); + expect(mockMarkOpenReportEnd).toHaveBeenCalledWith(mockReport.reportID, mockReport, {warm: true}); + }); + it('fires markOpenReportEnd with warm:false while the initial skeleton shows', () => { mockUseNetwork.mockReturnValue({isOffline: false}); From 49eafc2b8e5af05f3628c9e13cf0e5a5a0365b53 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 22 Oct 2026 14:16:30 +0100 Subject: [PATCH 60/61] fix: complete edit-message scrolling after list layout Co-authored-by: Codex --- .../MoneyRequestReportActionsList.tsx | 17 ++++++- .../MoneyRequestReportTransactionList.tsx | 5 ++ .../MoneyRequestReportUnifiedList.tsx | 5 ++ .../ReportActionCompose/useEditMessage.ts | 16 +++---- ...equestReportActionsListRejectModalTest.tsx | 46 +++++++++++++++++-- tests/unit/hooks/useEditMessage.test.ts | 32 ++++--------- 6 files changed, 84 insertions(+), 37 deletions(-) diff --git a/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx b/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx index 4eb733e6b67f..98946aff2f06 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx @@ -230,6 +230,20 @@ function MoneyRequestReportActionsListContent({reportIDFromRoute, onLayout}: Mon completeSkippedMarkAsRead, }); + const [editScrollRequestID, setEditScrollRequestID] = useState(0); + const completedEditScrollRequestIDRef = useRef(0); + const requestScrollToNewestAfterEdit = () => { + // Force a layout commit even when closing the editor leaves the row's height unchanged. + setEditScrollRequestID((requestID) => requestID + 1); + }; + const onCommitLayoutEffect = () => { + if (editScrollRequestID <= completedEditScrollRequestIDRef.current) { + return; + } + completedEditScrollRequestIDRef.current = editScrollRequestID; + scrollToBottom(); + }; + // Wait for the actions to arrive before jumping, and clear the route param afterwards so a later remount // doesn't yank the user back down. useEffect(() => { @@ -249,7 +263,7 @@ function MoneyRequestReportActionsListContent({reportIDFromRoute, onLayout}: Mon const isNewestReportAction = indexWithinReportActions === visibleReportActions.length - 1; return ( - + void; + /** Called after FlashList finishes measuring and committing its rows. */ + onCommitLayoutEffect?: () => void; + /** FlashList onScroll callback. */ onScroll: (event: NativeSyntheticEvent) => void; @@ -189,6 +192,7 @@ function MoneyRequestReportTransactionList({ onLastItemIndexChange, accessibilityLabel, onListLayout, + onCommitLayoutEffect, onScroll, onScrollBeginDrag, onContentSizeChange, @@ -467,6 +471,7 @@ function MoneyRequestReportTransactionList({ onLastItemIndexChange={onLastItemIndexChange} accessibilityLabel={accessibilityLabel} onLayout={onListLayout} + onCommitLayoutEffect={onCommitLayoutEffect} onScroll={onScroll} onScrollBeginDrag={onScrollBeginDrag} onContentSizeChange={onContentSizeChange} diff --git a/src/components/MoneyRequestReportView/MoneyRequestReportUnifiedList.tsx b/src/components/MoneyRequestReportView/MoneyRequestReportUnifiedList.tsx index c4fe72bf25f2..44a681fdab31 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestReportUnifiedList.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestReportUnifiedList.tsx @@ -98,6 +98,9 @@ type MoneyRequestReportUnifiedListProps = { /** Called when the list lays out. */ onLayout: (event: LayoutChangeEvent) => void; + /** Called after FlashList finishes measuring and committing its rows. */ + onCommitLayoutEffect?: () => void; + onScroll: (event: NativeSyntheticEvent) => void; /** Called when the user starts dragging the list. */ @@ -140,6 +143,7 @@ function MoneyRequestReportUnifiedList({ listRef, accessibilityLabel, onLayout, + onCommitLayoutEffect, onScroll, onScrollBeginDrag, onContentSizeChange, @@ -357,6 +361,7 @@ function MoneyRequestReportUnifiedList({ maintainVisibleContentPosition={{autoscrollToBottomThreshold: undefined}} onViewableItemsChanged={onViewableItemsChangedAdjusted} onLayout={handleLayout} + onCommitLayoutEffect={onCommitLayoutEffect} onEndReached={onEndReached} onEndReachedThreshold={0.75} onStartReached={onStartReached} diff --git a/src/pages/inbox/report/ReportActionCompose/useEditMessage.ts b/src/pages/inbox/report/ReportActionCompose/useEditMessage.ts index ce9e49df98fb..f04852e7ddde 100644 --- a/src/pages/inbox/report/ReportActionCompose/useEditMessage.ts +++ b/src/pages/inbox/report/ReportActionCompose/useEditMessage.ts @@ -62,20 +62,16 @@ function useEditMessage({ clearAllReportActionDrafts(); - // Give the edit-state update a frame before scrolling the newest action. - // Inline edits do not start a tracked transition, so TransitionTracker.runAfterTransitions - // cannot wait for their layout. This frame delay is not a layout-completion guarantee. if (!shouldScrollToLastMessage) { return; } - requestAnimationFrame(() => { - if (scrollToLastMessage) { - scrollToLastMessage(); - return; - } - reportScrollManager.scrollToBottom(); - }); + // The list owns this request after the editor unmounts and waits for its layout commit. + if (scrollToLastMessage) { + scrollToLastMessage(); + return; + } + reportScrollManager.scrollToBottom(); } /** diff --git a/tests/ui/MoneyRequestReportActionsListRejectModalTest.tsx b/tests/ui/MoneyRequestReportActionsListRejectModalTest.tsx index 7332aec89f59..caa688d7005e 100644 --- a/tests/ui/MoneyRequestReportActionsListRejectModalTest.tsx +++ b/tests/ui/MoneyRequestReportActionsListRejectModalTest.tsx @@ -39,8 +39,11 @@ const MOCK_UNIFIED_LAST_ITEM_INDEX = 37; const mockScrollToIndex = jest.fn(); const mockScrollToEnd = jest.fn(); const mockScrollToOffset = jest.fn(); +const mockNewestActionUnmount = jest.fn(); let mockIsNewestReportAction: boolean | undefined; let mockScrollToNewestAction: (() => void) | undefined; +let mockOnCommitLayoutEffect: (() => void) | undefined; +let mockShouldRenderNewestAction = true; jest.mock('@react-navigation/native', () => ({ ...jest.requireActual('@react-navigation/native'), @@ -97,6 +100,7 @@ jest.mock('@components/MoneyRequestReportView/MoneyRequestReportTransactionList' isLoadingInitialActions, listRef, onLastItemIndexChange, + onCommitLayoutEffect, visibleReportActions, renderReportAction, }: { @@ -108,17 +112,19 @@ jest.mock('@components/MoneyRequestReportView/MoneyRequestReportTransactionList' scrollToOffset: typeof mockScrollToOffset; }>; onLastItemIndexChange?: (index: number) => void; + onCommitLayoutEffect?: () => void; visibleReportActions: ReportAction[]; renderReportAction: (reportAction: ReportAction, index: number) => React.ReactElement; }) => { ReactActual.useImperativeHandle(listRef, () => ({scrollToIndex: mockScrollToIndex, scrollToEnd: mockScrollToEnd, scrollToOffset: mockScrollToOffset})); ReactActual.useLayoutEffect(() => onLastItemIndexChange?.(MOCK_UNIFIED_LAST_ITEM_INDEX), [onLastItemIndexChange]); + mockOnCommitLayoutEffect = onCommitLayoutEffect; const newestAction = visibleReportActions.at(-1); return ( {isLoadingInitialActions ? : null} - {newestAction ? renderReportAction(newestAction, visibleReportActions.length - 1) : null} + {newestAction && mockShouldRenderNewestAction ? renderReportAction(newestAction, visibleReportActions.length - 1) : null} {listFooterComponent} ); @@ -202,6 +208,12 @@ jest.mock('@pages/inbox/report/ReportActionsListItemRenderer', () => { jest.requireActual('@pages/inbox/report/ReportActionIndexContext'); return jest.fn(() => { + ReactActual.useEffect( + () => () => { + mockNewestActionUnmount(); + }, + [], + ); mockIsNewestReportAction = ReactActual.useContext(ReportActionIndexContextActual).isNewest; mockScrollToNewestAction = ReactActual.useContext(ReportActionScrollToNewestContextActual); return null; @@ -313,6 +325,8 @@ describe('MoneyRequestReportActionsList - Reject Educational Modal', () => { jest.clearAllMocks(); mockIsNewestReportAction = undefined; mockScrollToNewestAction = undefined; + mockOnCommitLayoutEffect = undefined; + mockShouldRenderNewestAction = true; jest.spyOn(NativeNavigation, 'useIsFocused').mockReturnValue(true); mockUseIsReportLoadPending.mockReturnValue(false); mockUseNetwork.mockReturnValue({isOffline: false}); @@ -322,7 +336,8 @@ describe('MoneyRequestReportActionsList - Reject Educational Modal', () => { }); }); - it('should use the unified list index when the newest action requests a bottom scroll', async () => { + it('keeps newest-action scroll requests until FlashList commits after the editor unmounts', async () => { + // Given a loaded money report with an editable newest action and an unchanged row height. await act(async () => { await Onyx.multiSet({ [`${ONYXKEYS.COLLECTION.REPORT}${FAKE_REPORT_ID}` as const]: mockReport, @@ -341,9 +356,34 @@ describe('MoneyRequestReportActionsList - Reject Educational Modal', () => { await waitForBatchedUpdatesWithAct(); expect(mockIsNewestReportAction).toBe(true); - act(() => mockScrollToNewestAction?.()); + mockScrollToIndex.mockClear(); + act(() => mockOnCommitLayoutEffect?.()); + expect(mockScrollToIndex).not.toHaveBeenCalled(); + + // When closing the editor queues a request and unmounts its consumer, without a size-change event. + const requestScroll = mockScrollToNewestAction; + mockShouldRenderNewestAction = false; + act(() => requestScroll?.()); + expect(mockNewestActionUnmount).toHaveBeenCalledTimes(1); + expect(mockScrollToIndex).not.toHaveBeenCalled(); + + // Then the list's layout commit consumes the request once and preserves its indexed landing. + const firstCommit = mockOnCommitLayoutEffect; + act(() => { + firstCommit?.(); + firstCommit?.(); + }); expect(mockScrollToIndex).toHaveBeenCalledWith({index: MOCK_UNIFIED_LAST_ITEM_INDEX, animated: false, viewPosition: 1}); + expect(mockScrollToIndex).toHaveBeenCalledTimes(1); expect(mockScrollToEnd).not.toHaveBeenCalled(); + + // A later edit needs its own layout commit; a repeated older callback cannot consume it. + mockShouldRenderNewestAction = true; + act(() => requestScroll?.()); + act(() => firstCommit?.()); + expect(mockScrollToIndex).toHaveBeenCalledTimes(1); + act(() => mockOnCommitLayoutEffect?.()); + expect(mockScrollToIndex).toHaveBeenCalledTimes(2); }); it('should show reject educational modal when reject option is selected and explanation has NOT been dismissed', async () => { diff --git a/tests/unit/hooks/useEditMessage.test.ts b/tests/unit/hooks/useEditMessage.test.ts index 01e2081db571..786323f255c7 100644 --- a/tests/unit/hooks/useEditMessage.test.ts +++ b/tests/unit/hooks/useEditMessage.test.ts @@ -90,20 +90,10 @@ function makeDebouncedValidator({flushResult}: {flushResult: boolean}): Debounce } describe('useEditMessage', () => { - let pendingAnimationFrame: FrameRequestCallback | undefined; - beforeAll(() => { Onyx.init({keys: ONYXKEYS}); }); - beforeEach(() => { - pendingAnimationFrame = undefined; - jest.spyOn(global, 'requestAnimationFrame').mockImplementation((callback) => { - pendingAnimationFrame = callback; - return 1; - }); - }); - afterEach(async () => { await act(async () => { await Onyx.clear(); @@ -112,10 +102,6 @@ describe('useEditMessage', () => { jest.restoreAllMocks(); }); - function finishActionLayout() { - act(() => pendingAnimationFrame?.(0)); - } - function renderUseEditMessage(overrides?: Partial) { const report = LHNTestUtils.getFakeReport(); const reportAction = LHNTestUtils.getFakeReportAction(); @@ -160,8 +146,10 @@ describe('useEditMessage', () => { }); it('scrolls to the bottom after deleting the newest message draft without a list-specific callback', () => { + // Given the newest message uses the list registered with the scroll manager. const {hook} = renderUseEditMessage({shouldScrollToLastMessage: true}); + // When deleting the draft closes its editor. act(() => { hook.result.current.publishDraft(' '); }); @@ -169,15 +157,16 @@ describe('useEditMessage', () => { mockShowDeleteModal.mock.calls.at(0)?.[3]?.(); }); - expect(mockScrollToBottom).not.toHaveBeenCalled(); - finishActionLayout(); + // Then the list receives the request immediately and owns its layout scheduling. expect(mockScrollToBottom).toHaveBeenCalledTimes(1); }); it('uses the list-specific scroll after deleting the newest message draft', () => { + // Given the list supplies its own edit-scroll request handler. const scrollToLastMessage = jest.fn(); const {hook} = renderUseEditMessage({shouldScrollToLastMessage: true, scrollToLastMessage}); + // When deleting the draft closes its editor. act(() => { hook.result.current.publishDraft(' '); }); @@ -185,23 +174,20 @@ describe('useEditMessage', () => { mockShowDeleteModal.mock.calls.at(0)?.[3]?.(); }); - expect(scrollToLastMessage).not.toHaveBeenCalled(); - finishActionLayout(); + // Then scheduling belongs to that list instead of a callback on the unmounting editor. expect(scrollToLastMessage).toHaveBeenCalledTimes(1); expect(mockScrollToBottom).not.toHaveBeenCalled(); }); - it('scrolls to the final saved action after the editor layout has been replaced', () => { + it('requests the newest action after saving without scheduling an editor frame', () => { // Given a draft on the newest action. const {hook} = renderUseEditMessage({shouldScrollToLastMessage: true}); - // When the edit is submitted, its optimistic update is queued before layout settles. + // When the edit is submitted and the editor closes. act(() => hook.result.current.publishDraft('Updated message')); expect(mockEditReportComment).toHaveBeenCalledTimes(1); - expect(mockScrollToBottom).not.toHaveBeenCalled(); - // Then scrolling runs after the restored action row gets a layout frame. - finishActionLayout(); + // Then the list already owns the scroll request before the editor can unmount. expect(mockScrollToBottom).toHaveBeenCalledTimes(1); }); From 8dc16d8205dfe912124c8e3f5d552b1ba2afc255 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 22 Oct 2026 14:22:09 +0100 Subject: [PATCH 61/61] docs: link upstream LegendList and Android anchoring fixes Co-authored-by: Codex --- patches/@legendapp/list/details.md | 6 +++++- patches/react-native/details.md | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/patches/@legendapp/list/details.md b/patches/@legendapp/list/details.md index 299029c6711f..262148fc96ce 100644 --- a/patches/@legendapp/list/details.md +++ b/patches/@legendapp/list/details.md @@ -13,7 +13,11 @@ - Reason: On Android, a chat that opens at its newest messages can briefly show older messages before it snaps back to the end. When rows above the viewport get their real, smaller heights, the content shrinks. Android clamps the scroll offset to the new end, and LegendList's scroll-preservation anchor then moves the offset by the full shrink again. The list stays above the end by that amount until the next scroll to the end. Account for Android's clamp and only move the anchor by what the clamp doesn't cover. - Scope: Only item-size scroll preservation on Android, and only when the content shrinks below the current offset. Data changes and active scroll targets already handle the clamp and are unchanged. iOS and web are unchanged. -- Upstream PR/issue: Not submitted yet. https://github.com/LegendApp/legend-list/pull/481 handles the same clamp for lists with a bottom inset. +- Upstream PR/issue: https://github.com/LegendApp/legend-list/pull/582 upstreams this patch. https://github.com/LegendApp/legend-list/pull/481 covers the related bottom-inset case. Keep this patch until the installed LegendList release includes this fix. - E/App issue: https://github.com/Expensify/App/pull/100733#issuecomment-6287555197 - PR introducing patch: https://github.com/Expensify/App/pull/100733 - Reproduction: In a DM, mark the third-newest message as unread and go back to the inbox. Open the DM again. It should open at the newest messages without a frame showing older messages. Also scroll through older history and send a new message to check scroll preservation and end following. + +### Related upstream work + +- https://github.com/LegendApp/legend-list/pull/583 refreshes the cached end-follow state when `maintainScrollAtEndThreshold` changes. This supports a fixed pixel follow distance across viewport changes when using LegendList's built-in end following. diff --git a/patches/react-native/details.md b/patches/react-native/details.md index 57efdef9932e..16582fef9e62 100644 --- a/patches/react-native/details.md +++ b/patches/react-native/details.md @@ -14,7 +14,10 @@ 2. Improves first visible view selection logic to handle Fabric's z-index-based view reordering by finding the view with the smallest position that's still greater than the scroll position 3. Preserves a positioned, zero-sized first child as a scroll anchor. LegendList moves this anchor to compensate for item measurements. Selecting its surrounding container or rejecting its empty frame prevents native scroll compensation and makes the chat jump as estimated rows shrink. LegendList 3.6 places the anchor at its adjustment offset plus 10,000,000 pixels, beyond the viewport. The strict visibility check also prevents a fixed, zero-sized spacer at or above the scroll offset in another scroll view from taking over its visible-content anchor. - Scope: This changes Android `MaintainVisibleContentPosition` for every React Native scroll view that enables it, not only the report-action list. Keep this in mind when updating React Native or investigating anchoring in other scroll views. -- Upstream PR/issue: https://github.com/facebook/react-native/pull/46247 (closed without merging; the zero-sized LegendList anchor fix is not covered there) +- Upstream PR/issue: https://github.com/react/react-native/pull/57959 covers z-index anchor selection. The follow-up guard preserving LegendList's positioned, zero-sized anchor is proposed in https://github.com/react/react-native/pull/57959#issuecomment-6341492122. +- Reproducer: https://github.com/react/react-native/pull/59360 adds the RNTester reproduction, without a runtime fix. The standalone reproduction and proposed anchor patch are in https://github.com/margelo/react-native-mvcp-reproducer. +- Earlier proposal: https://github.com/react/react-native/pull/46247 closed without merging. +- Removal: Drop this patch only when the adopted React Native release preserves all three anchoring behaviors described above. - E/App issue: 🛑 - PR Introducing Patch: https://github.com/Expensify/App/pull/46315 (introduced), https://github.com/Expensify/App/pull/45289 (refactored)