woocommerce/plugins/woo-ai/src/index.js

71 lines
2.0 KiB
JavaScript
Raw Normal View History

/**
* External dependencies
*/
import { render, createRoot } from '@wordpress/element';
import { QueryClient, QueryClientProvider } from 'react-query';
/**
* Internal dependencies
*/
import { WriteItForMeButtonContainer } from './product-description';
import { ProductNameSuggestions } from './product-name';
Suggest product categories with AI (#39437) * Accept arguments for the TinyContent getContent method. Used to fetch the plain text version of the description. * Add a class to the loading message content * Set the max description length as global constant * Create a MagicButton component and use it * Get plain text description instead of HTML * Return full category hierarchy (Parent > Child) * Add method to return all available categories on the product edit page * Helper function to generate product data instructions for the prompt * Helper function to select category checkboxes on DOM * Create component to display a list of suggestion items as pills * Add product category suggestions to product edit page * Use the AI package to get text completion * Add tracks * Add changelog * Fix merge conflict * Remove NoMatch state for category suggestions * Get available categories using WC REST API * Suggest new categories * Run separate prompts for existing and new category generation * Fix overflow in suggestion pills * Don't include existing selected categories in prompt * Add util to encode html entities * Exclude "Uncategorized" category from product data * Allow excluding properties from the product data instructions * Create category from suggestion if it doesn't exist * Show suggestions as links instead of pills * Ask for feedback after suggestion selected * Decode html entities in available categories results * Don't encode html entities when comparing available categories * Change feedback box style * Suggest only one category * Remove log * Show feedback box after generating suggestions Instead of showing it after a suggestion is selected * Fix typo
2023-09-12 16:59:26 +00:00
import { ProductCategorySuggestions } from './product-category';
import { WriteShortDescriptionButtonContainer } from './product-short-description';
import setPreferencesPersistence from './utils/preferencesPersistence';
import './index.scss';
// This sets up loading and saving the plugin's preferences.
setPreferencesPersistence();
const queryClient = new QueryClient();
const renderComponent = ( Component, rootElement ) => {
if ( ! rootElement ) {
return;
}
const WrappedComponent = () => (
<QueryClientProvider client={ queryClient }>
<Component />
</QueryClientProvider>
);
if ( createRoot ) {
createRoot( rootElement ).render( <WrappedComponent /> );
} else {
render( <WrappedComponent />, rootElement );
}
};
Suggest product categories with AI (#39437) * Accept arguments for the TinyContent getContent method. Used to fetch the plain text version of the description. * Add a class to the loading message content * Set the max description length as global constant * Create a MagicButton component and use it * Get plain text description instead of HTML * Return full category hierarchy (Parent > Child) * Add method to return all available categories on the product edit page * Helper function to generate product data instructions for the prompt * Helper function to select category checkboxes on DOM * Create component to display a list of suggestion items as pills * Add product category suggestions to product edit page * Use the AI package to get text completion * Add tracks * Add changelog * Fix merge conflict * Remove NoMatch state for category suggestions * Get available categories using WC REST API * Suggest new categories * Run separate prompts for existing and new category generation * Fix overflow in suggestion pills * Don't include existing selected categories in prompt * Add util to encode html entities * Exclude "Uncategorized" category from product data * Allow excluding properties from the product data instructions * Create category from suggestion if it doesn't exist * Show suggestions as links instead of pills * Ask for feedback after suggestion selected * Decode html entities in available categories results * Don't encode html entities when comparing available categories * Change feedback box style * Suggest only one category * Remove log * Show feedback box after generating suggestions Instead of showing it after a suggestion is selected * Fix typo
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 );
};
const descriptionButtonRoot = document.getElementById(
'woocommerce-ai-app-product-gpt-button'
);
const nameSuggestionsRoot = document.getElementById(
'woocommerce-ai-app-product-name-suggestions'
);
const shortDescriptionButtonRoot = document.getElementById(
'woocommerce-ai-app-product-short-description-gpt-button'
);
if ( window.JP_CONNECTION_INITIAL_STATE?.connectionStatus?.isActive ) {
renderComponent( WriteItForMeButtonContainer, descriptionButtonRoot );
renderComponent( ProductNameSuggestions, nameSuggestionsRoot );
Suggest product categories with AI (#39437) * Accept arguments for the TinyContent getContent method. Used to fetch the plain text version of the description. * Add a class to the loading message content * Set the max description length as global constant * Create a MagicButton component and use it * Get plain text description instead of HTML * Return full category hierarchy (Parent > Child) * Add method to return all available categories on the product edit page * Helper function to generate product data instructions for the prompt * Helper function to select category checkboxes on DOM * Create component to display a list of suggestion items as pills * Add product category suggestions to product edit page * Use the AI package to get text completion * Add tracks * Add changelog * Fix merge conflict * Remove NoMatch state for category suggestions * Get available categories using WC REST API * Suggest new categories * Run separate prompts for existing and new category generation * Fix overflow in suggestion pills * Don't include existing selected categories in prompt * Add util to encode html entities * Exclude "Uncategorized" category from product data * Allow excluding properties from the product data instructions * Create category from suggestion if it doesn't exist * Show suggestions as links instead of pills * Ask for feedback after suggestion selected * Decode html entities in available categories results * Don't encode html entities when comparing available categories * Change feedback box style * Suggest only one category * Remove log * Show feedback box after generating suggestions Instead of showing it after a suggestion is selected * Fix typo
2023-09-12 16:59:26 +00:00
renderProductCategorySuggestions();
renderComponent(
WriteShortDescriptionButtonContainer,
shortDescriptionButtonRoot
);
}