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';
|
2023-08-29 06:00:54 +00:00
|
|
|
|
2023-08-18 05:30:25 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { CustomizeStoreComponent } 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 '..';
|
2023-08-18 05:30:25 +00:00
|
|
|
|
2023-09-19 08:41:52 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
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,
|
|
|
|
}: {
|
|
|
|
parentMachine?: AnyInterpreter;
|
|
|
|
sendEventToParent?: Sender< customizeStoreStateMachineEvents >;
|
2023-08-29 06:00:54 +00:00
|
|
|
} ) => {
|
|
|
|
const [ state, send, service ] = useMachine(
|
|
|
|
designWithAiStateMachineDefinition,
|
|
|
|
{
|
|
|
|
devTools: process.env.NODE_ENV === 'development',
|
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
|
|
|
|
className={ `woocommerce-design-with-ai-__container woocommerce-design-with-ai-wizard__step-${ currentNodeCssLabel }` }
|
|
|
|
>
|
|
|
|
{ CurrentComponent ? (
|
|
|
|
<CurrentComponent
|
|
|
|
sendEvent={ send }
|
|
|
|
context={ state.context }
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div />
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
//loader should send event 'THEME_SUGGESTED' when it's done
|
2023-09-06 06:21:09 +00:00
|
|
|
export const DesignWithAi: CustomizeStoreComponent = ( { parentMachine } ) => {
|
2023-08-18 05:30:25 +00:00
|
|
|
return (
|
|
|
|
<>
|
2023-09-06 06:21:09 +00:00
|
|
|
<DesignWithAiController parentMachine={ parentMachine } />
|
2023-08-18 05:30:25 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|