File size: 2,503 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { isEnabled } from '@automattic/calypso-config';
import page, { type Callback } from '@automattic/calypso-router';
import { UniversalNavbarFooter } from '@automattic/wpcom-template-parts';
import Main from 'calypso/components/main';
import BodySectionCssClass from 'calypso/layout/body-section-css-class';
import { isUserLoggedIn } from 'calypso/state/current-user/selectors';
import SiteProfiler from './components/site-profiler';
import SiteProfilerV2 from './components/site-profiler-v2';

let SiteProfilerComponent = SiteProfiler;

if ( isEnabled( 'site-profiler/metrics' ) ) {
	SiteProfilerComponent = SiteProfilerV2;
}

export const handleDomainQueryParam: Callback = ( context, next ) => {
	const { querystring } = context;
	const queryParams = new URLSearchParams( querystring );
	const domainQueryParam = queryParams.get( 'domain' ) || '';

	if ( ! domainQueryParam ) {
		next();
	} else {
		page.redirect( `/site-profiler/${ domainQueryParam }` );
	}
};

export const redirectToBaseSiteProfilerRoute: Callback = ( context ) => {
	const { params, querystring } = context;

	if ( params?.domain ) {
		page.redirect( `/site-profiler/${ params.domain }` );
	} else if ( querystring ) {
		page.redirect( `/site-profiler?${ querystring }` );
	} else {
		page.redirect( '/site-profiler' );
	}
};

export const siteProfilerContext: Callback = ( context, next ) => {
	const isLoggedIn = isUserLoggedIn( context.store.getState() );
	const pathName = context.pathname || '';
	const routerDomain = pathName.split( '/site-profiler/' )[ 1 ]?.trim() || '';

	context.primary = (
		<>
			<Main fullWidthLayout>
				<BodySectionCssClass bodyClass={ [ 'is-global' ] } />
				<SiteProfilerComponent routerDomain={ routerDomain } />
			</Main>

			<UniversalNavbarFooter isLoggedIn={ isLoggedIn } />
		</>
	);

	next();
};

export const siteProfilerReportContext: Callback = ( context, next ) => {
	const isLoggedIn = isUserLoggedIn( context.store.getState() );
	const pathName = context.pathname || '';
	const routerParams = pathName.split( '/site-profiler/report/' )[ 1 ]?.trim() || '';
	const routerDomain = routerParams.split( '/' ).slice( 1 ).join( '/' );

	context.primary = (
		<>
			<Main fullWidthLayout>
				<BodySectionCssClass bodyClass={ [ 'is-global' ] } />
				<SiteProfilerComponent
					routerDomain={ routerDomain }
					hash={ context.params.hash }
					routerOrigin={ context.query?.ref }
				/>
			</Main>

			<UniversalNavbarFooter isLoggedIn={ isLoggedIn } />
		</>
	);

	next();
};