| @@ -1,0 +1,34 @@ | ||
| 1 | +/** | |
| 2 | + * Content Guidelines AI — Entry point. | |
| 3 | + * | |
| 4 | + * Injects Jetpack AI-powered generate/improve buttons into the | |
| 5 | + * Content Guidelines admin page (Gutenberg experimental feature). | |
| 6 | + */ | |
| 7 | +import { mapAiFeatureResponseToAiFeatureProps } from '@automattic/jetpack-shared-stores'; | |
| 8 | +import '@automattic/jetpack-shared-extension-utils/store/wordpress-com'; | |
| 9 | +import { dispatch } from '@wordpress/data'; | |
| 10 | +import './content-guidelines-ai/store'; | |
| 11 | +import './content-guidelines-ai/style.scss'; | |
| 12 | +import { startInjection } from './content-guidelines-ai/lib/inject'; | |
| 13 | + | |
| 14 | +// The AI feature state is resolved server-side and preloaded into the page | |
| 15 | +// (see _inc/content-guidelines-ai.php), so components render the correct | |
| 16 | +// locked/unlocked state on first paint with no client-side plan fetch. | |
| 17 | +// Without the preload (server-side lookup failed) render no AI UI at all — | |
| 18 | +// the store's optimistic hasFeature default would show unlocked buttons | |
| 19 | +// whose requests are destined to fail. | |
| 20 | +const aiFeature = window.jetpackContentGuidelinesAi?.aiFeature; | |
| 21 | + | |
| 22 | +if ( aiFeature ) { | |
| 23 | + const plansDispatch = dispatch( 'wordpress-com/plans' ); | |
| 24 | + plansDispatch.storeAiAssistantFeature( mapAiFeatureResponseToAiFeatureProps( aiFeature ) ); | |
| 25 | + // Mark the selector resolved so its resolver never fires a redundant | |
| 26 | + // fetch when components select getAiAssistantFeature. | |
| 27 | + plansDispatch.finishResolution( 'getAiAssistantFeature', [] ); | |
| 28 | + | |
| 29 | + if ( document.readyState === 'loading' ) { | |
| 30 | + document.addEventListener( 'DOMContentLoaded', startInjection ); | |
| 31 | + } else { | |
| 32 | + startInjection(); | |
| 33 | + } | |
| 34 | +} | |