11import { useRoute } from '@react-navigation/native' ;
2- import React , { useMemo } from 'react' ;
2+ import React , { useCallback , useMemo } from 'react' ;
3+ import type { OnyxCollection } from 'react-native-onyx' ;
34import ColumnsSettingsList from '@components/ColumnsSettingsList' ;
45import type { SearchCustomColumnIds } from '@components/Search/types' ;
56import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails' ;
@@ -14,6 +15,7 @@ import type {ReportSettingsNavigatorParamList} from '@navigation/types';
1415import CONST from '@src/CONST' ;
1516import ONYXKEYS from '@src/ONYXKEYS' ;
1617import type SCREENS from '@src/SCREENS' ;
18+ import type { Transaction } from '@src/types/onyx' ;
1719import arraysEqual from '@src/utils/arraysEqual' ;
1820
1921/**
@@ -36,16 +38,26 @@ function ReportDetailsColumnsPage() {
3638 const [ reportDetailsColumns ] = useOnyx ( ONYXKEYS . NVP_REPORT_DETAILS_COLUMNS ) ;
3739 const [ report ] = useOnyx ( `${ ONYXKEYS . COLLECTION . REPORT } ${ reportID } ` ) ;
3840 const [ policy ] = useOnyx ( `${ ONYXKEYS . COLLECTION . POLICY } ${ report ?. policyID } ` ) ;
39- const [ allTransactions ] = useOnyx ( ONYXKEYS . COLLECTION . TRANSACTION ) ;
40- const reportTransactions = Object . values ( allTransactions ?? { } ) . filter ( ( t ) : t is NonNullable < typeof t > => t != null && t . reportID === reportID ) ;
41+ // Selector keeps re-renders scoped to this report's transactions. We intentionally return undefined
42+ // while the collection is loading so the caller can distinguish "loading" from "no transactions".
43+ const reportTransactionsSelector = useCallback (
44+ ( transactions : OnyxCollection < Transaction > ) : Transaction [ ] | undefined => {
45+ if ( ! transactions ) {
46+ return undefined ;
47+ }
48+ return Object . values ( transactions ) . filter ( ( transaction ) : transaction is Transaction => ! ! transaction && transaction . reportID === reportID ) ;
49+ } ,
50+ [ reportID ] ,
51+ ) ;
52+ const [ reportTransactions ] = useOnyx ( ONYXKEYS . COLLECTION . TRANSACTION , { selector : reportTransactionsSelector } , [ reportTransactionsSelector ] ) ;
4153 const currentUserDetails = useCurrentUserPersonalDetails ( ) ;
4254
4355 const allTypeCustomColumns = Object . values ( CONST . SEARCH . REPORT_DETAILS_CUSTOM_COLUMNS ) as SearchCustomColumnIds [ ] ;
4456
4557 // Wait for transactions to load before rendering. ColumnsSettingsList snapshots
4658 // currentColumns in useState on mount and does not sync prop updates, so we must
4759 // pass the final value on first render.
48- const isLoading = ! allTransactions ;
60+ const isLoading = ! reportTransactions ;
4961
5062 // When no custom columns are saved, compute which columns getColumnsToShow would
5163 // return for this report so data-driven columns (e.g. Exchange rate, Original amount,
@@ -56,7 +68,7 @@ function ReportDetailsColumnsPage() {
5668 return savedColumns ;
5769 }
5870
59- if ( ! reportTransactions . length ) {
71+ if ( ! reportTransactions ? .length ) {
6072 return REPORT_DETAILS_DEFAULT_COLUMNS ;
6173 }
6274
0 commit comments