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 | 2x 54x 9x 45x 54x 61x 54x 50x 54x 24x 24x 24x 24x 3x 3x 8x 24x 8x 24x | /**
* External dependencies
*/
import {
AI_PROVIDER_UNKNOWN,
AI_PROVIDER_OTHER_KEY,
AI_PROVIDER_UNKNOWN_KEY,
type StatsAiProvider,
} from '@jetpack-premium-analytics/data';
import { __ } from '@wordpress/i18n';
import type { LeaderboardRowInput } from '@jetpack-premium-analytics/widgets-toolkit';
/** The row the companies past the list's slots fold into. */
export const OTHER_COMPANIES_ID = 'other-companies';
type LinkSearch = Record< string, unknown >;
/**
* The action of a company's row: its name opens the company's page.
*
* @param provider - The company.
* @param linkSearch - The window and the dashboard tab the company's page opens with.
* @return The row action, or none for the bots no company owns and for the summed overflow.
*/
function companyPageAction(
provider: StatsAiProvider,
linkSearch: LinkSearch
): LeaderboardRowInput[ 'action' ] {
if ( provider.key === AI_PROVIDER_UNKNOWN_KEY || provider.key === AI_PROVIDER_OTHER_KEY ) {
return undefined;
}
return {
kind: 'static',
link: {
to: '/report/$kind/$id',
params: { kind: 'ai-company', id: provider.key },
search: linkSearch,
},
};
}
function toCompanyRow( provider: StatsAiProvider, linkSearch: LinkSearch ): LeaderboardRowInput {
return {
id: provider.key,
label: provider.name,
value: provider.requests,
action: companyPageAction( provider, linkSearch ),
childrenLabel: __( 'Bots', 'jetpack-premium-analytics-pkg' ),
children: provider.bots.map( bot => ( {
id: `${ provider.key }/${ bot.name }`,
label:
bot.name === AI_PROVIDER_UNKNOWN
? __( 'Unnamed bots', 'jetpack-premium-analytics-pkg' )
: bot.name,
value: bot.requests,
} ) ),
};
}
/**
* The rows of the Top AI companies list: the companies with the most requests, the
* rest folded into one row, and the bots nobody owns last.
*
* @param providers - The companies, most requests first.
* @param maxRows - Rows the list shows.
* @param linkSearch - The window and the dashboard tab a company's page opens with.
* @return The rows, at most `maxRows` of them.
*/
export function buildCompanyRows(
providers: readonly StatsAiProvider[],
maxRows: number,
linkSearch: LinkSearch
): LeaderboardRowInput[] {
const toRow = ( provider: StatsAiProvider ) => toCompanyRow( provider, linkSearch );
const unattributed = providers.find( provider => provider.key === AI_PROVIDER_UNKNOWN_KEY );
const companies = providers.filter( provider => provider !== unattributed );
const slots = maxRows - ( unattributed ? 1 : 0 );
const folds = companies.length > slots;
const rows = companies.slice( 0, folds ? slots - 1 : slots ).map( toRow );
if ( folds ) {
const rest = companies.slice( slots - 1 );
rows.push( {
id: OTHER_COMPANIES_ID,
label: __( 'Other companies', 'jetpack-premium-analytics-pkg' ),
value: rest.reduce( ( sum, provider ) => sum + provider.requests, 0 ),
childrenLabel: __( 'Companies', 'jetpack-premium-analytics-pkg' ),
children: rest.map( toRow ),
} );
}
if ( unattributed ) {
rows.push( {
...toRow( unattributed ),
id: 'unattributed',
label: __( 'Unattributed', 'jetpack-premium-analytics-pkg' ),
} );
}
return rows;
}
|