File size: 2,616 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
import { SegmentedControl } from '@automattic/components';
import { useTranslate } from 'i18n-calypso';
import moment from 'moment';
import { useState } from 'react';
import './style.scss';
import { useDispatch } from 'calypso/state';
import { recordTracksEvent } from 'calypso/state/analytics/actions';

export const TIME_RANGE_OPTIONS = {
	'6-hours': '6-hours',
	'24-hours': '24-hours',
	'3-days': '3-days',
	'7-days': '7-days',
};

export function calculateTimeRange( selectedOption ) {
	const now = moment().unix();
	let start;
	let end;

	const bits = selectedOption.split( '-' );
	if ( bits.length === 2 && [ 'days', 'hours' ].includes( bits[ 1 ] ) ) {
		start = moment().subtract( parseInt( bits[ 0 ] ), bits[ 1 ] ).unix();
		end = now;
	} else {
		// Default to 24 hours in case of invalid selection
		start = moment().subtract( 24, 'hours' ).unix();
		end = now;
	}

	return { start, end };
}

export const TimeDateChartControls = ( { onTimeRangeChange } ) => {
	const translate = useTranslate();
	const dispatch = useDispatch();
	const [ selectedOption, setSelectedOption ] = useState( '24-hours' ); // Default selected option is '1' (24 hours)

	// Event handler to handle changes in the SegmentedControl selection
	const handleOptionClick = ( newSelectedOption ) => {
		dispatch(
			recordTracksEvent( 'calypso_site_monitoring_time_range_change', {
				time_range: newSelectedOption,
			} )
		);
		setSelectedOption( newSelectedOption );

		// Calculate the time range and pass it back to the parent component
		const timeRange = calculateTimeRange( newSelectedOption );
		onTimeRangeChange( timeRange );
	};

	const options = [
		{
			value: TIME_RANGE_OPTIONS[ '6-hours' ],
			label: translate( '6 hours' ),
		},
		{
			value: TIME_RANGE_OPTIONS[ '24-hours' ],
			label: translate( '24 hours' ),
		},
		{
			value: TIME_RANGE_OPTIONS[ '3-days' ],
			label: translate( '3 days' ),
		},
		{
			value: TIME_RANGE_OPTIONS[ '7-days' ],
			label: translate( '7 days' ),
		},
	];

	return (
		<div className="site-monitoring-time-range-picker__container">
			<div className="site-monitoring-time-range-picker__heading">
				{ translate( 'Time range' ) }
			</div>
			<SegmentedControl className="site-monitoring-time-range-picker__controls">
				{ options.map( ( option ) => {
					return (
						<SegmentedControl.Item
							key={ option.value }
							value={ option.value }
							selected={ selectedOption === option.value }
							onClick={ () => handleOptionClick( option.value ) }
						>
							{ option.label }
						</SegmentedControl.Item>
					);
				} ) }
			</SegmentedControl>
		</div>
	);
};