Skip to content

Commit

Permalink
Ad1: improve performance of last accessed raport
Browse files Browse the repository at this point in the history
  • Loading branch information
kosmydel committed Jul 5, 2024
1 parent 4e92053 commit 5352992
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import lodashEscape from 'lodash/escape';
import lodashFindLastIndex from 'lodash/findLastIndex';
import lodashIntersection from 'lodash/intersection';
import lodashIsEqual from 'lodash/isEqual';
import lodashMaxBy from 'lodash/maxBy';
import type {OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {OriginalMessageModifiedExpense} from 'src/types/onyx/OriginalMessage';
Expand Down Expand Up @@ -1182,7 +1183,13 @@ function findLastAccessedReport(
reportsValues = filterReportsByPolicyIDAndMemberAccountIDs(reportsValues, policyMemberAccountIDs, policyID);
}

let sortedReports = sortReportsByLastRead(reportsValues, reportMetadata);
console.time('findLastAccessedReport');
let sortedReports = reportsValues.filter(
(report) => !!report?.reportID && !!(reportMetadata?.[`${ONYXKEYS.COLLECTION.REPORT_METADATA}${report.reportID}`]?.lastVisitTime ?? report?.lastReadTime),
);
console.time('findLastAccessedReport');

const lastRead = lodashMaxBy(sortedReports, (a) => new Date(reportMetadata?.[`${ONYXKEYS.COLLECTION.REPORT_METADATA}${a?.reportID}`]?.lastVisitTime ?? a?.lastReadTime ?? '').valueOf());

let adminReport: OnyxEntry<Report>;
if (openOnAdminRoom) {
Expand Down Expand Up @@ -1233,7 +1240,7 @@ function findLastAccessedReport(
sortedReports = sortedReports.filter((report) => !isSystemChat(report)) ?? [];
}

return adminReport ?? sortedReports.at(-1);
return adminReport ?? lastRead;
}

/**
Expand Down Expand Up @@ -7295,7 +7302,6 @@ export {
shouldShowFlagComment,
shouldShowRBRForMissingSmartscanFields,
shouldUseFullTitleToDisplay,
sortReportsByLastRead,
updateOptimisticParentReportAction,
updateReportPreview,
temporary_getMoneyRequestOptions,
Expand Down

0 comments on commit 5352992

Please sign in to comment.