File size: 3,856 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
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
import { Gridicon } from '@automattic/components';
import styled from '@emotion/styled';
import { useTranslate } from 'i18n-calypso';
import { useState } from 'react';
import { useInterval } from 'calypso/lib/interval/use-interval';

const LoadingProgressContainer = styled.div`
	span {
		display: flex;
		align-items: center;
		font-size: 16px;
		font-weight: 400;
		line-height: 24px;
		margin-bottom: 15px;
		position: relative;

		.gridicons-checkmark {
			display: none;
		}

		&:before {
			content: '';
			display: inline-block;
			width: 1.875em;
			height: 1.875em;
			border-radius: 50%;
			margin-right: 10px;
			border: 1px solid transparent;
		}

		@keyframes rotate {
			from {
				transform: rotate( 0deg );
			}
			to {
				transform: rotate( 360deg );
			}
		}

		@-webkit-keyframes rotate {
			from {
				-webkit-transform: rotate( 0deg );
			}
			to {
				-webkit-transform: rotate( 360deg );
			}
		}

		&.complete {
			.gridicons-checkmark {
				display: block;
				position: absolute;
				height: 100%;
				left: 0.4375em;
				fill: #fff;
				width: 1.125em;
			}
			:before {
				background-color: var( --studio-green-30 );
				color: #fff;
			}
		}

		&.running:before {
			border: 1px solid var( --studio-gray-5 );
		}

		&.running:after {
			content: '';
			width: 1.875em;
			height: 1.875em;
			position: absolute;
			left: 0;
			top: 0;
			border: solid 1.5px rgba( 56, 88, 233, 1 );
			border-radius: 50%;
			border-right-color: transparent;
			border-bottom-color: transparent;
			-webkit-transition: all 0.5s ease-in;
			-webkit-animation-name: rotate;
			-webkit-animation-duration: 1s;
			-webkit-animation-iteration-count: infinite;
			-webkit-animation-timing-function: linear;

			transition: all 0.5s ease-in;
			animation-name: rotate;
			animation-duration: 1s;
			animation-iteration-count: infinite;
			animation-timing-function: linear;
		}

		&.incomplete {
			color: var( --studio-gray-50 );
			::before {
				border: 1px dashed var( --studio-gray-5 );
			}
		}
	}
`;

const useLoadingSteps = ( {
	isSavedReport,
	pageTitle,
	isLoadingPages,
}: {
	isSavedReport: boolean;
	pageTitle?: string;
	isLoadingPages?: boolean;
} ) => {
	const translate = useTranslate();

	const [ step, setStep ] = useState( 0 );

	let steps = [];

	if ( isLoadingPages ) {
		steps = [ translate( 'Getting your site pages' ) ];
	} else {
		steps = isSavedReport
			? [ translate( 'Getting your report' ) ]
			: [
					pageTitle
						? translate( 'Loading: %(pageTitle)s', { args: { pageTitle } } )
						: translate( 'Loading your site' ),
					translate( 'Measuring Core Web Vitals' ),
					translate( 'Taking screenshots' ),
					translate( 'Fetching historic data' ),
					translate( 'Identifying performance improvements' ),
					translate( 'Finalizing your results' ),
			  ];
	}

	useInterval(
		() => {
			setStep( ( step ) => step + 1 );
		},
		// 5 seconds between steps, except make sure we stop _before_ completing the last step
		step < steps.length - 1 && 5000 // 5 seconds
	);

	const stepStatus = ( index: number, step: number ) => {
		if ( step > index ) {
			return 'complete';
		}
		if ( step === index ) {
			return 'running';
		}
		return 'incomplete';
	};

	return {
		step,
		steps,
		stepStatus,
	};
};

export const PerformanceReportLoadingProgress = ( {
	pageTitle,
	isSavedReport,
	isLoadingPages,
	className,
}: {
	isSavedReport: boolean;
	pageTitle?: string;
	className?: string;
	isLoadingPages?: boolean;
} ) => {
	const { step, steps, stepStatus } = useLoadingSteps( {
		isSavedReport,
		pageTitle,
		isLoadingPages,
	} );

	return (
		<LoadingProgressContainer className={ className }>
			{ steps.map( ( heading, index ) => (
				<span key={ index } className={ stepStatus( index, step ) }>
					<Gridicon icon="checkmark" />
					{ heading }
				</span>
			) ) }
		</LoadingProgressContainer>
	);
};