Skip to content

Commit

Permalink
Stats: Memoize moment site timezone selector (#99170)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla authored Feb 1, 2025
1 parent d73d080 commit 7857f44
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions client/my-sites/stats/hooks/use-moment-site-zone.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import { createSelector } from '@automattic/state-utils';
import i18n from 'i18n-calypso';
import moment from 'moment';
import { useSelector } from 'calypso/state';
import { getSiteOption } from 'calypso/state/sites/selectors';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';

export function getMomentSiteZone( state: object, siteId: number | null ) {
let localeSlug = i18n.getLocaleSlug();
if ( localeSlug === null ) {
localeSlug = 'en';
}
export const getMomentSiteZone = createSelector(
( state: object, siteId: number | null ) => {
let localeSlug = i18n.getLocaleSlug();
if ( localeSlug === null ) {
localeSlug = 'en';
}

const localizedMoment = moment().locale( localeSlug );
const localizedMoment = moment().locale( localeSlug );

const gmtOffset = getSiteOption( state, siteId, 'gmt_offset' ) as number;
if ( Number.isFinite( gmtOffset ) ) {
return localizedMoment.utcOffset( gmtOffset );
}
const gmtOffset = getSiteOption( state, siteId, 'gmt_offset' ) as number;
if ( Number.isFinite( gmtOffset ) ) {
return localizedMoment.utcOffset( gmtOffset );
}

// Falls back to the browser's local timezone if no GMT offset is found
return localizedMoment;
}
// Falls back to the browser's local timezone if no GMT offset is found
return localizedMoment;
},
[ ( state, siteId ) => getSiteOption( state, siteId, 'gmt_offset' ), () => i18n.getLocaleSlug() ]
);

/**
* Get moment object based on site timezone.
Expand Down

0 comments on commit 7857f44

Please sign in to comment.