2023-10-09 05:19:08 +00:00
|
|
|
// @ts-expect-error -- No types for this exist yet.
|
|
|
|
// eslint-disable-next-line @woocommerce/dependency-group
|
|
|
|
import { store as coreStore } from '@wordpress/core-data';
|
2023-09-29 23:03:50 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { resolveSelect } from '@wordpress/data';
|
2023-10-09 05:19:08 +00:00
|
|
|
import { ONBOARDING_STORE_NAME, OPTIONS_STORE_NAME } from '@woocommerce/data';
|
2023-10-05 13:33:50 +00:00
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
2023-08-18 05:30:25 +00:00
|
|
|
|
2023-12-06 12:49:28 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2024-04-23 08:12:35 +00:00
|
|
|
import { FlowType, aiStatusResponse } from '../types';
|
2024-02-05 11:36:33 +00:00
|
|
|
import { isIframe } from '~/customize-store/utils';
|
2024-04-23 08:12:35 +00:00
|
|
|
import { isWooExpress } from '~/utils/is-woo-express';
|
2024-04-23 17:38:06 +00:00
|
|
|
import { trackEvent } from '../tracking';
|
2023-12-06 12:49:28 +00:00
|
|
|
|
2024-04-23 08:12:35 +00:00
|
|
|
export const fetchAiStatus = async (): Promise< aiStatusResponse > => {
|
2023-12-06 12:49:28 +00:00
|
|
|
const response = await fetch(
|
|
|
|
'https://status.openai.com/api/v2/status.json'
|
|
|
|
);
|
|
|
|
const data = await response.json();
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2023-08-18 05:30:25 +00:00
|
|
|
export const fetchThemeCards = async () => {
|
2023-10-05 13:33:50 +00:00
|
|
|
const themes = await apiFetch( {
|
|
|
|
path: '/wc-admin/onboarding/themes/recommended',
|
|
|
|
method: 'GET',
|
|
|
|
} );
|
|
|
|
|
|
|
|
return themes;
|
2023-08-18 05:30:25 +00:00
|
|
|
};
|
2023-09-29 23:03:50 +00:00
|
|
|
|
2024-03-27 13:13:52 +00:00
|
|
|
export const fetchCustomizeStoreCompleted = async () => {
|
|
|
|
const task = await resolveSelect( ONBOARDING_STORE_NAME ).getTask(
|
|
|
|
'customize-store'
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
customizeStoreTaskCompleted: task?.isComplete,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-02-05 11:36:33 +00:00
|
|
|
export const fetchIntroData = async () => {
|
|
|
|
const currentTemplatePromise =
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
resolveSelect( coreStore ).__experimentalGetTemplateForLink( '/' );
|
|
|
|
|
|
|
|
const maybePreviousTemplatePromise = resolveSelect(
|
|
|
|
OPTIONS_STORE_NAME
|
|
|
|
).getOption( 'woocommerce_admin_customize_store_completed_theme_id' );
|
|
|
|
|
|
|
|
const getTaskPromise = resolveSelect( ONBOARDING_STORE_NAME ).getTask(
|
|
|
|
'customize-store'
|
|
|
|
);
|
|
|
|
|
|
|
|
const themeDataPromise = fetchThemeCards();
|
|
|
|
|
|
|
|
const [ currentTemplate, maybePreviousTemplate, task, themeData ] =
|
|
|
|
await Promise.all( [
|
|
|
|
currentTemplatePromise,
|
|
|
|
maybePreviousTemplatePromise,
|
|
|
|
getTaskPromise,
|
|
|
|
themeDataPromise,
|
|
|
|
] );
|
|
|
|
|
|
|
|
let currentThemeIsAiGenerated = false;
|
|
|
|
if (
|
|
|
|
maybePreviousTemplate &&
|
|
|
|
currentTemplate?.id === maybePreviousTemplate
|
|
|
|
) {
|
|
|
|
currentThemeIsAiGenerated = true;
|
|
|
|
}
|
|
|
|
|
2023-10-18 10:15:36 +00:00
|
|
|
const customizeStoreTaskCompleted = task?.isComplete;
|
2023-09-29 23:03:50 +00:00
|
|
|
|
2024-03-13 13:55:04 +00:00
|
|
|
interface Theme {
|
|
|
|
stylesheet?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const theme = ( await resolveSelect( 'core' ).getCurrentTheme() ) as Theme;
|
|
|
|
|
2023-09-29 23:03:50 +00:00
|
|
|
return {
|
|
|
|
customizeStoreTaskCompleted,
|
2023-10-05 13:33:50 +00:00
|
|
|
themeData,
|
2024-03-13 13:55:04 +00:00
|
|
|
activeTheme: theme.stylesheet || '',
|
2023-10-09 05:19:08 +00:00
|
|
|
currentThemeIsAiGenerated,
|
2023-09-29 23:03:50 +00:00
|
|
|
};
|
|
|
|
};
|
2024-02-05 11:36:33 +00:00
|
|
|
|
|
|
|
const fetchIsFontLibraryAvailable = async () => {
|
|
|
|
try {
|
|
|
|
await apiFetch( {
|
2024-02-13 13:03:06 +00:00
|
|
|
path: '/wp/v2/font-collections?_fields=slug',
|
2024-02-05 11:36:33 +00:00
|
|
|
method: 'GET',
|
|
|
|
} );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch ( err ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-06-05 13:09:35 +00:00
|
|
|
const fetchIsPTKPatternsAPIAvailable = async () => {
|
|
|
|
try {
|
|
|
|
await apiFetch( {
|
|
|
|
path: '/wc/private/patterns',
|
|
|
|
method: 'GET',
|
|
|
|
} );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch ( err ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-02-05 11:36:33 +00:00
|
|
|
export const setFlags = async () => {
|
|
|
|
if ( ! isIframe( window ) ) {
|
|
|
|
// To improve the readability of the code, we want to use a dictionary
|
|
|
|
// where the key is the feature flag name and the value is the
|
|
|
|
// function to retrieve flag value.
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
const _featureFlags = {
|
|
|
|
FONT_LIBRARY_AVAILABLE: ( async () => {
|
|
|
|
const isFontLibraryAvailable =
|
|
|
|
await fetchIsFontLibraryAvailable();
|
|
|
|
window.__wcCustomizeStore = {
|
|
|
|
...window.__wcCustomizeStore,
|
|
|
|
isFontLibraryAvailable,
|
|
|
|
};
|
|
|
|
} )(),
|
2024-06-05 13:09:35 +00:00
|
|
|
PTK_PATTERNS_API_AVAILABLE: ( async () => {
|
|
|
|
const isPTKPatternsAPIAvailable =
|
|
|
|
await fetchIsPTKPatternsAPIAvailable();
|
|
|
|
window.__wcCustomizeStore = {
|
|
|
|
...window.__wcCustomizeStore,
|
|
|
|
isPTKPatternsAPIAvailable,
|
|
|
|
};
|
|
|
|
} )(),
|
2024-02-05 11:36:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Since the _featureFlags values are promises, we need to wait for
|
|
|
|
// all of them to resolve before returning.
|
|
|
|
await Promise.all( Object.values( _featureFlags ) );
|
|
|
|
}
|
2024-04-23 08:12:35 +00:00
|
|
|
|
|
|
|
// Set FlowType flag. We want to set the flag only in the parent window.
|
|
|
|
if ( isWooExpress() && ! isIframe( window ) ) {
|
|
|
|
try {
|
|
|
|
const { status } = await fetchAiStatus();
|
|
|
|
|
|
|
|
const isAiOnline =
|
|
|
|
status.indicator !== 'critical' && status.indicator !== 'major';
|
|
|
|
|
|
|
|
// @ts-expect-error temp workaround;
|
|
|
|
window.cys_aiOnline = status;
|
2024-04-23 17:38:06 +00:00
|
|
|
trackEvent( 'customize_your_store_ai_status', {
|
2024-04-23 08:12:35 +00:00
|
|
|
online: isAiOnline ? 'yes' : 'no',
|
|
|
|
} );
|
|
|
|
|
|
|
|
return isAiOnline ? FlowType.AIOnline : FlowType.AIOffline;
|
|
|
|
} catch ( e ) {
|
|
|
|
// @ts-expect-error temp workaround;
|
|
|
|
window.cys_aiOnline = false;
|
2024-04-23 17:38:06 +00:00
|
|
|
trackEvent( 'customize_your_store_ai_status', {
|
2024-04-23 08:12:35 +00:00
|
|
|
online: 'no',
|
|
|
|
} );
|
|
|
|
return FlowType.AIOffline;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FlowType.noAI;
|
2024-02-05 11:36:33 +00:00
|
|
|
};
|