-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
ApiCallLoader.tsx
250 lines (235 loc) · 6.36 KB
/
ApiCallLoader.tsx
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* External dependencies
*/
import { Loader } from '@woocommerce/onboarding';
import { __ } from '@wordpress/i18n';
import { useEffect, useRef, useState } from '@wordpress/element';
import { getNewPath } from '@woocommerce/navigation';
/**
* Internal dependencies
*/
import analyzingYourResponses from '../../assets/images/loader-analyzing-your-responses.svg';
import designingTheBestLook from '../../assets/images/loader-designing-the-best-look.svg';
import comparingTheTopPerformingStores from '../../assets/images/loader-comparing-top-performing-stores.svg';
import assemblingAiOptimizedStore from '../../assets/images/loader-assembling-ai-optimized-store.svg';
import applyingFinishingTouches from '../../assets/images/loader-applying-the-finishing-touches.svg';
import generatingContent from '../../assets/images/loader-generating-content.svg';
import openingTheDoors from '../../assets/images/loader-opening-the-doors.svg';
import {
attachIframeListeners,
createAugmentedSteps,
onIframeLoad,
} from '~/customize-store/utils';
const loaderSteps = [
{
title: __( 'Analyzing your responses', 'woocommerce' ),
image: (
<img
src={ analyzingYourResponses }
alt={ __( 'Analyzing your responses', 'woocommerce' ) }
/>
),
progress: 14,
},
{
title: __( 'Comparing the top performing stores', 'woocommerce' ),
image: (
<img
src={ comparingTheTopPerformingStores }
alt={ __(
'Comparing the top performing stores',
'woocommerce'
) }
/>
),
progress: 28,
},
{
title: __( 'Designing the best look for your business', 'woocommerce' ),
image: (
<img
src={ designingTheBestLook }
alt={ __(
'Designing the best look for your business',
'woocommerce'
) }
/>
),
progress: 42,
},
{
title: __( 'Generating content', 'woocommerce' ),
image: (
<img
src={ generatingContent }
alt={ __( 'Generating content', 'woocommerce' ) }
/>
),
progress: 56,
},
{
title: __( 'Assembling your AI-optimized store', 'woocommerce' ),
image: (
<img
src={ assemblingAiOptimizedStore }
alt={ __(
'Assembling your AI-optimized store',
'woocommerce'
) }
/>
),
progress: 70,
},
{
title: __( 'Applying the finishing touches', 'woocommerce' ),
image: (
<img
src={ applyingFinishingTouches }
alt={ __( 'Applying the finishing touches', 'woocommerce' ) }
/>
),
progress: 84,
},
{
title: __( 'Opening the doors', 'woocommerce' ),
image: (
<img
src={ openingTheDoors }
alt={ __( 'Opening the doors', 'woocommerce' ) }
/>
),
progress: 100,
},
];
// Loader for the API call without the last frame.
export const ApiCallLoader = () => {
const [ progress, setProgress ] = useState( 5 );
useEffect( () => {
const preload = ( src: string ) => {
const img = new Image();
img.src = src;
img.onload = () => {};
};
// We preload the these images to avoid flickering. We only need to preload them because the others are small enough to be inlined in base64.
preload( assemblingAiOptimizedStore );
preload( openingTheDoors );
}, [] );
const augmentedSteps = createAugmentedSteps(
loaderSteps.slice( 0, -1 ),
10
);
return (
<Loader>
<Loader.Sequence
interval={ ( 40 * 1000 ) / ( augmentedSteps.length - 1 ) }
shouldLoop={ false }
onChange={ ( index ) => {
// to get around bad set state timing issue
setTimeout( () => {
setProgress( augmentedSteps[ index ].progress );
}, 0 );
} }
>
{ augmentedSteps.map( ( step, index ) => (
<Loader.Layout key={ index }>
<Loader.Illustration>
{ step.image }
</Loader.Illustration>
<Loader.Title>{ step.title }</Loader.Title>
</Loader.Layout>
) ) }
</Loader.Sequence>
<Loader.ProgressBar
className="smooth-transition"
progress={ progress || 0 }
/>
</Loader>
);
};
const AssemblerHub = () => {
const assemblerUrl = getNewPath( {}, '/customize-store/assembler-hub', {} );
const iframe = useRef< HTMLIFrameElement | null >( null );
const [ isVisible, setIsVisible ] = useState( false );
useEffect( () => {
const currentIframe = iframe.current;
if ( currentIframe === null ) {
return;
}
window.addEventListener(
'popstate',
() => {
const apiLoaderUrl = getNewPath(
{},
'/customize-store/design-with-ai/api-call-loader',
{}
);
// Only catch the back button click when the user is on the main assember hub page
// and trying to go back to the api loader page
if ( 'admin.php' + window.location.search === apiLoaderUrl ) {
currentIframe.contentWindow?.postMessage(
{
type: 'assemberBackButtonClicked',
},
'*'
);
// When the user clicks the back button, push state changes to the previous step
// Set it back to the assembler hub
window.history?.pushState( {}, '', assemblerUrl );
}
},
false
);
}, [ assemblerUrl, iframe ] );
return (
<iframe
ref={ iframe }
onLoad={ ( frame ) => {
const showIframe = () => setIsVisible( true );
attachIframeListeners( frame.currentTarget );
onIframeLoad( showIframe );
// Ceiling wait time set to 60 seconds
setTimeout( showIframe, 60 * 1000 );
window.parent.history?.pushState( {}, '', assemblerUrl );
} }
style={ { opacity: isVisible ? 1 : 0 } }
src={ assemblerUrl }
title="assembler-hub"
className="cys-fullscreen-iframe"
/>
);
};
export const AssembleHubLoader = () => {
// Show the last two steps of the loader so that the last frame is the shortest time possible
const augmentedSteps = createAugmentedSteps( loaderSteps.slice( -2 ), 10 );
const [ progress, setProgress ] = useState( augmentedSteps[ 0 ].progress );
return (
<>
<Loader>
<Loader.Sequence
interval={ ( 10 * 1000 ) / ( augmentedSteps.length - 1 ) }
shouldLoop={ false }
onChange={ ( index ) => {
// to get around bad set state timing issue
setTimeout( () => {
setProgress( augmentedSteps[ index ].progress );
}, 0 );
} }
>
{ augmentedSteps.map( ( step, index ) => (
<Loader.Layout key={ index }>
<Loader.Illustration>
{ step.image }
</Loader.Illustration>
<Loader.Title>{ step.title }</Loader.Title>
</Loader.Layout>
) ) }
</Loader.Sequence>
<Loader.ProgressBar
className="smooth-transition"
progress={ progress || 0 }
/>
</Loader>
<AssemblerHub />
</>
);
};