File size: 2,851 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
import { Button, Tooltip } from '@wordpress/components';
import { Icon, calendar } from '@wordpress/icons';
import { Moment } from 'moment';
import { RefObject } from 'react';
import DateRange from 'calypso/components/date-range';
import { useLocalizedMoment } from 'calypso/components/localized-moment';
import useMomentSiteZone from 'calypso/my-sites/stats/hooks/use-moment-site-zone';
import { DateControlProps } from './types';
import './style.scss';

const COMPONENT_CLASS_NAME = 'date-control';

const DateControl = ( {
	onApplyButtonClick,
	onShortcutClick,
	onDateControlClick,
	tooltip,
	dateRange,
	overlay,
	shortcutList,
}: DateControlProps ) => {
	const moment = useLocalizedMoment();
	const siteToday = useMomentSiteZone();

	const getButtonLabel = () => {
		const localizedStartDate = moment( dateRange.chartStart );
		const localizedEndDate = moment( dateRange.chartEnd );

		// If it's the same day, show single date.
		if ( localizedStartDate.isSame( localizedEndDate, 'day' ) ) {
			return localizedStartDate.format( 'LL' );
		}

		// Only show year for the second date.
		if (
			localizedStartDate.year() === localizedEndDate.year() &&
			localizedStartDate.isSame( moment(), 'year' )
		) {
			return `${ localizedStartDate.format( 'MMM D' ) } - ${ localizedEndDate.format(
				'MMM D, YYYY'
			) }`;
		}

		return `${ localizedStartDate.format( 'll' ) } - ${ localizedEndDate.format( 'll' ) }`;
	};

	return (
		<div className={ COMPONENT_CLASS_NAME }>
			<DateRange
				selectedStartDate={ moment( dateRange.chartStart ) }
				selectedEndDate={ moment( dateRange.chartEnd ) }
				selectedShortcutId={ dateRange.shortcutId }
				lastSelectableDate={ siteToday }
				firstSelectableDate={ moment( '2010-01-01' ) }
				onDateCommit={ ( startDate: Moment, endDate: Moment, selectedShortcutId: string ) =>
					startDate && endDate && onApplyButtonClick( startDate, endDate, selectedShortcutId )
				}
				renderTrigger={ ( {
					onTriggerClick,
					buttonRef,
				}: {
					onTriggerClick: () => void;
					buttonRef: RefObject< typeof Button >;
				} ) => {
					return (
						<Tooltip text={ tooltip } placement="bottom-end">
							<Button
								onClick={ () => {
									onDateControlClick?.();
									onTriggerClick();
								} }
								ref={ buttonRef }
								__next40pxDefaultSize
							>
								{ getButtonLabel() }
								<Icon className="gridicon" icon={ calendar } />
							</Button>
						</Tooltip>
					);
				} }
				rootClass="date-control-picker"
				overlay={ overlay }
				displayShortcuts
				useArrowNavigation
				customTitle="Date Range"
				focusedMonth={ moment( dateRange.chartEnd ).toDate() }
				shortcutList={ shortcutList }
				onShortcutClick={ onShortcutClick }
				trackExternalDateChanges
			/>
		</div>
	);
};

export { DateControl as default, DateControl, COMPONENT_CLASS_NAME };