From c294213866f399ffb0ad5888b92f3c5a782a18dc Mon Sep 17 00:00:00 2001 From: Luiz Kowalski Date: Tue, 30 Jul 2024 15:32:10 -0300 Subject: [PATCH 1/4] Prevent feature data requests when another one is already happening --- .../logo-generator/components/generator-modal.tsx | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/projects/js-packages/ai-client/src/logo-generator/components/generator-modal.tsx b/projects/js-packages/ai-client/src/logo-generator/components/generator-modal.tsx index 38fc5b8ccaed9..401abe38ff72f 100644 --- a/projects/js-packages/ai-client/src/logo-generator/components/generator-modal.tsx +++ b/projects/js-packages/ai-client/src/logo-generator/components/generator-modal.tsx @@ -3,7 +3,7 @@ */ import { useAnalytics } from '@automattic/jetpack-shared-extension-utils'; import { Modal, Button } from '@wordpress/components'; -import { useDispatch } from '@wordpress/data'; +import { useDispatch, select } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; import { external, Icon } from '@wordpress/icons'; import clsx from 'clsx'; @@ -50,6 +50,7 @@ export const GeneratorModal: React.FC< GeneratorModalProps > = ( { const { tracks } = useAnalytics(); const { recordEvent: recordTracksEvent } = tracks; const { setSiteDetails, fetchAiAssistantFeature, loadLogoHistory } = useDispatch( STORE_NAME ); + const { getIsRequestingAiAssistantFeature } = select( STORE_NAME ); const [ loadingState, setLoadingState ] = useState< 'loadingFeature' | 'analyzing' | 'generating' | null >( null ); @@ -182,10 +183,13 @@ export const GeneratorModal: React.FC< GeneratorModalProps > = ( { // When the site details are set, we need to fetch the feature data. if ( ! requestedFeatureData.current ) { - requestedFeatureData.current = true; - fetchAiAssistantFeature(); + const isRequestingFeature = getIsRequestingAiAssistantFeature(); + if ( ! isRequestingFeature ) { + requestedFeatureData.current = true; + fetchAiAssistantFeature(); + } } - }, [ siteId, siteDetails, setSiteDetails ] ); + }, [ siteId, siteDetails, setSiteDetails, getIsRequestingAiAssistantFeature ] ); // Handles modal opening logic useEffect( () => { From c7743b9903cc9a39224aac33ff1582343ca37f3f Mon Sep 17 00:00:00 2001 From: Luiz Kowalski Date: Tue, 30 Jul 2024 15:41:06 -0300 Subject: [PATCH 2/4] Support modal reloads and handle error retry with a modal reload --- .../src/logo-generator/components/generator-modal.tsx | 11 ++++++++++- .../js-packages/ai-client/src/logo-generator/types.ts | 1 + .../extended-blocks/core-site-logo/index.tsx | 6 ++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/projects/js-packages/ai-client/src/logo-generator/components/generator-modal.tsx b/projects/js-packages/ai-client/src/logo-generator/components/generator-modal.tsx index 401abe38ff72f..4b16523e443d4 100644 --- a/projects/js-packages/ai-client/src/logo-generator/components/generator-modal.tsx +++ b/projects/js-packages/ai-client/src/logo-generator/components/generator-modal.tsx @@ -43,6 +43,7 @@ export const GeneratorModal: React.FC< GeneratorModalProps > = ( { isOpen, onClose, onApplyLogo, + onReload, siteDetails, context, placement, @@ -210,7 +211,15 @@ export const GeneratorModal: React.FC< GeneratorModalProps > = ( { if ( loadingState ) { body = ; } else if ( featureFetchError || firstLogoPromptFetchError ) { - body = ; + body = ( + { + closeModal(); + onReload?.(); + } } + /> + ); } else if ( needsFeature || needsMoreRequests ) { body = ( void; onApplyLogo: ( mediaId: number ) => void; + onReload: () => void; context: string; placement: string; } diff --git a/projects/plugins/jetpack/extensions/extended-blocks/core-site-logo/index.tsx b/projects/plugins/jetpack/extensions/extended-blocks/core-site-logo/index.tsx index 0fe0f9091e78d..f4465c2e0f4ca 100644 --- a/projects/plugins/jetpack/extensions/extended-blocks/core-site-logo/index.tsx +++ b/projects/plugins/jetpack/extensions/extended-blocks/core-site-logo/index.tsx @@ -99,6 +99,11 @@ const siteLogoEditWithAiComponents = createHigherOrderComponent( BlockEdit => { setIsLogoGeneratorModalVisible( false ); }, [] ); + const reloadModal = useCallback( () => { + closeModal(); + showModal(); + }, [ closeModal, showModal ] ); + const applyLogoHandler = useCallback( ( mediaId: number ) => { if ( mediaId ) { @@ -127,6 +132,7 @@ const siteLogoEditWithAiComponents = createHigherOrderComponent( BlockEdit => { isOpen={ isLogoGeneratorModalVisible } onClose={ closeModal } onApplyLogo={ applyLogoHandler } + onReload={ reloadModal } context={ PLACEMENT_CONTEXT } placement={ TOOL_PLACEMENT } siteDetails={ siteDetails } From 5ec33a49030927b60538e24ed33e6b098b3ebc8b Mon Sep 17 00:00:00 2001 From: Luiz Kowalski Date: Tue, 30 Jul 2024 15:48:28 -0300 Subject: [PATCH 3/4] changelog --- ...e-ai-logo-generator-prevent-multiple-feature-data-requests | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 projects/js-packages/ai-client/changelog/update-ai-logo-generator-prevent-multiple-feature-data-requests diff --git a/projects/js-packages/ai-client/changelog/update-ai-logo-generator-prevent-multiple-feature-data-requests b/projects/js-packages/ai-client/changelog/update-ai-logo-generator-prevent-multiple-feature-data-requests new file mode 100644 index 0000000000000..77d668a7d3337 --- /dev/null +++ b/projects/js-packages/ai-client/changelog/update-ai-logo-generator-prevent-multiple-feature-data-requests @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +AI Logo Generator: fix multiple feature requests error + retry handling. From 96910d856572bdec29183f26316429add96010b4 Mon Sep 17 00:00:00 2001 From: Luiz Kowalski Date: Tue, 30 Jul 2024 15:48:39 -0300 Subject: [PATCH 4/4] Changelog --- ...e-ai-logo-generator-prevent-multiple-feature-data-requests | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 projects/plugins/jetpack/changelog/update-ai-logo-generator-prevent-multiple-feature-data-requests diff --git a/projects/plugins/jetpack/changelog/update-ai-logo-generator-prevent-multiple-feature-data-requests b/projects/plugins/jetpack/changelog/update-ai-logo-generator-prevent-multiple-feature-data-requests new file mode 100644 index 0000000000000..d2c90abf1f8d1 --- /dev/null +++ b/projects/plugins/jetpack/changelog/update-ai-logo-generator-prevent-multiple-feature-data-requests @@ -0,0 +1,4 @@ +Significance: patch +Type: other + +AI Logo Generator: Fix the retry button when a feature request fails.