diff --git a/plugins/woocommerce-admin/client/customize-store/assembler-hub/sidebar/sidebar-navigation-screen.tsx b/plugins/woocommerce-admin/client/customize-store/assembler-hub/sidebar/sidebar-navigation-screen.tsx index 1f7cae0b47749..f42c1db8cc504 100644 --- a/plugins/woocommerce-admin/client/customize-store/assembler-hub/sidebar/sidebar-navigation-screen.tsx +++ b/plugins/woocommerce-admin/client/customize-store/assembler-hub/sidebar/sidebar-navigation-screen.tsx @@ -54,7 +54,7 @@ export const SidebarNavigationScreen = ( { backPath?: string; onNavigateBackClick?: () => void; } ) => { - const { sendEvent, context } = useContext( CustomizeStoreContext ); + const { context } = useContext( CustomizeStoreContext ); const [ openWarningModal, setOpenWarningModal ] = useState< boolean >( false ); const location = useLocation(); @@ -148,10 +148,10 @@ export const SidebarNavigationScreen = ( { { - sendEvent( + window.parent.__wcCustomizeStore.sendEventToIntroMachine( flowType && isAIFlow( flowType ) - ? 'GO_BACK_TO_DESIGN_WITH_AI' - : 'GO_BACK_TO_DESIGN_WITHOUT_AI' + ? { type: 'GO_BACK_TO_DESIGN_WITH_AI' } + : { type: 'GO_BACK_TO_DESIGN_WITHOUT_AI' } ); } } /> diff --git a/plugins/woocommerce-admin/client/customize-store/design-with-ai/actions.ts b/plugins/woocommerce-admin/client/customize-store/design-with-ai/actions.ts index 061e0dd7f97c8..b3dc42ac2f09e 100644 --- a/plugins/woocommerce-admin/client/customize-store/design-with-ai/actions.ts +++ b/plugins/woocommerce-admin/client/customize-store/design-with-ai/actions.ts @@ -2,11 +2,7 @@ * External dependencies */ import { assign, spawn } from 'xstate'; -import { - getQuery, - updateQueryString, - getNewPath, -} from '@woocommerce/navigation'; +import { getQuery, updateQueryString } from '@woocommerce/navigation'; import { recordEvent } from '@woocommerce/tracks'; import { dispatch } from '@wordpress/data'; import { OPTIONS_STORE_NAME } from '@woocommerce/data'; @@ -29,7 +25,6 @@ import { lookAndFeelCompleteEvent, toneOfVoiceCompleteEvent, } from './pages'; -import { attachIframeListeners, onIframeLoad } from '../utils'; const assignStartLoadingTime = assign< designWithAiStateMachineContext, @@ -292,50 +287,7 @@ const recordTracksStepCompleted = ( } ); }; -const redirectToAssemblerHub = async ( - context: designWithAiStateMachineContext -) => { - const assemblerUrl = getNewPath( {}, '/customize-store/assembler-hub', {} ); - const iframe = document.createElement( 'iframe' ); - iframe.classList.add( 'cys-fullscreen-iframe' ); - iframe.src = assemblerUrl; - - const showIframe = () => { - if ( iframe.style.opacity === '1' ) { - // iframe is already visible - return; - } - - const loader = document.getElementsByClassName( - 'woocommerce-onboarding-loader' - ); - if ( loader[ 0 ] ) { - ( loader[ 0 ] as HTMLElement ).style.display = 'none'; - } - - iframe.style.opacity = '1'; - - if ( context.startLoadingTime ) { - const endLoadingTime = performance.now(); - const timeToLoad = endLoadingTime - context.startLoadingTime; - recordEvent( 'customize_your_store_ai_wizard_loading_time', { - time_in_s: ( timeToLoad / 1000 ).toFixed( 2 ), - } ); - } - }; - - iframe.onload = () => { - // Hide loading UI - attachIframeListeners( iframe ); - onIframeLoad( showIframe ); - - // Ceiling wait time set to 60 seconds - setTimeout( showIframe, 60 * 1000 ); - window.history?.pushState( {}, '', assemblerUrl ); - }; - - document.body.appendChild( iframe ); - +const redirectToAssemblerHub = async () => { // This is a workaround to update the "activeThemeHasMods" in the parent's machine // state context. We should find a better way to do this using xstate actions, // since state machines should rely only on their context. @@ -345,33 +297,6 @@ const redirectToAssemblerHub = async ( // than the parent window. // Check https://github.com/woocommerce/woocommerce/pull/44206 for more details. window.parent.__wcCustomizeStore.activeThemeHasMods = true; - - // Listen for back button click - 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 ) { - iframe.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 - ); }; export const actions = { diff --git a/plugins/woocommerce-admin/client/customize-store/design-with-ai/pages/ApiCallLoader.tsx b/plugins/woocommerce-admin/client/customize-store/design-with-ai/pages/ApiCallLoader.tsx index fd86fb4c1640c..e369d2dc51b0e 100644 --- a/plugins/woocommerce-admin/client/customize-store/design-with-ai/pages/ApiCallLoader.tsx +++ b/plugins/woocommerce-admin/client/customize-store/design-with-ai/pages/ApiCallLoader.tsx @@ -3,7 +3,8 @@ */ import { Loader } from '@woocommerce/onboarding'; import { __ } from '@wordpress/i18n'; -import { useEffect, useState } from '@wordpress/element'; +import { useEffect, useRef, useState } from '@wordpress/element'; +import { getNewPath } from '@woocommerce/navigation'; /** * Internal dependencies @@ -15,7 +16,11 @@ import assemblingAiOptimizedStore from '../../assets/images/loader-assembling-ai 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 { createAugmentedSteps } from '~/customize-store/utils'; +import { + attachIframeListeners, + createAugmentedSteps, + onIframeLoad, +} from '~/customize-store/utils'; const loaderSteps = [ { @@ -150,6 +155,62 @@ export const ApiCallLoader = () => { ); }; +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 ( +