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: Store date range shortcut and chart period by site #98303

Merged
merged 2 commits into from
Jan 14, 2025
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
11 changes: 10 additions & 1 deletion client/components/stats-date-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import qs from 'qs';
import { findShortcutForRange } from 'calypso/components/date-range/use-shortcuts';
import { useLocalizedMoment } from 'calypso/components/localized-moment';
import { recordTracksEvent } from 'calypso/lib/analytics/tracks';
import { useSelector } from 'calypso/state';
import getSiteId from 'calypso/state/sites/selectors/get-site-id';
import DateControl from '../date-control';
import { DateRangePickerShortcut } from '../date-range/shortcuts';

Expand Down Expand Up @@ -89,6 +91,7 @@ const StatsDateControl = ( {

const moment = useLocalizedMoment();
const isOdysseyStats = config.isEnabled( 'is_running_in_jetpack_site' );
const siteId = useSelector( ( state ) => getSiteId( state, slug ) );

/**
* Remove start date from query params if it's out of range.
Expand Down Expand Up @@ -156,7 +159,13 @@ const StatsDateControl = ( {
} );

if ( appliedShortcut && appliedShortcut.id ) {
localStorage.setItem( 'jetpack_stats_stored_date_range_shortcut_id', appliedShortcut.id );
localStorage.setItem(
`jetpack_stats_stored_date_range_shortcut_id_${ siteId }`,
appliedShortcut.id
);
// Remove legacy item key.
localStorage.removeItem( 'jetpack_stats_stored_date_range_shortcut_id' );

// Apply the period from the found shortcut.
period = appliedShortcut.period;
}
Expand Down
10 changes: 8 additions & 2 deletions client/components/stats-interval-dropdown/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import clsx from 'clsx';
import { capitalize } from 'lodash';
import qs from 'qs';
import { useRef } from 'react';
import './style.scss';
import useOutsideClickCallback from 'calypso/lib/use-outside-click-callback';
import { useSelector } from 'calypso/state';
import getSiteId from 'calypso/state/sites/selectors/get-site-id';
import './style.scss';

const StatsIntervalDropdownListing = ( {
selected,
Expand Down Expand Up @@ -80,6 +82,8 @@ const IntervalDropdown = ( { slug, period, queryParams, intervals, onGatedHandle
// New interval listing that preserves date range.
// TODO: Figure out how to dismiss on select.

const siteId = useSelector( ( state ) => getSiteId( state, slug ) );

function generateNewLink( newPeriod ) {
const newRangeQuery = qs.stringify( Object.assign( {}, queryParams, {} ), {
addQueryPrefix: true,
Expand All @@ -101,7 +105,9 @@ const IntervalDropdown = ( { slug, period, queryParams, intervals, onGatedHandle
return;
}

localStorage.setItem( 'jetpack_stats_stored_period', interval );
localStorage.setItem( `jetpack_stats_stored_period_${ siteId }`, interval );
// Remove legacy item key.
localStorage.removeItem( 'jetpack_stats_stored_period' );

page( generateNewLink( interval ) );
}
Expand Down
12 changes: 8 additions & 4 deletions client/my-sites/stats/site.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,11 @@ function StatsBody( { siteId, chartTab = 'views', date, context, isInternal, ...
const getValidDateOrNullFromInput = ( inputDate, inputKey ) => {
// Use the stored chartStart and chartEnd if they are valid when the inputDate is absent.
if ( inputDate === undefined ) {
const appliedShortcutId = localStorage.getItem(
'jetpack_stats_stored_date_range_shortcut_id'
);
const appliedShortcutId =
localStorage.getItem( `jetpack_stats_stored_date_range_shortcut_id_${ siteId }` ) ||
// Fallback for the compatibility.
localStorage.getItem( 'jetpack_stats_stored_date_range_shortcut_id' );

const appliedShortcut = supportedShortcutList.find(
( shortcut ) => shortcut.id === appliedShortcutId
);
Expand Down Expand Up @@ -340,7 +342,9 @@ function StatsBody( { siteId, chartTab = 'views', date, context, isInternal, ...

useEffect( () => {
// Use the stored period if it's different from the current period.
const storedPeriod = localStorage.getItem( 'jetpack_stats_stored_period' );
const storedPeriod =
localStorage.getItem( `jetpack_stats_stored_period_${ siteId }` ) ||
localStorage.getItem( 'jetpack_stats_stored_period' );
if (
hasSiteLoadedFeatures &&
! shouldForceDefaultPeriod &&
Expand Down
Loading