2023-08-29 06:00:54 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useMachine, useSelector } from '@xstate/react';
|
|
|
|
import { useEffect, useState } from '@wordpress/element';
|
2023-09-06 06:21:09 +00:00
|
|
|
import { AnyInterpreter, Sender } from 'xstate';
|
2024-01-11 15:12:41 +00:00
|
|
|
import { getNewPath } from '@woocommerce/navigation';
|
2023-08-29 06:00:54 +00:00
|
|
|
|
2023-08-18 05:30:25 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-12-06 12:49:28 +00:00
|
|
|
import {
|
|
|
|
CustomizeStoreComponent,
|
2024-01-09 14:49:59 +00:00
|
|
|
FlowType,
|
2023-12-06 12:49:28 +00:00
|
|
|
customizeStoreStateMachineContext,
|
|
|
|
} from '../types';
|
2023-08-29 06:00:54 +00:00
|
|
|
import { designWithAiStateMachineDefinition } from './state-machine';
|
|
|
|
import { findComponentMeta } from '~/utils/xstate/find-component';
|
|
|
|
import {
|
|
|
|
BusinessInfoDescription,
|
|
|
|
ApiCallLoader,
|
|
|
|
LookAndFeel,
|
|
|
|
ToneOfVoice,
|
|
|
|
} from './pages';
|
|
|
|
import { customizeStoreStateMachineEvents } from '..';
|
2024-01-25 11:04:44 +00:00
|
|
|
import './style.scss';
|
2024-01-11 15:12:41 +00:00
|
|
|
import { isAIFlow } from '../guards';
|
|
|
|
import { navigateOrParent } from '../utils';
|
2024-03-26 02:28:51 +00:00
|
|
|
import { useXStateInspect } from '~/xstate';
|
2024-04-23 10:27:32 +00:00
|
|
|
import './entrepreneur-flow';
|
2023-09-19 08:41:52 +00:00
|
|
|
|
2023-08-18 05:30:25 +00:00
|
|
|
export type events = { type: 'THEME_SUGGESTED' };
|
2023-08-29 06:00:54 +00:00
|
|
|
export type DesignWithAiComponent =
|
|
|
|
| typeof BusinessInfoDescription
|
|
|
|
| typeof ApiCallLoader
|
|
|
|
| typeof LookAndFeel
|
|
|
|
| typeof ToneOfVoice;
|
|
|
|
export type DesignWithAiComponentMeta = {
|
|
|
|
component: DesignWithAiComponent;
|
|
|
|
};
|
|
|
|
|
2023-09-06 06:21:09 +00:00
|
|
|
export const DesignWithAiController = ( {
|
|
|
|
parentMachine,
|
2023-12-06 12:49:28 +00:00
|
|
|
parentContext,
|
2023-09-06 06:21:09 +00:00
|
|
|
}: {
|
|
|
|
parentMachine?: AnyInterpreter;
|
|
|
|
sendEventToParent?: Sender< customizeStoreStateMachineEvents >;
|
2023-12-06 12:49:28 +00:00
|
|
|
parentContext?: customizeStoreStateMachineContext;
|
2023-08-29 06:00:54 +00:00
|
|
|
} ) => {
|
2023-12-06 12:49:28 +00:00
|
|
|
// Assign aiOnline value from the parent context if it exists. Otherwise, ai is online by default.
|
|
|
|
designWithAiStateMachineDefinition.context.aiOnline =
|
2024-01-09 14:49:59 +00:00
|
|
|
parentContext?.flowType === FlowType.AIOnline;
|
2024-03-26 02:28:51 +00:00
|
|
|
|
|
|
|
const { versionEnabled } = useXStateInspect();
|
2023-08-29 06:00:54 +00:00
|
|
|
const [ state, send, service ] = useMachine(
|
|
|
|
designWithAiStateMachineDefinition,
|
|
|
|
{
|
2024-03-26 02:28:51 +00:00
|
|
|
devTools: versionEnabled === 'V4',
|
2023-09-06 06:21:09 +00:00
|
|
|
parent: parentMachine,
|
2023-08-29 06:00:54 +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< DesignWithAiComponentMeta >(
|
|
|
|
currentState?.meta ?? undefined
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
const [ CurrentComponent, setCurrentComponent ] =
|
|
|
|
useState< DesignWithAiComponent | null >( null );
|
|
|
|
useEffect( () => {
|
|
|
|
if ( currentNodeMeta?.component ) {
|
|
|
|
setCurrentComponent( () => currentNodeMeta?.component );
|
|
|
|
}
|
|
|
|
}, [ CurrentComponent, currentNodeMeta?.component ] );
|
|
|
|
|
|
|
|
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-design-with-ai__container woocommerce-design-with-ai-wizard__step-${ currentNodeCssLabel }` }
|
2023-08-29 06:00:54 +00:00
|
|
|
>
|
|
|
|
{ CurrentComponent ? (
|
|
|
|
<CurrentComponent
|
|
|
|
sendEvent={ send }
|
|
|
|
context={ state.context }
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div />
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
//loader should send event 'THEME_SUGGESTED' when it's done
|
2023-12-06 12:49:28 +00:00
|
|
|
export const DesignWithAi: CustomizeStoreComponent = ( {
|
|
|
|
parentMachine,
|
|
|
|
context,
|
|
|
|
} ) => {
|
2024-01-11 15:12:41 +00:00
|
|
|
const assemblerUrl = getNewPath( {}, '/customize-store', {} );
|
|
|
|
|
|
|
|
if ( ! isAIFlow( context.flowType ) ) {
|
|
|
|
navigateOrParent( window, assemblerUrl );
|
|
|
|
return null;
|
|
|
|
}
|
2023-08-18 05:30:25 +00:00
|
|
|
return (
|
|
|
|
<>
|
2023-12-06 12:49:28 +00:00
|
|
|
<DesignWithAiController
|
|
|
|
parentMachine={ parentMachine }
|
|
|
|
parentContext={ context }
|
|
|
|
/>
|
2023-08-18 05:30:25 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|