@@ -38,11 +38,12 @@ const reportSelector = (report: OnyxEntry<Report>): OnyxEntry<Report> =>
3838
3939type Props = {
4040 backTo : Route | undefined ;
41- transactionsReports : Report [ ] ;
41+ selectedReportID : string | undefined ;
42+ selectedPolicyID ?: string | undefined ;
4243 selectReport : ( item : ReportListItem ) => void ;
4344} ;
4445
45- function IOURequestEditReportCommon ( { backTo, transactionsReports , selectReport} : Props ) {
46+ function IOURequestEditReportCommon ( { backTo, selectedReportID , selectedPolicyID , selectReport} : Props ) {
4647 const { translate} = useLocalize ( ) ;
4748 const [ allReports ] = useOnyx ( ONYXKEYS . COLLECTION . REPORT , { selector : ( reports ) => mapOnyxCollectionItems ( reports , reportSelector ) , canBeMissing : true } ) ;
4849 const [ reportNameValuePairs ] = useOnyx ( ONYXKEYS . COLLECTION . REPORT_NAME_VALUE_PAIRS , { canBeMissing : true } ) ;
@@ -51,30 +52,28 @@ function IOURequestEditReportCommon({backTo, transactionsReports, selectReport}:
5152 const currentUserPersonalDetails = useCurrentUserPersonalDetails ( ) ;
5253 const [ searchValue , debouncedSearchValue , setSearchValue ] = useDebouncedState ( '' ) ;
5354
54- const expenseReports = useMemo (
55- ( ) =>
56- Object . values ( allPoliciesID ?? { } ) . flatMap ( ( policyID ) => {
57- if ( ! policyID ) {
58- return [ ] ;
59- }
60- const reports = getOutstandingReportsForUser (
61- policyID ,
62- transactionsReports . at ( 0 ) ?. ownerAccountID ?? currentUserPersonalDetails . accountID ,
63- allReports ?? { } ,
64- reportNameValuePairs ,
65- ) ;
66- return reports ;
67- } ) ,
68- [ allReports , currentUserPersonalDetails . accountID , transactionsReports , allPoliciesID , reportNameValuePairs ] ,
69- ) ;
55+ const selectedReport = useMemo ( ( ) => {
56+ if ( ! selectedReportID ) {
57+ return undefined ;
58+ }
59+ return allReports ?. [ `${ ONYXKEYS . COLLECTION . REPORT } ${ selectedReportID } ` ] ;
60+ } , [ allReports , selectedReportID ] ) ;
61+
62+ const expenseReports = useMemo ( ( ) => {
63+ if ( ! selectedReportID ) {
64+ return selectedPolicyID
65+ ? getOutstandingReportsForUser ( selectedPolicyID , currentUserPersonalDetails . accountID , allReports ?? { } , reportNameValuePairs )
66+ : Object . values ( allPoliciesID ?? { } ) . flatMap ( ( policyID ) =>
67+ getOutstandingReportsForUser ( policyID , currentUserPersonalDetails . accountID , allReports ?? { } , reportNameValuePairs ) ,
68+ ) ;
69+ }
70+ return getOutstandingReportsForUser ( selectedReport ?. policyID , currentUserPersonalDetails . accountID , allReports ?? { } , reportNameValuePairs ) ;
71+ } , [ allReports , currentUserPersonalDetails . accountID , selectedReport , reportNameValuePairs , allPoliciesID , selectedReportID , selectedPolicyID ] ) ;
7072
7173 const reportOptions : ReportListItem [ ] = useMemo ( ( ) => {
7274 if ( ! allReports ) {
7375 return [ ] ;
7476 }
75-
76- const onlyReport = transactionsReports . length === 1 ? transactionsReports . at ( 0 ) : undefined ;
77-
7877 return expenseReports
7978 . sort ( ( a , b ) => a ?. reportName ?. localeCompare ( b ?. reportName ?. toLowerCase ( ) ?? '' ) ?? 0 )
8079 . filter ( ( report ) => ! debouncedSearchValue || report ?. reportName ?. toLowerCase ( ) . includes ( debouncedSearchValue . toLowerCase ( ) ) )
@@ -83,9 +82,9 @@ function IOURequestEditReportCommon({backTo, transactionsReports, selectReport}:
8382 text : report . reportName ,
8483 value : report . reportID ,
8584 keyForList : report . reportID ,
86- isSelected : onlyReport && report . reportID === onlyReport ?. reportID ,
85+ isSelected : report . reportID === selectedReportID ,
8786 } ) ) ;
88- } , [ allReports , debouncedSearchValue , expenseReports , transactionsReports ] ) ;
87+ } , [ allReports , debouncedSearchValue , expenseReports , selectedReportID ] ) ;
8988
9089 const navigateBack = ( ) => {
9190 Navigation . goBack ( backTo ) ;
@@ -110,7 +109,7 @@ function IOURequestEditReportCommon({backTo, transactionsReports, selectReport}:
110109 textInputLabel = { expenseReports . length >= CONST . STANDARD_LIST_ITEM_LIMIT ? translate ( 'common.search' ) : undefined }
111110 shouldSingleExecuteRowSelect
112111 headerMessage = { headerMessage }
113- initiallyFocusedOptionKey = { transactionsReports . length === 1 ? transactionsReports . at ( 0 ) ?. reportID : undefined }
112+ initiallyFocusedOptionKey = { selectedReportID }
114113 ListItem = { UserListItem }
115114 />
116115 </ StepScreenWrapper >
0 commit comments