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: Update to fix header consistency #96661

Merged
merged 3 commits into from
Nov 24, 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
1 change: 1 addition & 0 deletions client/my-sites/stats/site.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ class StatsSite extends Component {
statsType="statsTopPosts"
showQueryDate
isShort
dateRange={ customChartRange }
/>
</StatsPeriodNavigation>
</StatsPeriodHeader>
Expand Down
44 changes: 40 additions & 4 deletions client/my-sites/stats/stats-date-picker/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,52 @@ class StatsDatePicker extends Component {
}

dateForDisplay() {
const { date, moment, period, translate, isShort } = this.props;
const { date, moment, period, translate, isShort, dateRange } = this.props;
const weekPeriodFormat = isShort ? 'll' : 'LL';

// If we have chartStart/chartEnd in dateRange, use those for the date range
if ( dateRange?.chartStart && dateRange?.chartEnd ) {
const startDate = moment( dateRange.chartStart );
const endDate = moment( dateRange.chartEnd );

// If it's the same day, show single date
if ( startDate.isSame( endDate, 'day' ) ) {
return startDate.format( 'LL' );
}

// If it's a full month
if (
startDate.isSame( startDate.clone().startOf( 'month' ), 'day' ) &&
endDate.isSame( endDate.clone().endOf( 'month' ), 'day' ) &&
startDate.isSame( endDate, 'month' )
) {
return startDate.format( 'MMMM YYYY' );
}

// If it's a full year
if (
startDate.isSame( startDate.clone().startOf( 'year' ), 'day' ) &&
endDate.isSame( endDate.clone().endOf( 'year' ), 'day' ) &&
startDate.isSame( endDate, 'year' )
) {
return startDate.format( 'YYYY' );
}

// Default to date range
return translate( '%(startDate)s - %(endDate)s', {
context: 'Date range for which stats are being displayed',
args: {
startDate: startDate.format( weekPeriodFormat ),
endDate: endDate.format( weekPeriodFormat ),
},
} );
}

// Ensure we have a moment instance here to work with.
const momentDate = moment.isMoment( date ) ? date : moment( date );
const localizedDate = moment( momentDate.format( 'YYYY-MM-DD' ) );
let formattedDate;

// ll is a date localized with abbreviated Month by momentjs
const weekPeriodFormat = isShort ? 'll' : 'LL';

switch ( period ) {
case 'week':
formattedDate = translate( '%(startDate)s - %(endDate)s', {
Expand Down
Loading