Skip to content

Commit

Permalink
Stats: Update to fix header consistency (#96661)
Browse files Browse the repository at this point in the history
* pass chartRange to DatePicker

* use chartStart/End ranges for the date range when we have them

* add checks for same day, full month, or full year
  • Loading branch information
annacmc authored Nov 24, 2024
1 parent 08c4bf7 commit 6ea0f58
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
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

0 comments on commit 6ea0f58

Please sign in to comment.