|
| 1 | +import { Modal, useToggleState } from "@yoast/ui-library"; |
| 2 | +import { useState, useEffect, useCallback, useRef } from "@wordpress/element"; |
| 3 | +import { __ } from "@wordpress/i18n"; |
| 4 | +import ApproveModal from "../containers/approve-modal"; |
| 5 | +import { AiGrantConsent } from "../../shared-admin/components"; |
| 6 | +import { ReplaceContentModal } from "./replace-content-modal"; |
| 7 | +import { FEATURE_MODAL_STATUS, CONTENT_PLANNER_STORE } from "../constants"; |
| 8 | +import { STORE_NAME_EDITOR, STORE_NAME_AI } from "../../ai-generator/constants"; |
| 9 | +import { useFetchContentSuggestions, useApplyOutline } from "../hooks"; |
| 10 | +import FeatureModal from "../containers/feature-modal"; |
| 11 | +import { useSelect, useDispatch } from "@wordpress/data"; |
| 12 | + |
| 13 | + |
| 14 | +/** |
| 15 | + * The modal that orchestrates the flow between the approve, content suggestions, |
| 16 | + * content outline, and replace content confirmation views. |
| 17 | + * |
| 18 | + * @returns {JSX.Element} The Content Planner Feature Modal. |
| 19 | + */ |
| 20 | +export const App = () => { |
| 21 | + const { status, hasConsent } = useSelect( select => { |
| 22 | + const contentPlannerSelectors = select( CONTENT_PLANNER_STORE ); |
| 23 | + return { |
| 24 | + status: contentPlannerSelectors.selectFeatureModalStatus(), |
| 25 | + hasConsent: select( STORE_NAME_AI ).selectHasAiGeneratorConsent(), |
| 26 | + }; |
| 27 | + }, [] ); |
| 28 | + const { setFeatureModalStatus, closeModal } = useDispatch( CONTENT_PLANNER_STORE ); |
| 29 | + const [ hasVisitedReplace, setHasVisitedReplace ] = useState( false ); |
| 30 | + const [ replaceContentModalIsOpen, toggleReplaceContentModal, , openReplaceContentModal ] = useToggleState( false ); |
| 31 | + const editedOutlineRef = useRef( null ); |
| 32 | + const handleApplyOutline = useApplyOutline( { editedOutlineRef } ); |
| 33 | + const fetchContentSuggestions = useFetchContentSuggestions(); |
| 34 | + const isConsentModalOpen = status === FEATURE_MODAL_STATUS.consent; |
| 35 | + |
| 36 | + /** |
| 37 | + * Handles the click on the "Get content suggestions" button in the ApproveModal. |
| 38 | + * Sets the flag to indicate the transition is coming from the ApproveModal, then fetches content suggestions. |
| 39 | + * The flag is used to determine whether to apply a cross-fade transition when showing the SuggestionsPanel. |
| 40 | + * Updates the modal status to "content-suggestions" once suggestions are requested. |
| 41 | + * |
| 42 | + * @returns {void} |
| 43 | + */ |
| 44 | + const handleGetSuggestionsClick = useCallback( () => { |
| 45 | + if ( ! hasConsent ) { |
| 46 | + setFeatureModalStatus( FEATURE_MODAL_STATUS.consent ); |
| 47 | + return; |
| 48 | + } |
| 49 | + fetchContentSuggestions(); |
| 50 | + }, [ hasConsent, setFeatureModalStatus, fetchContentSuggestions ] ); |
| 51 | + |
| 52 | + const handleConfirmReplace = useCallback( () => { |
| 53 | + handleApplyOutline(); |
| 54 | + }, [ handleApplyOutline ] ); |
| 55 | + |
| 56 | + useEffect( () => { |
| 57 | + if ( ! status ) { |
| 58 | + setHasVisitedReplace( false ); |
| 59 | + } |
| 60 | + }, [ status ] ); |
| 61 | + |
| 62 | + return ( |
| 63 | + <> |
| 64 | + <ApproveModal |
| 65 | + onClick={ handleGetSuggestionsClick } |
| 66 | + /> |
| 67 | + <FeatureModal |
| 68 | + editedOutlineRef={ editedOutlineRef } |
| 69 | + handleApplyOutline={ handleApplyOutline } |
| 70 | + openReplaceContentModal={ openReplaceContentModal } |
| 71 | + setHasVisitedReplace={ setHasVisitedReplace } |
| 72 | + /> |
| 73 | + <ReplaceContentModal |
| 74 | + onConfirm={ handleConfirmReplace } |
| 75 | + isOpen={ replaceContentModalIsOpen && hasVisitedReplace } |
| 76 | + onClose={ toggleReplaceContentModal } |
| 77 | + /> |
| 78 | + |
| 79 | + <Modal isOpen={ isConsentModalOpen } onClose={ closeModal } className="yst-introduction-modal"> |
| 80 | + <Modal.Panel |
| 81 | + className="yst-max-w-lg yst-p-0 yst-rounded-3xl" |
| 82 | + closeButtonScreenReaderText={ __( "Close modal", "wordpress-seo" ) } |
| 83 | + > |
| 84 | + <AiGrantConsent |
| 85 | + storeName={ STORE_NAME_AI } |
| 86 | + linkStoreName={ STORE_NAME_EDITOR } |
| 87 | + onConsentGranted={ fetchContentSuggestions } |
| 88 | + /> |
| 89 | + </Modal.Panel> |
| 90 | + </Modal> |
| 91 | + </> |
| 92 | + ); |
| 93 | +}; |
0 commit comments