File size: 3,681 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
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
import { Context } from '@automattic/calypso-router';
import { UniversalNavbarFooter, UniversalNavbarHeader } from '@automattic/wpcom-template-parts';
import { translate, fixMe } from 'i18n-calypso';
import EmptyContent from 'calypso/components/empty-content';
import Main from 'calypso/components/main';
import { getLoginUrl } from 'calypso/landing/stepper/utils/path';
import { WeeklyReportUnsubscribe } from 'calypso/performance-profiler/pages/weekly-report/unsubscribe';
import { isUserLoggedIn } from 'calypso/state/current-user/selectors';
import getCurrentLocaleSlug from 'calypso/state/selectors/get-current-locale-slug';
import { TabTypes } from './components/header';
import { PerformanceProfilerDashboardWrapper } from './pages/dashboard';
import { WeeklyReport } from './pages/weekly-report';

import './style.scss';

export function PerformanceProfilerWrapper( {
	children,
	isLoggedIn,
}: {
	children: React.ReactNode;
	isLoggedIn: boolean;
} ): JSX.Element {
	return (
		<>
			{ isLoggedIn && <UniversalNavbarHeader isLoggedIn /> }
			<Main fullWidthLayout>{ children }</Main>
			<UniversalNavbarFooter isLoggedIn={ isLoggedIn } />
		</>
	);
}

export function PerformanceProfilerDashboardContext( context: Context, next: () => void ): void {
	const isLoggedIn = isUserLoggedIn( context.store.getState() );

	if ( ! context.query?.url ) {
		window.location.href = '/speed-test/';
		return;
	}

	const url = context.query.url.startsWith( 'http' )
		? context.query.url
		: `https://${ context.query.url }`;

	context.primary = (
		<PerformanceProfilerWrapper isLoggedIn={ isLoggedIn }>
			<PerformanceProfilerDashboardWrapper
				url={ url }
				tab={
					[ TabTypes.mobile, TabTypes.desktop ].indexOf( context.query?.tab ) !== -1
						? context.query?.tab
						: TabTypes.mobile
				}
				hash={ context.query?.hash ?? '' }
				filter={ context.query?.filter }
			/>
		</PerformanceProfilerWrapper>
	);

	next();
}

export function WeeklyReportContext( context: Context, next: () => void ): void {
	const isLoggedIn = isUserLoggedIn( context.store.getState() );

	if ( ! isLoggedIn ) {
		const logInUrl = getLoginUrl( {
			variationName: 'performance-profiler-weekly-report-subscribe',
			redirectTo: `${ window.location.protocol }//${ window.location.host }${ context.path }`,
			locale: getCurrentLocaleSlug( context.store.getState() ),
		} );

		window.location.href = logInUrl;
		return;
	}

	const url = context.query?.url?.startsWith( 'http' )
		? context.query.url
		: `https://${ context.query?.url ?? '' }`;

	context.primary = (
		<PerformanceProfilerWrapper isLoggedIn={ isLoggedIn }>
			<WeeklyReport url={ url } hash={ context.query?.hash ?? '' } />
		</PerformanceProfilerWrapper>
	);

	next();
}

export function WeeklyReportUnsubscribeContext( context: Context, next: () => void ): void {
	const isLoggedIn = isUserLoggedIn( context.store.getState() );

	const url = context.query?.url?.startsWith( 'http' )
		? context.query.url
		: `https://${ context.query?.url ?? '' }`;

	context.primary = (
		<PerformanceProfilerWrapper isLoggedIn={ isLoggedIn }>
			<WeeklyReportUnsubscribe url={ url } hash={ context.query?.hash ?? '' } />
		</PerformanceProfilerWrapper>
	);

	next();
}

export const notFound = ( context: Context, next: () => void ) => {
	context.primary = (
		<EmptyContent
			className="content-404"
			title={ fixMe( {
				text: 'Page not found.',
				newCopy: translate( 'Page not found.' ),
				oldCopy: translate( 'Uh oh. Page not Found.' ),
			} ) }
			line={ translate( 'Sorry, the page you were looking for doesnt exist or has been moved.' ) }
			action={ translate( 'Return Home' ) }
			actionURL="/"
		/>
	);

	next();
};