diff --git a/src/pages/inbox/report/ReportActionsList.tsx b/src/pages/inbox/report/ReportActionsList.tsx index 4c6d19e4fd3e..923623ee0641 100644 --- a/src/pages/inbox/report/ReportActionsList.tsx +++ b/src/pages/inbox/report/ReportActionsList.tsx @@ -695,11 +695,26 @@ function ReportActionsList({ return isExpenseReport(report) || isIOUReport(report) || isInvoiceReport(report); }, [parentReportAction, report, sortedVisibleReportActions]); + // Precompute a reportActionID → index map so renderItem can resolve the real index in O(1) + // instead of scanning sortedVisibleReportActions with indexOf on every render. + const actionIndexMap = useMemo(() => { + const map = new Map(); + for (const [i, action] of sortedVisibleReportActions.entries()) { + map.set(action.reportActionID, i); + } + return map; + }, [sortedVisibleReportActions]); + const renderItem = useCallback( ({item: reportAction, index}: ListRenderItemInfo) => { const originalReportID = getOriginalReportID(report.reportID, reportAction, reportActionsFromOnyx); const showPreviousMessagesButton = reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED && !!isConciergeSidePanel && !!showHiddenHistory && !!hasPreviousMessages; + // Use the action's actual index in sortedVisibleReportActions rather than the FlashList-provided index, + // because useFlashListScrollKey may slice the data for deep-link scroll positioning, making the + // FlashList index offset from the full array and causing wrong displayAsGroup computation. + const safeIndex = actionIndexMap.get(reportAction.reportActionID) ?? index; + return ( <> {renderItem({ item: action, - index: sortedVisibleReportActions.indexOf(action), + index: actionIndexMap.get(action.reportActionID) ?? 0, } as ListRenderItemInfo)} ))} ); - }, [hideComposer, initialNumToRender, renderItem, shouldShowReportRecipientLocalTime, sortedVisibleReportActions, styles]); + }, [actionIndexMap, hideComposer, initialNumToRender, renderItem, shouldShowReportRecipientLocalTime, sortedVisibleReportActions, styles]); const onStartReached = useCallback(() => { if (!isSearchTopmostFullScreenRoute()) {