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 | 39x 1x 38x 25x 25x 25x 25x 25x 25x 25x 21x 25x 11x 25x 11x 25x 12x | /**
* External dependencies
*/
import {
isEdgeWindowTooLong,
useStatsAiProviders,
withoutComparison,
} from '@jetpack-premium-analytics/data';
import {
Leaderboard,
WIDGET_ROW_LIMIT,
WidgetRoot,
describeError,
useWidgetNavigationSearch,
useWidgetRootContext,
type LeaderboardRowInput,
type ReportParamsFieldAttributes,
} from '@jetpack-premium-analytics/widgets-toolkit';
import { __, sprintf } from '@wordpress/i18n';
import { useMemo } from 'react';
/**
* Internal dependencies
*/
import { OTHER_COMPANIES_ID, buildCompanyRows } from './build-company-rows';
import styles from './style.module.css';
import type { WidgetRenderProps } from '@wordpress/widget-primitives';
import type { ComponentProps } from 'react';
export type TopAiCompaniesAttributes = Record< never, never >;
type TopAiCompaniesRenderAttributes = TopAiCompaniesAttributes &
Partial< ReportParamsFieldAttributes >;
type TopAiCompaniesProps = WidgetRenderProps< TopAiCompaniesRenderAttributes > & {
/**
* Host callback to surface a widget error in the dashboard frame.
*/
setError?: ComponentProps< typeof WidgetRoot >[ 'setError' ];
};
function rowAriaLabel( row: LeaderboardRowInput ) {
if ( row.id === OTHER_COMPANIES_ID ) {
return __( 'View the other companies', 'jetpack-premium-analytics-pkg' );
}
return sprintf(
/* translators: %s: an AI company. */
__( 'View the bots of %s', 'jetpack-premium-analytics-pkg' ),
row.label
);
}
function TopAiCompaniesInner() {
const { reportParams } = useWidgetRootContext();
// The widget shows no comparison yet, so the comparison period is not requested.
const params = useMemo( () => withoutComparison( reportParams ), [ reportParams ] );
const { primary, isLoading, isFetching, isError, error, refetch } = useStatsAiProviders( params );
const isRangeTooLong = isEdgeWindowTooLong( reportParams );
// A window too long to request still holds the previous window's rows as placeholder data.
const providers = isRangeTooLong ? undefined : primary.data?.data;
const linkSearch = useWidgetNavigationSearch();
const rows = useMemo(
() => buildCompanyRows( providers ?? [], WIDGET_ROW_LIMIT, linkSearch ),
[ providers, linkSearch ]
);
const columns = useMemo(
() => ( {
label: __( 'Companies', 'jetpack-premium-analytics-pkg' ),
metric: 'crawl-requests' as const,
} ),
[]
);
const drillDown = useMemo(
() => ( {
backLabel: __( 'All AI companies', 'jetpack-premium-analytics-pkg' ),
rowAriaLabel,
} ),
[]
);
return (
<div className={ styles.root }>
<Leaderboard
rows={ rows }
// Placeholder rows from the previous window outlive a failed refetch.
status={ { isLoading, isFetching, isError: isError && ! rows.length, refetch } }
error={ describeError( error, {
retryDescription: __(
"We couldn't load the AI companies. Please try again in a moment.",
'jetpack-premium-analytics-pkg'
),
onRetry: refetch,
} ) }
empty={
isRangeTooLong
? {
description: __(
'Requests are available for up to a year at a time. Choose a shorter period.',
'jetpack-premium-analytics-pkg'
),
}
: undefined
}
columns={ columns }
drillDown={ drillDown }
/>
</div>
);
}
export default function TopAiCompanies( { attributes = {}, setError }: TopAiCompaniesProps ) {
return (
<WidgetRoot attributes={ attributes } setError={ setError }>
<TopAiCompaniesInner />
</WidgetRoot>
);
}
|