Skip to content

Commit

Permalink
Stats: Memoize gated intervals selector
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla committed Jan 31, 2025
1 parent 167214b commit 155ad9f
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions client/my-sites/stats/hooks/use-intervals.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createSelector } from '@automattic/state-utils';
import { translate } from 'i18n-calypso';
import {
STATS_FEATURE_INTERVAL_DROPDOWN_DAY,
Expand Down Expand Up @@ -43,8 +44,8 @@ const intervals = {
},
};

function useIntervals( siteId: number | null ): IntervalsType {
const gatedIntervals = useSelector( ( state ) => {
const getGatedIntervals = createSelector(
( state, siteId ) => {
return Object.keys( intervals ).reduce( ( acc, key ) => {
const interval = intervals[ key ];

Expand All @@ -56,9 +57,16 @@ function useIntervals( siteId: number | null ): IntervalsType {
},
};
}, {} );
} );
},
Object.values( intervals ).map(
( { statType } ) =>
( state: object, siteId ) =>
shouldGateStats( state, siteId, statType )
)
);

return gatedIntervals;
function useIntervals( siteId: number | null ): IntervalsType {
return useSelector( ( state ) => getGatedIntervals( state, siteId ) );
}

export default useIntervals;

0 comments on commit 155ad9f

Please sign in to comment.