Skip to content

Commit 9fb612f

Browse files
authored
My Jetpack: Make the Stats section's pieces reusable (#53919)
1 parent c457933 commit 9fb612f

4 files changed

Lines changed: 69 additions & 37 deletions

File tree

‎projects/packages/my-jetpack/_inc/components/stats-section/cards.jsx‎

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,14 @@ const getDynamicTitle = metric => {
111111
* @param {object} props.counts - Counts object for the current period.
112112
* @param {object} props.previousCounts - Counts object for the previous period.
113113
* @param {number} props.headingLevel - Heading level between 1 and 6.
114-
* @param {Array} props.chartData - Chart data for the bar chart visualization.
114+
* @param {?object} props.chartData - Chart data for the bar chart visualization.
115115
* @param {boolean} props.isLoading - Whether the data is loading.
116116
* @param {string} props.detailedStatsHref - Destination of the detailed stats link.
117117
* @param {Function} props.onDetailedStatsClick - Function to handle detailed stats click.
118+
* @param {boolean} [props.hideTitle] - Whether to leave out the linked title, when a container already has one.
119+
* @param {boolean} [props.chartBelowCounts] - Whether to show the chart after the counts instead of before them.
118120
*
119-
* @return {object} StatsCards React component.
121+
* @return {import('react').JSX.Element} StatsCards React component.
120122
*/
121123
const StatsCards = ( {
122124
counts,
@@ -126,6 +128,8 @@ const StatsCards = ( {
126128
isLoading,
127129
detailedStatsHref,
128130
onDetailedStatsClick,
131+
hideTitle = false,
132+
chartBelowCounts = false,
129133
} ) => {
130134
const Heading = `h${ headingLevel >= 1 && headingLevel <= 6 ? headingLevel : 3 }`;
131135

@@ -169,30 +173,36 @@ const StatsCards = ( {
169173
return icons[ metric ] || icons.views;
170174
}, [] );
171175

176+
const chart = (
177+
<StatsChart
178+
data={ transformedChartData }
179+
isLoading={ isLoading }
180+
href={ detailedStatsHref }
181+
onClick={ onDetailedStatsClick }
182+
selectedMetric={ selectedMetric }
183+
metricIcon={ getMetricIcon( selectedMetric ) }
184+
/>
185+
);
186+
172187
return (
173188
<div className={ styles[ 'section-stats-highlights' ] }>
174-
<Link
175-
tone="neutral"
176-
className={ styles[ 'section-title-container' ] }
177-
href={ detailedStatsHref }
178-
onClick={ detailedStatsHref ? onDetailedStatsClick : undefined }
179-
>
180-
<Heading className={ styles[ 'section-title' ] }>
181-
<span>{ getDynamicTitle( selectedMetric ) }</span>
182-
</Heading>
183-
<div>
184-
<Icon icon={ chevronRight } />
185-
</div>
186-
</Link>
189+
{ ! hideTitle && (
190+
<Link
191+
tone="neutral"
192+
className={ styles[ 'section-title-container' ] }
193+
href={ detailedStatsHref }
194+
onClick={ detailedStatsHref ? onDetailedStatsClick : undefined }
195+
>
196+
<Heading className={ styles[ 'section-title' ] }>
197+
<span>{ getDynamicTitle( selectedMetric ) }</span>
198+
</Heading>
199+
<div>
200+
<Icon icon={ chevronRight } />
201+
</div>
202+
</Link>
203+
) }
187204

188-
<StatsChart
189-
data={ transformedChartData }
190-
isLoading={ isLoading }
191-
href={ detailedStatsHref }
192-
onClick={ onDetailedStatsClick }
193-
selectedMetric={ selectedMetric }
194-
metricIcon={ getMetricIcon( selectedMetric ) }
195-
/>
205+
{ ! chartBelowCounts && chart }
196206

197207
<ul className={ clsx( styles[ 'cards-list' ], styles[ 'my-jetpack-stats-cards' ] ) }>
198208
<CountComparisonCard
@@ -256,6 +266,7 @@ const StatsCards = ( {
256266
onClick={ handleCommentsClick }
257267
/>
258268
</ul>
269+
{ chartBelowCounts && chart }
259270
</div>
260271
);
261272
};

‎projects/packages/my-jetpack/_inc/components/stats-section/index.jsx‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import StatsCards from './cards';
1111

1212
/**
1313
* Options for the stats visits hook
14+
*
15+
* @type {{ period: 'day', quantity: number, date: Date }}
1416
*/
15-
const VISITS_OPTIONS = {
17+
export const VISITS_OPTIONS = {
1618
period: 'day',
1719
quantity: 14,
1820
date: new Date( Date.now() + new Date().getTimezoneOffset() * 60000 ), // now in UTC to match Stats
@@ -22,9 +24,9 @@ const VISITS_OPTIONS = {
2224
* Process 14-day stats data to extract current and previous 7-day periods
2325
*
2426
* @param {object} visitsData - Raw visits data from API with fields and data arrays
25-
* @return {object} Object containing counts, previousCounts, and chartData for last 7 days
27+
* @return {{ counts: object, previousCounts: object, chartData: ?{ fields: string[], data: Array } }} Object containing counts, previousCounts, and chartData for last 7 days
2628
*/
27-
const processStatsData = visitsData => {
29+
export const processStatsData = visitsData => {
2830
if (
2931
! visitsData ||
3032
! visitsData.fields ||
@@ -119,6 +121,18 @@ const processStatsData = visitsData => {
119121
};
120122
};
121123

124+
/**
125+
* Build the link to the detailed stats page.
126+
*
127+
* @param {string} manageUrl - The Stats product's manage URL.
128+
* @param {boolean} premiumAnalyticsEnabled - Whether the URL is the Premium Analytics dashboard.
129+
* @return {string|undefined} The link, or undefined without a manage URL.
130+
*/
131+
export const getDetailedStatsHref = ( manageUrl, premiumAnalyticsEnabled ) => {
132+
const query = premiumAnalyticsEnabled ? '' : '&force_refresh=1';
133+
return manageUrl ? `${ manageUrl }${ query }` : undefined;
134+
};
135+
122136
/**
123137
* Stats section component for My Jetpack Stats product card with a chart and counts.
124138
*
@@ -165,8 +179,7 @@ const StatsSection = () => {
165179
[ status, manageUrl ]
166180
);
167181

168-
const viewStatsQuery = premiumAnalyticsEnabled ? '' : '&force_refresh=1';
169-
const viewStatsHref = manageUrl ? `${ manageUrl }${ viewStatsQuery }` : undefined;
182+
const viewStatsHref = getDetailedStatsHref( manageUrl, premiumAnalyticsEnabled );
170183

171184
const viewStatsButton = {
172185
href: viewStatsHref,

‎projects/packages/my-jetpack/_inc/hooks/use-stats-visits/index.ts‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,24 @@ const useStatsVisits = ( blogID: string, isEnabled: boolean, options: StatsVisit
6666
return `${ baseEndpoint }?${ queryParams }`;
6767
}, [ blogID, queryParams ] );
6868

69-
const { data, isLoading, error } = useSimpleQuery< StatsVisitsData >( {
70-
name: QUERY_STATS_VISITS_KEY,
71-
query: { path: endpoint() },
72-
options: {
73-
enabled: isEnabled,
74-
gcTime: 5000,
75-
refetchOnMount: 'always',
76-
},
77-
} );
69+
const { data, isLoading, isFetching, isError, error, refetch } =
70+
useSimpleQuery< StatsVisitsData >( {
71+
name: QUERY_STATS_VISITS_KEY,
72+
query: { path: endpoint() },
73+
options: {
74+
enabled: isEnabled,
75+
gcTime: 5000,
76+
refetchOnMount: 'always',
77+
},
78+
} );
7879

7980
return {
8081
data,
8182
isLoading,
83+
isFetching,
84+
isError,
8285
error,
86+
refetch,
8387
};
8488
};
8589

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: changed
3+
Comment: Make the Stats section's data helpers, link and cards reusable; no visible change.
4+

0 commit comments

Comments
 (0)