woocommerce/plugins/woocommerce-admin/client/customize-store/design-with-ai/state-machine.tsx

497 lines
10 KiB
TypeScript
Raw Normal View History

/**
* External dependencies
*/
CYS - Add LookAndFeel and ToneOfVoice pages (#39979) * Add ProgressBar component to @woocommerce/components * Add changelog * Remove html.wp-toolbar in fullscreen mode * Add base style * Add Tell us a bit more about your business page * Fix merge conflict issues * Send BUSINESS_INFO_DESCRIPTION_COMPLETE event when continue button is clicked * Remove duplicated style import * Add changefile(s) from automation for the following project(s): @woocommerce/components, woocommerce * Lint fix * Add 'Look and Feel' and 'Tone of voice' pages'; * Use correct classname * Minor changes * Textearea color should be gray-900 after the user enter text * guide font weight should be 500 * Fix layout shift when a choice is selected * Fix choices width for tone of voice page * Use context value for the default * Revert button margin top * Fix default selection * Add X button * Decrease the margin by 20px to accommodate the height of the close button * Add close action * Include @woocommerce/ai package * Add AI service * Use AI service * Parse JSON from in function * Fix assignLookAndTone event type * Update plugins/woocommerce-admin/client/customize-store/design-with-ai/components/choice/choice.scss Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> * Update plugins/woocommerce-admin/client/customize-store/design-with-ai/services.ts Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> * Log when AI API endpoint request fails * Add spinner when user clicks the continue button * streamlined unnecessary isRequesting context and forwarded close event * pnpm-lock changes from trunk * lint fixes * ai package test passWithNoTests * changelog * reset pnpm-lock to trunk * Dev: update pnpm-lock.yaml and jest preset config (#40045) * Update pnpm-lock.yaml * Update jest-preset config to fix unexpected token error --------- Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> Co-authored-by: rjchow <me@rjchow.com>
2023-09-06 06:21:09 +00:00
import { createMachine, sendParent } from 'xstate';
import { getQuery } from '@woocommerce/navigation';
/**
* Internal dependencies
*/
import {
designWithAiStateMachineContext,
designWithAiStateMachineEvents,
FontPairing,
ColorPaletteResponse,
HomepageTemplate,
} from './types';
import {
BusinessInfoDescription,
LookAndFeel,
ToneOfVoice,
ApiCallLoader,
AssembleHubLoader,
} from './pages';
import { actions } from './actions';
CYS - Add LookAndFeel and ToneOfVoice pages (#39979) * Add ProgressBar component to @woocommerce/components * Add changelog * Remove html.wp-toolbar in fullscreen mode * Add base style * Add Tell us a bit more about your business page * Fix merge conflict issues * Send BUSINESS_INFO_DESCRIPTION_COMPLETE event when continue button is clicked * Remove duplicated style import * Add changefile(s) from automation for the following project(s): @woocommerce/components, woocommerce * Lint fix * Add 'Look and Feel' and 'Tone of voice' pages'; * Use correct classname * Minor changes * Textearea color should be gray-900 after the user enter text * guide font weight should be 500 * Fix layout shift when a choice is selected * Fix choices width for tone of voice page * Use context value for the default * Revert button margin top * Fix default selection * Add X button * Decrease the margin by 20px to accommodate the height of the close button * Add close action * Include @woocommerce/ai package * Add AI service * Use AI service * Parse JSON from in function * Fix assignLookAndTone event type * Update plugins/woocommerce-admin/client/customize-store/design-with-ai/components/choice/choice.scss Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> * Update plugins/woocommerce-admin/client/customize-store/design-with-ai/services.ts Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> * Log when AI API endpoint request fails * Add spinner when user clicks the continue button * streamlined unnecessary isRequesting context and forwarded close event * pnpm-lock changes from trunk * lint fixes * ai package test passWithNoTests * changelog * reset pnpm-lock to trunk * Dev: update pnpm-lock.yaml and jest preset config (#40045) * Update pnpm-lock.yaml * Update jest-preset config to fix unexpected token error --------- Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> Co-authored-by: rjchow <me@rjchow.com>
2023-09-06 06:21:09 +00:00
import { services } from './services';
import { defaultColorPalette } from './prompts';
export const hasStepInUrl = (
_ctx: unknown,
_evt: unknown,
{ cond }: { cond: unknown }
) => {
const { path = '' } = getQuery() as { path: string };
const pathFragments = path.split( '/' );
return (
pathFragments[ 3 ] === // [0] '', [1] 'customize-store', [2] cys step slug [3] design-with-ai step slug
( cond as { step: string | undefined } ).step
);
};
2023-12-06 12:49:28 +00:00
export const isAiOnline = ( _ctx: designWithAiStateMachineContext ) => {
return _ctx.aiOnline;
};
export const designWithAiStateMachineDefinition = createMachine(
{
id: 'designWithAi',
predictableActionArguments: true,
preserveActionOrder: true,
schema: {
context: {} as designWithAiStateMachineContext,
events: {} as designWithAiStateMachineEvents,
},
invoke: {
src: 'browserPopstateHandler',
},
on: {
EXTERNAL_URL_UPDATE: {
target: 'navigate',
},
AI_WIZARD_CLOSED_BEFORE_COMPLETION: {
actions: [
sendParent( ( _context, event ) => event ),
'recordTracksStepClosed',
],
},
},
context: {
startLoadingTime: null,
businessInfoDescription: {
descriptionText: '',
},
lookAndFeel: {
choice: '',
},
toneOfVoice: {
choice: '',
},
aiSuggestions: {
// Default color palette, font pairing are used as fallbacks when the AI endpoint fails.
defaultColorPalette: {
default: 'Ancient Bronze',
} as ColorPaletteResponse,
fontPairing: 'Rubik + Inter' as FontPairing[ 'pair_name' ],
homepageTemplate:
'template1' as HomepageTemplate[ 'homepage_template' ],
},
apiCallLoader: {
hasErrors: false,
},
2023-12-06 12:49:28 +00:00
aiOnline: true,
isBlockTheme: false,
},
initial: 'navigate',
states: {
navigate: {
always: [
{
target: 'businessInfoDescription',
cond: {
type: 'hasStepInUrl',
step: 'business-info-description',
},
},
{
target: 'lookAndFeel',
cond: {
type: 'hasStepInUrl',
step: 'look-and-feel',
},
},
{
target: 'toneOfVoice',
cond: {
type: 'hasStepInUrl',
step: 'tone-of-voice',
},
},
{
target: 'apiCallLoader',
cond: {
type: 'hasStepInUrl',
step: 'api-call-loader',
},
},
{
target: 'businessInfoDescription',
},
],
},
businessInfoDescription: {
id: 'businessInfoDescription',
initial: 'preBusinessInfoDescription',
states: {
preBusinessInfoDescription: {
// if we need to prefetch options, other settings previously populated from core profiler, do it here
always: {
target: 'businessInfoDescription',
},
},
businessInfoDescription: {
meta: {
component: BusinessInfoDescription,
},
entry: [
{
type: 'recordTracksStepViewed',
step: 'business_info_description',
},
],
on: {
BUSINESS_INFO_DESCRIPTION_COMPLETE: {
actions: [
'assignBusinessInfoDescription',
'spawnSaveDescriptionToOption',
{
type: 'recordTracksStepCompleted',
step: 'business_info_description',
},
],
target: 'postBusinessInfoDescription',
CYS - Add LookAndFeel and ToneOfVoice pages (#39979) * Add ProgressBar component to @woocommerce/components * Add changelog * Remove html.wp-toolbar in fullscreen mode * Add base style * Add Tell us a bit more about your business page * Fix merge conflict issues * Send BUSINESS_INFO_DESCRIPTION_COMPLETE event when continue button is clicked * Remove duplicated style import * Add changefile(s) from automation for the following project(s): @woocommerce/components, woocommerce * Lint fix * Add 'Look and Feel' and 'Tone of voice' pages'; * Use correct classname * Minor changes * Textearea color should be gray-900 after the user enter text * guide font weight should be 500 * Fix layout shift when a choice is selected * Fix choices width for tone of voice page * Use context value for the default * Revert button margin top * Fix default selection * Add X button * Decrease the margin by 20px to accommodate the height of the close button * Add close action * Include @woocommerce/ai package * Add AI service * Use AI service * Parse JSON from in function * Fix assignLookAndTone event type * Update plugins/woocommerce-admin/client/customize-store/design-with-ai/components/choice/choice.scss Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> * Update plugins/woocommerce-admin/client/customize-store/design-with-ai/services.ts Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> * Log when AI API endpoint request fails * Add spinner when user clicks the continue button * streamlined unnecessary isRequesting context and forwarded close event * pnpm-lock changes from trunk * lint fixes * ai package test passWithNoTests * changelog * reset pnpm-lock to trunk * Dev: update pnpm-lock.yaml and jest preset config (#40045) * Update pnpm-lock.yaml * Update jest-preset config to fix unexpected token error --------- Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> Co-authored-by: rjchow <me@rjchow.com>
2023-09-06 06:21:09 +00:00
},
},
},
postBusinessInfoDescription: {
always: {
target: '#lookAndFeel',
},
},
},
},
lookAndFeel: {
id: 'lookAndFeel',
initial: 'preLookAndFeel',
states: {
preLookAndFeel: {
always: {
target: 'lookAndFeel',
},
},
lookAndFeel: {
meta: {
component: LookAndFeel,
},
entry: [
{
type: 'updateQueryStep',
step: 'look-and-feel',
},
{
type: 'recordTracksStepViewed',
step: 'look_and_feel',
},
],
on: {
LOOK_AND_FEEL_COMPLETE: {
actions: [
{
type: 'recordTracksStepCompleted',
step: 'look_and_feel',
},
'assignLookAndFeel',
],
target: 'postLookAndFeel',
},
},
},
postLookAndFeel: {
always: {
target: '#toneOfVoice',
},
},
},
},
toneOfVoice: {
id: 'toneOfVoice',
initial: 'preToneOfVoice',
states: {
preToneOfVoice: {
always: {
target: 'toneOfVoice',
},
},
toneOfVoice: {
meta: {
component: ToneOfVoice,
},
entry: [
{
type: 'updateQueryStep',
step: 'tone-of-voice',
},
{
type: 'recordTracksStepViewed',
step: 'tone_of_voice',
},
],
on: {
TONE_OF_VOICE_COMPLETE: {
actions: [
'assignToneOfVoice',
{
type: 'recordTracksStepCompleted',
step: 'tone_of_voice',
},
],
target: 'postToneOfVoice',
},
},
},
postToneOfVoice: {
always: {
target: '#apiCallLoader',
},
},
},
},
apiCallLoader: {
id: 'apiCallLoader',
initial: 'preApiCallLoader',
states: {
preApiCallLoader: {
always: {
target: 'apiCallLoader',
},
},
apiCallLoader: {
meta: {
component: ApiCallLoader,
},
entry: [
{
type: 'updateQueryStep',
step: 'api-call-loader',
},
'assignStartLoadingTime',
],
type: 'parallel',
states: {
chooseColorPairing: {
2023-12-06 12:49:28 +00:00
initial: 'executeOrSkip',
states: {
2023-12-06 12:49:28 +00:00
executeOrSkip: {
always: [
{
target: 'pending',
cond: 'isAiOnline',
},
{
target: 'success',
},
],
},
pending: {
invoke: {
src: 'queryAiEndpoint',
data: ( context ) => {
return {
...defaultColorPalette,
prompt: defaultColorPalette.prompt(
context
.businessInfoDescription
.descriptionText,
context.lookAndFeel
.choice,
context.toneOfVoice
.choice
),
};
},
onDone: {
actions: [
'assignDefaultColorPalette',
],
target: 'success',
},
// If there's an error we don't want to block the user from proceeding.
onError: {
target: 'success',
},
},
},
success: { type: 'final' },
},
},
chooseFontPairing: {
initial: 'pending',
states: {
pending: {
entry: [ 'assignFontPairing' ],
always: {
target: 'success',
},
},
success: { type: 'final' },
},
},
updateStorePatterns: {
2023-12-06 12:49:28 +00:00
initial: 'executeOrSkip',
states: {
2023-12-06 12:49:28 +00:00
executeOrSkip: {
always: [
{
target: 'pending',
cond: 'isAiOnline',
},
{
target: 'resetPatternsAndProducts',
2023-12-06 12:49:28 +00:00
},
],
},
pending: {
invoke: {
src: 'updateStorePatterns',
onDone: {
target: 'success',
},
onError: {
actions: [
'assignAPICallLoaderError',
],
target: '#toneOfVoice',
},
},
},
resetPatternsAndProducts: {
2023-12-06 12:49:28 +00:00
invoke: {
src: 'resetPatternsAndProducts',
2023-12-06 12:49:28 +00:00
onDone: {
target: 'success',
},
onError: {
actions: [
'assignAPICallLoaderError',
],
target: '#toneOfVoice',
},
},
},
success: { type: 'final' },
},
},
installAndActivateTheme: {
initial: 'pending',
states: {
pending: {
invoke: {
src: 'installAndActivateTheme',
onDone: {
target: 'success',
},
onError: {
actions: [
'assignAPICallLoaderError',
],
target: '#toneOfVoice',
},
},
},
success: { type: 'final' },
},
},
},
onDone: 'postApiCallLoader',
},
postApiCallLoader: {
type: 'parallel',
states: {
assembleSite: {
initial: 'pending',
states: {
pending: {
invoke: {
src: 'assembleSite',
onDone: {
target: 'done',
},
onError: {
actions: [
'assignAPICallLoaderError',
],
target: '#toneOfVoice',
},
},
},
done: {
type: 'final',
},
},
},
saveAiResponse: {
initial: 'pending',
states: {
pending: {
invoke: {
src: 'saveAiResponseToOption',
onDone: {
target: 'done',
},
onError: {
target: 'failed',
},
},
},
done: {
type: 'final',
},
failed: {
type: 'final',
},
},
},
installPatterns: {
initial: 'pending',
states: {
pending: {
invoke: {
src: 'installPatterns',
onDone: {
target: 'success',
},
onError: {
target: 'success',
},
},
},
success: {
type: 'final',
},
},
},
},
onDone: {
target: '#designWithAi.showAssembleHub',
},
},
},
},
showAssembleHub: {
meta: {
component: AssembleHubLoader,
},
entry: [ 'redirectToAssemblerHub' ],
type: 'final',
},
},
},
{
actions,
CYS - Add LookAndFeel and ToneOfVoice pages (#39979) * Add ProgressBar component to @woocommerce/components * Add changelog * Remove html.wp-toolbar in fullscreen mode * Add base style * Add Tell us a bit more about your business page * Fix merge conflict issues * Send BUSINESS_INFO_DESCRIPTION_COMPLETE event when continue button is clicked * Remove duplicated style import * Add changefile(s) from automation for the following project(s): @woocommerce/components, woocommerce * Lint fix * Add 'Look and Feel' and 'Tone of voice' pages'; * Use correct classname * Minor changes * Textearea color should be gray-900 after the user enter text * guide font weight should be 500 * Fix layout shift when a choice is selected * Fix choices width for tone of voice page * Use context value for the default * Revert button margin top * Fix default selection * Add X button * Decrease the margin by 20px to accommodate the height of the close button * Add close action * Include @woocommerce/ai package * Add AI service * Use AI service * Parse JSON from in function * Fix assignLookAndTone event type * Update plugins/woocommerce-admin/client/customize-store/design-with-ai/components/choice/choice.scss Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> * Update plugins/woocommerce-admin/client/customize-store/design-with-ai/services.ts Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> * Log when AI API endpoint request fails * Add spinner when user clicks the continue button * streamlined unnecessary isRequesting context and forwarded close event * pnpm-lock changes from trunk * lint fixes * ai package test passWithNoTests * changelog * reset pnpm-lock to trunk * Dev: update pnpm-lock.yaml and jest preset config (#40045) * Update pnpm-lock.yaml * Update jest-preset config to fix unexpected token error --------- Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com> Co-authored-by: rjchow <me@rjchow.com>
2023-09-06 06:21:09 +00:00
services,
guards: {
hasStepInUrl,
2023-12-06 12:49:28 +00:00
isAiOnline,
},
}
);