@@ -31,7 +31,7 @@ import useViewportOffsetTop from '@hooks/useViewportOffsetTop';
3131import { hideEmojiPicker } from '@libs/actions/EmojiPickerAction' ;
3232import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID' ;
3333import Log from '@libs/Log' ;
34- import { selectAllTransactionsForReport , shouldDisplayReportTableView } from '@libs/MoneyRequestReportUtils' ;
34+ import { selectAllTransactionsForReport , shouldDisplayReportTableView , shouldWaitForTransactions as shouldWaitForTransactionsUtil } from '@libs/MoneyRequestReportUtils' ;
3535import Navigation , { navigationRef } from '@libs/Navigation/Navigation' ;
3636import type { PlatformStackScreenProps } from '@libs/Navigation/PlatformStackNavigation/types' ;
3737import clearReportNotifications from '@libs/Notification/clearReportNotifications' ;
@@ -98,6 +98,7 @@ type ReportScreenNavigationProps = PlatformStackScreenProps<ReportsSplitNavigato
9898type ReportScreenProps = ReportScreenNavigationProps ;
9999
100100const defaultReportMetadata = {
101+ hasOnceLoadedReportActions : false ,
101102 isLoadingInitialReportActions : true ,
102103 isLoadingOlderReportActions : false ,
103104 hasLoadingOlderReportActionsError : false ,
@@ -152,7 +153,7 @@ function ReportScreen({route, navigation}: ReportScreenProps) {
152153 const [ userLeavingStatus ] = useOnyx ( `${ ONYXKEYS . COLLECTION . REPORT_USER_IS_LEAVING_ROOM } ${ reportIDFromRoute } ` , { initialValue : false , canBeMissing : true } ) ;
153154 const [ reportOnyx ] = useOnyx ( `${ ONYXKEYS . COLLECTION . REPORT } ${ reportIDFromRoute } ` , { allowStaleData : true , canBeMissing : true } ) ;
154155 const [ reportNameValuePairsOnyx ] = useOnyx ( `${ ONYXKEYS . COLLECTION . REPORT_NAME_VALUE_PAIRS } ${ reportIDFromRoute } ` , { allowStaleData : true , canBeMissing : true } ) ;
155- const [ reportMetadata = defaultReportMetadata ] = useOnyx ( `${ ONYXKEYS . COLLECTION . REPORT_METADATA } ${ reportIDFromRoute } ` , { canBeMissing : true } ) ;
156+ const [ reportMetadata = defaultReportMetadata ] = useOnyx ( `${ ONYXKEYS . COLLECTION . REPORT_METADATA } ${ reportIDFromRoute } ` , { canBeMissing : false , allowStaleData : true } ) ;
156157 const [ policies ] = useOnyx ( ONYXKEYS . COLLECTION . POLICY , { allowStaleData : true , initialValue : { } , canBeMissing : false } ) ;
157158 const [ parentReportAction ] = useOnyx ( `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ getNonEmptyStringOnyxID ( reportOnyx ?. parentReportID ) } ` , {
158159 canEvict : false ,
@@ -293,7 +294,7 @@ function ReportScreen({route, navigation}: ReportScreenProps) {
293294 // OpenReport will be called each time the user scrolls up the report a bit, clicks on report preview, and then goes back.
294295 const isLinkedMessagePageReady = isLinkedMessageAvailable && ( reportActions . length - indexOfLinkedMessage >= CONST . REPORT . MIN_INITIAL_REPORT_ACTION_COUNT || doesCreatedActionExists ( ) ) ;
295296
296- const [ reportTransactions = [ ] ] = useOnyx ( ONYXKEYS . COLLECTION . TRANSACTION , {
297+ const [ reportTransactions ] = useOnyx ( ONYXKEYS . COLLECTION . TRANSACTION , {
297298 selector : ( allTransactions ) : OnyxTypes . Transaction [ ] => selectAllTransactionsForReport ( allTransactions , reportIDFromRoute , reportActions ) ,
298299 canBeMissing : false ,
299300 } ) ;
@@ -307,11 +308,14 @@ function ReportScreen({route, navigation}: ReportScreenProps) {
307308 const didSubscribeToReportLeavingEvents = useRef ( false ) ;
308309 const isTransactionThreadView = isReportTransactionThread ( report ) ;
309310 const isMoneyRequestOrInvoiceReport = isMoneyRequestReport ( report ) || isInvoiceReport ( report ) ;
311+ // Prevent the empty state flash by ensuring transaction data is fully loaded before deciding which view to render
312+ // We need to wait for both the selector to finish AND ensure we're not in a loading state where transactions could still populate
313+ const shouldWaitForTransactions = shouldWaitForTransactionsUtil ( report , reportTransactions , reportMetadata ) ;
310314
311315 const prevTransactions = usePrevious ( reportTransactions ) ;
312316
313317 const newTransactions = useMemo ( ( ) => {
314- if ( ! prevTransactions || reportTransactions . length <= prevTransactions . length ) {
318+ if ( ! reportTransactions || ! prevTransactions || reportTransactions . length <= prevTransactions . length ) {
315319 return CONST . EMPTY_ARRAY as unknown as OnyxTypes . Transaction [ ] ;
316320 }
317321 return reportTransactions . filter ( ( transaction ) => ! prevTransactions ?. some ( ( prevTransaction ) => prevTransaction . transactionID === transaction . transactionID ) ) ;
@@ -765,7 +769,7 @@ function ReportScreen({route, navigation}: ReportScreenProps) {
765769 }
766770
767771 // If true reports that are considered MoneyRequest | InvoiceReport will get the new report table view
768- const shouldDisplayMoneyRequestActionsList = isMoneyRequestOrInvoiceReport && shouldDisplayReportTableView ( report , reportTransactions ) ;
772+ const shouldDisplayMoneyRequestActionsList = isMoneyRequestOrInvoiceReport && shouldDisplayReportTableView ( report , reportTransactions ?? [ ] ) ;
769773
770774 return (
771775 < ActionListContext . Provider value = { actionListValue } >
@@ -812,8 +816,8 @@ function ReportScreen({route, navigation}: ReportScreenProps) {
812816 style = { [ styles . flex1 , styles . justifyContentEnd , styles . overflowHidden ] }
813817 testID = "report-actions-view-wrapper"
814818 >
815- { ! report && < ReportActionsSkeletonView /> }
816- { ! ! report && ! shouldDisplayMoneyRequestActionsList ? (
819+ { ( ! report || shouldWaitForTransactions ) && < ReportActionsSkeletonView /> }
820+ { ! ! report && ! shouldDisplayMoneyRequestActionsList && ! shouldWaitForTransactions ? (
817821 < ReportActionsView
818822 report = { report }
819823 reportActions = { reportActions }
@@ -824,7 +828,7 @@ function ReportScreen({route, navigation}: ReportScreenProps) {
824828 transactionThreadReportID = { transactionThreadReportID }
825829 />
826830 ) : null }
827- { ! ! report && shouldDisplayMoneyRequestActionsList ? (
831+ { ! ! report && shouldDisplayMoneyRequestActionsList && ! shouldWaitForTransactions ? (
828832 < MoneyRequestReportActionsList
829833 report = { report }
830834 policy = { policy }
@@ -833,7 +837,7 @@ function ReportScreen({route, navigation}: ReportScreenProps) {
833837 newTransactions = { newTransactions }
834838 hasOlderActions = { hasOlderActions }
835839 hasNewerActions = { hasNewerActions }
836- isLoadingInitialReportActions = { reportMetadata ?. isLoadingInitialReportActions }
840+ showReportActionsLoadingState = { reportMetadata ?. isLoadingInitialReportActions && ! reportMetadata ?. hasOnceLoadedReportActions }
837841 />
838842 ) : null }
839843 { isCurrentReportLoadedFromOnyx ? (
0 commit comments