File size: 2,469 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
// Copied and modified from woocommerce: https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce-admin/client/core-profiler/pages/Loader.tsx#L26
// TODO: Remove this once we create a reusable component
import { useState, useEffect } from '@wordpress/element';
import clsx from 'clsx';
import { HTMLAttributes } from 'react';

import './woo-loader.scss';

export type Stage = {
	title: string;
	image?: React.ComponentType;
	label: string;
	text: string;
	duration?: number;
	element?: JSX.Element;
	progress: number;
};

export type Stages = Array< Stage >;

type ProgressBarProps = {
	className?: string;
	percent?: number;
	color?: string;
	bgcolor?: string;
};

const ProgressBar = ( {
	className = '',
	percent = 0,
	color = '#674399',
	bgcolor = 'var(--wp-admin-theme-color)',
}: ProgressBarProps ) => {
	const containerStyles = {
		backgroundColor: bgcolor,
	};

	const fillerStyles: HTMLAttributes< HTMLDivElement >[ 'style' ] = {
		backgroundColor: color,
		width: `${ percent }%`,
		display: percent === 0 ? 'none' : 'inherit',
	};

	return (
		<div className={ `jetpack-connect-progress-bar ${ className }` }>
			<div className="jetpack-connect-progress-bar__container" style={ containerStyles }>
				<div className="jetpack-connect-progress-bar__filler" style={ fillerStyles } />
			</div>
		</div>
	);
};

type Props = {
	className: string;
	stages: Stage[];
};

export const WooLoader = ( { className, stages }: Props ) => {
	const [ currentStageIndex, setCurrentStageIndex ] = useState( 0 );
	const stage = stages[ currentStageIndex ];

	useEffect( () => {
		const interval = setInterval(
			() => {
				setCurrentStageIndex( ( _currentStageIndex ) =>
					stages[ _currentStageIndex + 1 ] ? _currentStageIndex + 1 : 0
				);
			},
			stages[ currentStageIndex ]?.duration ?? 3000
		);

		return () => clearInterval( interval );
	}, [ stages, currentStageIndex ] );

	return (
		<div className={ clsx( 'jetpack-connect-woocommerce-loader', className ) }>
			<div className="jetpack-connect-loader-wrapper">
				{ stage.image && <stage.image /> }

				<h1 className="jetpack-connect-loader__title">{ stage.title }</h1>
				<ProgressBar
					className="progress-bar"
					percent={ stage.progress ?? 0 }
					color="var(--wp-admin-theme-color)"
					bgcolor="#E0E0E0"
				/>
				<p className="jetpack-connect-loader__paragraph">
					<b>{ stage?.label } </b>
					{ stage?.text }
					{ stage?.element }
				</p>
			</div>
		</div>
	);
};