Back

100% Statements 9/9
100% Branches 2/2
100% Functions 6/6
100% Lines 7/7

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                2x     2x                 19x                           10x                                             19x 10x 2x                                                          
import { Link, type Field } from '@jetpack-premium-analytics/externals';
import { formatMetricValue } from '@jetpack-premium-analytics/formatters';
import { __, sprintf } from '@wordpress/i18n';
import { ChipSelectEdit } from './chip-select-edit';
import type { StatsSettings } from '@jetpack-premium-analytics/data';
 
type SiteRole = { slug: string; name: string; count: number | null };
 
const CONFIGURE_STATS_URL = 'https://jetpack.com/support/jetpack-stats/configure-jetpack-stats/';
 
// `Stats\Settings::update()` keeps this role whatever is sent, so the form never offers it.
const LOCKED_ROLE = 'administrator';
 
/**
 * The Settings tab's fields, over the roles this site has.
 *
 * @param roles - The site's roles, with how many users hold each.
 * @return The fields.
 */
export function getFields( roles: SiteRole[] ): Field< StatsSettings >[] {
	const elements = roles.map( role => ( {
		value: role.slug,
		// Sites with many users get no counts, since counting them is slow.
		label:
			role.count === null
				? role.name
				: sprintf(
						/* translators: 1: user role name, 2: number of users with that role. */
						__( '%1$s (%2$s)', 'jetpack-premium-analytics-pkg' ),
						role.name,
						formatMetricValue( role.count, 'number', { decimals: 0 } )
					),
	} ) );
 
	return [
		{
			id: 'admin_bar',
			type: 'boolean',
			Edit: 'toggle',
			label: __( 'Include a small chart in admin bar', 'jetpack-premium-analytics-pkg' ),
			description: (
				<>
					{ __(
						'Displays a 48-hour traffic snapshot and information on your site activity, including visitors and popular posts or pages.',
						'jetpack-premium-analytics-pkg'
					) }{ ' ' }
					<Link href={ `${ CONFIGURE_STATS_URL }#see-a-48-hour-traffic-snapshot` } openInNewTab>
						{ __( 'Learn more', 'jetpack-premium-analytics-pkg' ) }
					</Link>
				</>
			),
		},
		{
			id: 'roles',
			type: 'array',
			label: __( 'Allow Jetpack Stats to be viewed by', 'jetpack-premium-analytics-pkg' ),
			description: __( 'Administrators can always view stats.', 'jetpack-premium-analytics-pkg' ),
			elements: elements.filter( element => element.value !== LOCKED_ROLE ),
			getValue: ( { item } ) => item.roles.filter( role => role !== LOCKED_ROLE ),
			setValue: ( { value } ) => ( { roles: [ LOCKED_ROLE, ...value ] } ),
			Edit: ChipSelectEdit,
		},
		{
			id: 'count_roles',
			type: 'array',
			label: __( 'Count logged in page views from', 'jetpack-premium-analytics-pkg' ),
			description: (
				<>
					{ __(
						'Views from other logged-in users are not counted.',
						'jetpack-premium-analytics-pkg'
					) }{ ' ' }
					<Link href={ `${ CONFIGURE_STATS_URL }#manage-what-views-are-counted` } openInNewTab>
						{ __( 'Learn more', 'jetpack-premium-analytics-pkg' ) }
					</Link>
				</>
			),
			elements,
			Edit: ChipSelectEdit,
		},
		{
			id: 'wpcom_reader_views_enabled',
			type: 'boolean',
			Edit: 'toggle',
			label: __( 'Show post views for this site', 'jetpack-premium-analytics-pkg' ),
		},
	];
}