2023-10-09 05:19:08 +00:00
|
|
|
// @ts-expect-error -- No types for this exist yet.
|
|
|
|
// eslint-disable-next-line @woocommerce/dependency-group
|
|
|
|
import { store as coreStore } from '@wordpress/core-data';
|
2023-08-18 05:30:25 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2024-02-05 11:36:33 +00:00
|
|
|
import { Sender, createMachine } from 'xstate';
|
2023-08-18 05:30:25 +00:00
|
|
|
import { useEffect, useMemo, useState } from '@wordpress/element';
|
|
|
|
import { useMachine, useSelector } from '@xstate/react';
|
2023-09-21 02:06:39 +00:00
|
|
|
import {
|
|
|
|
getNewPath,
|
|
|
|
getQuery,
|
|
|
|
updateQueryString,
|
2024-04-22 09:26:32 +00:00
|
|
|
getHistory,
|
2023-09-21 02:06:39 +00:00
|
|
|
} from '@woocommerce/navigation';
|
2023-09-19 11:02:02 +00:00
|
|
|
import { OPTIONS_STORE_NAME } from '@woocommerce/data';
|
2023-10-09 05:19:08 +00:00
|
|
|
import { dispatch, resolveSelect } from '@wordpress/data';
|
|
|
|
import { Spinner } from '@woocommerce/components';
|
2023-10-05 13:33:50 +00:00
|
|
|
import { getAdminLink } from '@woocommerce/settings';
|
2023-11-01 11:40:12 +00:00
|
|
|
import { PluginArea } from '@wordpress/plugins';
|
2024-02-01 11:30:50 +00:00
|
|
|
|
2023-08-18 05:30:25 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { useFullScreen } from '~/utils';
|
|
|
|
import {
|
|
|
|
Intro,
|
|
|
|
events as introEvents,
|
|
|
|
services as introServices,
|
|
|
|
actions as introActions,
|
|
|
|
} from './intro';
|
|
|
|
import { DesignWithAi, events as designWithAiEvents } from './design-with-ai';
|
2024-01-11 14:32:16 +00:00
|
|
|
import { DesignWithoutAi } from './design-without-ai';
|
|
|
|
|
2023-08-28 01:28:05 +00:00
|
|
|
import { AssemblerHub, events as assemblerHubEvents } from './assembler-hub';
|
2023-11-10 08:35:46 +00:00
|
|
|
import {
|
|
|
|
events as transitionalEvents,
|
|
|
|
services as transitionalServices,
|
|
|
|
actions as transitionalActions,
|
|
|
|
} from './transitional';
|
2023-08-18 05:30:25 +00:00
|
|
|
import { findComponentMeta } from '~/utils/xstate/find-component';
|
|
|
|
import {
|
|
|
|
CustomizeStoreComponentMeta,
|
|
|
|
CustomizeStoreComponent,
|
|
|
|
customizeStoreStateMachineContext,
|
2024-01-09 14:49:59 +00:00
|
|
|
FlowType,
|
2023-08-18 05:30:25 +00:00
|
|
|
} from './types';
|
2023-09-30 00:17:36 +00:00
|
|
|
import { ThemeCard } from './intro/types';
|
2023-08-29 06:00:54 +00:00
|
|
|
import './style.scss';
|
2024-03-04 19:09:36 +00:00
|
|
|
import { navigateOrParent, attachParentListeners, isIframe } from './utils';
|
2023-10-27 09:06:13 +00:00
|
|
|
import useBodyClass from './hooks/use-body-class';
|
2024-01-09 14:49:59 +00:00
|
|
|
import { isWooExpress } from '~/utils/is-woo-express';
|
2024-03-26 02:28:51 +00:00
|
|
|
import { useXStateInspect } from '~/xstate';
|
2023-08-29 06:00:54 +00:00
|
|
|
|
2023-08-18 05:30:25 +00:00
|
|
|
export type customizeStoreStateMachineEvents =
|
|
|
|
| introEvents
|
|
|
|
| designWithAiEvents
|
2023-09-06 06:21:09 +00:00
|
|
|
| assemblerHubEvents
|
2023-09-15 04:01:02 +00:00
|
|
|
| transitionalEvents
|
2023-09-12 06:32:50 +00:00
|
|
|
| { type: 'AI_WIZARD_CLOSED_BEFORE_COMPLETION'; payload: { step: string } }
|
2024-01-25 11:04:44 +00:00
|
|
|
| { type: 'EXTERNAL_URL_UPDATE' }
|
2024-03-18 15:49:41 +00:00
|
|
|
| { type: 'INSTALL_FONTS' }
|
2024-02-01 11:30:50 +00:00
|
|
|
| { type: 'NO_AI_FLOW_ERROR'; payload: { hasError: boolean } }
|
|
|
|
| { type: 'IS_FONT_LIBRARY_AVAILABLE'; payload: boolean };
|
2023-08-18 05:30:25 +00:00
|
|
|
|
2023-09-12 06:32:50 +00:00
|
|
|
const updateQueryStep = (
|
|
|
|
_context: unknown,
|
|
|
|
_evt: unknown,
|
|
|
|
{ action }: { action: unknown }
|
|
|
|
) => {
|
|
|
|
const { path } = getQuery() as { path: string };
|
|
|
|
const step = ( action as { step: string } ).step;
|
|
|
|
const pathFragments = path.split( '/' ); // [0] '', [1] 'customize-store', [2] step slug [3] design-with-ai, assembler-hub path fragments
|
|
|
|
if ( pathFragments[ 1 ] === 'customize-store' ) {
|
|
|
|
if ( pathFragments[ 2 ] !== step ) {
|
|
|
|
// this state machine is only concerned with [2], so we ignore changes to [3]
|
|
|
|
// [1] is handled by router at root of wc-admin
|
|
|
|
updateQueryString( {}, `/customize-store/${ step }` );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-21 02:06:39 +00:00
|
|
|
const redirectToWooHome = () => {
|
2023-10-26 08:46:50 +00:00
|
|
|
const url = getNewPath( {}, '/', {} );
|
|
|
|
navigateOrParent( window, url );
|
2023-09-21 02:06:39 +00:00
|
|
|
};
|
|
|
|
|
2024-04-22 09:26:32 +00:00
|
|
|
const goBack = () => {
|
|
|
|
const history = getHistory();
|
|
|
|
if (
|
|
|
|
history.__experimentalLocationStack.length >= 2 &&
|
|
|
|
! history.__experimentalLocationStack[
|
|
|
|
history.__experimentalLocationStack.length - 2
|
|
|
|
].search.includes( 'customize-store' )
|
|
|
|
) {
|
|
|
|
// If the previous location is not a customize-store step, go back in history.
|
|
|
|
history.back();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
redirectToWooHome();
|
|
|
|
};
|
|
|
|
|
2023-10-05 13:33:50 +00:00
|
|
|
const redirectToThemes = ( _context: customizeStoreStateMachineContext ) => {
|
2024-03-05 10:15:09 +00:00
|
|
|
if ( isWooExpress() ) {
|
|
|
|
window.location.href =
|
|
|
|
_context?.intro?.themeData?._links?.browse_all?.href ??
|
|
|
|
getAdminLink( 'themes.php' );
|
|
|
|
} else {
|
|
|
|
window.location.href = getAdminLink(
|
|
|
|
'admin.php?page=wc-admin&tab=themes&path=%2Fextensions'
|
|
|
|
);
|
|
|
|
}
|
2023-09-30 00:17:36 +00:00
|
|
|
};
|
|
|
|
|
2023-09-19 11:02:02 +00:00
|
|
|
const markTaskComplete = async () => {
|
2023-10-09 05:19:08 +00:00
|
|
|
const currentTemplate = await resolveSelect(
|
|
|
|
coreStore
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
).__experimentalGetTemplateForLink( '/' );
|
2023-09-19 11:02:02 +00:00
|
|
|
return dispatch( OPTIONS_STORE_NAME ).updateOptions( {
|
|
|
|
woocommerce_admin_customize_store_completed: 'yes',
|
2023-10-09 05:19:08 +00:00
|
|
|
// we use this on the intro page to determine if this same theme was used in the last customization
|
|
|
|
woocommerce_admin_customize_store_completed_theme_id:
|
|
|
|
currentTemplate.id ?? undefined,
|
2023-09-19 11:02:02 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2023-09-12 06:32:50 +00:00
|
|
|
const browserPopstateHandler =
|
|
|
|
() => ( sendBack: Sender< { type: 'EXTERNAL_URL_UPDATE' } > ) => {
|
|
|
|
const popstateHandler = () => {
|
|
|
|
sendBack( { type: 'EXTERNAL_URL_UPDATE' } );
|
|
|
|
};
|
|
|
|
window.addEventListener( 'popstate', popstateHandler );
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener( 'popstate', popstateHandler );
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-11-10 08:35:46 +00:00
|
|
|
const CYSSpinner = () => (
|
|
|
|
<div className="woocommerce-customize-store__loading">
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2023-09-12 06:32:50 +00:00
|
|
|
export const machineActions = {
|
|
|
|
updateQueryStep,
|
2023-09-21 02:06:39 +00:00
|
|
|
redirectToWooHome,
|
2023-09-30 00:17:36 +00:00
|
|
|
redirectToThemes,
|
2024-04-22 09:26:32 +00:00
|
|
|
goBack,
|
2023-08-18 05:30:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const customizeStoreStateMachineActions = {
|
|
|
|
...introActions,
|
2023-11-10 08:35:46 +00:00
|
|
|
...transitionalActions,
|
2023-09-12 06:32:50 +00:00
|
|
|
...machineActions,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const customizeStoreStateMachineServices = {
|
|
|
|
...introServices,
|
2023-11-10 08:35:46 +00:00
|
|
|
...transitionalServices,
|
2023-09-12 06:32:50 +00:00
|
|
|
browserPopstateHandler,
|
2023-09-19 11:02:02 +00:00
|
|
|
markTaskComplete,
|
2023-08-18 05:30:25 +00:00
|
|
|
};
|
|
|
|
export const customizeStoreStateMachineDefinition = createMachine( {
|
|
|
|
id: 'customizeStore',
|
2024-02-05 11:36:33 +00:00
|
|
|
initial: 'setFlags',
|
2023-08-18 05:30:25 +00:00
|
|
|
predictableActionArguments: true,
|
|
|
|
preserveActionOrder: true,
|
|
|
|
schema: {
|
|
|
|
context: {} as customizeStoreStateMachineContext,
|
|
|
|
events: {} as customizeStoreStateMachineEvents,
|
|
|
|
services: {} as {
|
|
|
|
fetchThemeCards: { data: ThemeCard[] };
|
|
|
|
},
|
|
|
|
},
|
|
|
|
context: {
|
|
|
|
intro: {
|
2023-09-29 23:03:50 +00:00
|
|
|
hasErrors: false,
|
2023-10-05 13:33:50 +00:00
|
|
|
themeData: {
|
|
|
|
themes: [] as ThemeCard[],
|
|
|
|
_links: {
|
|
|
|
browse_all: {
|
|
|
|
href: getAdminLink( 'themes.php' ),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-08-18 05:30:25 +00:00
|
|
|
activeTheme: '',
|
2023-09-29 23:03:50 +00:00
|
|
|
customizeStoreTaskCompleted: false,
|
2023-10-09 05:19:08 +00:00
|
|
|
currentThemeIsAiGenerated: false,
|
2023-08-18 05:30:25 +00:00
|
|
|
},
|
2023-11-10 08:35:46 +00:00
|
|
|
transitionalScreen: {
|
|
|
|
hasCompleteSurvey: false,
|
|
|
|
},
|
2024-01-09 14:49:59 +00:00
|
|
|
flowType: FlowType.noAI,
|
2024-02-01 11:30:50 +00:00
|
|
|
isFontLibraryAvailable: null,
|
2024-02-05 11:36:33 +00:00
|
|
|
activeThemeHasMods: undefined,
|
2023-08-18 05:30:25 +00:00
|
|
|
} as customizeStoreStateMachineContext,
|
2023-09-12 06:32:50 +00:00
|
|
|
invoke: {
|
|
|
|
src: 'browserPopstateHandler',
|
|
|
|
},
|
|
|
|
on: {
|
2024-03-04 19:09:36 +00:00
|
|
|
GO_BACK_TO_DESIGN_WITH_AI: {
|
|
|
|
target: 'designWithAi',
|
|
|
|
actions: [ { type: 'updateQueryStep', step: 'design-with-ai' } ],
|
|
|
|
},
|
|
|
|
GO_BACK_TO_DESIGN_WITHOUT_AI: {
|
|
|
|
target: 'intro',
|
|
|
|
actions: [ { type: 'updateQueryStep', step: 'intro' } ],
|
|
|
|
},
|
2023-09-12 06:32:50 +00:00
|
|
|
EXTERNAL_URL_UPDATE: {
|
|
|
|
target: 'navigate',
|
|
|
|
},
|
|
|
|
AI_WIZARD_CLOSED_BEFORE_COMPLETION: {
|
|
|
|
target: 'intro',
|
|
|
|
actions: [ { type: 'updateQueryStep', step: 'intro' } ],
|
|
|
|
},
|
2024-01-25 11:04:44 +00:00
|
|
|
NO_AI_FLOW_ERROR: {
|
|
|
|
target: 'intro',
|
|
|
|
actions: [
|
|
|
|
{ type: 'assignNoAIFlowError' },
|
|
|
|
{ type: 'updateQueryStep', step: 'intro' },
|
|
|
|
],
|
|
|
|
},
|
2024-03-18 15:49:41 +00:00
|
|
|
INSTALL_FONTS: {
|
|
|
|
target: 'designWithoutAi.installFonts',
|
|
|
|
},
|
2023-09-12 06:32:50 +00:00
|
|
|
},
|
2023-08-18 05:30:25 +00:00
|
|
|
states: {
|
2024-02-05 11:36:33 +00:00
|
|
|
setFlags: {
|
|
|
|
invoke: {
|
|
|
|
src: 'setFlags',
|
|
|
|
onDone: {
|
|
|
|
actions: 'assignFlags',
|
|
|
|
target: 'navigate',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-09-12 06:32:50 +00:00
|
|
|
navigate: {
|
|
|
|
always: [
|
|
|
|
{
|
|
|
|
target: 'intro',
|
|
|
|
cond: {
|
|
|
|
type: 'hasStepInUrl',
|
|
|
|
step: 'intro',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
target: 'designWithAi',
|
|
|
|
cond: {
|
|
|
|
type: 'hasStepInUrl',
|
|
|
|
step: 'design-with-ai',
|
|
|
|
},
|
|
|
|
},
|
2024-01-11 14:32:16 +00:00
|
|
|
{
|
|
|
|
target: 'designWithoutAi',
|
|
|
|
cond: {
|
|
|
|
type: 'hasStepInUrl',
|
|
|
|
step: 'design',
|
|
|
|
},
|
|
|
|
},
|
2023-09-12 06:32:50 +00:00
|
|
|
{
|
|
|
|
target: 'assemblerHub',
|
|
|
|
cond: {
|
|
|
|
type: 'hasStepInUrl',
|
|
|
|
step: 'assembler-hub',
|
|
|
|
},
|
|
|
|
},
|
2023-09-18 11:01:12 +00:00
|
|
|
{
|
|
|
|
target: 'transitionalScreen',
|
|
|
|
cond: {
|
|
|
|
type: 'hasStepInUrl',
|
|
|
|
step: 'transitional',
|
|
|
|
},
|
|
|
|
},
|
2023-09-12 06:32:50 +00:00
|
|
|
{
|
|
|
|
target: 'intro',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2023-08-18 05:30:25 +00:00
|
|
|
intro: {
|
|
|
|
id: 'intro',
|
2024-01-09 14:49:59 +00:00
|
|
|
initial: 'flowType',
|
2023-08-18 05:30:25 +00:00
|
|
|
states: {
|
2024-01-09 14:49:59 +00:00
|
|
|
flowType: {
|
2024-01-11 15:12:41 +00:00
|
|
|
always: [
|
|
|
|
{
|
|
|
|
target: 'fetchIntroData',
|
|
|
|
cond: 'isNotWooExpress',
|
|
|
|
actions: 'assignNoAI',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
target: 'checkAiStatus',
|
|
|
|
cond: 'isWooExpress',
|
|
|
|
},
|
|
|
|
],
|
2024-01-09 14:49:59 +00:00
|
|
|
},
|
2024-01-11 14:32:16 +00:00
|
|
|
checkAiStatus: {
|
|
|
|
initial: 'pending',
|
2023-12-06 12:49:28 +00:00
|
|
|
states: {
|
2024-01-11 14:32:16 +00:00
|
|
|
pending: {
|
|
|
|
invoke: {
|
|
|
|
src: 'fetchAiStatus',
|
|
|
|
onDone: {
|
|
|
|
actions: 'assignAiStatus',
|
|
|
|
target: 'success',
|
|
|
|
},
|
|
|
|
onError: {
|
|
|
|
actions: 'assignAiOffline',
|
|
|
|
target: 'success',
|
2023-12-06 12:49:28 +00:00
|
|
|
},
|
|
|
|
},
|
2023-09-29 23:03:50 +00:00
|
|
|
},
|
2024-01-11 14:32:16 +00:00
|
|
|
success: { type: 'final' },
|
|
|
|
},
|
|
|
|
onDone: 'fetchIntroData',
|
|
|
|
},
|
|
|
|
fetchIntroData: {
|
|
|
|
initial: 'pending',
|
|
|
|
states: {
|
|
|
|
pending: {
|
|
|
|
invoke: {
|
|
|
|
src: 'fetchIntroData',
|
|
|
|
onError: {
|
|
|
|
actions: 'assignFetchIntroDataError',
|
|
|
|
target: 'success',
|
|
|
|
},
|
|
|
|
onDone: {
|
|
|
|
target: 'success',
|
|
|
|
actions: [
|
|
|
|
'assignThemeData',
|
2024-03-13 13:55:04 +00:00
|
|
|
'assignActiveTheme',
|
2024-01-11 14:32:16 +00:00
|
|
|
'assignCustomizeStoreCompleted',
|
|
|
|
'assignCurrentThemeIsAiGenerated',
|
|
|
|
],
|
2023-12-06 12:49:28 +00:00
|
|
|
},
|
|
|
|
},
|
2023-08-18 05:30:25 +00:00
|
|
|
},
|
2024-01-11 14:32:16 +00:00
|
|
|
success: { type: 'final' },
|
2023-08-18 05:30:25 +00:00
|
|
|
},
|
2023-12-06 12:49:28 +00:00
|
|
|
onDone: 'intro',
|
2023-08-18 05:30:25 +00:00
|
|
|
},
|
|
|
|
intro: {
|
|
|
|
meta: {
|
|
|
|
component: Intro,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
on: {
|
|
|
|
CLICKED_ON_BREADCRUMB: {
|
2024-04-22 09:26:32 +00:00
|
|
|
actions: 'goBack',
|
2023-08-18 05:30:25 +00:00
|
|
|
},
|
2023-09-29 07:44:22 +00:00
|
|
|
DESIGN_WITH_AI: {
|
|
|
|
actions: [ 'recordTracksDesignWithAIClicked' ],
|
|
|
|
target: 'designWithAi',
|
|
|
|
},
|
2024-01-11 14:32:16 +00:00
|
|
|
DESIGN_WITHOUT_AI: {
|
|
|
|
actions: [ 'recordTracksDesignWithAIClicked' ],
|
|
|
|
target: 'designWithoutAi',
|
|
|
|
},
|
2023-08-18 05:30:25 +00:00
|
|
|
SELECTED_NEW_THEME: {
|
2023-09-29 07:44:22 +00:00
|
|
|
actions: [ 'recordTracksThemeSelected' ],
|
|
|
|
target: 'appearanceTask',
|
|
|
|
},
|
|
|
|
SELECTED_ACTIVE_THEME: {
|
|
|
|
actions: [ 'recordTracksThemeSelected' ],
|
2023-08-28 01:28:05 +00:00
|
|
|
target: 'appearanceTask',
|
2023-08-18 05:30:25 +00:00
|
|
|
},
|
|
|
|
SELECTED_BROWSE_ALL_THEMES: {
|
2023-09-30 00:17:36 +00:00
|
|
|
actions: [
|
|
|
|
'recordTracksBrowseAllThemesClicked',
|
|
|
|
'redirectToThemes',
|
|
|
|
],
|
2023-08-18 05:30:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-01-11 14:32:16 +00:00
|
|
|
designWithoutAi: {
|
|
|
|
initial: 'preDesignWithoutAi',
|
|
|
|
states: {
|
|
|
|
preDesignWithoutAi: {
|
|
|
|
always: {
|
|
|
|
target: 'designWithoutAi',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
designWithoutAi: {
|
|
|
|
entry: [ { type: 'updateQueryStep', step: 'design' } ],
|
|
|
|
meta: {
|
|
|
|
component: DesignWithoutAi,
|
|
|
|
},
|
|
|
|
},
|
2024-03-18 15:49:41 +00:00
|
|
|
// This state is used to install fonts and then redirect to the assembler hub.
|
|
|
|
installFonts: {
|
|
|
|
entry: [
|
|
|
|
{
|
|
|
|
type: 'updateQueryStep',
|
|
|
|
step: 'design/install-fonts',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
meta: {
|
|
|
|
component: DesignWithoutAi,
|
|
|
|
},
|
|
|
|
},
|
2024-01-11 14:32:16 +00:00
|
|
|
},
|
|
|
|
},
|
2023-08-18 05:30:25 +00:00
|
|
|
designWithAi: {
|
|
|
|
initial: 'preDesignWithAi',
|
|
|
|
states: {
|
|
|
|
preDesignWithAi: {
|
|
|
|
always: {
|
|
|
|
target: 'designWithAi',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
designWithAi: {
|
|
|
|
meta: {
|
|
|
|
component: DesignWithAi,
|
|
|
|
},
|
2023-09-12 06:32:50 +00:00
|
|
|
entry: [
|
|
|
|
{ type: 'updateQueryStep', step: 'design-with-ai' },
|
|
|
|
],
|
2023-08-18 05:30:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
on: {
|
|
|
|
THEME_SUGGESTED: {
|
|
|
|
target: 'assemblerHub',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
assemblerHub: {
|
2024-02-22 08:30:11 +00:00
|
|
|
initial: 'fetchActiveThemeHasMods',
|
2023-09-12 06:32:50 +00:00
|
|
|
states: {
|
2024-02-22 08:30:11 +00:00
|
|
|
fetchActiveThemeHasMods: {
|
|
|
|
invoke: {
|
|
|
|
src: 'fetchActiveThemeHasMods',
|
|
|
|
onDone: {
|
|
|
|
actions: 'assignActiveThemeHasMods',
|
|
|
|
target: 'checkActiveThemeHasMods',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-01-23 10:15:19 +00:00
|
|
|
checkActiveThemeHasMods: {
|
|
|
|
always: [
|
|
|
|
{
|
2024-02-05 11:36:33 +00:00
|
|
|
// Redirect to the "intro step" if the active theme has no modifications.
|
|
|
|
cond: 'activeThemeHasNoMods',
|
2024-01-23 10:15:19 +00:00
|
|
|
actions: [
|
|
|
|
{ type: 'updateQueryStep', step: 'intro' },
|
|
|
|
],
|
|
|
|
target: '#customizeStore.intro',
|
|
|
|
},
|
|
|
|
{
|
2024-02-05 11:36:33 +00:00
|
|
|
// Otherwise, proceed to the next step.
|
2024-01-23 10:15:19 +00:00
|
|
|
cond: 'activeThemeHasMods',
|
|
|
|
target: 'preCheckAiStatus',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2024-01-11 15:12:41 +00:00
|
|
|
preCheckAiStatus: {
|
|
|
|
always: [
|
|
|
|
{
|
|
|
|
cond: 'isWooExpress',
|
|
|
|
target: 'checkAiStatus',
|
|
|
|
},
|
|
|
|
{ cond: 'isNotWooExpress', target: 'assemblerHub' },
|
|
|
|
],
|
|
|
|
},
|
2023-12-06 12:49:28 +00:00
|
|
|
checkAiStatus: {
|
|
|
|
invoke: {
|
|
|
|
src: 'fetchAiStatus',
|
|
|
|
onDone: {
|
|
|
|
actions: 'assignAiStatus',
|
|
|
|
target: 'assemblerHub',
|
|
|
|
},
|
|
|
|
onError: {
|
|
|
|
actions: 'assignAiOffline',
|
|
|
|
target: 'assemblerHub',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-09-12 06:32:50 +00:00
|
|
|
assemblerHub: {
|
|
|
|
entry: [
|
|
|
|
{ type: 'updateQueryStep', step: 'assembler-hub' },
|
|
|
|
],
|
|
|
|
meta: {
|
|
|
|
component: AssemblerHub,
|
|
|
|
},
|
|
|
|
},
|
2023-09-15 04:01:02 +00:00
|
|
|
postAssemblerHub: {
|
2023-11-10 08:35:46 +00:00
|
|
|
invoke: [
|
|
|
|
{
|
|
|
|
src: 'markTaskComplete',
|
|
|
|
// eslint-disable-next-line xstate/no-ondone-outside-compound-state
|
|
|
|
onDone: {
|
|
|
|
target: '#customizeStore.transitionalScreen',
|
|
|
|
},
|
2023-09-15 04:01:02 +00:00
|
|
|
},
|
2023-11-10 08:35:46 +00:00
|
|
|
{
|
|
|
|
// Pre-fetch survey completed option so we can show the screen immediately.
|
|
|
|
src: 'fetchSurveyCompletedOption',
|
|
|
|
},
|
|
|
|
],
|
2023-09-15 04:01:02 +00:00
|
|
|
},
|
2023-08-28 01:28:05 +00:00
|
|
|
},
|
2023-08-18 05:30:25 +00:00
|
|
|
on: {
|
|
|
|
FINISH_CUSTOMIZATION: {
|
2023-09-15 04:01:02 +00:00
|
|
|
target: '.postAssemblerHub',
|
2023-08-18 05:30:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-09-15 04:01:02 +00:00
|
|
|
transitionalScreen: {
|
2024-03-27 13:13:52 +00:00
|
|
|
initial: 'fetchCustomizeStoreCompleted',
|
2023-11-10 08:35:46 +00:00
|
|
|
states: {
|
2024-03-27 13:13:52 +00:00
|
|
|
fetchCustomizeStoreCompleted: {
|
|
|
|
invoke: {
|
|
|
|
src: 'fetchCustomizeStoreCompleted',
|
|
|
|
onDone: {
|
|
|
|
actions: 'assignCustomizeStoreCompleted',
|
|
|
|
target: 'checkCustomizeStoreCompleted',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
checkCustomizeStoreCompleted: {
|
|
|
|
always: [
|
|
|
|
{
|
|
|
|
// Redirect to the "intro step" if the active theme has no modifications.
|
|
|
|
cond: 'customizeTaskIsNotCompleted',
|
|
|
|
actions: [
|
|
|
|
{ type: 'updateQueryStep', step: 'intro' },
|
|
|
|
],
|
|
|
|
target: '#customizeStore.intro',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Otherwise, proceed to the next step.
|
|
|
|
cond: 'customizeTaskIsCompleted',
|
|
|
|
target: 'preTransitional',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2023-11-10 08:35:46 +00:00
|
|
|
preTransitional: {
|
|
|
|
meta: {
|
|
|
|
component: CYSSpinner,
|
|
|
|
},
|
|
|
|
invoke: {
|
|
|
|
src: 'fetchSurveyCompletedOption',
|
|
|
|
onError: {
|
2024-01-30 08:54:16 +00:00
|
|
|
target: 'preCheckAiStatus', // leave it as initialised default on error
|
2023-11-10 08:35:46 +00:00
|
|
|
},
|
|
|
|
onDone: {
|
2024-01-30 08:54:16 +00:00
|
|
|
target: 'preCheckAiStatus',
|
2023-11-10 08:35:46 +00:00
|
|
|
actions: [ 'assignHasCompleteSurvey' ],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-01-30 08:54:16 +00:00
|
|
|
preCheckAiStatus: {
|
|
|
|
always: [
|
|
|
|
{
|
|
|
|
cond: 'isWooExpress',
|
|
|
|
target: 'checkAiStatus',
|
|
|
|
},
|
|
|
|
{ cond: 'isNotWooExpress', target: 'transitional' },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
checkAiStatus: {
|
|
|
|
invoke: {
|
|
|
|
src: 'fetchAiStatus',
|
|
|
|
onDone: {
|
|
|
|
actions: 'assignAiStatus',
|
|
|
|
target: 'transitional',
|
|
|
|
},
|
|
|
|
onError: {
|
|
|
|
actions: 'assignAiOffline',
|
|
|
|
target: 'transitional',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-11-10 08:35:46 +00:00
|
|
|
transitional: {
|
|
|
|
entry: [
|
|
|
|
{ type: 'updateQueryStep', step: 'transitional' },
|
|
|
|
],
|
|
|
|
meta: {
|
|
|
|
component: AssemblerHub,
|
|
|
|
},
|
|
|
|
on: {
|
|
|
|
GO_BACK_TO_HOME: {
|
|
|
|
actions: 'redirectToWooHome',
|
|
|
|
},
|
|
|
|
COMPLETE_SURVEY: {
|
|
|
|
actions: 'completeSurvey',
|
|
|
|
},
|
|
|
|
},
|
2023-09-15 04:01:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-08-28 01:28:05 +00:00
|
|
|
appearanceTask: {},
|
2023-08-18 05:30:25 +00:00
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
2024-02-01 11:30:50 +00:00
|
|
|
declare global {
|
|
|
|
interface Window {
|
|
|
|
__wcCustomizeStore: {
|
|
|
|
isFontLibraryAvailable: boolean | null;
|
2024-02-05 11:36:33 +00:00
|
|
|
activeThemeHasMods: boolean | undefined;
|
2024-03-04 19:09:36 +00:00
|
|
|
sendEventToIntroMachine: (
|
|
|
|
typeEvent: customizeStoreStateMachineEvents
|
|
|
|
) => void;
|
2024-02-01 11:30:50 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-18 05:30:25 +00:00
|
|
|
export const CustomizeStoreController = ( {
|
|
|
|
actionOverrides,
|
|
|
|
servicesOverrides,
|
|
|
|
}: {
|
|
|
|
actionOverrides: Partial< typeof customizeStoreStateMachineActions >;
|
|
|
|
servicesOverrides: Partial< typeof customizeStoreStateMachineServices >;
|
|
|
|
} ) => {
|
|
|
|
useFullScreen( [ 'woocommerce-customize-store' ] );
|
|
|
|
|
|
|
|
const augmentedStateMachine = useMemo( () => {
|
|
|
|
return customizeStoreStateMachineDefinition.withConfig( {
|
|
|
|
services: {
|
|
|
|
...customizeStoreStateMachineServices,
|
|
|
|
...servicesOverrides,
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
...customizeStoreStateMachineActions,
|
|
|
|
...actionOverrides,
|
|
|
|
},
|
2023-09-12 06:32:50 +00:00
|
|
|
guards: {
|
|
|
|
hasStepInUrl: ( _ctx, _evt, { cond }: { cond: unknown } ) => {
|
|
|
|
const { path = '' } = getQuery() as { path: string };
|
|
|
|
const pathFragments = path.split( '/' );
|
|
|
|
return (
|
|
|
|
pathFragments[ 2 ] === // [0] '', [1] 'customize-store', [2] step slug
|
|
|
|
( cond as { step: string | undefined } ).step
|
|
|
|
);
|
|
|
|
},
|
2023-12-06 12:49:28 +00:00
|
|
|
isAiOnline: ( _ctx ) => {
|
2024-01-09 14:49:59 +00:00
|
|
|
return _ctx.flowType === FlowType.AIOnline;
|
2023-12-06 12:49:28 +00:00
|
|
|
},
|
|
|
|
isAiOffline: ( _ctx ) => {
|
2024-01-09 14:49:59 +00:00
|
|
|
return _ctx.flowType === FlowType.AIOffline;
|
2023-12-06 12:49:28 +00:00
|
|
|
},
|
2024-01-09 14:49:59 +00:00
|
|
|
isWooExpress: () => isWooExpress(),
|
|
|
|
isNotWooExpress: () => ! isWooExpress(),
|
2024-01-23 10:15:19 +00:00
|
|
|
activeThemeHasMods: ( _ctx ) => {
|
2024-02-05 11:36:33 +00:00
|
|
|
return !! _ctx.activeThemeHasMods;
|
2024-01-23 10:15:19 +00:00
|
|
|
},
|
2024-02-05 11:36:33 +00:00
|
|
|
activeThemeHasNoMods: ( _ctx ) => {
|
|
|
|
return ! _ctx.activeThemeHasMods;
|
2024-01-23 10:15:19 +00:00
|
|
|
},
|
2024-03-27 13:13:52 +00:00
|
|
|
customizeTaskIsCompleted: ( _ctx ) => {
|
|
|
|
return _ctx.intro.customizeStoreTaskCompleted;
|
|
|
|
},
|
|
|
|
customizeTaskIsNotCompleted: ( _ctx ) => {
|
|
|
|
return ! _ctx.intro.customizeStoreTaskCompleted;
|
|
|
|
},
|
2023-09-12 06:32:50 +00:00
|
|
|
},
|
2023-08-18 05:30:25 +00:00
|
|
|
} );
|
|
|
|
}, [ actionOverrides, servicesOverrides ] );
|
|
|
|
|
2024-03-26 02:28:51 +00:00
|
|
|
const { versionEnabled } = useXStateInspect();
|
|
|
|
|
2023-08-18 05:30:25 +00:00
|
|
|
const [ state, send, service ] = useMachine( augmentedStateMachine, {
|
2024-03-26 02:28:51 +00:00
|
|
|
devTools: versionEnabled === 'V4',
|
2023-08-18 05:30:25 +00:00
|
|
|
} );
|
2024-02-01 11:30:50 +00:00
|
|
|
|
2024-03-04 19:09:36 +00:00
|
|
|
useEffect( () => {
|
|
|
|
if ( isIframe( window ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
window.__wcCustomizeStore = {
|
|
|
|
...window.__wcCustomizeStore,
|
|
|
|
// This is needed because the iframe loads the entire Customize Store app.
|
|
|
|
// This means that the iframe instance will have different state machines
|
|
|
|
// than the parent window.
|
|
|
|
// Check https://github.com/woocommerce/woocommerce/issues/45278 for more details.
|
|
|
|
sendEventToIntroMachine: (
|
|
|
|
typeEvent: customizeStoreStateMachineEvents
|
|
|
|
) => send( typeEvent ),
|
|
|
|
};
|
|
|
|
}, [ send ] );
|
|
|
|
|
|
|
|
window.__wcCustomizeStore = {
|
|
|
|
...window.__wcCustomizeStore,
|
|
|
|
};
|
|
|
|
|
2023-08-18 05:30:25 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps -- false positive due to function name match, this isn't from react std lib
|
|
|
|
const currentNodeMeta = useSelector( service, ( currentState ) =>
|
|
|
|
findComponentMeta< CustomizeStoreComponentMeta >(
|
|
|
|
currentState?.meta ?? undefined
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
const [ CurrentComponent, setCurrentComponent ] =
|
|
|
|
useState< CustomizeStoreComponent | null >( null );
|
|
|
|
useEffect( () => {
|
|
|
|
if ( currentNodeMeta?.component ) {
|
|
|
|
setCurrentComponent( () => currentNodeMeta?.component );
|
|
|
|
}
|
|
|
|
}, [ CurrentComponent, currentNodeMeta?.component ] );
|
|
|
|
|
2023-10-26 08:46:50 +00:00
|
|
|
// Run listeners for parent window.
|
|
|
|
useEffect( () => {
|
|
|
|
const removeListener = attachParentListeners();
|
|
|
|
return removeListener;
|
|
|
|
}, [] );
|
|
|
|
|
2023-10-27 09:06:13 +00:00
|
|
|
useBodyClass( 'is-fullscreen-mode' );
|
|
|
|
|
2023-08-18 05:30:25 +00:00
|
|
|
const currentNodeCssLabel =
|
|
|
|
state.value instanceof Object
|
|
|
|
? Object.keys( state.value )[ 0 ]
|
|
|
|
: state.value;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div
|
2023-10-02 08:28:13 +00:00
|
|
|
className={ `woocommerce-customize-store__container woocommerce-customize-store__step-${ currentNodeCssLabel }` }
|
2023-08-18 05:30:25 +00:00
|
|
|
>
|
|
|
|
{ CurrentComponent ? (
|
|
|
|
<CurrentComponent
|
2023-09-06 06:21:09 +00:00
|
|
|
parentMachine={ service }
|
2023-08-18 05:30:25 +00:00
|
|
|
sendEvent={ send }
|
|
|
|
context={ state.context }
|
2023-10-05 12:36:08 +00:00
|
|
|
currentState={ state.value }
|
2023-08-18 05:30:25 +00:00
|
|
|
/>
|
|
|
|
) : (
|
2023-11-10 08:35:46 +00:00
|
|
|
<CYSSpinner />
|
2023-08-18 05:30:25 +00:00
|
|
|
) }
|
|
|
|
</div>
|
2023-11-01 11:40:12 +00:00
|
|
|
{ /* @ts-expect-error 'scope' does exist. @types/wordpress__plugins is outdated. */ }
|
|
|
|
<PluginArea scope="woocommerce-customize-store" />
|
2023-08-18 05:30:25 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CustomizeStoreController;
|