2022-09-15 03:58:47 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useState, useEffect, useCallback } from '@wordpress/element';
|
|
|
|
import { Guide } from '@wordpress/components';
|
|
|
|
import { useSearchParams } from 'react-router-dom';
|
|
|
|
import { updateQueryString } from '@woocommerce/navigation';
|
2022-09-15 04:10:05 +00:00
|
|
|
import { registerPlugin } from '@wordpress/plugins';
|
|
|
|
import { addFilter, removeFilter } from '@wordpress/hooks';
|
|
|
|
import { getAdminLink } from '@woocommerce/settings';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2022-09-15 04:06:03 +00:00
|
|
|
import { useDispatch } from '@wordpress/data';
|
|
|
|
import { OPTIONS_STORE_NAME } from '@woocommerce/data';
|
2022-09-15 08:26:43 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2022-09-15 03:58:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { ModalIllustrationLayout } from './layouts/ModalIllustrationLayout';
|
|
|
|
import {
|
|
|
|
useJetpackPluginState,
|
|
|
|
JetpackPluginStates,
|
|
|
|
useSendMagicLink,
|
2022-10-13 08:12:40 +00:00
|
|
|
SendMagicLinkStates,
|
2022-09-15 03:58:47 +00:00
|
|
|
} from './components';
|
|
|
|
import {
|
|
|
|
EmailSentPage,
|
2023-10-03 08:19:15 +00:00
|
|
|
MobileAppInstallPage,
|
2022-09-15 03:58:47 +00:00
|
|
|
JetpackAlreadyInstalledPage,
|
|
|
|
} from './pages';
|
|
|
|
import './style.scss';
|
2022-09-15 04:10:05 +00:00
|
|
|
import { SETUP_TASK_HELP_ITEMS_FILTER } from '../../activity-panel/panels/help';
|
2022-09-15 03:58:47 +00:00
|
|
|
|
|
|
|
export const MobileAppModal = () => {
|
2022-09-15 08:26:43 +00:00
|
|
|
const [ guideIsOpen, setGuideIsOpen ] = useState( false );
|
|
|
|
const [ isReturningFromWordpressConnection, setIsReturning ] =
|
|
|
|
useState( false );
|
2022-09-15 03:58:47 +00:00
|
|
|
|
|
|
|
const { state, jetpackConnectionData } = useJetpackPluginState();
|
2022-09-15 04:06:03 +00:00
|
|
|
const { updateOptions } = useDispatch( OPTIONS_STORE_NAME );
|
2022-09-15 03:58:47 +00:00
|
|
|
|
|
|
|
const [ pageContent, setPageContent ] = useState< React.ReactNode >();
|
|
|
|
const [ searchParams ] = useSearchParams();
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if ( searchParams.get( 'mobileAppModal' ) ) {
|
|
|
|
setGuideIsOpen( true );
|
|
|
|
} else {
|
|
|
|
setGuideIsOpen( false );
|
|
|
|
}
|
|
|
|
|
2022-09-15 08:26:43 +00:00
|
|
|
if ( searchParams.get( 'jetpackState' ) === 'returning' ) {
|
|
|
|
setIsReturning( true );
|
|
|
|
}
|
|
|
|
}, [ searchParams ] );
|
2022-09-15 03:58:47 +00:00
|
|
|
|
|
|
|
const [ hasSentEmail, setHasSentEmail ] = useState( false );
|
2022-09-15 08:26:43 +00:00
|
|
|
const [ isRetryingMagicLinkSend, setIsRetryingMagicLinkSend ] =
|
|
|
|
useState( false );
|
2022-09-15 03:58:47 +00:00
|
|
|
|
2022-10-13 08:12:40 +00:00
|
|
|
const { requestState: magicLinkRequestStatus, fetchMagicLinkApiCall } =
|
|
|
|
useSendMagicLink();
|
2022-09-15 03:58:47 +00:00
|
|
|
|
|
|
|
const sendMagicLink = useCallback( () => {
|
|
|
|
fetchMagicLinkApiCall();
|
2022-09-15 08:26:43 +00:00
|
|
|
recordEvent( 'magic_prompt_send_signin_link_click' );
|
2022-09-15 03:58:47 +00:00
|
|
|
}, [ fetchMagicLinkApiCall ] );
|
|
|
|
|
2022-10-13 08:12:40 +00:00
|
|
|
useEffect( () => {
|
|
|
|
if ( magicLinkRequestStatus === SendMagicLinkStates.SUCCESS ) {
|
|
|
|
setHasSentEmail( true );
|
|
|
|
}
|
|
|
|
}, [ magicLinkRequestStatus ] );
|
|
|
|
|
2022-09-15 03:58:47 +00:00
|
|
|
useEffect( () => {
|
|
|
|
if ( hasSentEmail ) {
|
|
|
|
setPageContent(
|
|
|
|
<EmailSentPage
|
2022-09-15 08:26:43 +00:00
|
|
|
returnToSendLinkPage={ () => {
|
|
|
|
setHasSentEmail( false );
|
|
|
|
setIsRetryingMagicLinkSend( true );
|
|
|
|
recordEvent( 'magic_prompt_retry_send_signin_link' );
|
|
|
|
} }
|
2022-09-15 03:58:47 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else if (
|
|
|
|
state === JetpackPluginStates.FULL_CONNECTION &&
|
2022-09-15 04:10:05 +00:00
|
|
|
jetpackConnectionData?.currentUser?.wpcomUser?.email &&
|
2022-09-15 03:58:47 +00:00
|
|
|
! hasSentEmail
|
|
|
|
) {
|
|
|
|
const wordpressAccountEmailAddress =
|
|
|
|
jetpackConnectionData?.currentUser.wpcomUser.email;
|
|
|
|
setPageContent(
|
|
|
|
<JetpackAlreadyInstalledPage
|
|
|
|
wordpressAccountEmailAddress={
|
|
|
|
wordpressAccountEmailAddress
|
|
|
|
}
|
2022-09-15 08:26:43 +00:00
|
|
|
isRetryingMagicLinkSend={ isRetryingMagicLinkSend }
|
2022-10-13 08:12:40 +00:00
|
|
|
sendMagicLinkStatus={ magicLinkRequestStatus }
|
2022-09-15 03:58:47 +00:00
|
|
|
sendMagicLinkHandler={ sendMagicLink }
|
|
|
|
/>
|
|
|
|
);
|
2023-10-03 08:06:36 +00:00
|
|
|
} else {
|
|
|
|
// Shows the installation page by default.
|
2023-10-03 08:19:15 +00:00
|
|
|
setPageContent( <MobileAppInstallPage /> );
|
2022-09-15 03:58:47 +00:00
|
|
|
}
|
|
|
|
}, [
|
|
|
|
sendMagicLink,
|
|
|
|
hasSentEmail,
|
|
|
|
isReturningFromWordpressConnection,
|
2022-09-15 04:10:05 +00:00
|
|
|
jetpackConnectionData?.currentUser?.wpcomUser?.email,
|
2022-09-15 03:58:47 +00:00
|
|
|
state,
|
2022-09-15 08:26:43 +00:00
|
|
|
isRetryingMagicLinkSend,
|
2022-10-13 08:12:40 +00:00
|
|
|
magicLinkRequestStatus,
|
2022-09-15 03:58:47 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{ guideIsOpen && (
|
|
|
|
<Guide
|
|
|
|
onFinish={ () => {
|
2022-09-15 04:06:03 +00:00
|
|
|
updateOptions( {
|
|
|
|
woocommerce_admin_dismissed_mobile_app_modal: 'yes',
|
|
|
|
} );
|
2022-09-15 03:58:47 +00:00
|
|
|
// clear the search params that we use so that the URL is clean
|
|
|
|
updateQueryString(
|
|
|
|
{
|
|
|
|
jetpackState: undefined,
|
|
|
|
mobileAppModal: undefined,
|
|
|
|
},
|
|
|
|
undefined,
|
|
|
|
Object.fromEntries( searchParams.entries() )
|
|
|
|
);
|
|
|
|
} }
|
|
|
|
className={ 'woocommerce__mobile-app-welcome-modal' }
|
|
|
|
pages={ [
|
|
|
|
{
|
|
|
|
content: (
|
|
|
|
<ModalIllustrationLayout body={ pageContent } />
|
|
|
|
),
|
|
|
|
},
|
|
|
|
] }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2022-09-15 04:10:05 +00:00
|
|
|
|
|
|
|
export const MOBILE_APP_MODAL_HELP_ENTRY_FILTER_CALLBACK =
|
|
|
|
'wc/admin/mobile-app-help-entry-callback';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This component exists to add the mobile app entry to the help panel.
|
|
|
|
* If the user has no pathway to achieve the required Jetpack connection,
|
|
|
|
* then we don't want to show the help panel entry.
|
|
|
|
*/
|
|
|
|
export const MobileAppHelpMenuEntryLoader = () => {
|
|
|
|
const { state } = useJetpackPluginState();
|
|
|
|
|
|
|
|
const filterHelpMenuEntries = useCallback(
|
|
|
|
( helpMenuEntries ) => {
|
|
|
|
if (
|
|
|
|
state === JetpackPluginStates.INITIALIZING ||
|
|
|
|
state === JetpackPluginStates.USER_CANNOT_INSTALL ||
|
|
|
|
state === JetpackPluginStates.NOT_OWNER_OF_CONNECTION
|
|
|
|
) {
|
|
|
|
return helpMenuEntries;
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
...helpMenuEntries,
|
|
|
|
{
|
|
|
|
title: __( 'Get the WooCommerce app', 'woocommerce' ),
|
|
|
|
link: getAdminLink(
|
|
|
|
'./admin.php?page=wc-admin&mobileAppModal=true'
|
|
|
|
),
|
|
|
|
linkType: 'wc-admin',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
},
|
|
|
|
[ state ]
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
removeFilter(
|
|
|
|
SETUP_TASK_HELP_ITEMS_FILTER,
|
|
|
|
MOBILE_APP_MODAL_HELP_ENTRY_FILTER_CALLBACK
|
|
|
|
);
|
|
|
|
addFilter(
|
|
|
|
SETUP_TASK_HELP_ITEMS_FILTER,
|
|
|
|
MOBILE_APP_MODAL_HELP_ENTRY_FILTER_CALLBACK,
|
|
|
|
filterHelpMenuEntries,
|
|
|
|
10
|
|
|
|
);
|
|
|
|
}, [ filterHelpMenuEntries ] );
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
registerPlugin( 'woocommerce-mobile-app-modal', {
|
|
|
|
render: MobileAppHelpMenuEntryLoader,
|
|
|
|
// @ts-expect-error 'scope' does exist. @types/wordpress__plugins is outdated.
|
|
|
|
scope: 'woocommerce-admin',
|
|
|
|
} );
|