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: datetime localization improvements #97724

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 1 addition & 5 deletions client/my-sites/stats/controller.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import moment from 'moment';
import AsyncLoad from 'calypso/components/async-load';
import { bumpStat } from 'calypso/lib/analytics/mc';
import { getSiteFragment, getStatsDefaultSitePage } from 'calypso/lib/route';
import { getMomentSiteZone } from 'calypso/my-sites/stats/hooks/use-moment-site-zone';
import { getSite, getSiteOption } from 'calypso/state/sites/selectors';
import { setNextLayoutFocus } from 'calypso/state/ui/layout-focus/actions';
import { getCurrentLayoutFocus } from 'calypso/state/ui/layout-focus/selectors';
Expand Down Expand Up @@ -65,11 +66,6 @@ function getWordAdsFilters( siteId ) {
];
}

function getMomentSiteZone( state, siteId ) {
const gmtOffset = getSiteOption( state, siteId, 'gmt_offset' );
return moment().utcOffset( Number.isFinite( gmtOffset ) ? gmtOffset : 0 );
}

export function redirectToActivity( context ) {
if ( context.params.site ) {
page.redirect( '/activity-log/' + context.params.site );
Expand Down
27 changes: 17 additions & 10 deletions client/my-sites/stats/hooks/use-moment-site-zone.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import moment, { Moment } from 'moment';
import { useLocalizedMoment } from 'calypso/components/localized-moment';
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,
localizedMoment?: () => Moment
) {
export function getMomentSiteZone( state: object, siteId: number | null ) {
let localeSlug = i18n.getLocaleSlug();
if ( localeSlug === null ) {
localeSlug = 'en';
}

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

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

// Falls back to the browser's local timezone if no GMT offset is found
return localizedMoment;
}

/**
* Get moment object based on site timezone.
*/
export default function useMomentSiteZone() {
const siteId = useSelector( getSelectedSiteId );
const localizedMoment = useLocalizedMoment();
return useSelector( ( state ) => getMomentSiteZone( state, siteId, localizedMoment ) );
return useSelector( ( state ) => getMomentSiteZone( state, siteId ) );
}
Loading