Skip to content

Commit

Permalink
Ad2: Calculate useLastAccessedReportID only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
kosmydel committed Jul 5, 2024
1 parent 5352992 commit 41dff74
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
14 changes: 9 additions & 5 deletions src/hooks/useLastAccessedReportID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down
11 changes: 8 additions & 3 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Report>;
if (openOnAdminRoom) {
Expand Down Expand Up @@ -1241,6 +1245,7 @@ function findLastAccessedReport(
}

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

/**
Expand Down
6 changes: 5 additions & 1 deletion src/pages/home/ReportScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ function ReportScreen({
currentReportID = '',
navigation,
}: ReportScreenProps) {
console.time('ReportScreen - render');
const styles = useThemeStyles();
const {translate} = useLocalize();
const reportIDFromRoute = getReportID(route);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -734,6 +736,8 @@ function ReportScreen({
);
}

console.timeEnd('ReportScreen - render');

return (
<ActionListContext.Provider value={actionListValue}>
<ReactionListContext.Provider value={reactionListRef}>
Expand Down

0 comments on commit 41dff74

Please sign in to comment.