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';
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
export const fetchIntroData = 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 maybePreviousTemplatePromise = resolveSelect(
|
2023-10-09 05:19:08 +00:00
|
|
|
OPTIONS_STORE_NAME
|
|
|
|
).getOption( 'woocommerce_admin_customize_store_completed_theme_id' );
|
|
|
|
|
2023-10-18 10:15:36 +00:00
|
|
|
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',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const getTaskPromise = resolveSelect( ONBOARDING_STORE_NAME ).getTask(
|
|
|
|
'customize-store'
|
|
|
|
);
|
|
|
|
|
|
|
|
const themeDataPromise = fetchThemeCards();
|
|
|
|
|
|
|
|
const [
|
|
|
|
currentTemplate,
|
|
|
|
maybePreviousTemplate,
|
|
|
|
styleRevs,
|
|
|
|
rawPages,
|
|
|
|
task,
|
|
|
|
themeData,
|
|
|
|
] = await Promise.all( [
|
|
|
|
currentTemplatePromise,
|
|
|
|
maybePreviousTemplatePromise,
|
|
|
|
styleRevsPromise,
|
|
|
|
hasModifiedPagesPromise,
|
|
|
|
getTaskPromise,
|
|
|
|
themeDataPromise,
|
|
|
|
] );
|
|
|
|
|
|
|
|
let currentThemeIsAiGenerated = false;
|
2023-10-09 05:19:08 +00:00
|
|
|
if (
|
|
|
|
maybePreviousTemplate &&
|
|
|
|
currentTemplate?.id === maybePreviousTemplate
|
|
|
|
) {
|
|
|
|
currentThemeIsAiGenerated = true;
|
|
|
|
}
|
2023-09-29 23:03:50 +00:00
|
|
|
|
2023-10-18 10:15:36 +00:00
|
|
|
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
|
|
|
|
|
|
|
const customizeStoreTaskCompleted = task?.isComplete;
|
2023-09-29 23:03:50 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
activeThemeHasMods,
|
|
|
|
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
|
|
|
};
|
|
|
|
};
|