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
|
|
|
|
*/
|
|
|
|
import { aiStatusResponse } from '../types';
|
2024-02-05 11:36:33 +00:00
|
|
|
import { isIframe } from '~/customize-store/utils';
|
2023-12-06 12:49:28 +00:00
|
|
|
|
|
|
|
export const fetchAiStatus = () => async (): Promise< aiStatusResponse > => {
|
|
|
|
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-02-05 11:36:33 +00:00
|
|
|
export const fetchActiveThemeHasMods = async () => {
|
2023-10-18 10:15:36 +00:00
|
|
|
const currentTemplatePromise =
|
2023-09-29 23:03:50 +00:00
|
|
|
// @ts-expect-error No types for this exist yet.
|
2023-10-18 10:15:36 +00:00
|
|
|
resolveSelect( coreStore ).__experimentalGetTemplateForLink( '/' );
|
|
|
|
|
|
|
|
const styleRevsPromise =
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
resolveSelect( coreStore ).getCurrentThemeGlobalStylesRevisions();
|
|
|
|
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
const hasModifiedPagesPromise = resolveSelect( coreStore ).getEntityRecords(
|
|
|
|
'postType',
|
|
|
|
'page',
|
|
|
|
{
|
|
|
|
per_page: 100,
|
|
|
|
_fields: [ 'id', '_links.version-history' ],
|
|
|
|
orderby: 'menu_order',
|
|
|
|
order: 'asc',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2024-02-05 11:36:33 +00:00
|
|
|
const [ currentTemplate, styleRevs, rawPages ] = await Promise.all( [
|
2023-10-18 10:15:36 +00:00
|
|
|
currentTemplatePromise,
|
|
|
|
styleRevsPromise,
|
|
|
|
hasModifiedPagesPromise,
|
|
|
|
] );
|
|
|
|
|
|
|
|
const hasModifiedPages = rawPages?.some(
|
|
|
|
( page: { _links: { [ key: string ]: string[] } } ) => {
|
|
|
|
return page._links?.[ 'version-history' ]?.length > 1;
|
|
|
|
}
|
|
|
|
);
|
2023-09-29 23:03:50 +00:00
|
|
|
|
|
|
|
const activeThemeHasMods =
|
2023-10-05 03:42:05 +00:00
|
|
|
!! currentTemplate?.modified ||
|
2023-09-29 23:03:50 +00:00
|
|
|
styleRevs?.length > 0 ||
|
2023-10-05 03:42:05 +00:00
|
|
|
hasModifiedPages;
|
2023-10-18 10:15:36 +00:00
|
|
|
|
2024-02-05 11:36:33 +00:00
|
|
|
return activeThemeHasMods;
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
return {
|
|
|
|
customizeStoreTaskCompleted,
|
2023-10-05 13:33:50 +00:00
|
|
|
themeData,
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
} )(),
|
|
|
|
ACTIVE_THEME_HAS_MODS: ( async () => {
|
|
|
|
const activeThemeHasMods = await fetchActiveThemeHasMods();
|
|
|
|
window.__wcCustomizeStore = {
|
|
|
|
...window.__wcCustomizeStore,
|
|
|
|
activeThemeHasMods,
|
|
|
|
};
|
|
|
|
} )(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Since the _featureFlags values are promises, we need to wait for
|
|
|
|
// all of them to resolve before returning.
|
|
|
|
await Promise.all( Object.values( _featureFlags ) );
|
|
|
|
}
|
|
|
|
};
|