Back

100% Statements 22/22
84.61% Branches 11/13
60% Functions 3/5
100% Lines 22/22

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                                    2x             2x   39x 1x     38x 38x 1x 1x 1x                 37x 1x 1x 1x               36x 36x   36x 15x     36x 17x                               1x 1x 1x      
/**
 * External dependencies
 */
import { getSectionDatePolicy } from '@jetpack-premium-analytics/data';
import { store as coreStore } from '@wordpress/core-data';
import { resolveSelect } from '@wordpress/data';
import { redirect } from '@wordpress/route';
/**
 * Internal dependencies
 */
import { dateWindowRedirect } from '../date-window-redirect';
import { getConnectionlessSectionSlugs, isPremiumAnalyticsSiteConnected } from '../site-readiness';
import { dashboardSectionOf, withoutCarriedYearWindow } from '../stats-date-window';
import { WIDGET_MODULES_QUERY } from '../use-widget-modules';
import { DASHBOARD_SECTIONS_QUERY } from './hooks/use-dashboard-sections';
import { seedDashboardDateSearch, type DashboardSearch } from './section-date-window';
 
// Settings was a dashboard tab before it had a route; the server keeps the slug reserved.
const SETTINGS_SECTION = 'settings';
 
/**
 * Route lifecycle for the dashboard. The initial analytics sync is not a guard
 * — only the store section waits on it and shows progress meanwhile (see
 * `stage.tsx`).
 */
export const route = {
	beforeLoad: async ( { search }: { search?: DashboardSearch } = {} ) => {
		if ( ! isPremiumAnalyticsSiteConnected() && getConnectionlessSectionSlugs().length === 0 ) {
			throw redirect( { to: '/connect' } );
		}
 
		const current: DashboardSearch = search ?? {};
		if ( current.section === SETTINGS_SECTION ) {
			const settingsSearch = { ...current };
			delete settingsSearch.section;
			throw redirect( {
				to: '/settings',
				replace: true,
				// Cast for the same reason as the redirect below.
				search: settingsSearch as unknown as never,
			} );
		}
 
		// Only the AI company page scopes by company; carried here, it would filter the AI tab's widgets.
		if ( current.company !== undefined ) {
			const unscoped = { ...current };
			delete unscoped.company;
			throw redirect( {
				to: '/',
				replace: true,
				// Cast for the same reason as the redirect below.
				search: unscoped as unknown as never,
			} );
		}
 
		const policy = getSectionDatePolicy( dashboardSectionOf( current.section ) );
		const params = withoutCarriedYearWindow( current, policy );
 
		const limited = dateWindowRedirect( params, policy, {
			seed: next => seedDashboardDateSearch( next, policy ),
			requested: params,
		} );
		if ( limited ) {
			throw redirect( {
				to: '/',
				replace: true,
				viewTransition: false,
				/*
				 * The router is built dynamically, so '/' has no typed search schema
				 * (TanStack widens it to `never`); cast as the routing package does.
				 */
				search: limited.search as unknown as never,
				state: limited.state,
			} );
		}
	},
	// Boot runs this alongside its REST index fetch, so the sections and widget types the stage
	// reads go out in parallel with it. Same queries as the hooks, or core-data fetches twice.
	loader: () => {
		const core = resolveSelect( coreStore );
		core.getEntityRecords( 'root', 'dashboardSection', DASHBOARD_SECTIONS_QUERY ).catch( () => {} );
		core.getEntityRecords( 'root', 'widgetModule', WIDGET_MODULES_QUERY ).catch( () => {} );
	},
};