Back

75% Statements 6/8
42.85% Branches 3/7
100% Functions 2/2
75% Lines 6/8

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                                                                          1x                       1x     1x 1x                             1x                                                           1x            
/**
 * External dependencies
 */
import {
	HEADLINE_COUNT_FORMAT,
	MetricTabsChart,
	MetricTabsChartSkeleton,
	WidgetRoot,
	WidgetState,
	describeError,
	useWidgetRootContext,
	type MetricTabsChartType,
	type ReportParamsFieldAttributes,
} from '@jetpack-premium-analytics/widgets-toolkit';
import { __ } from '@wordpress/i18n';
/**
 * Internal dependencies
 */
import styles from './style.module.css';
import useCompanyRequests from './use-company-requests';
import type { WidgetRenderProps } from '@wordpress/widget-primitives';
import type { ComponentProps } from 'react';
 
type AiCompanySummaryAttributes = {
	chartType?: MetricTabsChartType;
};
 
type AiCompanySummaryRenderAttributes = AiCompanySummaryAttributes &
	Partial< ReportParamsFieldAttributes >;
type AiCompanySummaryProps = WidgetRenderProps< AiCompanySummaryRenderAttributes > & {
	/**
	 * Host callback to surface a widget error in the dashboard frame.
	 */
	setError?: ComponentProps< typeof WidgetRoot >[ 'setError' ];
};
 
function AiCompanySummaryInner( { chartType }: { chartType: MetricTabsChartType } ) {
	const { reportParams } = useWidgetRootContext();
	const {
		metrics,
		period,
		isLoading,
		isFetching,
		isError,
		isEmpty,
		hasCompany,
		isRangeTooLong,
		error,
		refetch,
	} = useCompanyRequests( reportParams );
 
	let empty;
	if ( ! hasCompany ) {
		empty = {
			description: __(
				'Open an AI company to see its requests here.',
				'jetpack-premium-analytics-pkg'
			),
		};
	} else Eif ( isRangeTooLong ) {
		empty = {
			description: __(
				'Requests are available for up to a year at a time. Choose a shorter period.',
				'jetpack-premium-analytics-pkg'
			),
		};
	}
 
	return (
		<div className={ styles.root }>
			<WidgetState
				isLoading={ isLoading }
				isFetching={ isFetching }
				isError={ isError }
				isEmpty={ isEmpty }
				error={ describeError( error, {
					retryDescription: __(
						"We couldn't load this company's requests. Please try again in a moment.",
						'jetpack-premium-analytics-pkg'
					),
					onRetry: refetch,
				} ) }
				empty={ empty }
				renderLoading={ <MetricTabsChartSkeleton /> }
			>
				<MetricTabsChart
					metrics={ metrics }
					dataFormat={ HEADLINE_COUNT_FORMAT }
					chartType={ chartType }
					groupLabel={ __( 'Request metric', 'jetpack-premium-analytics-pkg' ) }
					tickResolution={ period }
				/>
			</WidgetState>
		</div>
	);
}
 
export default function AiCompanySummary( { attributes = {}, setError }: AiCompanySummaryProps ) {
	return (
		<WidgetRoot attributes={ attributes } setError={ setError }>
			<AiCompanySummaryInner chartType={ attributes.chartType ?? 'line' } />
		</WidgetRoot>
	);
}