Back

100% Statements 37/37
92.85% Branches 26/28
100% Functions 22/22
100% Lines 35/35

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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256                                                                                                                                                11x         4x           8x         9x           10x           6x             10x               7x           9x           51x             10x           4x           49x                 17x             4x                       166x 166x 138x       28x                     4x   4x     2x       6x                                   165x 165x 165x   165x 3x     162x 5x     157x                               61x 61x   61x       61x    
/**
 * External dependencies
 */
import { getScriptData, isSimpleSite } from '@automattic/jetpack-script-data';
import { isDashboardSectionAvailable } from '@jetpack-premium-analytics/data';
import { __ } from '@wordpress/i18n';
/**
 * Internal dependencies
 */
import { isVideoPressAvailable } from '../site-readiness';
// Import from `config/tabs` directly, not the `config` barrel — it re-exports JSX,
// and `route.ts` imports this registry in `beforeLoad`, which must stay React-free.
import { resolveTabId as resolveCommentsTabId } from './comments/config/tabs';
import { resolveSection as resolveEarningsSection } from './earnings/config/tabs';
import { resolveSection as resolveLocationsSection } from './locations/config/tabs';
import { resolveSection as resolveUtmSection } from './utm/config/tabs';
import type { ReportPageRecord } from '../single-report/registry';
import type { DashboardSectionSlug, StatsFeatureKey } from '@jetpack-premium-analytics/data';
 
/**
 * A single report's registration in the report registry.
 *
 * Labels are getters rather than plain strings so translations apply after the
 * i18n locale data has loaded.
 */
export type ReportDefinition = {
	/**
	 * Stable, URL-friendly identifier. Matches the `$report` path segment
	 * (e.g. `/reports/posts`) and keys the component in the stage.
	 */
	id: string;
 
	/**
	 * Dashboard tab the report belongs to. Unrelated to `resolveSection`, which names
	 * a tab *inside* the report.
	 */
	dashboardSection: DashboardSectionSlug;
 
	/**
	 * The report's name: its trailing crumb, the crumb back to it from a detail
	 * page, and the heading over its records on an untabbed report. `All pages`.
	 */
	getLabel: () => string;
 
	/**
	 * Resolve a raw `?section=` value to a section this report owns, falling back
	 * to its default, so a shareable URL never persists a section the report can't
	 * render. Omit for reports that have no sections.
	 */
	resolveSection?: ( value: string | undefined ) => string;
 
	/**
	 * Whether the report is available on this site, resolved at lookup time.
	 * An unavailable report is treated like an unknown one — the route guard
	 * redirects it to the dashboard. Omit for reports available everywhere.
	 */
	isAvailable?: () => boolean;
 
	/**
	 * The paid Stats feature the report shows; a plan without it gets the locked page. Omit for
	 * reports every plan includes.
	 */
	feature?: StatsFeatureKey;
};
 
/**
 * The report registry: one entry per report, keyed by its `id`.
 *
 * To add a report, drop a `<id>/` folder under `routes/reports/` that
 * default-exports its page component, add an entry here and its component to
 * `report-pages.ts`; no new route needed.
 */
export const REPORTS: Record< string, ReportDefinition > = {
	'annual-insights': {
		id: 'annual-insights',
		feature: 'insights',
		dashboardSection: 'insights',
		getLabel: () => __( 'Annual insights', 'jetpack-premium-analytics-pkg' ),
	},
	authors: {
		id: 'authors',
		feature: 'authors',
		dashboardSection: 'traffic',
		getLabel: () => __( 'Top authors', 'jetpack-premium-analytics-pkg' ),
	},
	'comment-followers': {
		id: 'comment-followers',
		dashboardSection: 'subscribers',
		getLabel: () => __( 'Comments Subscribers', 'jetpack-premium-analytics-pkg' ),
	},
	clicks: {
		id: 'clicks',
		feature: 'clicks',
		dashboardSection: 'traffic',
		getLabel: () => __( 'Clicks', 'jetpack-premium-analytics-pkg' ),
	},
	comments: {
		id: 'comments',
		feature: 'insights',
		dashboardSection: 'insights',
		getLabel: () => __( 'All comments', 'jetpack-premium-analytics-pkg' ),
		resolveSection: resolveCommentsTabId,
	},
	downloads: {
		id: 'downloads',
		feature: 'file-downloads',
		dashboardSection: 'traffic',
		getLabel: () => __( 'File downloads', 'jetpack-premium-analytics-pkg' ),
		// Download tracking is WPCOM-only, so Calypso shows the module on Simple
		// sites only; mirrors the gate in `src/widget-type-support.php`.
		isAvailable: isSimpleSite,
	},
	earnings: {
		id: 'earnings',
		dashboardSection: 'ads',
		getLabel: () => __( 'Earnings history', 'jetpack-premium-analytics-pkg' ),
		resolveSection: resolveEarningsSection,
	},
	emails: {
		id: 'emails',
		dashboardSection: 'subscribers',
		getLabel: () => __( 'Emails', 'jetpack-premium-analytics-pkg' ),
	},
	locations: {
		id: 'locations',
		feature: 'countries',
		dashboardSection: 'traffic',
		getLabel: () => __( 'All locations', 'jetpack-premium-analytics-pkg' ),
		resolveSection: resolveLocationsSection,
	},
	'search-terms': {
		id: 'search-terms',
		feature: 'search-terms',
		dashboardSection: 'traffic',
		getLabel: () => __( 'Search terms', 'jetpack-premium-analytics-pkg' ),
	},
	tags: {
		id: 'tags',
		feature: 'tags',
		dashboardSection: 'traffic',
		getLabel: () => __( 'Tags & categories', 'jetpack-premium-analytics-pkg' ),
	},
	videos: {
		id: 'videos',
		feature: 'videos',
		dashboardSection: 'traffic',
		getLabel: () => __( 'Videos', 'jetpack-premium-analytics-pkg' ),
		// Play counts only exist for VideoPress-hosted videos; mirrors the gate in
		// `src/widget-type-support.php`.
		isAvailable: isVideoPressAvailable,
	},
	utm: {
		id: 'utm',
		feature: 'utm',
		dashboardSection: 'traffic',
		getLabel: () => __( 'All UTM values', 'jetpack-premium-analytics-pkg' ),
		resolveSection: resolveUtmSection,
	},
	referrers: {
		id: 'referrers',
		feature: 'referrers',
		dashboardSection: 'traffic',
		getLabel: () => __( 'Referrers', 'jetpack-premium-analytics-pkg' ),
	},
};
 
/**
 * The reports page the server registered under a path, as it publishes it in
 * `premium_analytics.reports_pages`: a report page of widgets, without an entity scope.
 *
 * @param id - The `$report` path segment.
 * @return The page record, or undefined when no registered page uses the path.
 */
export function getReportsPage( id: string | undefined ): ReportPageRecord | undefined {
	const pages = getScriptData()?.premium_analytics?.reports_pages;
	if ( ! id || ! Array.isArray( pages ) ) {
		return undefined;
	}
 
	// The script data type keeps the layout loose; the server serializes `DashboardWidget` instances.
	return pages.find( candidate => candidate.path === id ) as unknown as
		ReportPageRecord | undefined;
}
 
/**
 * The definition of a registered reports page: what the guards and the crumbs read of it.
 *
 * @param page - The page record.
 * @return The definition.
 */
function toReportDefinition( page: ReportPageRecord ): ReportDefinition {
	const { sections } = page;
 
	return {
		id: page.path,
		dashboardSection: page.dashboard_section as DashboardSectionSlug,
		getLabel: () => page.label,
		...( sections.length
			? {
					resolveSection: value =>
						( sections.find( section => section.slug === value ) ?? sections[ 0 ] ).slug,
				}
			: {} ),
		...( page.feature ? { feature: page.feature as StatsFeatureKey } : {} ),
	};
}
 
/**
 * Look up a report definition by its id: a registered reports page first, then the bundled map.
 *
 * The id comes from the URL, so this is an own-property check: a plain index
 * would resolve an inherited name such as `constructor` to a function.
 *
 * @param id - The `$report` path segment (may be missing on a malformed URL).
 * @return The matching definition, or `undefined` when the id is missing, unknown, or in a
 * section the dashboard does not expose.
 */
export function getReportDefinition( id: string | undefined ): ReportDefinition | undefined {
	const page = getReportsPage( id );
	const bundled = id && Object.hasOwn( REPORTS, id ) ? REPORTS[ id ] : undefined;
	const definition = page ? toReportDefinition( page ) : bundled;
 
	if ( definition?.isAvailable && ! definition.isAvailable() ) {
		return undefined;
	}
 
	if ( definition && ! isDashboardSectionAvailable( definition.dashboardSection ) ) {
		return undefined;
	}
 
	return definition;
}
 
/**
 * Resolve the report origin behind a detail breadcrumb.
 *
 * A section is kept only when it round-trips through the referring report's own
 * resolver, so a section from a different report is never linked.
 *
 * @param origin - The report origin read from the current search params.
 * @return The referring report definition and its validated section, when valid.
 */
export function resolveReportOrigin( origin: { report: string; section?: string } | undefined ): {
	definition?: ReportDefinition;
	section?: string;
} {
	const definition = getReportDefinition( origin?.report );
	const rawSection = origin?.section;
	const section =
		rawSection && definition?.resolveSection?.( rawSection ) === rawSection
			? rawSection
			: undefined;
 
	return { definition, section };
}