@@ -36,10 +36,10 @@ import type {PlatformStackRouteProp} from '@libs/Navigation/PlatformStackNavigat
3636import {
3737 getFirstVisibleReportActionID ,
3838 getReportActionMessage ,
39- getSortedReportActions ,
4039 isConsecutiveActionMadeByPreviousActor ,
4140 isCurrentActionUnread ,
4241 isDeletedParentAction ,
42+ isNewerReportAction ,
4343 isReportPreviewAction ,
4444 isReversedTransaction ,
4545 isSentMoneyReportAction ,
@@ -209,11 +209,24 @@ function ReportActionsList({
209209 const isMoneyRequestOrInvoiceReport = useMemo ( ( ) => isMoneyRequestReport ( report ) || isInvoiceReport ( report ) , [ report ] ) ;
210210 const shouldFocusToTopOnMount = useMemo ( ( ) => isTransactionThreadReport || isMoneyRequestOrInvoiceReport , [ isMoneyRequestOrInvoiceReport , isTransactionThreadReport ] ) ;
211211 const renderedVisibleReportActions = useMemo ( ( ) => {
212- if ( ! draftReportAction || sortedVisibleReportActions . some ( ( action ) => action . reportActionID === draftReportAction . reportActionID ) ) {
212+ if ( ! draftReportAction ) {
213213 return sortedVisibleReportActions ;
214214 }
215215
216- return getSortedReportActions ( [ ...sortedVisibleReportActions , draftReportAction ] , true ) ;
216+ let insertIndex = sortedVisibleReportActions . length ;
217+ // Insert the synthetic draft into the already-descending render list without treating it as a persisted report action.
218+ for ( const [ index , action ] of sortedVisibleReportActions . entries ( ) ) {
219+ if ( action . reportActionID === draftReportAction . reportActionID ) {
220+ return sortedVisibleReportActions ;
221+ }
222+ if ( insertIndex === sortedVisibleReportActions . length && isNewerReportAction ( draftReportAction , action ) ) {
223+ insertIndex = index ;
224+ }
225+ }
226+
227+ const visibleReportActionsWithDraft = [ ...sortedVisibleReportActions ] ;
228+ visibleReportActionsWithDraft . splice ( insertIndex , 0 , draftReportAction ) ;
229+ return visibleReportActionsWithDraft ;
217230 } , [ draftReportAction , sortedVisibleReportActions ] ) ;
218231 const draftMessageHTML = draftReportAction ? getReportActionMessage ( draftReportAction ) ?. html : undefined ;
219232 const isSyntheticDraftVisible = ! ! draftReportAction && renderedVisibleReportActions !== sortedVisibleReportActions ;
0 commit comments