Skip to content
Merged
Show file tree
Hide file tree
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 May 30, 2025
2c05d9e
update transactions
TMisiukiewicz May 30, 2025
fd897f5
Merge remote-tracking branch 'upstream/main' into perf/improve-calcul…
TMisiukiewicz Jun 2, 2025
fb9ab4b
Merge branch 'main' into perf/improve-calculating-displayed-lhn-reports
TMisiukiewicz Jun 6, 2025
e4f4da4
Merge branch 'main' into perf/improve-calculating-displayed-lhn-reports
TMisiukiewicz Jun 9, 2025
761b2b7
Merge branch 'main' into perf/improve-calculating-displayed-lhn-reports
TMisiukiewicz Jun 10, 2025
a692705
Merge remote-tracking branch 'upstream/main' into perf/improve-calcul…
TMisiukiewicz Jun 11, 2025
8a391ac
create logic for getting reports to update
TMisiukiewicz Jun 11, 2025
75606f0
cleanup the implementation
TMisiukiewicz Jun 11, 2025
7b584ec
simplify disabling eslint rules
TMisiukiewicz Jun 11, 2025
21833bc
create copy of displayed reports
TMisiukiewicz Jun 11, 2025
339fd43
stop calling updates when there are no changed reports
TMisiukiewicz Jun 11, 2025
a3280de
rerun on entire when mode or betas change
TMisiukiewicz Jun 11, 2025
0f05e46
fix priority mode when updating
TMisiukiewicz Jun 11, 2025
4ddc229
bring back isReportArchieved
TMisiukiewicz Jun 11, 2025
3a006a4
update dependency array of getUpdatedReports
TMisiukiewicz Jun 11, 2025
a3c3b1e
simplify solution to avoid rerenders
TMisiukiewicz Jun 12, 2025
d101a0c
fix updating all reports on mode/betas change
TMisiukiewicz Jun 12, 2025
24be1e7
disable react compiler rule for refs
TMisiukiewicz Jun 12, 2025
9e8e300
fix SidebarOrderTest
TMisiukiewicz Jun 12, 2025
59657bd
reduce diff in useSidebarOrderedReports
TMisiukiewicz Jun 12, 2025
3db5cf8
fix SidebarUtils perf tests
TMisiukiewicz Jun 12, 2025
56cb70c
fix lint changed
TMisiukiewicz Jun 12, 2025
1b53222
Merge branch 'main' into perf/improve-calculating-displayed-lhn-reports
TMisiukiewicz Jun 12, 2025
5caeab6
prioritize full recompute over report updates
TMisiukiewicz Jun 12, 2025
4f42dd6
Merge remote-tracking branch 'upstream/main' into perf/improve-calcul…
TMisiukiewicz Jun 13, 2025
9aaa129
remove disable rule from SIdebarUtils
TMisiukiewicz Jun 13, 2025
8824080
move derivedCurrentReportID to useMemo dep array
TMisiukiewicz Jun 13, 2025
977590a
Revert "move derivedCurrentReportID to useMemo dep array"
TMisiukiewicz Jun 13, 2025
216b34f
Merge remote-tracking branch 'upstream/main' into perf/improve-calcul…
TMisiukiewicz Jun 16, 2025
49a3163
get rid of eslint rules disabling
TMisiukiewicz Jun 16, 2025
b263699
remove usage of ref
TMisiukiewicz Jun 16, 2025
5fbaf92
Merge remote-tracking branch 'upstream/main' into perf/improve-calcul…
TMisiukiewicz Jun 18, 2025
279b93f
Merge remote-tracking branch 'upstream/main' into perf/improve-calcul…
TMisiukiewicz Jun 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 107 additions & 14 deletions src/hooks/useSidebarOrderedReports.tsx
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';
Expand All @@ -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'>;
Expand All @@ -24,6 +25,8 @@ type SidebarOrderedReportsContextValue = {
policyMemberAccountIDs: number[];
};

type ReportsToDisplayInLHN = Record<string, OnyxTypes.Report & {hasErrorsOtherThanFailedReceipt?: boolean}>;

const SidebarOrderedReportsContext = createContext<SidebarOrderedReportsContextValue>({
orderedReports: [],
currentReportID: '',
Expand Down Expand Up @@ -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(() => {

Copy link
Copy Markdown
Contributor

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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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[] => {
Expand Down Expand Up @@ -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,
Expand All @@ -131,4 +224,4 @@ function useSidebarOrderedReports() {
}

export {SidebarOrderedReportsContext, SidebarOrderedReportsContextProvider, useSidebarOrderedReports};
export type {PartialPolicyForSidebar};
export type {PartialPolicyForSidebar, ReportsToDisplayInLHN};
Loading