Repository navigation
perf: improve calculating displayed reports in LHN #63980
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
mountiny
merged 34 commits into
Expensify:main
from
callstack-internal:perf/improve-calculating-displayed-lhn-reports
Jun 20, 2025
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
f919b6a
wip: atomic updates in gorid
TMisiukiewicz 2c05d9e
update transactions
TMisiukiewicz fd897f5
Merge remote-tracking branch 'upstream/main' into perf/improve-calcul…
TMisiukiewicz fb9ab4b
Merge branch 'main' into perf/improve-calculating-displayed-lhn-reports
TMisiukiewicz e4f4da4
Merge branch 'main' into perf/improve-calculating-displayed-lhn-reports
TMisiukiewicz 761b2b7
Merge branch 'main' into perf/improve-calculating-displayed-lhn-reports
TMisiukiewicz a692705
Merge remote-tracking branch 'upstream/main' into perf/improve-calcul…
TMisiukiewicz 8a391ac
create logic for getting reports to update
TMisiukiewicz 75606f0
cleanup the implementation
TMisiukiewicz 7b584ec
simplify disabling eslint rules
TMisiukiewicz 21833bc
create copy of displayed reports
TMisiukiewicz 339fd43
stop calling updates when there are no changed reports
TMisiukiewicz a3280de
rerun on entire when mode or betas change
TMisiukiewicz 0f05e46
fix priority mode when updating
TMisiukiewicz 4ddc229
bring back isReportArchieved
TMisiukiewicz 3a006a4
update dependency array of getUpdatedReports
TMisiukiewicz a3c3b1e
simplify solution to avoid rerenders
TMisiukiewicz d101a0c
fix updating all reports on mode/betas change
TMisiukiewicz 24be1e7
disable react compiler rule for refs
TMisiukiewicz 9e8e300
fix SidebarOrderTest
TMisiukiewicz 59657bd
reduce diff in useSidebarOrderedReports
TMisiukiewicz 3db5cf8
fix SidebarUtils perf tests
TMisiukiewicz 56cb70c
fix lint changed
TMisiukiewicz 1b53222
Merge branch 'main' into perf/improve-calculating-displayed-lhn-reports
TMisiukiewicz 5caeab6
prioritize full recompute over report updates
TMisiukiewicz 4f42dd6
Merge remote-tracking branch 'upstream/main' into perf/improve-calcul…
TMisiukiewicz 9aaa129
remove disable rule from SIdebarUtils
TMisiukiewicz 8824080
move derivedCurrentReportID to useMemo dep array
TMisiukiewicz 977590a
Revert "move derivedCurrentReportID to useMemo dep array"
TMisiukiewicz 216b34f
Merge remote-tracking branch 'upstream/main' into perf/improve-calcul…
TMisiukiewicz 49a3163
get rid of eslint rules disabling
TMisiukiewicz b263699
remove usage of ref
TMisiukiewicz 5fbaf92
Merge remote-tracking branch 'upstream/main' into perf/improve-calcul…
TMisiukiewicz 279b93f
Merge remote-tracking branch 'upstream/main' into perf/improve-calcul…
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import React, {createContext, useCallback, useContext, useMemo} from 'react'; | ||
| import React, {createContext, useCallback, useContext, useEffect, useMemo, useState} from 'react'; | ||
| import type {OnyxEntry} from 'react-native-onyx'; | ||
| import {useOnyx} from 'react-native-onyx'; | ||
| import {getPolicyEmployeeListByIdWithoutCurrentUser} from '@libs/PolicyUtils'; | ||
|
|
@@ -9,6 +9,7 @@ import type * as OnyxTypes from '@src/types/onyx'; | |
| import mapOnyxCollectionItems from '@src/utils/mapOnyxCollectionItems'; | ||
| import useCurrentReportID from './useCurrentReportID'; | ||
| import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails'; | ||
| import usePrevious from './usePrevious'; | ||
| import useResponsiveLayout from './useResponsiveLayout'; | ||
|
|
||
| type PartialPolicyForSidebar = Pick<OnyxTypes.Policy, 'type' | 'name' | 'avatarURL' | 'employeeList'>; | ||
|
|
@@ -24,6 +25,8 @@ type SidebarOrderedReportsContextValue = { | |
| policyMemberAccountIDs: number[]; | ||
| }; | ||
|
|
||
| type ReportsToDisplayInLHN = Record<string, OnyxTypes.Report & {hasErrorsOtherThanFailedReceipt?: boolean}>; | ||
|
|
||
| const SidebarOrderedReportsContext = createContext<SidebarOrderedReportsContextValue>({ | ||
| orderedReports: [], | ||
| currentReportID: '', | ||
|
|
@@ -51,32 +54,122 @@ function SidebarOrderedReportsContextProvider({ | |
| currentReportIDForTests, | ||
| }: SidebarOrderedReportsContextProviderProps) { | ||
| const [priorityMode] = useOnyx(ONYXKEYS.NVP_PRIORITY_MODE, {initialValue: CONST.PRIORITY_MODE.DEFAULT, canBeMissing: true}); | ||
| const [chatReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {canBeMissing: true}); | ||
| const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: (c) => mapOnyxCollectionItems(c, policySelector), canBeMissing: true}); | ||
| const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, {canBeMissing: true}); | ||
| const [reportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, {canBeMissing: true}); | ||
| const [reportsDrafts] = useOnyx(ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT, {initialValue: {}, canBeMissing: true}); | ||
| const draftAmount = Object.keys(reportsDrafts ?? {}).length; | ||
| const [chatReports, {sourceValue: reportUpdates}] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {canBeMissing: true}); | ||
| const [policies, {sourceValue: policiesUpdates}] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: (c) => mapOnyxCollectionItems(c, policySelector), canBeMissing: true}); | ||
| const [transactions, {sourceValue: transactionsUpdates}] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION, {canBeMissing: true}); | ||
| const [transactionViolations, {sourceValue: transactionViolationsUpdates}] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, {canBeMissing: true}); | ||
| const [reportNameValuePairs, {sourceValue: reportNameValuePairsUpdates}] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, {canBeMissing: true}); | ||
| const [, {sourceValue: reportsDraftsUpdates}] = useOnyx(ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT, {initialValue: {}, canBeMissing: true}); | ||
| const [betas] = useOnyx(ONYXKEYS.BETAS, {canBeMissing: true}); | ||
| const [reportAttributes] = useOnyx(ONYXKEYS.DERIVED.REPORT_ATTRIBUTES, {selector: (value) => value?.reports, canBeMissing: true}); | ||
| const [currentReportsToDisplay, setCurrentReportsToDisplay] = useState<ReportsToDisplayInLHN>({}); | ||
|
|
||
| const {shouldUseNarrowLayout} = useResponsiveLayout(); | ||
| const {accountID} = useCurrentUserPersonalDetails(); | ||
| const currentReportIDValue = useCurrentReportID(); | ||
| const derivedCurrentReportID = currentReportIDForTests ?? currentReportIDValue?.currentReportIDFromPath ?? currentReportIDValue?.currentReportID; | ||
|
|
||
| const policyMemberAccountIDs = useMemo(() => getPolicyEmployeeListByIdWithoutCurrentUser(policies, undefined, accountID), [policies, accountID]); | ||
| const prevBetas = usePrevious(betas); | ||
| const prevPriorityMode = usePrevious(priorityMode); | ||
|
|
||
| /** | ||
| * Find the reports that need to be updated in the LHN | ||
| */ | ||
| const getUpdatedReports = useCallback(() => { | ||
| let reportsToUpdate: string[] = []; | ||
|
|
||
| if (betas !== prevBetas || priorityMode !== prevPriorityMode) { | ||
| reportsToUpdate = Object.keys(chatReports ?? {}); | ||
| } else if (reportUpdates) { | ||
| reportsToUpdate = Object.keys(reportUpdates ?? {}); | ||
| } else if (reportNameValuePairsUpdates) { | ||
| reportsToUpdate = Object.keys(reportNameValuePairsUpdates ?? {}).map((key) => key.replace(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, ONYXKEYS.COLLECTION.REPORT)); | ||
| } else if (transactionsUpdates) { | ||
| reportsToUpdate = Object.values(transactionsUpdates ?? {}).map((transaction) => `${ONYXKEYS.COLLECTION.REPORT}${transaction?.reportID}`); | ||
| } else if (transactionViolationsUpdates) { | ||
| reportsToUpdate = Object.keys(transactionViolationsUpdates ?? {}) | ||
| .map((key) => key.replace(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, ONYXKEYS.COLLECTION.TRANSACTION)) | ||
| .map((key) => `${ONYXKEYS.COLLECTION.REPORT}${transactions?.[key]?.reportID}`); | ||
| } else if (reportsDraftsUpdates) { | ||
| reportsToUpdate = Object.keys(reportsDraftsUpdates).map((key) => key.replace(ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT, ONYXKEYS.COLLECTION.REPORT)); | ||
| } else if (policiesUpdates) { | ||
| const updatedPolicies = Object.keys(policiesUpdates).map((key) => key.replace(ONYXKEYS.COLLECTION.POLICY, '')); | ||
| reportsToUpdate = Object.entries(chatReports ?? {}) | ||
| .filter(([, value]) => { | ||
| if (!value?.policyID) { | ||
| return; | ||
| } | ||
|
|
||
| return updatedPolicies.includes(value.policyID); | ||
| }) | ||
| .map(([key]) => key); | ||
| } | ||
|
|
||
| return reportsToUpdate; | ||
| }, [ | ||
| reportUpdates, | ||
| reportNameValuePairsUpdates, | ||
| transactionsUpdates, | ||
| transactionViolationsUpdates, | ||
| reportsDraftsUpdates, | ||
| policiesUpdates, | ||
| chatReports, | ||
| transactions, | ||
| betas, | ||
| priorityMode, | ||
| prevBetas, | ||
| prevPriorityMode, | ||
| ]); | ||
|
|
||
| const reportsToDisplayInLHN = useMemo(() => { | ||
| const updatedReports = getUpdatedReports(); | ||
| const shouldDoIncrementalUpdate = updatedReports.length > 0 && Object.keys(currentReportsToDisplay).length > 0; | ||
|
|
||
| let reportsToDisplay = {}; | ||
|
|
||
| if (shouldDoIncrementalUpdate) { | ||
| reportsToDisplay = SidebarUtils.updateReportsToDisplayInLHN( | ||
| currentReportsToDisplay, | ||
| chatReports, | ||
| updatedReports, | ||
| derivedCurrentReportID, | ||
| priorityMode === CONST.PRIORITY_MODE.GSD, | ||
| betas, | ||
| policies, | ||
| transactionViolations, | ||
| reportNameValuePairs, | ||
| reportAttributes, | ||
| ); | ||
| } else { | ||
| reportsToDisplay = SidebarUtils.getReportsToDisplayInLHN( | ||
| derivedCurrentReportID, | ||
| chatReports, | ||
| betas, | ||
| policies, | ||
| priorityMode, | ||
| transactionViolations, | ||
| reportNameValuePairs, | ||
| reportAttributes, | ||
| ); | ||
| } | ||
| return reportsToDisplay; | ||
| // Rule disabled intentionally — triggering a re-render on currentReportsToDisplay would cause an infinite loop | ||
| // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps | ||
|
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. Same here |
||
| }, [getUpdatedReports, chatReports, derivedCurrentReportID, priorityMode, betas, policies, transactionViolations, reportNameValuePairs, reportAttributes]); | ||
|
|
||
| useEffect(() => { | ||
| setCurrentReportsToDisplay(reportsToDisplayInLHN); | ||
| }, [reportsToDisplayInLHN]); | ||
|
|
||
| const getOrderedReportIDs = useCallback( | ||
| (currentReportID?: string) => | ||
| SidebarUtils.getOrderedReportIDs(currentReportID, chatReports, betas, policies, priorityMode, transactionViolations, reportNameValuePairs, reportAttributes), | ||
| // we need reports draft in deps array to reload the list when a draft is added or removed | ||
| () => SidebarUtils.sortReportsToDisplayInLHN(reportsToDisplayInLHN, priorityMode, reportNameValuePairs, reportAttributes), | ||
| // Rule disabled intentionally - reports should be sorted only when the reportsToDisplayInLHN changes | ||
| // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps | ||
| [chatReports, betas, policies, priorityMode, transactionViolations, draftAmount, reportNameValuePairs, reportAttributes], | ||
| [reportsToDisplayInLHN], | ||
| ); | ||
|
|
||
| const orderedReportIDs = useMemo(() => getOrderedReportIDs(), [getOrderedReportIDs]); | ||
|
|
||
| // Get the actual reports based on the ordered IDs | ||
| const getOrderedReports = useCallback( | ||
| (reportIDs: string[]): OnyxTypes.Report[] => { | ||
|
|
@@ -107,7 +200,7 @@ function SidebarOrderedReportsContextProvider({ | |
| derivedCurrentReportID !== '-1' && | ||
| orderedReportIDs.indexOf(derivedCurrentReportID) === -1 | ||
| ) { | ||
| const updatedReportIDs = getOrderedReportIDs(derivedCurrentReportID); | ||
| const updatedReportIDs = getOrderedReportIDs(); | ||
| const updatedReports = getOrderedReports(updatedReportIDs); | ||
| return { | ||
| orderedReports: updatedReports, | ||
|
|
@@ -131,4 +224,4 @@ function useSidebarOrderedReports() { | |
| } | ||
|
|
||
| export {SidebarOrderedReportsContext, SidebarOrderedReportsContextProvider, useSidebarOrderedReports}; | ||
| export type {PartialPolicyForSidebar}; | ||
| export type {PartialPolicyForSidebar, ReportsToDisplayInLHN}; | ||
Oops, something went wrong.
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.
Coming from #77342, we shouldn't use if/else logic to limit one collection per render