-
Notifications
You must be signed in to change notification settings - Fork 3.9k
perf: reduce getAllReportTransactions usage in ReportPreview #46500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
neil-marcellini
merged 10 commits into
Expensify:main
from
callstack-internal:perf/reduce-getAllReportTransactions-usage
Jul 31, 2024
+44
−26
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
020fe1b
reduce usage of getAllReportTransactions in ReportPreview
TMisiukiewicz 52fcb34
update ReportPreview to operate on a single instance of transactions
TMisiukiewicz a8ab569
memoize report transactions
TMisiukiewicz c719638
Merge remote-tracking branch 'upstream/main' into perf/reduce-getAllR…
TMisiukiewicz b89c139
code review updates
TMisiukiewicz 7d0d08b
get transactions from Onyx in MoneyReportView
TMisiukiewicz 4264fb4
code update
TMisiukiewicz e8df1c9
use reports transactions mapping & reduce diff
TMisiukiewicz 2714eec
reduce diff
TMisiukiewicz 6a22933
fix tests
TMisiukiewicz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -542,6 +542,7 @@ Onyx.connect({ | |
| }); | ||
|
|
||
| let allTransactions: OnyxCollection<Transaction> = {}; | ||
| let reportsTransactions: Record<string, Transaction[]> = {}; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NAB: Maybe rename to |
||
| Onyx.connect({ | ||
| key: ONYXKEYS.COLLECTION.TRANSACTION, | ||
| waitForCollectionCallback: true, | ||
|
|
@@ -550,6 +551,20 @@ Onyx.connect({ | |
| return; | ||
| } | ||
| allTransactions = Object.fromEntries(Object.entries(value).filter(([, transaction]) => transaction)); | ||
|
|
||
| reportsTransactions = Object.values(value).reduce<Record<string, Transaction[]>>((all, transaction) => { | ||
| const reportsMap = all; | ||
| if (!transaction) { | ||
| return reportsMap; | ||
| } | ||
|
|
||
| if (!reportsMap[transaction.reportID]) { | ||
| reportsMap[transaction.reportID] = []; | ||
| } | ||
| reportsMap[transaction.reportID].push(transaction); | ||
|
|
||
| return all; | ||
| }, {}); | ||
| }, | ||
| }); | ||
|
|
||
|
|
@@ -1419,7 +1434,7 @@ function isPolicyAdmin(policyID: string, policies: OnyxCollection<Policy>): bool | |
| * Checks whether all the transactions linked to the IOU report are of the Distance Request type with pending routes | ||
| */ | ||
| function hasOnlyTransactionsWithPendingRoutes(iouReportID: string | undefined): boolean { | ||
| const transactions = TransactionUtils.getAllReportTransactions(iouReportID); | ||
| const transactions = reportsTransactions[iouReportID ?? ''] ?? []; | ||
|
|
||
| // Early return false in case not having any transaction | ||
| if (!transactions || transactions.length === 0) { | ||
|
|
@@ -1513,7 +1528,7 @@ function hasOnlyNonReimbursableTransactions(iouReportID: string | undefined): bo | |
| return false; | ||
| } | ||
|
|
||
| const transactions = TransactionUtils.getAllReportTransactions(iouReportID); | ||
| const transactions = reportsTransactions[iouReportID ?? ''] ?? []; | ||
| if (!transactions || transactions.length === 0) { | ||
| return false; | ||
| } | ||
|
|
@@ -2456,7 +2471,7 @@ function requiresAttentionFromCurrentUser(optionOrReport: OnyxEntry<Report> | Op | |
| * | ||
| */ | ||
| function hasNonReimbursableTransactions(iouReportID: string | undefined): boolean { | ||
| const transactions = TransactionUtils.getAllReportTransactions(iouReportID); | ||
| const transactions = reportsTransactions[iouReportID ?? ''] ?? []; | ||
| return transactions.filter((transaction) => transaction.reimbursable === false).length > 0; | ||
| } | ||
|
|
||
|
|
@@ -2927,7 +2942,7 @@ const changeMoneyRequestHoldStatus = (reportAction: OnyxEntry<ReportAction>, bac | |
| * Gets all transactions on an IOU report with a receipt | ||
| */ | ||
| function getTransactionsWithReceipts(iouReportID: string | undefined): Transaction[] { | ||
| const transactions = TransactionUtils.getAllReportTransactions(iouReportID); | ||
| const transactions = reportsTransactions[iouReportID ?? ''] ?? []; | ||
| return transactions.filter((transaction) => TransactionUtils.hasReceipt(transaction)); | ||
| } | ||
|
|
||
|
|
@@ -2967,7 +2982,9 @@ function getLinkedTransaction(reportAction: OnyxEntry<ReportAction | OptimisticI | |
| * Check if any of the transactions in the report has required missing fields | ||
| */ | ||
| function hasMissingSmartscanFields(iouReportID: string): boolean { | ||
| return TransactionUtils.getAllReportTransactions(iouReportID).some(TransactionUtils.hasMissingSmartscanFields); | ||
| const reportTransactions = reportsTransactions[iouReportID] ?? []; | ||
|
|
||
| return reportTransactions.some(TransactionUtils.hasMissingSmartscanFields); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -5524,15 +5541,15 @@ function shouldDisplayTransactionThreadViolations( | |
| * Checks to see if a report contains a violation | ||
| */ | ||
| function hasViolations(reportID: string, transactionViolations: OnyxCollection<TransactionViolation[]>): boolean { | ||
| const transactions = TransactionUtils.getAllReportTransactions(reportID); | ||
| const transactions = reportsTransactions[reportID] ?? []; | ||
| return transactions.some((transaction) => TransactionUtils.hasViolation(transaction.transactionID, transactionViolations)); | ||
| } | ||
|
|
||
| /** | ||
| * Checks to see if a report contains a violation of type `warning` | ||
| */ | ||
| function hasWarningTypeViolations(reportID: string, transactionViolations: OnyxCollection<TransactionViolation[]>): boolean { | ||
| const transactions = TransactionUtils.getAllReportTransactions(reportID); | ||
| const transactions = reportsTransactions[reportID] ?? []; | ||
| return transactions.some((transaction) => TransactionUtils.hasWarningTypeViolation(transaction.transactionID, transactionViolations)); | ||
| } | ||
|
|
||
|
|
@@ -6747,23 +6764,23 @@ function navigateToPrivateNotes(report: OnyxEntry<Report>, session: OnyxEntry<Se | |
| * Get all held transactions of a iouReport | ||
| */ | ||
| function getAllHeldTransactions(iouReportID?: string): Transaction[] { | ||
| const transactions = TransactionUtils.getAllReportTransactions(iouReportID); | ||
| const transactions = reportsTransactions[iouReportID ?? ''] ?? []; | ||
| return transactions.filter((transaction) => TransactionUtils.isOnHold(transaction)); | ||
| } | ||
|
|
||
| /** | ||
| * Check if Report has any held expenses | ||
| */ | ||
| function hasHeldExpenses(iouReportID?: string): boolean { | ||
| const transactions = TransactionUtils.getAllReportTransactions(iouReportID); | ||
| const transactions = reportsTransactions[iouReportID ?? ''] ?? []; | ||
| return transactions.some((transaction) => TransactionUtils.isOnHold(transaction)); | ||
| } | ||
|
|
||
| /** | ||
| * Check if all expenses in the Report are on hold | ||
| */ | ||
| function hasOnlyHeldExpenses(iouReportID: string, transactions?: OnyxCollection<Transaction>): boolean { | ||
| const reportTransactions = TransactionUtils.getAllReportTransactions(iouReportID, transactions); | ||
| function hasOnlyHeldExpenses(iouReportID: string): boolean { | ||
| const reportTransactions = reportsTransactions[iouReportID ?? ''] ?? []; | ||
| return reportTransactions.length > 0 && !reportTransactions.some((transaction) => !TransactionUtils.isOnHold(transaction)); | ||
| } | ||
|
|
||
|
|
@@ -6783,9 +6800,10 @@ function hasUpdatedTotal(report: OnyxInputOrEntry<Report>, policy: OnyxInputOrEn | |
| return true; | ||
| } | ||
|
|
||
| const transactions = TransactionUtils.getAllReportTransactions(report.reportID); | ||
| const hasPendingTransaction = transactions.some((transaction) => !!transaction.pendingAction); | ||
| const hasTransactionWithDifferentCurrency = transactions.some((transaction) => transaction.currency !== report.currency); | ||
| const allReportTransactions = reportsTransactions[report.reportID] ?? []; | ||
|
|
||
| const hasPendingTransaction = allReportTransactions.some((transaction) => !!transaction.pendingAction); | ||
| const hasTransactionWithDifferentCurrency = allReportTransactions.some((transaction) => transaction.currency !== report.currency); | ||
| const hasDifferentWorkspaceCurrency = report.pendingFields?.createChat && isExpenseReport(report) && report.currency !== policy?.outputCurrency; | ||
| const hasOptimisticHeldExpense = hasHeldExpenses(report.reportID) && report?.unheldTotal === undefined; | ||
|
|
||
|
|
@@ -6796,14 +6814,14 @@ function hasUpdatedTotal(report: OnyxInputOrEntry<Report>, policy: OnyxInputOrEn | |
| * Return held and full amount formatted with used currency | ||
| */ | ||
| function getNonHeldAndFullAmount(iouReport: OnyxEntry<Report>, policy: OnyxEntry<Policy>): string[] { | ||
| const transactions = TransactionUtils.getAllReportTransactions(iouReport?.reportID ?? '-1'); | ||
| const hasPendingTransaction = transactions.some((transaction) => !!transaction.pendingAction); | ||
| const reportTransactions = reportsTransactions[iouReport?.reportID ?? ''] ?? []; | ||
| const hasPendingTransaction = reportTransactions.some((transaction) => !!transaction.pendingAction); | ||
|
|
||
| // if the report is an expense report, the total amount should be negated | ||
| const coefficient = isExpenseReport(iouReport) ? -1 : 1; | ||
|
|
||
| if (hasUpdatedTotal(iouReport, policy) && hasPendingTransaction) { | ||
| const unheldTotal = transactions.reduce((currentVal, transaction) => currentVal - (!TransactionUtils.isOnHold(transaction) ? transaction.amount : 0), 0); | ||
| const unheldTotal = reportTransactions.reduce((currentVal, transaction) => currentVal - (!TransactionUtils.isOnHold(transaction) ? transaction.amount : 0), 0); | ||
|
|
||
| return [CurrencyUtils.convertToDisplayString(unheldTotal, iouReport?.currency), CurrencyUtils.convertToDisplayString((iouReport?.total ?? 0) * coefficient, iouReport?.currency)]; | ||
| } | ||
|
|
@@ -7043,7 +7061,7 @@ function getTripTransactions(tripRoomReportID: string | undefined, reportFieldTo | |
| const tripTransactionReportIDs = Object.values(ReportConnection.getAllReports() ?? {}) | ||
| .filter((report) => report && report?.[reportFieldToCompare] === tripRoomReportID) | ||
| .map((report) => report?.reportID); | ||
| return tripTransactionReportIDs.flatMap((reportID) => TransactionUtils.getAllReportTransactions(reportID)); | ||
| return tripTransactionReportIDs.flatMap((reportID) => reportsTransactions[reportID ?? ''] ?? []); | ||
| } | ||
|
|
||
| function getTripIDFromTransactionParentReport(transactionParentReport: OnyxEntry<Report> | undefined | null): string | undefined { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This prevents the header from being refreshed when transactions for the report change causing #47084