2024-01-11 14:32:16 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2024-01-25 11:04:44 +00:00
|
|
|
import { createMachine } from 'xstate';
|
2024-01-11 14:32:16 +00:00
|
|
|
import { getQuery } from '@woocommerce/navigation';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
|
2024-01-19 11:03:19 +00:00
|
|
|
import { ApiCallLoader, AssembleHubLoader } from './pages/ApiCallLoader';
|
2024-01-11 14:32:16 +00:00
|
|
|
|
|
|
|
import { FlowType } from '../types';
|
|
|
|
import { DesignWithoutAIStateMachineContext } from './types';
|
|
|
|
import { services } from './services';
|
|
|
|
import { actions } from './actions';
|
2024-02-06 10:28:26 +00:00
|
|
|
import { isFontLibraryAvailable } from './guards';
|
2024-01-11 14:32:16 +00:00
|
|
|
|
|
|
|
export const hasStepInUrl = (
|
|
|
|
_ctx: unknown,
|
|
|
|
_evt: unknown,
|
|
|
|
{ cond }: { cond: unknown }
|
|
|
|
) => {
|
|
|
|
const { path = '' } = getQuery() as { path: string };
|
|
|
|
const pathFragments = path.split( '/' );
|
|
|
|
return (
|
|
|
|
pathFragments[ 2 ] === // [0] '', [1] 'customize-store', [2] design step slug
|
|
|
|
( cond as { step: string | undefined } ).step
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-03-18 15:49:41 +00:00
|
|
|
export const hasFontInstallInUrl = () => {
|
|
|
|
const { path = '' } = getQuery() as { path: string };
|
|
|
|
const pathFragments = path.split( '/' );
|
|
|
|
return (
|
|
|
|
pathFragments[ 2 ] === 'design' &&
|
|
|
|
pathFragments[ 3 ] === 'install-fonts'
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-06-05 13:09:35 +00:00
|
|
|
export const hasPatternInstallInUrl = () => {
|
|
|
|
const { path = '' } = getQuery() as { path: string };
|
|
|
|
const pathFragments = path.split( '/' );
|
|
|
|
return (
|
|
|
|
pathFragments[ 2 ] === 'design' &&
|
|
|
|
pathFragments[ 3 ] === 'install-patterns'
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-03-18 15:49:41 +00:00
|
|
|
const installFontFamiliesState = {
|
|
|
|
initial: 'checkFontLibrary',
|
|
|
|
states: {
|
|
|
|
checkFontLibrary: {
|
|
|
|
always: [
|
|
|
|
{
|
|
|
|
cond: {
|
|
|
|
type: 'isFontLibraryAvailable',
|
|
|
|
},
|
|
|
|
target: 'pending',
|
|
|
|
},
|
|
|
|
{ target: 'success' },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
pending: {
|
|
|
|
invoke: {
|
|
|
|
src: 'installFontFamilies',
|
|
|
|
onDone: {
|
|
|
|
target: 'success',
|
|
|
|
},
|
|
|
|
onError: {
|
|
|
|
actions: 'redirectToIntroWithError',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
success: {
|
|
|
|
type: 'final',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-01-25 11:04:44 +00:00
|
|
|
export type DesignWithoutAIStateMachineEvents =
|
|
|
|
| { type: 'EXTERNAL_URL_UPDATE' }
|
2024-03-18 15:49:41 +00:00
|
|
|
| { type: 'INSTALL_FONTS' }
|
2024-06-05 13:09:35 +00:00
|
|
|
| { type: 'INSTALL_PATTERNS' }
|
2024-01-25 11:04:44 +00:00
|
|
|
| { type: 'NO_AI_FLOW_ERROR'; payload: { hasError: boolean } };
|
|
|
|
|
2024-01-11 14:32:16 +00:00
|
|
|
export const designWithNoAiStateMachineDefinition = createMachine(
|
|
|
|
{
|
|
|
|
id: 'designWithoutAI',
|
|
|
|
predictableActionArguments: true,
|
|
|
|
preserveActionOrder: true,
|
|
|
|
schema: {
|
|
|
|
context: {} as DesignWithoutAIStateMachineContext,
|
2024-01-25 11:04:44 +00:00
|
|
|
events: {} as DesignWithoutAIStateMachineEvents,
|
2024-01-11 14:32:16 +00:00
|
|
|
},
|
|
|
|
invoke: {
|
|
|
|
src: 'browserPopstateHandler',
|
|
|
|
},
|
|
|
|
on: {
|
|
|
|
EXTERNAL_URL_UPDATE: {
|
|
|
|
target: 'navigate',
|
|
|
|
},
|
2024-03-18 15:49:41 +00:00
|
|
|
INSTALL_FONTS: {
|
|
|
|
target: 'installFontFamilies',
|
|
|
|
},
|
2024-06-05 13:09:35 +00:00
|
|
|
INSTALL_PATTERNS: {
|
|
|
|
target: 'installPatterns',
|
|
|
|
},
|
2024-01-11 14:32:16 +00:00
|
|
|
},
|
|
|
|
context: {
|
|
|
|
startLoadingTime: null,
|
|
|
|
flowType: FlowType.noAI,
|
|
|
|
apiCallLoader: {
|
|
|
|
hasErrors: false,
|
|
|
|
},
|
2024-02-06 10:28:26 +00:00
|
|
|
isFontLibraryAvailable: false,
|
2024-06-05 13:09:35 +00:00
|
|
|
isPTKPatternsAPIAvailable: false,
|
2024-06-05 14:54:29 +00:00
|
|
|
isBlockTheme: false,
|
2024-01-11 14:32:16 +00:00
|
|
|
},
|
|
|
|
initial: 'navigate',
|
|
|
|
states: {
|
|
|
|
navigate: {
|
|
|
|
always: [
|
2024-03-18 15:49:41 +00:00
|
|
|
{
|
|
|
|
cond: {
|
|
|
|
type: 'hasFontInstallInUrl',
|
|
|
|
step: 'design',
|
|
|
|
},
|
|
|
|
target: 'installFontFamilies',
|
|
|
|
},
|
2024-06-05 13:09:35 +00:00
|
|
|
{
|
|
|
|
cond: {
|
|
|
|
type: 'hasPatternInstallInUrl',
|
|
|
|
step: 'design',
|
|
|
|
},
|
|
|
|
target: 'installPatterns',
|
|
|
|
},
|
2024-01-11 14:32:16 +00:00
|
|
|
{
|
|
|
|
cond: {
|
|
|
|
type: 'hasStepInUrl',
|
|
|
|
step: 'design',
|
|
|
|
},
|
2024-01-18 14:52:49 +00:00
|
|
|
target: 'preAssembleSite',
|
2024-01-11 14:32:16 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2024-03-18 15:49:41 +00:00
|
|
|
installFontFamilies: {
|
|
|
|
meta: {
|
|
|
|
component: ApiCallLoader,
|
|
|
|
},
|
|
|
|
initial: 'enableTracking',
|
|
|
|
states: {
|
|
|
|
enableTracking: {
|
|
|
|
invoke: {
|
|
|
|
src: 'enableTracking',
|
|
|
|
onDone: {
|
|
|
|
target: 'checkFontLibrary',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
checkFontLibrary:
|
|
|
|
installFontFamiliesState.states.checkFontLibrary,
|
|
|
|
pending: installFontFamiliesState.states.pending,
|
|
|
|
success: {
|
|
|
|
type: 'final',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
onDone: {
|
|
|
|
target: '#designWithoutAI.showAssembleHub',
|
|
|
|
},
|
|
|
|
},
|
2024-06-05 13:09:35 +00:00
|
|
|
installPatterns: {
|
|
|
|
meta: {
|
|
|
|
component: ApiCallLoader,
|
|
|
|
},
|
|
|
|
initial: 'enableTracking',
|
|
|
|
states: {
|
|
|
|
enableTracking: {
|
|
|
|
invoke: {
|
|
|
|
src: 'enableTracking',
|
|
|
|
onDone: {
|
|
|
|
target: 'fetchPatterns',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fetchPatterns: {
|
|
|
|
invoke: {
|
|
|
|
src: 'installPatterns',
|
|
|
|
onDone: {
|
|
|
|
target: 'success',
|
|
|
|
},
|
|
|
|
onError: {
|
|
|
|
actions: 'redirectToIntroWithError',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
success: {
|
|
|
|
type: 'final',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
onDone: {
|
|
|
|
target: '#designWithoutAI.showAssembleHub',
|
|
|
|
},
|
|
|
|
},
|
2024-01-11 14:32:16 +00:00
|
|
|
preAssembleSite: {
|
2024-01-18 14:52:49 +00:00
|
|
|
initial: 'preApiCallLoader',
|
2024-01-16 13:01:31 +00:00
|
|
|
id: 'preAssembleSite',
|
2024-01-11 14:32:16 +00:00
|
|
|
states: {
|
2024-01-18 14:52:49 +00:00
|
|
|
preApiCallLoader: {
|
|
|
|
meta: {
|
|
|
|
// @todo: Move the current component in a common folder or create a new one dedicated to this flow.
|
|
|
|
component: ApiCallLoader,
|
|
|
|
},
|
|
|
|
type: 'parallel',
|
2024-01-11 14:32:16 +00:00
|
|
|
states: {
|
2024-01-18 14:52:49 +00:00
|
|
|
installAndActivateTheme: {
|
|
|
|
initial: 'pending',
|
|
|
|
states: {
|
|
|
|
pending: {
|
|
|
|
invoke: {
|
|
|
|
src: 'installAndActivateTheme',
|
|
|
|
onDone: {
|
|
|
|
target: 'success',
|
|
|
|
},
|
2024-01-25 11:04:44 +00:00
|
|
|
onError: {
|
|
|
|
actions:
|
|
|
|
'redirectToIntroWithError',
|
|
|
|
},
|
2024-01-18 14:52:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
success: { type: 'final' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
createProducts: {
|
|
|
|
initial: 'pending',
|
|
|
|
states: {
|
|
|
|
pending: {
|
|
|
|
invoke: {
|
|
|
|
src: 'createProducts',
|
|
|
|
onDone: {
|
|
|
|
target: 'success',
|
|
|
|
},
|
2024-01-25 11:04:44 +00:00
|
|
|
onError: {
|
|
|
|
actions:
|
|
|
|
'redirectToIntroWithError',
|
|
|
|
},
|
2024-01-18 14:52:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
success: {
|
|
|
|
type: 'final',
|
|
|
|
},
|
|
|
|
},
|
2024-01-11 14:32:16 +00:00
|
|
|
},
|
2024-02-01 17:16:12 +00:00
|
|
|
installFontFamilies: {
|
2024-03-18 15:49:41 +00:00
|
|
|
initial: installFontFamiliesState.initial,
|
2024-02-01 17:16:12 +00:00
|
|
|
states: {
|
2024-03-18 15:49:41 +00:00
|
|
|
checkFontLibrary:
|
|
|
|
installFontFamiliesState.states
|
|
|
|
.checkFontLibrary,
|
|
|
|
pending:
|
|
|
|
installFontFamiliesState.states.pending,
|
2024-02-01 17:16:12 +00:00
|
|
|
success: {
|
|
|
|
type: 'final',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-01-11 14:32:16 +00:00
|
|
|
},
|
2024-04-02 08:35:15 +00:00
|
|
|
onDone: {
|
|
|
|
target: 'assembleSite',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
assembleSite: {
|
|
|
|
initial: 'pending',
|
|
|
|
states: {
|
|
|
|
pending: {
|
|
|
|
invoke: {
|
|
|
|
src: 'assembleSite',
|
|
|
|
onDone: {
|
|
|
|
target: 'success',
|
|
|
|
},
|
|
|
|
onError: {
|
|
|
|
actions: 'redirectToIntroWithError',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
success: {
|
|
|
|
type: 'final',
|
|
|
|
},
|
|
|
|
},
|
2024-01-11 14:32:16 +00:00
|
|
|
onDone: {
|
|
|
|
target: '#designWithoutAI.showAssembleHub',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
showAssembleHub: {
|
2024-01-16 13:01:31 +00:00
|
|
|
id: 'showAssembleHub',
|
2024-01-11 14:32:16 +00:00
|
|
|
meta: {
|
|
|
|
component: AssembleHubLoader,
|
|
|
|
},
|
|
|
|
entry: [ 'redirectToAssemblerHub' ],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
actions,
|
|
|
|
services,
|
|
|
|
guards: {
|
|
|
|
hasStepInUrl,
|
2024-02-06 10:28:26 +00:00
|
|
|
isFontLibraryAvailable,
|
2024-03-18 15:49:41 +00:00
|
|
|
hasFontInstallInUrl,
|
2024-06-05 13:09:35 +00:00
|
|
|
hasPatternInstallInUrl,
|
2024-01-11 14:32:16 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|