/**
* 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: (
),
progress: 14,
},
{
title: __( 'Comparing the top performing stores', 'woocommerce' ),
image: (
),
progress: 28,
},
{
title: __( 'Designing the best look for your business', 'woocommerce' ),
image: (
),
progress: 42,
},
{
title: __( 'Generating content', 'woocommerce' ),
image: (
),
progress: 56,
},
{
title: __( 'Assembling your AI-optimized store', 'woocommerce' ),
image: (
),
progress: 70,
},
{
title: __( 'Applying the finishing touches', 'woocommerce' ),
image: (
),
progress: 84,
},
{
title: __( 'Opening the doors', 'woocommerce' ),
image: (
),
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 (
{
// to get around bad set state timing issue
setTimeout( () => {
setProgress( augmentedSteps[ index ].progress );
}, 0 );
} }
>
{ augmentedSteps.map( ( step, index ) => (
{ step.image }
{ step.title }
) ) }
);
};
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 (