File size: 3,212 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
105
106
107
108
109
import { Button } from '@wordpress/components';
import clsx from 'clsx';
import { useTranslate } from 'i18n-calypso';
import { Metrics } from 'calypso/data/site-profiler/types';
import { recordTracksEvent } from 'calypso/lib/analytics/tracks';
import { Valuation } from 'calypso/performance-profiler/types/performance-metrics';
import { profilerVersion } from 'calypso/performance-profiler/utils/profiler-version';

import './style.scss';

type StatusSectionProps = {
	value: Valuation;
	activeTab: Metrics | null;
	recommendationsQuantity?: number;
	recommendationsRef?: React.RefObject< HTMLDivElement > | null;
	onRecommendationsFilterChange?: ( filter: string ) => void;
};

export const StatusSection = ( props: StatusSectionProps ) => {
	const translate = useTranslate();

	const {
		value,
		recommendationsQuantity,
		recommendationsRef,
		activeTab,
		onRecommendationsFilterChange,
	} = props;
	const getStatus = ( value: Valuation ) => {
		if ( value === 'bad' ) {
			return 'poor';
		} else if ( value === 'needsImprovement' ) {
			return 'neutral';
		}
		return 'good';
	};
	const status = getStatus( value );
	const statusText = {
		poor: translate( 'Poor' ),
		neutral: translate( 'Needs improvement' ),
		good: translate( 'Excellent' ),
	}[ status ];

	const recordRecommendationsClickEvent = ( filter: string ) =>
		recordTracksEvent( 'calypso_performance_profiler_recommendations_link_click', {
			filter,
			version: profilerVersion(),
		} );

	return (
		<div className="status-section">
			<div className={ clsx( 'status-badge', { [ status ]: true } ) }>{ statusText }</div>
			{ !! recommendationsQuantity && (
				<div className="recommendations-text">
					{ activeTab === 'overall'
						? translate( '{{a}}View all recommendations{{/a}}', {
								components: {
									a: (
										/* eslint-disable-next-line jsx-a11y/anchor-is-valid */
										<Button
											variant="link"
											className="button"
											role="button"
											tabIndex={ 0 }
											onClick={ () => {
												recordRecommendationsClickEvent( 'all' );
												onRecommendationsFilterChange?.( '' );
												recommendationsRef?.current?.scrollIntoView( {
													behavior: 'smooth',
													block: 'start',
												} );
											} }
										/>
									),
								},
						  } )
						: translate(
								'{{a}}View %(quantity)d recommendation{{/a}}',
								'{{a}}View %(quantity)d recommendations{{/a}}',
								{
									count: recommendationsQuantity,
									args: { quantity: recommendationsQuantity },
									components: {
										a: (
											/* eslint-disable-next-line jsx-a11y/anchor-is-valid */
											<Button
												variant="link"
												className="button"
												role="button"
												tabIndex={ 0 }
												onClick={ () => {
													recordRecommendationsClickEvent( activeTab ?? '' );
													onRecommendationsFilterChange?.( activeTab ?? '' );
													recommendationsRef?.current?.scrollIntoView( {
														behavior: 'smooth',
														block: 'start',
													} );
												} }
											/>
										),
									},
								}
						  ) }
				</div>
			) }
		</div>
	);
};