File size: 3,038 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
import { recordTracksEvent } from '@automattic/calypso-analytics';
import config from '@automattic/calypso-config';
import { Icon, Button, ButtonGroup } from '@wordpress/components';
import { chartBar, trendingUp } from '@wordpress/icons';
import PropTypes from 'prop-types';
import { useDispatch } from 'react-redux';
import Legend from 'calypso/components/chart/legend';
import IntervalDropdown from 'calypso/components/stats-interval-dropdown';
import { useSelector } from 'calypso/state';
import { isJetpackSite } from 'calypso/state/sites/selectors';
import { toggleUpsellModal } from 'calypso/state/stats/paid-stats-upsell/actions';
import useIntervals from '../hooks/use-intervals';

const ChartHeader = ( {
	siteId,
	slug,
	period,
	queryParams,
	activeTab,
	activeLegend,
	availableLegend,
	onLegendClick,
	charts,
	chartType,
	onChartTypeChange,
} ) => {
	const intervals = useIntervals( siteId );
	const dispatch = useDispatch();
	const isSiteJetpackNotAtomic = useSelector( ( state ) => {
		return isJetpackSite( state, siteId, {
			treatAtomicAsJetpackSite: false,
		} );
	} );

	const isChartLibraryEnabled = config.isEnabled( 'stats/chart-library' );

	const onGatedHandler = ( events, source, statType ) => {
		// Stop the popup from showing for Jetpack sites.
		if ( isSiteJetpackNotAtomic ) {
			return;
		}

		events.forEach( ( event ) => recordTracksEvent( event.name, event.params ) );
		dispatch( toggleUpsellModal( siteId, statType ) );
	};

	const handleChartTypeChange = ( newType ) => {
		onChartTypeChange( newType );
	};

	return (
		<div className="stats-chart-tabs__header">
			<div className="stats-chart-tabs__header-title">{ activeTab?.label }</div>
			<Legend
				activeCharts={ activeLegend }
				activeTab={ activeTab }
				availableCharts={ availableLegend }
				clickHandler={ onLegendClick }
				tabs={ charts }
			/>
			<IntervalDropdown
				slug={ slug }
				period={ period }
				queryParams={ queryParams }
				intervals={ intervals }
				onGatedHandler={ onGatedHandler }
			/>
			{ isChartLibraryEnabled && (
				<ButtonGroup className="stats-chart-tabs__type-toggle">
					<Button
						icon={ <Icon icon={ trendingUp } /> }
						size="compact"
						variant={ chartType === 'line' ? 'primary' : 'secondary' }
						onClick={ () => handleChartTypeChange( 'line' ) }
						aria-label="Switch to line chart"
					/>
					<Button
						icon={ <Icon icon={ chartBar } /> }
						size="compact"
						variant={ chartType === 'bar' ? 'primary' : 'secondary' }
						onClick={ () => handleChartTypeChange( 'bar' ) }
						aria-label="Switch to bar chart"
					/>
				</ButtonGroup>
			) }
		</div>
	);
};

ChartHeader.propTypes = {
	activeTab: PropTypes.object,
	controls: PropTypes.node,
	activeLegend: PropTypes.arrayOf( PropTypes.string ),
	availableLegend: PropTypes.arrayOf( PropTypes.string ),
	onLegendClick: PropTypes.func,
	charts: PropTypes.array,
	chartType: PropTypes.oneOf( [ 'line', 'bar' ] ).isRequired,
	onChartTypeChange: PropTypes.func.isRequired,
};

export default ChartHeader;