Skip to content

Commit

Permalink
Calculate useLastAccessedReportID only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
kosmydel committed Jul 8, 2024
1 parent 51dd814 commit 23574ab
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/hooks/useLastAccessedReportID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,15 @@ function addSubscriber(subscriber: () => void) {

/**
* Get the last accessed reportID.
* @param skip - Whether to skip the calculation of last accessed report ID
*/
export default function useLastAccessedReportID(shouldOpenOnAdminRoom: boolean) {
export default function useLastAccessedReportID(shouldOpenOnAdminRoom: boolean, skip: boolean) {
const {canUseDefaultRooms} = usePermissions();
const {activeWorkspaceID} = useActiveWorkspace();

const getSnapshot = useCallback(() => {
if (skip) {
return undefined;
}
const policyMemberAccountIDs = getPolicyEmployeeListByIdWithoutCurrentUser(policies, activeWorkspaceID, accountID);
return ReportUtils.findLastAccessedReport(
reports,
Expand All @@ -140,8 +143,7 @@ export default function useLastAccessedReportID(shouldOpenOnAdminRoom: boolean)
activeWorkspaceID,
policyMemberAccountIDs,
)?.reportID;
}, [activeWorkspaceID, canUseDefaultRooms, shouldOpenOnAdminRoom]);

}, [activeWorkspaceID, canUseDefaultRooms, shouldOpenOnAdminRoom, skip]);
// 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
3 changes: 2 additions & 1 deletion src/pages/home/ReportScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,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

0 comments on commit 23574ab

Please sign in to comment.