File size: 3,464 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { Gridicon } from '@automattic/components';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';
import titlecase from 'to-title-case';
import { gaRecordEvent } from 'calypso/lib/analytics/ga';

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

	static propTypes = {
		siteId: PropTypes.number,
		path: PropTypes.string,
		title: PropTypes.string,
		titleLink: PropTypes.string,
		showInfo: PropTypes.bool,
		showModule: PropTypes.bool,
		isCollapsed: PropTypes.bool,
		showActions: PropTypes.bool,
		showCollapse: PropTypes.bool,
		onActionClick: PropTypes.func,
	};

	static defaultProps = {
		showCollapse: true,
		showModule: true,
		showActions: true,
		onActionClick: () => {},
	};

	toggleInfo = ( event ) => {
		event.stopPropagation();
		event.preventDefault();
		const { path, onActionClick, showInfo } = this.props;
		const gaEvent = showInfo ? 'Closed' : 'Opened';

		if ( path ) {
			gaRecordEvent( 'Stats', gaEvent + ' More Information Panel', titlecase( path ) );
		}

		onActionClick( {
			showInfo: ! showInfo,
		} );
	};

	toggleModule = ( event ) => {
		event.preventDefault();
		const { path, onActionClick, showModule } = this.props;
		const gaEvent = showModule ? 'Collapsed' : 'Expanded';

		if ( path ) {
			gaRecordEvent( 'Stats', gaEvent + ' Module', titlecase( path ) );
		}

		onActionClick( {
			showModule: ! showModule,
		} );
	};

	renderActions = () => {
		const { showCollapse, showInfo, showActions } = this.props;
		const infoIcon = showInfo ? 'info' : 'info-outline';

		if ( ! showActions ) {
			return null;
		}

		return (
			<ul className="module-header-actions">
				<li className="module-header-action toggle-info">
					{ /* eslint-disable-next-line jsx-a11y/anchor-is-valid */ }
					<a
						href="#"
						className="module-header-action-link"
						aria-label={ this.props.translate( 'Show or hide panel information', {
							context: 'Stats panel action',
						} ) }
						title={ this.props.translate( 'Show or hide panel information', {
							context: 'Stats panel action',
						} ) }
						onClick={ this.toggleInfo }
					>
						<Gridicon icon={ infoIcon } />
					</a>
				</li>
				{ showCollapse ? this.renderChevron() : null }
			</ul>
		);
	};

	renderChevron = () => {
		return (
			<li className="module-header-action toggle-services">
				{ /* eslint-disable-next-line jsx-a11y/anchor-is-valid */ }
				<a
					href="#"
					className="module-header-action-link"
					aria-label={ this.props.translate( 'Expand or collapse panel', {
						context: 'Stats panel action',
					} ) }
					title={ this.props.translate( 'Expand or collapse panel', {
						context: 'Stats panel action',
					} ) }
					onClick={ this.toggleModule }
				>
					<Gridicon icon="chevron-down" />
				</a>
			</li>
		);
	};

	renderTitle = () => {
		const { title, titleLink } = this.props;

		if ( titleLink ) {
			return (
				<h3 className="module-header-title">
					<a href={ titleLink } className="module-header__link">
						<span className="module-header__right-icon">
							<Gridicon icon="stats" />
						</span>
						{ title }
					</a>
				</h3>
			);
		}

		return <h3 className="module-header-title">{ title }</h3>;
	};

	render() {
		return (
			<div className="module-header">
				{ this.renderTitle() }
				{ this.renderActions() }
			</div>
		);
	}
}

export default localize( StatsModuleHeader );