Back

27.27% Statements 3/11
15.38% Branches 2/13
28.57% Functions 2/7
27.27% Lines 3/11

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                                      1x                                                                                                               1x                                                   1x                    
/**
 * External dependencies
 */
import {
	useSiteHomeUrl,
	type PostThumbnailUrls,
	type StatsTopPostsComparisonItem,
} from '@jetpack-premium-analytics/data';
import {
	MetricWithComparison,
	PostTitleLink,
	REPORT_TITLE_LINK_CLASS_NAMES,
	ReportThumbnail,
} from '@jetpack-premium-analytics/widgets-toolkit';
import { __ } from '@wordpress/i18n';
import { page as pageIcon, post as postIcon } from '@wordpress/icons';
import type { Field } from '@jetpack-premium-analytics/externals';
import type { JSX } from 'react';
 
const VIEWS_DATA_FORMAT = {
	type: 'number',
	options: { decimals: 0, useMultipliers: false },
} as const;
 
type PostTitleProps = {
	/*
	 * The post row.
	 */
	item: StatsTopPostsComparisonItem;
 
	/*
	 * The window and origin a link to the post's page carries.
	 */
	detailSearch: Record< string, unknown >;
};
 
/**
 * A post row's title. A row with an ID opens the post's page; the public URL is the fallback
 * for a row without one. The API sends no URL for the homepage row, which takes the site home
 * and never the post's page: the homepage is not a single post.
 *
 * @param {PostTitleProps} props - The props.
 * @return The linked or plain post title.
 */
function PostTitle( { item, detailSearch }: PostTitleProps ): JSX.Element {
	const homeUrl = useSiteHomeUrl();
 
	const isHomepage = item.type === 'homepage';
	const title = String( item.label ?? '' );
 
	return (
		<PostTitleLink
			id={ isHomepage ? undefined : item.id }
			label={ title }
			link={ isHomepage ? homeUrl : item.link }
			search={ detailSearch }
			classNames={ REPORT_TITLE_LINK_CLASS_NAMES }
			title={ title }
		/>
	);
}
 
/**
 * The fields of the posts table. A getter, so the labels translate after the locale data loads.
 *
 * @param withComparison - Whether to render available period-over-period deltas.
 * @param detailSearch   - The window and origin a link to a post's page carries.
 * @param thumbnailUrls  - Thumbnail URLs keyed by post ID.
 * @return The field config.
 */
export function getPostsFields(
	withComparison: boolean,
	detailSearch: Record< string, unknown >,
	thumbnailUrls: PostThumbnailUrls = {}
): Field< StatsTopPostsComparisonItem >[] {
	return [
		{
			id: 'title',
			label: __( 'Post title', 'jetpack-premium-analytics-pkg' ),
			enableGlobalSearch: true,
			enableHiding: false,
			getValue: ( { item } ) => String( item.label ?? '' ),
			render: ( { item } ) => <PostTitle item={ item } detailSearch={ detailSearch } />,
		},
		{
			id: 'thumbnail',
			type: 'media',
			label: __( 'Thumbnail', 'jetpack-premium-analytics-pkg' ),
			enableHiding: false,
			render: ( { item } ) => (
				<ReportThumbnail
					thumbnailUrl={ thumbnailUrls[ Number( item.id ) ] }
					fallbackIcon={ item.type === 'page' ? pageIcon : postIcon }
				/>
			),
		},
		{
			id: 'views',
			label: __( 'Views', 'jetpack-premium-analytics-pkg' ),
			getValue: ( { item } ) => item.views,
			render: ( { item } ) => (
				<MetricWithComparison
					value={ item.views }
					previousValue={ withComparison ? item.previousViews : undefined }
					dataFormat={ VIEWS_DATA_FORMAT }
					fontSize="md"
				/>
			),
		},
	];
}