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 (