diff --git a/client/my-sites/stats/hooks/use-subscribers-overview.ts b/client/my-sites/stats/hooks/use-subscribers-overview.ts index a33e36e83d948..97b23c4713a07 100644 --- a/client/my-sites/stats/hooks/use-subscribers-overview.ts +++ b/client/my-sites/stats/hooks/use-subscribers-overview.ts @@ -5,10 +5,13 @@ import { useSubscribersQueries } from './use-subscribers-query'; const DATES_TO_QUERY = [ 0, 30, 60, 90 ]; const DATES_TO_QUERY_EXCLUDE_TODAY = [ 30, 60, 90 ]; +// Used to allow comparisons between translated strings +const TODAY_LABEL = translate( 'Today' ); + function getLabels( dateToQuery: number ) { switch ( dateToQuery ) { case 0: - return translate( 'Today' ); + return TODAY_LABEL; case 30: return translate( '30 days ago' ); case 60: @@ -20,6 +23,15 @@ function getLabels( dateToQuery: number ) { } } +function getNote( heading: string ) { + let note = translate( 'As of today' ); + if ( heading !== TODAY_LABEL ) { + const prefix = translate( 'Since' ); + note = `${ prefix } ${ heading }`; + } + return note; +} + // calculate the date to query for based on the number of days to subtract function calculateQueryDate( daysToSubtract: number ) { const today = new Date(); @@ -43,9 +55,11 @@ export default function useSubscribersOverview( siteId: number | null, isTodayEx const overviewData = subscribersData.map( ( data, index ) => { const count = data?.data?.[ 0 ]?.subscribers || null; const heading = getLabels( datesToQuery[ index ] ); + const note = getNote( heading ); return { count, heading, + note, }; } ); diff --git a/client/my-sites/stats/stats-subscribers-overview/index.tsx b/client/my-sites/stats/stats-subscribers-overview/index.tsx index ed12c1838e80d..53485872a1172 100644 --- a/client/my-sites/stats/stats-subscribers-overview/index.tsx +++ b/client/my-sites/stats/stats-subscribers-overview/index.tsx @@ -14,13 +14,7 @@ const SubscribersOverview: React.FC< SubscribersOverviewProps > = ( { siteId } ) return (
- { overviewData.map( ( { count, heading }, index ) => { - let note = translate( 'As of today' ); - if ( heading !== 'Today' ) { - const prefix = translate( 'Since' ); - note = `${ prefix } ${ heading }`; - } - + { overviewData.map( ( { count, heading, note }, index ) => { return ( // TODO: Communicate loading vs error state to the user. - +
) } diff --git a/packages/components/src/highlight-cards/count-card.tsx b/packages/components/src/highlight-cards/count-card.tsx index 61ebb28fe0e06..0a5934ced3d88 100644 --- a/packages/components/src/highlight-cards/count-card.tsx +++ b/packages/components/src/highlight-cards/count-card.tsx @@ -1,8 +1,9 @@ +import { arrowDown, arrowUp, Icon } from '@wordpress/icons'; import clsx from 'clsx'; import { useRef, useState } from 'react'; import { Card } from '../'; import Popover from '../popover'; -import { formatNumber } from './lib/numbers'; +import { formatNumber, subtract } from './lib/numbers'; interface CountCardProps { heading?: React.ReactNode; @@ -10,16 +11,32 @@ interface CountCardProps { label?: string; note?: string; showValueTooltip?: boolean; - value: number | string | null; + value: number | null; + previousValue?: number | null; } -function TooltipContent( { value, label, note }: CountCardProps ) { +export function TooltipContent( { value, label, note, previousValue }: CountCardProps ) { + const difference = subtract( value, previousValue ); + + let trendClass = 'highlight-card-tooltip-count-difference-positive'; + let trendIcon = arrowUp; + if ( difference !== null && difference < 0 ) { + trendClass = 'highlight-card-tooltip-count-difference-negative'; + trendIcon = arrowDown; + } + return (
- { formatNumber( value as number, false ) } + { formatNumber( value, false ) } { label && ` ${ label }` } + { difference !== null && difference !== 0 && ( + + + { formatNumber( Math.abs( difference ), false ) } + + ) } { note &&
{ note }
}
); diff --git a/packages/components/src/highlight-cards/style.scss b/packages/components/src/highlight-cards/style.scss index da51f42497c82..a4bcdb437b217 100644 --- a/packages/components/src/highlight-cards/style.scss +++ b/packages/components/src/highlight-cards/style.scss @@ -396,6 +396,21 @@ $highlight-card-tooltip-font: Inter, $sans !default; color: var(--studio-gray-10); } +.highlight-card-tooltip-count-difference-positive { + color: var(--studio-green-10); +} + +.highlight-card-tooltip-count-difference-negative { + color: var(--studio-red-10); +} + +.highlight-card-tooltip-count-difference-positive svg, +.highlight-card-tooltip-count-difference-negative svg { + fill: currentColor; + margin-left: 16px; + vertical-align: text-bottom; +} + .highlight-card-tooltip-note { color: var(--studio-gray-30); font-size: $font-body-small;