/** * Growth Component. */ import { useQuery } from '@tanstack/react-query'; import { __ } from '@wordpress/i18n'; import { Block } from '@/components/Blocks/Block'; import { BlockHeading } from '@/components/Blocks/BlockHeading'; import { BlockContent } from '@/components/Blocks/BlockContent'; import ExplanationAndStatsItem from '@/components/Common/ExplanationAndStatsItem'; import { ForecastAnnotation } from '@/components/Forecast'; import { getGrowthData, GrowthBlockData } from '@/api/getGrowthData'; import { useBlockConfig } from '@/hooks/useBlockConfig'; import { BlockComponentProps } from '@/store/reports/types'; // Metric-explainer keys per row, resolved in metricDefinitions.js. const GROWTH_METRIC_KEYS: Record = { 'forecast-this-year': 'growth_forecast_this_year', 'forecast-this-month': 'growth_forecast_this_month', 'forecast-next-year': 'growth_forecast_next_year', 'forecast-next-month': 'growth_forecast_next_month' }; const PLACEHOLDER_DATA: GrowthBlockData = { items: Object.fromEntries( Object.keys( GROWTH_METRIC_KEYS ).map( ( key ) => [ key, { title: '-', subtitle: '-', value: '-', exactValue: null, change: null, changeStatus: null, tooltipText: null, isForecast: key.startsWith( 'forecast' ) } ]) ), metadata: { growth_rate: 0, limited_data: true } }; /** * Growth component: the forecasted revenue for the current month and year, * and for the next month and year. Calendar-anchored to now — the picked * date range deliberately does not apply, matching the forecast view of the * revenue chart it sits next to; visitor filters do apply. * * @return {JSX.Element} The Growth component. */ const GrowthBlock = ( props: BlockComponentProps ): JSX.Element => { const { startDate, endDate, range, filters, index } = useBlockConfig( props ); const growthQuery = useQuery({ queryKey: [ 'growth', filters ], queryFn: () => getGrowthData({ startDate, endDate, range, filters: filters as Record }), placeholderData: PLACEHOLDER_DATA, gcTime: 10000 }); const growth = growthQuery.data ?? PLACEHOLDER_DATA; const showAnnotation = ! growthQuery.isPlaceholderData && ! growthQuery.isError; const blockHeadingProps = { title: __( 'Growth', 'burst-statistics' ), isReport: props.isReport, reportBlockIndex: index, isLoading: growthQuery.isFetching }; return ( { Object.entries( growth.items ).map( ([ key, item ]) => ( ) ) } { showAnnotation && ( ) } ); }; export default GrowthBlock;