forked from woocommerce/google-listings-and-ads
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetric-number.js
More file actions
151 lines (142 loc) · 4.18 KB
/
metric-number.js
File metadata and controls
151 lines (142 loc) · 4.18 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* External dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import {
useMemo,
createInterpolateElement,
renderToString,
} from '@wordpress/element';
import { SummaryNumber } from '@woocommerce/components';
import GridiconInfoOutline from 'gridicons/dist/info-outline';
/**
* Internal dependencies
*/
import './metric-number.scss';
import AppTooltip from '.~/components/app-tooltip';
import TrackableLink from '.~/components/trackable-link';
import { MISSING_FREE_LISTINGS_DATA } from '.~/data/utils';
const googleMCReportingDashboardURL =
'https://merchants.google.com/mc/reporting/dashboard';
/**
* SummeryNumber annotated about missing data.
* To be used in ProgramsReport.
*
* Renders SummaryNumber with label that contains an info icon with a tootip
* informing about missing data for some metrics.
*
* @param {Object} props
* @param {import('./index').Metric} props.metric Metrics label and formatting characteristics.
* @param {string} [props.href] An internal link to the report focused on this metric.
* @param {boolean} [props.selected] Whether show a highlight style on this metric.
* @param {Function} [props.onLinkClickCallback] A function to be called after a SummaryNumber, rendered as a link, is clicked.
* @param {import('.~/data/utils').PerformanceMetrics} props.data Data as get from API.
*
* @return {SummaryNumber} Filled SummaryNumber.
*
* @fires gla_google_mc_link_click with `{ context: 'reports' }`
*/
const MetricNumber = ( {
href,
selected,
onLinkClickCallback,
metric,
data: { value, prevValue, delta, missingFreeListingsData },
} ) => {
const valueProps = useMemo( () => {
return {
value: metric.formatFn( value ),
prevValue: metric.formatFn( prevValue ),
};
}, [ metric, value, prevValue ] );
let markedLabel = metric.label;
const infos = [];
const ariaInfos = [];
// Until ~Q4 2021, metrics for all programs, may lack data for free listings.
// And Free Listings API may not respond with data.
if ( missingFreeListingsData !== MISSING_FREE_LISTINGS_DATA.NONE ) {
const text = __(
'This data is currently available for paid campaigns only.',
'google-listings-and-ads'
);
infos.push( text );
ariaInfos.push( text );
}
if ( missingFreeListingsData === MISSING_FREE_LISTINGS_DATA.FOR_REQUEST ) {
const text = __(
'Please try again later, or go to <googleMerchantCenterLink /> to track your performance for Google Free Listings.',
'google-listings-and-ads'
);
infos.push(
createInterpolateElement( text, {
googleMerchantCenterLink: (
<TrackableLink
eventName="gla_google_mc_link_click"
eventProps={ {
context: 'reports',
href: googleMCReportingDashboardURL,
} }
type="external"
target="_blank"
href={ googleMCReportingDashboardURL }
// Stop propagation to avoid triggering the <SummaryNumber> `href` prop
// that redirects the browser to incorrect pages.
onClick={ ( e ) => e.stopPropagation() }
>
{ __(
'Google Merchant Center',
'google-listings-and-ads'
) }
</TrackableLink>
),
} )
);
// `aria-label` prop only accepts a pure text.
const textElement = createInterpolateElement( text, {
googleMerchantCenterLink: (
<>
{ sprintf(
// translators: %s: link to Google Merchant Center.
__(
'Google Merchant Center (%s)',
'google-listings-and-ads'
),
googleMCReportingDashboardURL
) }
</>
),
} );
ariaInfos.push( renderToString( textElement ) );
}
if ( infos.length > 0 ) {
const infoElements = infos.map( ( info, index ) => (
<div className="gla-reports__metric-info" key={ index }>
{ info }
</div>
) );
markedLabel = (
<div className="gla-reports__metric-label">
{ metric.label }
<AppTooltip text={ infoElements }>
<GridiconInfoOutline
className="gla-reports__metric-infoicon"
role="img"
aria-label={ ariaInfos.join( ' ' ) }
size={ 16 }
/>
</AppTooltip>
</div>
);
}
return (
<SummaryNumber
label={ markedLabel }
href={ href }
selected={ selected }
delta={ delta }
onLinkClickCallback={ onLinkClickCallback }
{ ...valueProps }
/>
);
};
export default MetricNumber;