File size: 2,891 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { localize } from 'i18n-calypso';
import { values } from 'lodash';
import moment from 'moment';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import QuerySiteStats from 'calypso/components/data/query-site-stats';
import { withLocalizedMoment } from 'calypso/components/localized-moment';
import compareProps from 'calypso/lib/compare-props';
import { getSiteOption } from 'calypso/state/sites/selectors';
import { getSiteStatsPostStreakData } from 'calypso/state/stats/lists/selectors';
import StatsHeatMapLegend from '../../stats-heap-map/legend';
import Month from './month';

import './style.scss';

class PostingActivitySection extends Component {
	static displayName = 'PostingActivitySection';

	static propTypes = {
		siteId: PropTypes.number,
		query: PropTypes.object,
	};

	getMonthComponents = () => {
		const { streakData } = this.props;
		// Compute maximum per-day post count from the streakData. It's used to scale the chart.
		const maxPosts = Math.max( ...values( streakData ) );
		const months = [];

		for ( let i = 11; i >= 0; i-- ) {
			const startDate = this.props.moment().subtract( i, 'months' ).startOf( 'month' );
			months.push(
				<Month key={ i } startDate={ startDate } streakData={ streakData } max={ maxPosts } />
			);
		}

		return months;
	};

	render() {
		const { siteId, query, translate } = this.props;

		/* eslint-disable jsx-a11y/click-events-have-key-events, wpcalypso/jsx-classname-namespace */
		return (
			<div className="post-trends">
				{ siteId && <QuerySiteStats siteId={ siteId } statType="statsStreak" query={ query } /> }

				<div className="post-trends__heading">
					<h3 className="post-trends__title">{ translate( 'Posting activity' ) }</h3>
				</div>
				<div ref={ this.wrapperRef } className="post-trends__wrapper">
					<div ref={ this.yearRef } className="post-trends__year">
						{ this.getMonthComponents() }
					</div>

					<StatsHeatMapLegend
						labelFewer={ translate( 'Fewer Posts', {
							context: 'Legend label in stats post trends visualization',
						} ) }
						labelMore={ translate( 'More Posts', {
							context: 'Legend label in stats post trends visualization',
						} ) }
					/>
				</div>
			</div>
		);
	}
}

const mapStateToProps = ( state, { siteId } ) => {
	const query = {
		startDate: moment()
			.locale( 'en' )
			.subtract( 1, 'year' )
			.startOf( 'month' )
			.format( 'YYYY-MM-DD' ),
		endDate: moment().locale( 'en' ).endOf( 'month' ).format( 'YYYY-MM-DD' ),
		gmtOffset: getSiteOption( state, siteId, 'gmt_offset' ),
		max: 3000,
	};

	return {
		streakData: getSiteStatsPostStreakData( state, siteId, query ),
		query,
	};
};

export default connect( mapStateToProps, null, null, {
	areStatePropsEqual: compareProps( { deep: [ 'query' ] } ),
} )( localize( withLocalizedMoment( PostingActivitySection ) ) );