File size: 3,041 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
import { Button } from '@wordpress/components';
import { Icon, check, lock } from '@wordpress/icons';
import clsx from 'clsx';
import moment, { Moment } from 'moment';
import PropTypes from 'prop-types';
import { DATE_FORMAT } from 'calypso/my-sites/stats/constants';
import { useShortcuts } from './use-shortcuts';

type MomentOrNull = Moment | null;

export interface DateRangePickerShortcut {
	id: string;
	label: string;
	startDate: string;
	endDate: string;
	period: string;
	statType?: string;
	isGated?: boolean;
}

interface DateRangePickerShortcutsProps {
	selectedShortcutId?: string;
	onClick: (
		newFromDate: moment.Moment,
		newToDate: moment.Moment,
		shortcut?: DateRangePickerShortcut
	) => void;
	onShortcutClick?: ( shortcut: DateRangePickerShortcut ) => void;
	locked?: boolean;
	startDate?: MomentOrNull;
	endDate?: MomentOrNull;
	shortcutList?: DateRangePickerShortcut[];
}

const DateRangePickerShortcuts = ( {
	selectedShortcutId,
	onClick,
	onShortcutClick, // Optional callback function for tracking shortcut clicks
	locked = false,
	startDate,
	endDate,
	shortcutList,
}: DateRangePickerShortcutsProps ) => {
	const normalizeDate = ( date: MomentOrNull ) => {
		return date ? date.startOf( 'day' ) : date;
	};

	// Normalize dates to start of day
	const normalizedStartDate = startDate ? normalizeDate( startDate ) : null;
	const normalizedEndDate = endDate ? normalizeDate( endDate ) : null;

	const { supportedShortcutList: defaultShortcutList, selectedShortcut } = useShortcuts( {
		chartStart: normalizedStartDate?.format( DATE_FORMAT ) ?? '',
		chartEnd: normalizedEndDate?.format( DATE_FORMAT ) ?? '',
		shortcutId: selectedShortcutId,
	} );

	shortcutList = shortcutList || defaultShortcutList;

	const handleClick = ( shortcut: DateRangePickerShortcut ) => {
		! locked &&
			shortcut.startDate &&
			shortcut.endDate &&
			onClick( moment( shortcut.startDate ), moment( shortcut.endDate ), shortcut );

		// Call the onShortcutClick if provided
		onShortcutClick && onShortcutClick( shortcut );
	};

	selectedShortcutId = selectedShortcutId || selectedShortcut?.id || 'custom_date_range';

	return (
		<div className="date-range-picker-shortcuts__inner">
			<ul className="date-range-picker-shortcuts__list">
				{ shortcutList.map( ( shortcut, idx ) => (
					<li
						className={ clsx( 'date-range-picker-shortcuts__shortcut', {
							'is-selected': shortcut.id === selectedShortcutId,
						} ) }
						key={ shortcut.id || idx }
					>
						<Button onClick={ () => handleClick( shortcut ) }>
							<span>{ shortcut.label }</span>
							{ shortcut.id === selectedShortcutId && <Icon icon={ check } /> }
							{ shortcut.isGated && <Icon icon={ lock } /> }
						</Button>
					</li>
				) ) }
			</ul>
		</div>
	);
};

DateRangePickerShortcuts.propTypes = {
	selectedShortcutId: PropTypes.string,
	onClick: PropTypes.func.isRequired,
	onShortcutClick: PropTypes.func,
	locked: PropTypes.bool,
	startDate: PropTypes.object,
	endDate: PropTypes.object,
};

export default DateRangePickerShortcuts;