Back

100% Statements 26/26
100% Branches 16/16
100% Functions 17/17
100% Lines 26/26

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                                                                                                                                                14x         4x           8x         9x           8x           6x             8x               7x           9x           51x             16x             8x           4x           47x                 16x             4x                             271x   271x 3x     268x 6x     262x                               98x 98x   98x       98x    
/**
 * External dependencies
 */
import { isSimpleSite } from '@automattic/jetpack-script-data';
import { isDashboardSectionAvailable, POST_DETAIL_SECTION } 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 { resolveTabId } from './posts/config/tabs';
import { resolveSection as resolveUtmSection } from './utm/config/tabs';
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 lazy 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 loader to
 * `report-page-loaders.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,
	},
	posts: {
		id: 'posts',
		feature: 'top-posts',
		dashboardSection: POST_DETAIL_SECTION,
		getLabel: () => __( 'All pages', 'jetpack-premium-analytics-pkg' ),
		resolveSection: resolveTabId,
	},
	'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' ),
	},
};
 
/**
 * Look up a report definition by its id.
 *
 * 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 definition = id && Object.hasOwn( REPORTS, id ) ? REPORTS[ id ] : undefined;
 
	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 };
}