@@ -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,14 +209,28 @@ 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+ // Insert the synthetic draft into the already-descending render list without treating it as a persisted report action.
217+ for ( const [ index , action ] of sortedVisibleReportActions . entries ( ) ) {
218+ if ( action . reportActionID === draftReportAction . reportActionID ) {
219+ return sortedVisibleReportActions ;
220+ }
221+ if ( isNewerReportAction ( draftReportAction , action ) ) {
222+ const visibleReportActionsWithDraft = [ ...sortedVisibleReportActions ] ;
223+ visibleReportActionsWithDraft . splice ( index , 0 , draftReportAction ) ;
224+ return visibleReportActionsWithDraft ;
225+ }
226+ }
227+
228+ const visibleReportActionsWithDraft = [ ...sortedVisibleReportActions ] ;
229+ visibleReportActionsWithDraft . push ( draftReportAction ) ;
230+ return visibleReportActionsWithDraft ;
217231 } , [ draftReportAction , sortedVisibleReportActions ] ) ;
218232 const draftMessageHTML = draftReportAction ? getReportActionMessage ( draftReportAction ) ?. html : undefined ;
219- const isSyntheticDraftVisible = ! ! draftReportAction && ! sortedVisibleReportActions . some ( ( action ) => action . reportActionID === draftReportAction . reportActionID ) ;
233+ const isSyntheticDraftVisible = ! ! draftReportAction && renderedVisibleReportActions !== sortedVisibleReportActions ;
220234 const draftAutoScrollKey = isSyntheticDraftVisible ? `${ draftReportAction . reportActionID } :${ draftMessageHTML ?? '' } ` : '' ;
221235 const previousDraftAutoScrollKey = usePrevious ( draftAutoScrollKey ) ;
222236 const topReportAction = renderedVisibleReportActions . at ( - 1 ) ;
@@ -276,12 +290,12 @@ function ReportActionsList({
276290 } , [ reportLastReadTime ] ) ;
277291
278292 useEffect ( ( ) => {
279- if ( ! draftReportAction || ! sortedVisibleReportActions . some ( ( action ) => action . reportActionID === draftReportAction . reportActionID ) ) {
293+ if ( ! draftReportAction || isSyntheticDraftVisible ) {
280294 return ;
281295 }
282296
283297 clearDraft ( ) ;
284- } , [ clearDraft , draftReportAction , sortedVisibleReportActions ] ) ;
298+ } , [ clearDraft , draftReportAction , isSyntheticDraftVisible ] ) ;
285299
286300 const prevUnreadMarkerReportActionID = useRef < string | null > ( null ) ;
287301
@@ -721,7 +735,7 @@ function ReportActionsList({
721735 return isExpenseReport ( report ) || isIOUReport ( report ) || isInvoiceReport ( report ) ;
722736 } , [ parentReportAction , renderedVisibleReportActions , report ] ) ;
723737
724- // Precompute a reportActionID → index map so renderItem can resolve the real index in O(1)
738+ // Precompute a reportActionID -> index map so renderItem can resolve the real index in O(1)
725739 // instead of scanning renderedVisibleReportActions with indexOf on every render.
726740 const actionIndexMap = useMemo ( ( ) => {
727741 const map = new Map < string , number > ( ) ;
0 commit comments