Repository navigation
perf: Let Home sections read only the data they show #108229
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
base: main
Are you sure you want to change the base?
Changes from all commits
e6221d2
19661a4
d5b1bce
b3cf1e9
c18e144
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout'; | |
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
|
|
||
| import {CAROUSEL_SOURCE, setActiveTransactionIDs} from '@libs/actions/TransactionThreadNavigation'; | ||
| import Log from '@libs/Log'; | ||
| import Navigation from '@libs/Navigation/Navigation'; | ||
| import {buildQueryStringFromFilterFormValues} from '@libs/SearchQueryUtils'; | ||
| import type {TransactionThreadNavigationDescriptor} from '@libs/TransactionThreadNavigationUtils'; | ||
|
|
@@ -26,12 +27,13 @@ import {useIsFocused} from '@react-navigation/native'; | |
| import {guidedSetupAndTourStatusSelector} from '@selectors/Onboarding'; | ||
| import {fullPolicySnapshotLastModifiedByPolicyIDSelector} from '@selectors/Policy'; | ||
| import React from 'react'; | ||
| import Onyx from 'react-native-onyx'; | ||
|
|
||
| import type {RecentlyAddedExpense} from './useRecentlyAddedData'; | ||
| import type {RecentlyAddedItem} from './useRecentlyAddedData'; | ||
|
|
||
| import RecentlyAddedPlaceholder from './RecentlyAddedPlaceholder'; | ||
| import RecentlyAddedRow from './RecentlyAddedRow'; | ||
| import {useRecentlyAddedData} from './useRecentlyAddedData'; | ||
| import {buildRecentlyAddedExpense, useRecentlyAddedData} from './useRecentlyAddedData'; | ||
|
|
||
| function RecentlyAddedSection() { | ||
| const {transactions, isAwaitingFirstResult} = useRecentlyAddedData(); | ||
|
|
@@ -47,7 +49,7 @@ function RecentlyAddedSection() { | |
| const {email: currentUserEmail, accountID: currentUserAccountID} = useCurrentUserPersonalDetails(); | ||
| const [personalDetails] = usePersonalDetailsByIDs([ | ||
| currentUserAccountID, | ||
| ...transactions.flatMap((transaction) => [transaction.reportAction?.actorAccountID, transaction.report?.ownerAccountID]), | ||
| ...transactions.flatMap((transaction) => [transaction.reportAction?.actorAccountID, transaction.snapshotReport?.ownerAccountID]), | ||
| ]); | ||
| const isAnonymousUser = useIsAnonymousUser(); | ||
| const [conciergeReportID] = useOnyx(ONYXKEYS.CONCIERGE_REPORT_ID); | ||
|
|
@@ -59,7 +61,29 @@ function RecentlyAddedSection() { | |
| const hasExpenses = transactions.length > 0; | ||
| const listBottomPadding = shouldUseNarrowLayout ? styles.pb2 : styles.pb5; | ||
|
|
||
| const openExpense = (expense: RecentlyAddedExpense) => { | ||
| const openExpense = async (item: RecentlyAddedItem) => { | ||
| // Rows read their own live data, so the full siblings are read only now, on press. | ||
| let localTransactions; | ||
| let localReports; | ||
| try { | ||
| [localTransactions, localReports] = await Promise.all([ | ||
| // eslint-disable-next-line rulesdir/no-onyx-get-snapshot-key -- Home is never inside a Search scope, so the rows read live data too | ||
| Onyx.multiGet(transactions.map((sibling) => `${ONYXKEYS.COLLECTION.TRANSACTION}${sibling.transactionID}` as const)), | ||
| // eslint-disable-next-line rulesdir/no-onyx-get-snapshot-key -- Home is never inside a Search scope, so the rows read live data too | ||
| Onyx.multiGet(transactions.map((sibling) => `${ONYXKEYS.COLLECTION.REPORT}${sibling.reportID}` as const)), | ||
| ]); | ||
| } catch (error) { | ||
| Log.warn('[RecentlyAddedSection] Failed to read the expenses to open', {error}); | ||
| return; | ||
| } | ||
| const resolvedSiblings = transactions | ||
| .map((sibling, index) => buildRecentlyAddedExpense(sibling, localTransactions.at(index), localReports.at(index), translate)) | ||
| .filter((sibling) => !!sibling); | ||
| const expense = resolvedSiblings.find((sibling) => sibling.transactionID === item.transactionID); | ||
| if (!expense) { | ||
| return; | ||
| } | ||
|
|
||
| // Resolve only the tapped expense now. getReportIDToOpenForExpense may create a transaction thread, so | ||
| // resolving every sibling up front would create a thread for each multi-expense sibling on a single tap. | ||
| // Instead, seed the cheap snapshot-derived descriptors and let the carousel resolve each sibling lazily, | ||
|
|
@@ -80,7 +104,7 @@ function RecentlyAddedSection() { | |
| // pending and re-adds it if the delete rolls back, both from its live transaction. A descriptor would keep | ||
| // it in the arrows after the delete syncs and its live copy is gone, landing the arrow on "not here". | ||
| const siblingTransactionIDs = transactions.map((sibling) => sibling.transactionID); | ||
| const navigableSiblings = transactions.filter((sibling) => sibling.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE); | ||
| const navigableSiblings = resolvedSiblings.filter((sibling) => sibling.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE); | ||
| const siblingDescriptorsByTransactionID = navigableSiblings.reduce<Record<string, TransactionThreadNavigationDescriptor>>((map, sibling) => { | ||
| // eslint-disable-next-line no-param-reassign | ||
| map[sibling.transactionID] = { | ||
|
|
@@ -144,11 +168,13 @@ function RecentlyAddedSection() { | |
| containerStyles={hasExpenses || isAwaitingFirstResult ? listBottomPadding : undefined} | ||
| > | ||
| {hasExpenses ? ( | ||
| transactions.map((expense, index) => ( | ||
| transactions.map((item, index) => ( | ||
| <RecentlyAddedRow | ||
| key={expense.transactionID} | ||
| expense={expense} | ||
| onPress={() => openExpense(expense)} | ||
| key={item.transactionID} | ||
| item={item} | ||
| onPress={() => { | ||
| openExpense(item); | ||
|
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:
Contributor
Author
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. Added a try/catch around the sibling read in openExpense: a failed read is logged and the tap does nothing, instead of leaving a rejected promise nobody handles. |
||
| }} | ||
| shouldShowSeparator={index < transactions.length - 1} | ||
| shouldShowReceiptPreview={isFocused} | ||
| rowStyle={cardPaddingHorizontal} | ||
|
|
||
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.
Right now the tests add the report and its expenses at the same time. Could we add one where the report shows up later and check that the count goes from 0 to 1?
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.
Added "should count a flagged expense once its report arrives after the expense and its violation": the expense and its violation are written first (count 0), then only the report lands and the count goes to 1.