Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 11x 11x 11x 11x 26x 26x 12x | /**
* External dependencies
*/
import { formatMetricValue } from '@jetpack-premium-analytics/formatters';
import {
getOverLimitMessage,
getPlanUpgradeUrl,
usePlanUsage,
WidgetRoot,
WidgetState,
type ReportParamsFieldAttributes,
} from '@jetpack-premium-analytics/widgets-toolkit';
import { createInterpolateElement } from '@wordpress/element';
import { __, _n, sprintf } from '@wordpress/i18n';
import { percent } from '@wordpress/icons';
import { Link, Stack, Text } from '@jetpack-premium-analytics/externals';
import clsx from 'clsx';
/**
* Internal dependencies
*/
import styles from './style.module.css';
import type { PlanUsageAttributes } from './widget';
import type { WidgetRenderProps } from '@wordpress/widget-primitives';
// The usage endpoint ignores report params — it reports the current billing
// cycle, with no date range or comparison period — but WidgetRoot still expects
// them on `attributes`.
type PlanUsageRenderAttributes = PlanUsageAttributes & Partial< ReportParamsFieldAttributes >;
type PlanUsageWidgetProps = WidgetRenderProps< PlanUsageRenderAttributes >;
type PlanUsageBarProps = {
/**
* The plan's billable views limit for the cycle. Loading, error, and
* no-limit states are handled by `WidgetState` before the bar renders.
*/
limit: number;
usage?: number;
daysToReset?: number;
/** Number of recent billing cycles the site has exceeded its limit. */
overLimitMonths?: number | null;
};
/**
* Presentational bar, following the Stats "Plan usage" section. Renders the
* populated state only — `WidgetState` owns loading, error, and unavailable.
*/
function PlanUsageBar( { limit, usage, daysToReset, overLimitMonths }: PlanUsageBarProps ) {
const usageValue = usage ?? 0;
const isOverLimit = usageValue >= limit;
const upgradeHref = getPlanUpgradeUrl();
return (
<Stack
className={ styles.root }
direction="column"
align="stretch"
justify="safe center"
gap="md"
>
<div className={ clsx( styles.progress, isOverLimit && styles.isOverLimit ) }>
{ /* The fill is value-driven, so the dynamic width needs no inline style. */ }
<progress
className={ styles.progressMeter }
value={ Math.min( usageValue, limit ) }
max={ limit }
aria-label={ __( 'Plan usage', 'jetpack-premium-analytics-pkg' ) }
/>
<Text className={ styles.progressLabel } variant="body-sm">
{ sprintf(
/* translators: 1: views used in the current cycle, 2: the plan's views limit. */
__( '%1$s / %2$s views', 'jetpack-premium-analytics-pkg' ),
formatMetricValue( usageValue, 'number', { decimals: 0 } ),
formatMetricValue( limit, 'number', { decimals: 0 } )
) }
</Text>
{ daysToReset !== undefined && (
<Text className={ styles.progressLabel } variant="body-sm">
{ sprintf(
/* translators: %d: number of days until the billing cycle resets. */
_n(
'Restarts in %d day',
'Restarts in %d days',
daysToReset,
'jetpack-premium-analytics-pkg'
),
daysToReset
) }
</Text>
) }
</div>
{ ( !! overLimitMonths || upgradeHref ) && (
<Text className={ styles.note } variant="body-sm">
{ overLimitMonths ? (
<>
<strong>{ getOverLimitMessage( overLimitMonths ) }</strong>{ ' ' }
</>
) : null }
{ /* Without script data there is no purchase URL, and a Link with no
href renders styled but non-actionable — omit the sentence. */ }
{ upgradeHref &&
createInterpolateElement(
__(
'Do you want to increase your views limit? <a>Upgrade now</a>',
'jetpack-premium-analytics-pkg'
),
{ a: <Link href={ upgradeHref } /> }
) }
</Text>
) }
</Stack>
);
}
/**
* The plan-usage endpoint is a point-in-time reading of the connected plan, so
* it takes no report params.
*/
function PlanUsageReport() {
const { data, isLoading, isFetching, isError, refetch, limit, overLimitMonths } = usePlanUsage();
return (
<WidgetState
isLoading={ isLoading }
isFetching={ isFetching }
// The query keeps prior data via `placeholderData`, so a transient refetch
// failure keeps the usage bar visible; only surface the error when there is
// nothing to show.
isError={ ! data && isError }
isEmpty={ ! limit }
error={ {
description: __(
"We couldn't load plan usage. Please try again in a moment.",
'jetpack-premium-analytics-pkg'
),
actions: [ { label: __( 'Retry', 'jetpack-premium-analytics-pkg' ), onClick: refetch } ],
} }
empty={ {
icon: percent,
description: __(
"Plan usage isn't available for your current plan.",
'jetpack-premium-analytics-pkg'
),
} }
>
{ limit !== null && (
<PlanUsageBar
limit={ limit }
usage={ data?.current_usage?.views_count }
daysToReset={ data?.current_usage?.days_to_reset }
overLimitMonths={ overLimitMonths }
/>
) }
</WidgetState>
);
}
export default function PlanUsage( { attributes = {} }: PlanUsageWidgetProps ) {
return (
<WidgetRoot attributes={ attributes }>
<PlanUsageReport />
</WidgetRoot>
);
}
|