Back

0% Statements 0/15
0% Branches 0/5
0% Functions 0/8
0% Lines 0/12

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 117 118 119 120 121 122 123 124 125 126                                                                                                                                                                                                                                                           
/**
 * External dependencies
 */
import { useStatsArchives } from '@jetpack-premium-analytics/data';
import { Stack } from '@jetpack-premium-analytics/externals';
import {
	DataViewsDrilldownNative,
	WidgetRoot,
	WidgetState,
	buildArchiveRows,
	describeError,
	getPostsReportQueryParams,
	useWidgetRootContext,
	type ArchiveRow,
	type ReportParamsFieldAttributes,
} from '@jetpack-premium-analytics/widgets-toolkit';
import { __ } from '@wordpress/i18n';
import { useMemo } from 'react';
/**
 * Internal dependencies
 */
import { getArchivesFields } from './fields';
import styles from './style.module.css';
import type { WidgetRenderProps } from '@wordpress/widget-primitives';
 
export type ArchivesTableAttributes = Record< never, never >;
 
type ArchivesTableRenderAttributes = ArchivesTableAttributes &
	Partial< ReportParamsFieldAttributes >;
 
const INITIAL_VIEW = {
	sort: { field: 'views', direction: 'desc' as const },
	layout: {
		styles: {
			views: { align: 'end' as const },
		},
	},
};
 
/**
 * Stable row id for the archives table.
 *
 * @param item - The archive row.
 * @return The row id.
 */
function getArchiveRowId( item: ArchiveRow ): string {
	return item.id;
}
 
/**
 * The parent row of a nested archive row.
 *
 * @param item - The archive row.
 * @return The parent row ID, if the row is nested.
 */
function getArchiveRowParentId( item: ArchiveRow ): string | undefined {
	return item.parentId;
}
 
/**
 * Every archive page of the period, grouped by type, as a searchable table.
 *
 * @return The table, or the state that stands in for it.
 */
function ArchivesTable() {
	const { reportParams } = useWidgetRootContext();
 
	const recordsParams = useMemo(
		() => getPostsReportQueryParams( reportParams ),
		[ reportParams ]
	);
	const { comparisonRows, hasComparison, isLoading, isFetching, isError, error, refetch } =
		useStatsArchives( recordsParams );
	const items = useMemo( () => comparisonRows?.rows ?? [], [ comparisonRows ] );
	const rows = useMemo( () => buildArchiveRows( items ), [ items ] );
	const fields = useMemo( () => getArchivesFields( hasComparison ), [ hasComparison ] );
 
	return (
		<Stack direction="column" className={ styles.root }>
			<WidgetState
				isLoading={ isLoading }
				isFetching={ isFetching }
				isError={ rows.length === 0 && isError }
				isEmpty={ rows.length === 0 }
				error={ describeError( error, {
					retryDescription: __(
						"We couldn't load posts. Please try again in a moment.",
						'jetpack-premium-analytics-pkg'
					),
					onRetry: refetch,
				} ) }
			>
				<div className={ styles.table }>
					<DataViewsDrilldownNative< ArchiveRow >
						data={ rows }
						fields={ fields }
						getItemId={ getArchiveRowId }
						getItemParentId={ getArchiveRowParentId }
						initialView={ INITIAL_VIEW }
						searchLabel={ __( 'Search archives', 'jetpack-premium-analytics-pkg' ) }
						hideLevelMarkers
						collapsible
						defaultExpanded="none"
					/>
				</div>
			</WidgetState>
		</Stack>
	);
}
 
/**
 * The archives table of the All pages report.
 *
 * @param {WidgetRenderProps} props - The host's props.
 * @return The widget.
 */
export default function ArchivesTableWidget( {
	attributes = {},
}: WidgetRenderProps< ArchivesTableRenderAttributes > ) {
	return (
		<WidgetRoot attributes={ attributes }>
			<ArchivesTable />
		</WidgetRoot>
	);
}