2023-08-29 06:00:54 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { assign } from 'xstate';
|
2023-09-12 06:32:50 +00:00
|
|
|
import { getQuery, updateQueryString } from '@woocommerce/navigation';
|
2023-09-15 04:48:12 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2023-08-29 06:00:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import {
|
2023-09-20 07:27:08 +00:00
|
|
|
ColorPaletteResponse,
|
2023-08-29 06:00:54 +00:00
|
|
|
designWithAiStateMachineContext,
|
|
|
|
designWithAiStateMachineEvents,
|
2023-09-20 04:26:15 +00:00
|
|
|
FontPairing,
|
2023-09-19 08:41:52 +00:00
|
|
|
LookAndToneCompletionResponse,
|
2023-08-29 06:00:54 +00:00
|
|
|
} from './types';
|
2023-09-15 04:48:12 +00:00
|
|
|
import { aiWizardClosedBeforeCompletionEvent } from './events';
|
2023-09-06 06:21:09 +00:00
|
|
|
import {
|
|
|
|
businessInfoDescriptionCompleteEvent,
|
|
|
|
lookAndFeelCompleteEvent,
|
|
|
|
toneOfVoiceCompleteEvent,
|
|
|
|
} from './pages';
|
2023-08-29 06:00:54 +00:00
|
|
|
|
|
|
|
const assignBusinessInfoDescription = assign<
|
|
|
|
designWithAiStateMachineContext,
|
|
|
|
designWithAiStateMachineEvents
|
|
|
|
>( {
|
2023-09-07 09:05:47 +00:00
|
|
|
businessInfoDescription: ( _context, event: unknown ) => {
|
2023-08-29 06:00:54 +00:00
|
|
|
return {
|
|
|
|
descriptionText: ( event as businessInfoDescriptionCompleteEvent )
|
|
|
|
.payload,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
} );
|
2023-09-06 06:21:09 +00:00
|
|
|
|
|
|
|
const assignLookAndFeel = assign<
|
|
|
|
designWithAiStateMachineContext,
|
|
|
|
designWithAiStateMachineEvents
|
|
|
|
>( {
|
2023-09-07 09:05:47 +00:00
|
|
|
lookAndFeel: ( _context, event: unknown ) => {
|
2023-09-06 06:21:09 +00:00
|
|
|
return {
|
|
|
|
choice: ( event as lookAndFeelCompleteEvent ).payload,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
const assignToneOfVoice = assign<
|
|
|
|
designWithAiStateMachineContext,
|
|
|
|
designWithAiStateMachineEvents
|
|
|
|
>( {
|
2023-09-07 09:05:47 +00:00
|
|
|
toneOfVoice: ( _context, event: unknown ) => {
|
2023-09-06 06:21:09 +00:00
|
|
|
return {
|
|
|
|
choice: ( event as toneOfVoiceCompleteEvent ).payload,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
const assignLookAndTone = assign<
|
|
|
|
designWithAiStateMachineContext,
|
|
|
|
designWithAiStateMachineEvents
|
|
|
|
>( {
|
2023-09-07 09:05:47 +00:00
|
|
|
lookAndFeel: ( _context, event: unknown ) => {
|
2023-09-06 06:21:09 +00:00
|
|
|
return {
|
2023-09-07 09:05:47 +00:00
|
|
|
choice: ( event as { data: LookAndToneCompletionResponse } ).data
|
|
|
|
.look,
|
2023-09-06 06:21:09 +00:00
|
|
|
};
|
|
|
|
},
|
2023-09-07 09:05:47 +00:00
|
|
|
toneOfVoice: ( _context, event: unknown ) => {
|
2023-09-06 06:21:09 +00:00
|
|
|
return {
|
2023-09-07 09:05:47 +00:00
|
|
|
choice: ( event as { data: LookAndToneCompletionResponse } ).data
|
|
|
|
.tone,
|
2023-09-06 06:21:09 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
2023-09-19 08:41:52 +00:00
|
|
|
const assignDefaultColorPalette = assign<
|
|
|
|
designWithAiStateMachineContext,
|
|
|
|
designWithAiStateMachineEvents
|
|
|
|
>( {
|
|
|
|
aiSuggestions: ( context, event: unknown ) => {
|
|
|
|
return {
|
|
|
|
...context.aiSuggestions,
|
|
|
|
defaultColorPalette: (
|
|
|
|
event as {
|
|
|
|
data: {
|
2023-09-20 07:27:08 +00:00
|
|
|
response: ColorPaletteResponse;
|
2023-09-19 08:41:52 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
).data.response,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
2023-09-20 04:26:15 +00:00
|
|
|
const assignFontPairing = assign<
|
|
|
|
designWithAiStateMachineContext,
|
|
|
|
designWithAiStateMachineEvents
|
|
|
|
>( {
|
|
|
|
aiSuggestions: ( context, event: unknown ) => {
|
|
|
|
return {
|
|
|
|
...context.aiSuggestions,
|
|
|
|
fontPairing: (
|
|
|
|
event as {
|
|
|
|
data: {
|
|
|
|
response: FontPairing;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
).data.response.pair_name,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
2023-09-06 06:21:09 +00:00
|
|
|
const logAIAPIRequestError = () => {
|
|
|
|
// log AI API request error
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log( 'API Request error' );
|
|
|
|
};
|
|
|
|
|
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] cys step slug [3] design-with-ai step slug
|
|
|
|
if (
|
|
|
|
pathFragments[ 1 ] === 'customize-store' &&
|
|
|
|
pathFragments[ 2 ] === 'design-with-ai'
|
|
|
|
) {
|
|
|
|
if ( pathFragments[ 3 ] !== 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/design-with-ai/${ step }`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-15 04:48:12 +00:00
|
|
|
const recordTracksStepViewed = (
|
|
|
|
_context: unknown,
|
|
|
|
_event: unknown,
|
|
|
|
{ action }: { action: unknown }
|
|
|
|
) => {
|
|
|
|
const { step } = action as { step: string };
|
|
|
|
recordEvent( 'customize_your_store_ai_wizard_step_view', {
|
|
|
|
step,
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
const recordTracksStepClosed = (
|
|
|
|
_context: unknown,
|
|
|
|
event: aiWizardClosedBeforeCompletionEvent
|
|
|
|
) => {
|
|
|
|
const { step } = event.payload;
|
|
|
|
recordEvent( `customize_your_store_ai_wizard_step_close`, {
|
|
|
|
step: step.replaceAll( '-', '_' ),
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
const recordTracksStepCompleted = (
|
|
|
|
_context: unknown,
|
|
|
|
_event: unknown,
|
|
|
|
{ action }: { action: unknown }
|
|
|
|
) => {
|
|
|
|
const { step } = action as { step: string };
|
|
|
|
recordEvent( 'customize_your_store_ai_wizard_step_complete', {
|
|
|
|
step,
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2023-08-29 06:00:54 +00:00
|
|
|
export const actions = {
|
|
|
|
assignBusinessInfoDescription,
|
2023-09-06 06:21:09 +00:00
|
|
|
assignLookAndFeel,
|
|
|
|
assignToneOfVoice,
|
|
|
|
assignLookAndTone,
|
2023-09-19 08:41:52 +00:00
|
|
|
assignDefaultColorPalette,
|
2023-09-20 04:26:15 +00:00
|
|
|
assignFontPairing,
|
2023-09-06 06:21:09 +00:00
|
|
|
logAIAPIRequestError,
|
2023-09-12 06:32:50 +00:00
|
|
|
updateQueryStep,
|
2023-09-15 04:48:12 +00:00
|
|
|
recordTracksStepViewed,
|
|
|
|
recordTracksStepClosed,
|
|
|
|
recordTracksStepCompleted,
|
2023-08-29 06:00:54 +00:00
|
|
|
};
|