From 41dff749202c7c11eb6d74ed57f69a78cc61ca2b Mon Sep 17 00:00:00 2001 From: Jakub Kosmydel <104823336+kosmydel@users.noreply.github.com> Date: Fri, 5 Jul 2024 16:12:44 +0200 Subject: [PATCH] Ad2: Calculate useLastAccessedReportID only when needed --- src/hooks/useLastAccessedReportID.ts | 14 +++++++++----- src/libs/ReportUtils.ts | 11 ++++++++--- src/pages/home/ReportScreen.tsx | 6 +++++- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/hooks/useLastAccessedReportID.ts b/src/hooks/useLastAccessedReportID.ts index 16a4a6bc2a31..dda3460210de 100644 --- a/src/hooks/useLastAccessedReportID.ts +++ b/src/hooks/useLastAccessedReportID.ts @@ -124,13 +124,16 @@ function addSubscriber(subscriber: () => void) { /** * Get the last accessed reportID. */ -export default function useLastAccessedReportID(shouldOpenOnAdminRoom: boolean) { +export default function useLastAccessedReportID(shouldOpenOnAdminRoom: boolean, shouldRun: boolean) { const {canUseDefaultRooms} = usePermissions(); const {activeWorkspaceID} = useActiveWorkspace(); - const getSnapshot = useCallback(() => { + if (!shouldRun) { + return undefined; + } const policyMemberAccountIDs = getPolicyEmployeeListByIdWithoutCurrentUser(policies, activeWorkspaceID, accountID); - return ReportUtils.findLastAccessedReport( + console.time('findLastAccessedReport'); + const res = ReportUtils.findLastAccessedReport( reports, !canUseDefaultRooms, policies, @@ -140,8 +143,9 @@ export default function useLastAccessedReportID(shouldOpenOnAdminRoom: boolean) activeWorkspaceID, policyMemberAccountIDs, )?.reportID; - }, [activeWorkspaceID, canUseDefaultRooms, shouldOpenOnAdminRoom]); - + console.timeEnd('findLastAccessedReport'); + return res; + }, [activeWorkspaceID, canUseDefaultRooms, shouldOpenOnAdminRoom, shouldRun]); // We need access to all the data from these Onyx.connect calls, but we don't want to re-render the consuming component // unless the derived value (lastAccessedReportID) changes. To address these, we'll wrap everything with useSyncExternalStore return useSyncExternalStore(addSubscriber, getSnapshot); diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 0b0b42b72336..e0c9a45fd1d3 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1183,13 +1183,17 @@ function findLastAccessedReport( reportsValues = filterReportsByPolicyIDAndMemberAccountIDs(reportsValues, policyMemberAccountIDs, policyID); } - console.time('findLastAccessedReport'); + console.time('sortReportsByLastRead'); + + // OLD SOLUTION + // let sortedReports = sortReportsByLastRead(reportsValues, reportMetadata); + + // NEW SOLUTION 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()); + console.timeEnd('sortReportsByLastRead'); let adminReport: OnyxEntry; if (openOnAdminRoom) { @@ -1241,6 +1245,7 @@ function findLastAccessedReport( } return adminReport ?? lastRead; + // return adminReport ?? sortedReports.at(-1); } /** diff --git a/src/pages/home/ReportScreen.tsx b/src/pages/home/ReportScreen.tsx index 539b46aa5b06..dda7be4c692a 100644 --- a/src/pages/home/ReportScreen.tsx +++ b/src/pages/home/ReportScreen.tsx @@ -138,6 +138,7 @@ function ReportScreen({ currentReportID = '', navigation, }: ReportScreenProps) { + console.time('ReportScreen - render'); const styles = useThemeStyles(); const {translate} = useLocalize(); const reportIDFromRoute = getReportID(route); @@ -170,7 +171,8 @@ function ReportScreen({ const permissions = useDeepCompareRef(reportOnyx?.permissions); // Check if there's a reportID in the route. If not, set it to the last accessed reportID - const lastAccessedReportID = useLastAccessedReportID(!!route.params.openOnAdminRoom); + const lastAccessedReportID = useLastAccessedReportID(!!route.params.openOnAdminRoom, !route.params.reportID); + useEffect(() => { // Don't update if there is a reportID in the params already if (route.params.reportID) { @@ -734,6 +736,8 @@ function ReportScreen({ ); } + console.timeEnd('ReportScreen - render'); + return (