-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathstats-locations.tsx
More file actions
426 lines (376 loc) · 12.9 KB
/
Copy pathstats-locations.tsx
File metadata and controls
426 lines (376 loc) · 12.9 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import page from '@automattic/calypso-router';
import { SimplifiedSegmentedControl, StatsCard } from '@automattic/components';
import { mapMarker } from '@wordpress/icons';
import { useTranslate } from 'i18n-calypso';
import { useState, useEffect, useMemo } from 'react';
import QuerySiteStats from 'calypso/components/data/query-site-stats';
import InlineSupportLink from 'calypso/components/inline-support-link';
import useLocationViewsQuery, {
StatsLocationViewsData,
} from 'calypso/my-sites/stats/hooks/use-location-views-query';
import { useShouldGateStats } from 'calypso/my-sites/stats/hooks/use-should-gate-stats';
import StatsCardUpsell from 'calypso/my-sites/stats/stats-card-upsell';
import StatsListCard from 'calypso/my-sites/stats/stats-list/stats-list-card';
import StatsModulePlaceholder from 'calypso/my-sites/stats/stats-module/placeholder';
import {
getPathWithUpdatedQueryString,
trackStatsAnalyticsEvent,
} from 'calypso/my-sites/stats/utils';
import { useDispatch, useSelector } from 'calypso/state';
import { isJetpackSite } from 'calypso/state/sites/selectors';
import getEnvStatsFeatureSupportChecks from 'calypso/state/sites/selectors/get-env-stats-feature-supports';
import { receiveSiteStats } from 'calypso/state/stats/lists/actions';
import { getSiteStatsNormalizedData } from 'calypso/state/stats/lists/selectors';
import { normalizers } from 'calypso/state/stats/lists/utils';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';
import EmptyModuleCard from '../../../components/empty-module-card/empty-module-card';
import { STAT_TYPE_COUNTRY_VIEWS } from '../../../constants';
import Geochart from '../../../geochart';
import StatsCardUpdateJetpackVersion from '../../../stats-card-upsell/stats-card-update-jetpack-version';
import StatsCardSkeleton from '../shared/stats-card-skeleton';
import StatsInfoArea from '../shared/stats-info-area';
import { StatsDefaultModuleProps, StatsQueryType } from '../types';
import CountryFilter from './country-filter';
import sampleLocations from './sample-locations';
import { OPTION_KEYS, UrlGeoMode, GEO_MODES } from './types';
import useOptionLabels from './use-option-labels';
import './style.scss';
type SelectOptionType = {
label: string;
value: string;
};
interface StatsModuleLocationsProps extends StatsDefaultModuleProps {
initialGeoMode?: string;
query: StatsQueryType & { geoMode?: UrlGeoMode };
}
const StatsLocations: React.FC< StatsModuleLocationsProps > = ( {
initialGeoMode,
query,
summaryUrl,
summary = false,
listItemClassName,
} ) => {
const dispatch = useDispatch();
const translate = useTranslate();
const siteId = useSelector( getSelectedSiteId ) as number;
const statType = STAT_TYPE_COUNTRY_VIEWS;
const isSiteJetpackNotAtomic = useSelector( ( state ) =>
isJetpackSite( state, siteId, { treatAtomicAsJetpackSite: false } )
);
const supportContext = isSiteJetpackNotAtomic ? 'stats-locations-jetpack' : 'stats-locations';
// selectOption is in plural form i.e. 'countries'! Possible something to unify in the future.
const appliedGeoModeFromUrl = useMemo( () => {
const urlGeoMode = query.geoMode ?? initialGeoMode;
return urlGeoMode && urlGeoMode in GEO_MODES ? urlGeoMode : OPTION_KEYS.COUNTRIES;
}, [ query.geoMode, initialGeoMode ] );
const [ countryFilter, setCountryFilter ] = useState< string | null >( null );
// Set the state locally to avoid a page being reloaded by URL changes.
const [ selectedLocalOption, setSelectedLocalOption ] = useState( () => {
return appliedGeoModeFromUrl;
} );
const selectedOption = useMemo( () => {
if ( summary ) {
return appliedGeoModeFromUrl;
}
return selectedLocalOption;
}, [ summary, appliedGeoModeFromUrl, selectedLocalOption ] );
const optionLabels = useOptionLabels();
// Use StatsModule to display paywall upsell.
const shouldGateStatsModule = useShouldGateStats( statType );
const shouldGateTab = useShouldGateStats( optionLabels[ selectedOption ].feature );
const shouldGate = shouldGateStatsModule || shouldGateTab;
// Mapping plural to singular form where all other places are using.
const geoMode = GEO_MODES[ selectedOption ];
const title = translate( 'Locations' );
const { supportsLocationsStats: supportsLocationsStatsFeature } = useSelector( ( state ) =>
getEnvStatsFeatureSupportChecks( state, siteId )
);
// Main location data query
const {
data: locationsViewsData,
isLoading: isRequestingData,
isError,
} = useLocationViewsQuery( siteId, geoMode, query, countryFilter, {
enabled: ! shouldGate && supportsLocationsStatsFeature,
} );
const normalizedLocationsViewsData = useMemo( () => {
if ( isRequestingData || ! locationsViewsData ) {
return [];
}
const normalizedStats = normalizers.statsCountryViews(
locationsViewsData as StatsLocationViewsData,
query
);
if ( ! Array.isArray( normalizedStats ) ) {
return [];
}
return query?.max ? normalizedStats.slice( 0, query.max ) : normalizedStats;
}, [ locationsViewsData, query, isRequestingData ] );
// The legacy endpoint that only supports countries (not regions or cities)
// will be used when the new Locations Stats feature is not available.
const legacyCountriesViewsData = useSelector( ( state ) =>
getSiteStatsNormalizedData( state, siteId, statType, query )
) as [ id: number, label: string ];
const data = supportsLocationsStatsFeature
? normalizedLocationsViewsData
: legacyCountriesViewsData;
// Only fetch separate countries list if we're not already in country tab
// This is to avoid fetching the same data twice.
const { data: countriesList, isLoading: isRequestingCountriesList } = useLocationViewsQuery(
siteId,
'country',
query,
null,
{
enabled: ! shouldGate && supportsLocationsStatsFeature && geoMode !== 'country',
}
);
const normalizedCountriesList = useMemo( () => {
if ( isRequestingCountriesList || ! countriesList ) {
return [];
}
const normalizedStats = normalizers.statsCountryViews(
countriesList as StatsLocationViewsData,
query
);
if ( ! Array.isArray( normalizedStats ) ) {
return [];
}
return normalizedStats;
}, [ countriesList, query, isRequestingCountriesList ] );
useEffect( () => {
if ( isRequestingCountriesList || isRequestingData || isRequestingCountriesList ) {
return;
}
let dataToDispatch;
if ( geoMode === 'country' ) {
dataToDispatch = countriesList;
} else {
dataToDispatch = locationsViewsData;
}
if ( dataToDispatch ) {
dispatch(
receiveSiteStats( siteId, 'statsCountryViews', query, dataToDispatch, Date.now() )
);
}
}, [
countriesList,
geoMode,
locationsViewsData,
isRequestingCountriesList,
isRequestingData,
dispatch,
query,
siteId,
] );
const onCountryChange = ( value: string ) => {
trackStatsAnalyticsEvent( 'stats_locations_module_country_filter_changed', {
stat_type: optionLabels[ selectedOption ].feature,
country: value,
blog_id: siteId,
} );
setCountryFilter( value );
};
const changeViewButton = ( selection: SelectOptionType ) => {
const filter = selection.value;
trackStatsAnalyticsEvent( 'stats_locations_module_menu_clicked', {
stat_type: optionLabels[ filter ].feature,
blog_id: siteId,
} );
if ( summary ) {
page( getPathWithUpdatedQueryString( { geoMode: filter } ) );
} else {
setSelectedLocalOption( filter );
}
};
const onShowMoreClick = () => {
trackStatsAnalyticsEvent( 'stats_locations_module_show_more_clicked', {
stat_type: optionLabels[ selectedOption ].feature,
blog_id: siteId,
} );
};
// Need to keep the old tabs on Traffic page.
const toggleControlComponent = ! summary && (
<>
<SimplifiedSegmentedControl
className="stats-module-locations__tabs"
options={ Object.entries( optionLabels ).map( ( entry ) => ( {
value: entry[ 0 ], // optionLabels key
label: entry[ 1 ].selectLabel, // optionLabels object value
} ) ) }
initialSelected={ selectedOption }
onSelect={ changeViewButton }
/>
</>
);
const emptyMessage = (
<EmptyModuleCard
icon={ mapMarker }
description={ translate(
'Stats on visitors and their {{link}}viewing location{{/link}} will appear here to learn from where you are getting visits.',
{
comment: '{{link}} links to support documentation.',
components: {
link: <InlineSupportLink supportContext={ supportContext } showIcon={ false } />,
},
context: 'Stats: Info box label when the Countries module is empty',
}
) }
/>
);
const divisionsTooltip = (
<StatsInfoArea>
{ translate(
'Countries and their subdivisions are based on {{link}}ISO 3166{{/link}} standards.',
{
comment: '{{link}} links to ISO standards.',
components: {
link: (
<a
target="_blank"
rel="noreferrer"
href="https://www.iso.org/maintenance_agencies.html#72482"
/>
),
},
context: 'Stats: Link in a popover for Regions/Cities module when the module has data',
}
) }
</StatsInfoArea>
);
const titleTooltip = (
<StatsInfoArea>
{ translate(
'Visitors’ {{link}}viewing location{{/link}} by countries, regions and cities.',
{
comment: '{{link}} links to support documentation.',
components: {
link: <InlineSupportLink supportContext={ supportContext } showIcon={ false } />,
},
context: 'Stats: Link in a popover for Countries module when the module has data',
}
) }
</StatsInfoArea>
);
const showJetpackUpgradePrompt = geoMode !== 'country' && ! supportsLocationsStatsFeature;
const showUpsell = shouldGate || showJetpackUpgradePrompt;
const locationData = showUpsell ? sampleLocations : data;
const hasLocationData = Array.isArray( locationData ) && locationData.length > 0;
const heroElementActions = (
<div className="stats-module-locations__actions">
{ geoMode !== 'country' && (
<CountryFilter
countries={ normalizedCountriesList }
defaultLabel={ optionLabels[ selectedOption ].countryFilterLabel }
selectedCountry={ countryFilter }
onCountryChange={ onCountryChange }
tooltip={ divisionsTooltip }
/>
) }
</div>
);
const heroElement = (
<>
<Geochart data={ locationData } geoMode={ geoMode } skipQuery customHeight={ 480 } />
{ ! summaryUrl && heroElementActions }
</>
);
const getModuleOverlay = () => {
if ( shouldGate ) {
return (
<StatsCardUpsell siteId={ siteId } statType={ optionLabels[ selectedOption ].feature } />
);
}
if ( showJetpackUpgradePrompt ) {
return <StatsCardUpdateJetpackVersion siteId={ siteId } statType="locations" />;
}
return null;
};
const getFinalSummaryUrl = () => {
if ( ! summaryUrl ) {
return undefined;
}
return getPathWithUpdatedQueryString(
{
geoMode: selectedOption,
},
summaryUrl
);
};
const moduleOverlay = getModuleOverlay();
return (
<>
{ ! shouldGate && ! supportsLocationsStatsFeature && geoMode === 'country' && (
<QuerySiteStats statType={ statType } siteId={ siteId } query={ query } />
) }
{ isRequestingData && ! shouldGate && (
<StatsCardSkeleton
className="locations-skeleton"
isLoading={ isRequestingData }
title={ title }
type={ 3 }
withHero
withSplitHeader
toggleControl={ toggleControlComponent }
mainItemLabel={ optionLabels[ selectedOption ]?.headerLabel }
metricLabel={ translate( 'Views' ) }
titleNodes={ titleTooltip }
/>
) }
{ ( ( ! isRequestingData && ! isError && hasLocationData ) || shouldGate ) && (
// show data or an overlay
<>
{ /* @ts-expect-error TODO: Refactor StatsListCard with TypeScript. */ }
<StatsListCard
title={ title }
titleNodes={ titleTooltip }
moduleType="locations"
data={ locationData }
emptyMessage={ emptyMessage }
metricLabel={ translate( 'Views' ) }
loader={ isRequestingData && <StatsModulePlaceholder isLoading={ isRequestingData } /> }
splitHeader
heroElement={ heroElement }
mainItemLabel={ optionLabels[ selectedOption ]?.headerLabel }
toggleControl={ toggleControlComponent }
showMore={
summaryUrl
? {
url: getFinalSummaryUrl(),
label:
Array.isArray( locationData ) && locationData.length >= 10
? translate( 'View all', {
context: 'Stats: Button link to show more detailed stats information',
} )
: translate( 'View details', {
context: 'Stats: Button label to see the detailed content of a panel',
} ),
}
: undefined
}
onShowMoreClick={ onShowMoreClick }
overlay={ moduleOverlay }
listItemClassName={ listItemClassName }
/>
</>
) }
{ ! isRequestingData && ! hasLocationData && ! shouldGate && (
// show empty state
<StatsCard
title={ title }
isEmpty
emptyMessage={ emptyMessage }
footerAction={
summaryUrl
? {
url: getFinalSummaryUrl(),
label: translate( 'View more' ),
}
: undefined
}
/>
) }
</>
);
};
export default StatsLocations;