2023-06-08 20:20:05 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { render, createRoot } from '@wordpress/element';
|
2023-08-09 12:15:18 +00:00
|
|
|
import { QueryClient, QueryClientProvider } from 'react-query';
|
2023-06-08 20:20:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { WriteItForMeButtonContainer } from './product-description';
|
|
|
|
import { ProductNameSuggestions } from './product-name';
|
2023-09-12 16:59:26 +00:00
|
|
|
import { ProductCategorySuggestions } from './product-category';
|
2023-09-06 10:36:14 +00:00
|
|
|
import { WriteShortDescriptionButtonContainer } from './product-short-description';
|
|
|
|
import setPreferencesPersistence from './utils/preferencesPersistence';
|
2023-06-08 20:20:05 +00:00
|
|
|
|
|
|
|
import './index.scss';
|
|
|
|
|
2023-09-06 10:36:14 +00:00
|
|
|
// This sets up loading and saving the plugin's preferences.
|
|
|
|
setPreferencesPersistence();
|
|
|
|
|
2023-08-09 12:15:18 +00:00
|
|
|
const queryClient = new QueryClient();
|
|
|
|
|
2023-06-08 20:20:05 +00:00
|
|
|
const renderComponent = ( Component, rootElement ) => {
|
|
|
|
if ( ! rootElement ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-09 12:15:18 +00:00
|
|
|
const WrappedComponent = () => (
|
|
|
|
<QueryClientProvider client={ queryClient }>
|
|
|
|
<Component />
|
|
|
|
</QueryClientProvider>
|
|
|
|
);
|
|
|
|
|
2023-06-08 20:20:05 +00:00
|
|
|
if ( createRoot ) {
|
2023-08-09 12:15:18 +00:00
|
|
|
createRoot( rootElement ).render( <WrappedComponent /> );
|
2023-06-08 20:20:05 +00:00
|
|
|
} else {
|
2023-08-09 12:15:18 +00:00
|
|
|
render( <WrappedComponent />, rootElement );
|
2023-06-08 20:20:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-12 16:59:26 +00:00
|
|
|
const renderProductCategorySuggestions = () => {
|
|
|
|
const root = document.createElement( 'div' );
|
|
|
|
root.id = 'woocommerce-ai-app-product-category-suggestions';
|
|
|
|
|
|
|
|
renderComponent( ProductCategorySuggestions, root );
|
|
|
|
|
|
|
|
// Insert the category suggestions node in the product category meta box.
|
|
|
|
document.getElementById( 'taxonomy-product_cat' ).append( root );
|
|
|
|
};
|
|
|
|
|
2023-06-08 20:20:05 +00:00
|
|
|
const descriptionButtonRoot = document.getElementById(
|
|
|
|
'woocommerce-ai-app-product-gpt-button'
|
|
|
|
);
|
|
|
|
const nameSuggestionsRoot = document.getElementById(
|
|
|
|
'woocommerce-ai-app-product-name-suggestions'
|
|
|
|
);
|
|
|
|
|
2023-09-06 10:36:14 +00:00
|
|
|
const shortDescriptionButtonRoot = document.getElementById(
|
|
|
|
'woocommerce-ai-app-product-short-description-gpt-button'
|
|
|
|
);
|
|
|
|
|
2023-06-08 20:20:05 +00:00
|
|
|
if ( window.JP_CONNECTION_INITIAL_STATE?.connectionStatus?.isActive ) {
|
|
|
|
renderComponent( WriteItForMeButtonContainer, descriptionButtonRoot );
|
|
|
|
renderComponent( ProductNameSuggestions, nameSuggestionsRoot );
|
2023-09-12 16:59:26 +00:00
|
|
|
renderProductCategorySuggestions();
|
2023-09-06 10:36:14 +00:00
|
|
|
renderComponent(
|
|
|
|
WriteShortDescriptionButtonContainer,
|
|
|
|
shortDescriptionButtonRoot
|
|
|
|
);
|
2023-06-08 20:20:05 +00:00
|
|
|
}
|