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
import { recordTracksEvent } from '@automattic/calypso-analytics';
import config from '@automattic/calypso-config';
import { Popover } from '@automattic/components';
import { Button } from '@wordpress/components';
import { Icon, chevronDown, chevronUp, check } from '@wordpress/icons';
import clsx from 'clsx';
import { useState, useRef } from 'react';
import { OPTION_KEYS as SELECTED_OPTION_KEYS } from './stats-module-utm';

import './stats-module-utm-dropdown.scss';

interface UTMDropdownProps {
	className: string;
	buttonLabel: string;
	onSelect: ( key: string ) => void;
	selectOptions: Record< string, { selectLabel: string; isGrouped?: boolean } >;
	selected: keyof typeof SELECTED_OPTION_KEYS;
}

const BASE_CLASS_NAME = 'stats-utm-picker';

const UTMDropdown: React.FC< UTMDropdownProps > = ( {
	className,
	buttonLabel,
	onSelect,
	selectOptions,
	selected, // which option is indicated as selected
} ) => {
	const isOdysseyStats = config.isEnabled( 'is_running_in_jetpack_site' );
	const infoReferenceElement = useRef( null );
	const [ popoverOpened, togglePopoverOpened ] = useState( false );

	const togglePopoverVisibility = () => {
		const event_from = isOdysseyStats ? 'jetpack_odyssey' : 'calypso';

		if ( ! popoverOpened ) {
			// record an event for opening the date picker
			recordTracksEvent( `${ event_from }_stats_utm_dropdown_opened` );
		}

		togglePopoverOpened( ! popoverOpened );
	};

	const handleOptionSelection = ( key: string ) => {
		onSelect( key );

		// publish an event
		const event_from = isOdysseyStats ? 'jetpack_odyssey' : 'calypso';
		recordTracksEvent( `${ event_from }_stats_utm_dropdown_option_selected`, {
			option: key,
		} );

		togglePopoverOpened( false );
	};

	return (
		<div className={ clsx( className, BASE_CLASS_NAME ) }>
			<Button onClick={ togglePopoverVisibility } ref={ infoReferenceElement }>
				{ buttonLabel }
				{ popoverOpened ? (
					<Icon className="gridicon" icon={ chevronUp } />
				) : (
					<Icon className="gridicon" icon={ chevronDown } />
				) }
			</Button>
			<Popover
				position="bottom"
				context={ infoReferenceElement?.current }
				isVisible={ popoverOpened }
				className={ `${ BASE_CLASS_NAME }__popover-wrapper` }
				onClose={ () => togglePopoverOpened( false ) }
				hideArrow
			>
				<ul className={ `${ BASE_CLASS_NAME }__popover-list` }>
					{ Object.entries( selectOptions ).map( ( [ key, option ], index ) => {
						const isSelected = key === selected;

						return (
							<li
								key={ key }
								className={ clsx( `${ BASE_CLASS_NAME }__popover-list-item`, {
									[ 'is-selected' ]: isSelected,
									[ 'is-grouped' ]: option.isGrouped,
									[ 'is-not-grouped' ]: ! option.isGrouped,
								} ) }
							>
								<Button key={ index } onClick={ () => handleOptionSelection( key ) }>
									<span>{ option.selectLabel }</span>
									{ isSelected && <Icon icon={ check } /> }
								</Button>
							</li>
						);
					} ) }
				</ul>
			</Popover>
		</div>
	);
};

export default UTMDropdown;