Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stats: Memoize gated intervals selector #99171

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;