|
1 | | -import React, {useEffect, useMemo, useRef, useState} from 'react'; |
| 1 | +import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react'; |
2 | 2 | import {InteractionManager} from 'react-native'; |
3 | 3 | import type {OnyxCollection} from 'react-native-onyx'; |
4 | 4 | import EmptyStateComponent from '@components/EmptyStateComponent'; |
@@ -56,32 +56,39 @@ function AddUnreportedExpense({route}: AddUnreportedExpensePageType) { |
56 | 56 | const session = useSession(); |
57 | 57 | const shouldShowUnreportedTransactionsSkeletons = isLoadingUnreportedTransactions && hasMoreUnreportedTransactionsResults && !isOffline; |
58 | 58 |
|
59 | | - function getUnreportedTransactions(transactions: OnyxCollection<Transaction>) { |
60 | | - if (!transactions) { |
61 | | - return []; |
62 | | - } |
63 | | - return Object.values(transactions || {}).filter((item) => { |
64 | | - const isUnreported = item?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID || item?.reportID === ''; |
65 | | - if (!isUnreported) { |
66 | | - return false; |
67 | | - } |
68 | | - |
69 | | - if (isPerDiemRequest(item)) { |
70 | | - // Only show per diem expenses if the target workspace has per diem enabled and the per diem expense was created in the same workspace |
71 | | - const workspacePerDiemUnit = getPerDiemCustomUnit(policy); |
72 | | - const perDiemCustomUnitID = item?.comment?.customUnit?.customUnitID; |
73 | | - |
74 | | - return canSubmitPerDiemExpenseFromWorkspace(policy) && (!perDiemCustomUnitID || perDiemCustomUnitID === workspacePerDiemUnit?.customUnitID); |
| 59 | + const getUnreportedTransactions = useCallback( |
| 60 | + (transactions: OnyxCollection<Transaction>) => { |
| 61 | + if (!transactions) { |
| 62 | + return []; |
75 | 63 | } |
| 64 | + return Object.values(transactions || {}).filter((item) => { |
| 65 | + const isUnreported = item?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID || item?.reportID === ''; |
| 66 | + if (!isUnreported) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + if (isPerDiemRequest(item)) { |
| 71 | + // Only show per diem expenses if the target workspace has per diem enabled and the per diem expense was created in the same workspace |
| 72 | + const workspacePerDiemUnit = getPerDiemCustomUnit(policy); |
| 73 | + const perDiemCustomUnitID = item?.comment?.customUnit?.customUnitID; |
| 74 | + |
| 75 | + return canSubmitPerDiemExpenseFromWorkspace(policy) && (!perDiemCustomUnitID || perDiemCustomUnitID === workspacePerDiemUnit?.customUnitID); |
| 76 | + } |
| 77 | + |
| 78 | + return true; |
| 79 | + }); |
| 80 | + }, |
| 81 | + [policy], |
| 82 | + ); |
76 | 83 |
|
77 | | - return true; |
78 | | - }); |
79 | | - } |
80 | | - |
81 | | - const [transactions = getEmptyArray<Transaction>()] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION, { |
82 | | - selector: (_transactions) => getUnreportedTransactions(_transactions), |
83 | | - canBeMissing: true, |
84 | | - }); |
| 84 | + const [transactions = getEmptyArray<Transaction>()] = useOnyx( |
| 85 | + ONYXKEYS.COLLECTION.TRANSACTION, |
| 86 | + { |
| 87 | + selector: getUnreportedTransactions, |
| 88 | + canBeMissing: true, |
| 89 | + }, |
| 90 | + [getUnreportedTransactions], |
| 91 | + ); |
85 | 92 |
|
86 | 93 | const fetchMoreUnreportedTransactions = () => { |
87 | 94 | if (!hasMoreUnreportedTransactionsResults || isLoadingUnreportedTransactions) { |
|
0 commit comments