Skip to content

Commit 3e4193a

Browse files
authored
Merge pull request Expensify#71269 from mkzie2/revert-71180-revert-70814-mkzie2-issue/70400
Revert "Revert "remove inline selector for transaction key""
2 parents 174e69f + 4bd44c0 commit 3e4193a

15 files changed

Lines changed: 119 additions & 121 deletions

src/components/ReportActionItem/TransactionPreview/index.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type {TransactionDuplicateNavigatorParamList} from '@navigation/types';
2121
import {clearWalletTermsError} from '@userActions/PaymentMethods';
2222
import {clearIOUError} from '@userActions/Report';
2323
import CONST from '@src/CONST';
24+
import useTransactionsByID from '@src/hooks/useTransactionsByID';
2425
import ONYXKEYS from '@src/ONYXKEYS';
2526
import SCREENS from '@src/SCREENS';
2627
import {isEmptyObject} from '@src/types/utils/EmptyObject';
@@ -57,14 +58,7 @@ function TransactionPreview(props: TransactionPreviewProps) {
5758

5859
// Get transaction violations for given transaction id from onyx, find duplicated transactions violations and get duplicates
5960
const allDuplicateIDs = useMemo(() => violations?.find((violation) => violation.name === CONST.VIOLATIONS.DUPLICATED_TRANSACTION)?.data?.duplicates ?? [], [violations]);
60-
const [allDuplicates] = useOnyx(
61-
ONYXKEYS.COLLECTION.TRANSACTION,
62-
{
63-
selector: (allTransactions) => allDuplicateIDs.map((id) => allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${id}`]),
64-
canBeMissing: true,
65-
},
66-
[allDuplicateIDs],
67-
);
61+
const [allDuplicates] = useTransactionsByID(allDuplicateIDs);
6862
const duplicates = useMemo(() => removeSettledAndApprovedTransactions(allDuplicates ?? []), [allDuplicates]);
6963
const sessionAccountID = session?.accountID;
7064
const areThereDuplicates = allDuplicateIDs.length > 0 && duplicates.length > 0 && allDuplicateIDs.length === duplicates.length;

src/hooks/useReportTransactions.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {useCallback} from 'react';
2+
import type {OnyxCollection} from 'react-native-onyx';
13
import ONYXKEYS from '@src/ONYXKEYS';
24
import type {Transaction} from '@src/types/onyx';
35
import getEmptyArray from '@src/types/utils/getEmptyArray';
@@ -7,16 +9,25 @@ import useOnyx from './useOnyx';
79
* Hook to get all transactions for a specific report
810
*/
911
function useReportTransactions(reportID: string | undefined): Transaction[] {
10-
const [reportTransactions = getEmptyArray<Transaction>()] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION, {
11-
selector: (transactions) => {
12+
const reportTransactionsSelector = useCallback(
13+
(transactions: OnyxCollection<Transaction>) => {
1214
if (!transactions || !reportID) {
1315
return [];
1416
}
1517

1618
return Object.values(transactions).filter((transaction): transaction is Transaction => !!transaction && transaction.reportID === reportID);
1719
},
18-
canBeMissing: true,
19-
});
20+
[reportID],
21+
);
22+
23+
const [reportTransactions = getEmptyArray<Transaction>()] = useOnyx(
24+
ONYXKEYS.COLLECTION.TRANSACTION,
25+
{
26+
selector: reportTransactionsSelector,
27+
canBeMissing: true,
28+
},
29+
[reportTransactionsSelector],
30+
);
2031

2132
return reportTransactions;
2233
}

src/hooks/useTransactionsByID.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {useCallback} from 'react';
2+
import type {OnyxCollection} from 'react-native-onyx';
3+
import ONYXKEYS from '@src/ONYXKEYS';
4+
import type {Transaction} from '@src/types/onyx';
5+
import useOnyx from './useOnyx';
6+
7+
function useTransactionsByID(transactionIDs: string[] | undefined) {
8+
const transactionsSelector = useCallback(
9+
(transactions: OnyxCollection<Transaction>) => transactionIDs?.map((id) => transactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${id}`]) ?? [],
10+
[transactionIDs],
11+
);
12+
13+
const [transactions] = useOnyx(
14+
ONYXKEYS.COLLECTION.TRANSACTION,
15+
{
16+
selector: transactionsSelector,
17+
canBeMissing: true,
18+
},
19+
[transactionsSelector],
20+
);
21+
22+
return [transactions];
23+
}
24+
25+
export default useTransactionsByID;

src/hooks/useTripTransactions.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {useCallback} from 'react';
2+
import type {OnyxCollection} from 'react-native-onyx';
13
import ONYXKEYS from '@src/ONYXKEYS';
24
import type {Transaction} from '@src/types/onyx';
35
import getEmptyArray from '@src/types/utils/getEmptyArray';
@@ -19,19 +21,27 @@ function useTripTransactions(reportID: string | undefined): Transaction[] {
1921
Object.values(reports ?? {})
2022
.filter((report) => report && report.chatReportID === reportID)
2123
.map((report) => report?.reportID),
24+
canBeMissing: true,
2225
});
26+
27+
const tripTransactionsSelector = useCallback(
28+
(transactions: OnyxCollection<Transaction>) => {
29+
if (!tripTransactionReportIDs.length) {
30+
return [];
31+
}
32+
33+
return Object.values(transactions ?? {}).filter((transaction): transaction is Transaction => !!transaction && tripTransactionReportIDs.includes(transaction.reportID));
34+
},
35+
[tripTransactionReportIDs],
36+
);
37+
2338
const [tripTransactions = getEmptyArray<Transaction>()] = useOnyx(
2439
ONYXKEYS.COLLECTION.TRANSACTION,
2540
{
26-
selector: (transactions) => {
27-
if (!tripTransactionReportIDs.length) {
28-
return [];
29-
}
30-
31-
return Object.values(transactions ?? {}).filter((transaction): transaction is Transaction => !!transaction && tripTransactionReportIDs.includes(transaction.reportID));
32-
},
41+
selector: tripTransactionsSelector,
42+
canBeMissing: true,
3343
},
34-
[tripTransactionReportIDs],
44+
[tripTransactionsSelector],
3545
);
3646
return tripTransactions;
3747
}

src/pages/AddUnreportedExpense.tsx

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useEffect, useMemo, useRef, useState} from 'react';
1+
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
22
import {InteractionManager} from 'react-native';
33
import type {OnyxCollection} from 'react-native-onyx';
44
import EmptyStateComponent from '@components/EmptyStateComponent';
@@ -56,32 +56,39 @@ function AddUnreportedExpense({route}: AddUnreportedExpensePageType) {
5656
const session = useSession();
5757
const shouldShowUnreportedTransactionsSkeletons = isLoadingUnreportedTransactions && hasMoreUnreportedTransactionsResults && !isOffline;
5858

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 [];
7563
}
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+
);
7683

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+
);
8592

8693
const fetchMoreUnreportedTransactions = () => {
8794
if (!hasMoreUnreportedTransactionsResults || isLoadingUnreportedTransactions) {

src/pages/Search/EmptySearchView.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import variables from '@styles/variables';
4242
import CONST from '@src/CONST';
4343
import ONYXKEYS from '@src/ONYXKEYS';
4444
import ROUTES from '@src/ROUTES';
45-
import type {IntroSelected, PersonalDetails, Policy} from '@src/types/onyx';
45+
import type {IntroSelected, PersonalDetails, Policy, Transaction} from '@src/types/onyx';
4646
import type {SearchDataTypes} from '@src/types/onyx/SearchResults';
4747

4848
type EmptySearchViewProps = {
@@ -142,6 +142,9 @@ function EmptySearchView({similarSearchHash, type, groupBy, hasResults}: EmptySe
142142
);
143143
}
144144

145+
const hasTransactionsSelector = (transactions: OnyxCollection<Transaction>) =>
146+
Object.values(transactions ?? {}).filter((transaction) => transaction?.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE).length > 0;
147+
145148
function EmptySearchViewContent({
146149
similarSearchHash,
147150
type,
@@ -166,7 +169,7 @@ function EmptySearchViewContent({
166169

167170
const [hasTransactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION, {
168171
canBeMissing: true,
169-
selector: (transactions) => Object.values(transactions ?? {}).filter((transaction) => transaction?.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE).length > 0,
172+
selector: hasTransactionsSelector,
170173
});
171174
const [tryNewDot] = useOnyx(ONYXKEYS.NVP_TRY_NEW_DOT, {selector: tryNewDotOnyxSelector, canBeMissing: true});
172175

src/pages/TransactionDuplicate/Confirmation.tsx

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import useLocalize from '@hooks/useLocalize';
1717
import useOnyx from '@hooks/useOnyx';
1818
import useReviewDuplicatesNavigation from '@hooks/useReviewDuplicatesNavigation';
1919
import useThemeStyles from '@hooks/useThemeStyles';
20+
import useTransactionsByID from '@hooks/useTransactionsByID';
2021
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
2122
import Navigation from '@libs/Navigation/Navigation';
2223
import type {PlatformStackRouteProp} from '@libs/Navigation/PlatformStackNavigation/types';
@@ -52,14 +53,7 @@ function Confirmation() {
5253
() => transactionViolations?.find((violation) => violation.name === CONST.VIOLATIONS.DUPLICATED_TRANSACTION)?.data?.duplicates ?? [],
5354
[transactionViolations],
5455
);
55-
const [allDuplicates] = useOnyx(
56-
ONYXKEYS.COLLECTION.TRANSACTION,
57-
{
58-
selector: (allTransactions) => allDuplicateIDs.map((id) => allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${id}`]),
59-
canBeMissing: true,
60-
},
61-
[allDuplicateIDs],
62-
);
56+
const [allDuplicates] = useTransactionsByID(allDuplicateIDs);
6357

6458
const compareResult = TransactionUtils.compareDuplicateTransactionFields(transaction, allDuplicates, reviewDuplicates?.reportID);
6559
const {goBack} = useReviewDuplicatesNavigation(Object.keys(compareResult.change ?? {}), 'confirmation', route.params.threadReportID, route.params.backTo);
@@ -69,14 +63,8 @@ function Confirmation() {
6963
const reportAction = Object.values(reportActions ?? {}).find(
7064
(action) => ReportActionsUtils.isMoneyRequestAction(action) && ReportActionsUtils.getOriginalMessage(action)?.IOUTransactionID === reviewDuplicates?.transactionID,
7165
);
72-
const [duplicates] = useOnyx(
73-
ONYXKEYS.COLLECTION.TRANSACTION,
74-
{
75-
selector: (allTransactions) => reviewDuplicates?.duplicates.map((id) => allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${id}`]),
76-
canBeMissing: true,
77-
},
78-
[reviewDuplicates?.duplicates],
79-
);
66+
67+
const [duplicates] = useTransactionsByID(reviewDuplicates?.duplicates);
8068
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`, {canBeMissing: true});
8169
const transactionsMergeParams = useMemo(
8270
() => TransactionUtils.buildMergeDuplicatesParams(reviewDuplicates, duplicates ?? [], newTransaction),

src/pages/TransactionDuplicate/DuplicateTransactionItem.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type DuplicateTransactionItemProps = {
2121
policies: OnyxCollection<Policy>;
2222
};
2323

24+
const linkedTransactionRouteErrorSelector = (transaction: OnyxEntry<Transaction>) => transaction?.errorFields?.route ?? null;
25+
2426
function DuplicateTransactionItem({transaction, index, allReports, policies}: DuplicateTransactionItemProps) {
2527
const styles = useThemeStyles();
2628
const [userWalletTierName] = useOnyx(ONYXKEYS.USER_WALLET, {selector: (wallet) => wallet?.tierName, canBeMissing: false});
@@ -50,7 +52,7 @@ function DuplicateTransactionItem({transaction, index, allReports, policies}: Du
5052

5153
const [linkedTransactionRouteError] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${isMoneyRequestAction(action) && getOriginalMessage(action)?.IOUTransactionID}`, {
5254
canBeMissing: true,
53-
selector: (transactionItem) => transactionItem?.errorFields?.route ?? null,
55+
selector: linkedTransactionRouteErrorSelector,
5456
});
5557

5658
const contextValue = useMemo(() => ({shouldOpenReportInRHP: true}), []);

src/pages/TransactionDuplicate/ReviewBillable.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ScreenWrapper from '@components/ScreenWrapper';
55
import useLocalize from '@hooks/useLocalize';
66
import useOnyx from '@hooks/useOnyx';
77
import useReviewDuplicatesNavigation from '@hooks/useReviewDuplicatesNavigation';
8+
import useTransactionsByID from '@hooks/useTransactionsByID';
89
import {setReviewDuplicatesKey} from '@libs/actions/Transaction';
910
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
1011
import type {PlatformStackRouteProp} from '@libs/Navigation/PlatformStackNavigation/types';
@@ -29,14 +30,8 @@ function ReviewBillable() {
2930
() => transactionViolations?.find((violation) => violation.name === CONST.VIOLATIONS.DUPLICATED_TRANSACTION)?.data?.duplicates ?? [],
3031
[transactionViolations],
3132
);
32-
const [allDuplicates] = useOnyx(
33-
ONYXKEYS.COLLECTION.TRANSACTION,
34-
{
35-
selector: (allTransactions) => allDuplicateIDs.map((id) => allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${id}`]),
36-
canBeMissing: true,
37-
},
38-
[allDuplicateIDs],
39-
);
33+
34+
const [allDuplicates] = useTransactionsByID(allDuplicateIDs);
4035
const compareResult = compareDuplicateTransactionFields(transaction, allDuplicates, reviewDuplicates?.reportID);
4136
const stepNames = Object.keys(compareResult.change ?? {}).map((key, index) => (index + 1).toString());
4237
const {currentScreenIndex, goBack, navigateToNextScreen} = useReviewDuplicatesNavigation(

src/pages/TransactionDuplicate/ReviewCategory.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ScreenWrapper from '@components/ScreenWrapper';
55
import useLocalize from '@hooks/useLocalize';
66
import useOnyx from '@hooks/useOnyx';
77
import useReviewDuplicatesNavigation from '@hooks/useReviewDuplicatesNavigation';
8+
import useTransactionsByID from '@hooks/useTransactionsByID';
89
import {setReviewDuplicatesKey} from '@libs/actions/Transaction';
910
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
1011
import type {PlatformStackRouteProp} from '@libs/Navigation/PlatformStackNavigation/types';
@@ -29,14 +30,7 @@ function ReviewCategory() {
2930
() => transactionViolations?.find((violation) => violation.name === CONST.VIOLATIONS.DUPLICATED_TRANSACTION)?.data?.duplicates ?? [],
3031
[transactionViolations],
3132
);
32-
const [allDuplicates] = useOnyx(
33-
ONYXKEYS.COLLECTION.TRANSACTION,
34-
{
35-
selector: (allTransactions) => allDuplicateIDs.map((id) => allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${id}`]),
36-
canBeMissing: true,
37-
},
38-
[allDuplicateIDs],
39-
);
33+
const [allDuplicates] = useTransactionsByID(allDuplicateIDs);
4034

4135
const compareResult = compareDuplicateTransactionFields(transaction, allDuplicates, reviewDuplicates?.reportID);
4236
const stepNames = Object.keys(compareResult.change ?? {}).map((key, index) => (index + 1).toString());

0 commit comments

Comments
 (0)