/* eslint-disable @woocommerce/dependency-group */ /* eslint-disable @typescript-eslint/ban-ts-comment */ /** * External dependencies */ import { Button, CheckboxControl, // @ts-ignore No types for this exist yet. __experimentalItemGroup as ItemGroup, Modal, // @ts-ignore No types for this exist yet. __experimentalNavigatorButton as NavigatorButton, // @ts-ignore No types for this exist yet. } from '@wordpress/components'; import { createInterpolateElement, useContext, useState, } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import interpolateComponents from '@automattic/interpolate-components'; // @ts-expect-error Missing type. import SidebarNavigationItem from '@wordpress/edit-site/build-module/components/sidebar-navigation-item'; /** * Internal dependencies */ import { ADMIN_URL } from '~/utils/admin-settings'; import { SidebarNavigationScreen } from './sidebar-navigation-screen'; import { trackEvent } from '~/customize-store/tracking'; import { FlowType } from '~/customize-store/types'; import { CustomizeStoreContext } from '..'; import { Link } from '@woocommerce/components'; import { PATTERN_CATEGORIES } from './pattern-screen/categories'; import { capitalize } from 'lodash'; import { getNewPath, navigateTo } from '@woocommerce/navigation'; import { useSelect } from '@wordpress/data'; import { OPTIONS_STORE_NAME } from '@woocommerce/data'; import { useNetworkStatus } from '~/utils/react-hooks/use-network-status'; import { isIframe, sendMessageToParent } from '~/customize-store/utils'; export const SidebarNavigationScreenHomepagePTK = ( { onNavigateBackClick, }: { onNavigateBackClick: () => void; } ) => { const { context, sendEvent } = useContext( CustomizeStoreContext ); const isNetworkOffline = useNetworkStatus(); const isPTKPatternsAPIAvailable = context.isPTKPatternsAPIAvailable; const trackingAllowed = useSelect( ( sel ) => sel( OPTIONS_STORE_NAME ).getOption( 'woocommerce_allow_tracking' ) ); const isTrackingDisallowed = trackingAllowed === 'no' || ! trackingAllowed; let notice; if ( isNetworkOffline ) { notice = __( "Looks like we can't detect your network. Please double-check your internet connection and refresh the page.", 'woocommerce' ); } else if ( ! isPTKPatternsAPIAvailable ) { notice = __( "Unfortunately, we're experiencing some technical issues — please come back later to access more patterns.", 'woocommerce' ); } else if ( isTrackingDisallowed ) { notice = __( 'Opt in to usage tracking to get access to more patterns.', 'woocommerce' ); } const [ isModalOpen, setIsModalOpen ] = useState( false ); const openModal = () => setIsModalOpen( true ); const closeModal = () => setIsModalOpen( false ); const [ optInDataSharing, setIsOptInDataSharing ] = useState< boolean >( true ); const optIn = () => { trackEvent( 'customize_your_store_assembler_hub_opt_in_usage_tracking' ); }; const skipOptIn = () => { trackEvent( 'customize_your_store_assembler_hub_skip_opt_in_usage_tracking' ); }; const aiOnline = context.flowType === FlowType.AIOnline; const title = aiOnline ? __( 'Change your homepage', 'woocommerce' ) : __( 'Choose your homepage', 'woocommerce' ); const sidebarMessage = aiOnline ? __( 'Based on the most successful stores in your industry and location, our AI tool has recommended this template for your business. Prefer a different layout? Choose from the templates below now, or later via the Editor.', 'woocommerce' ) : __( 'Create an engaging homepage by selecting one of our pre-designed layouts. You can continue customizing this page, including the content, later via the Editor.', 'woocommerce' ); return ( { trackEvent( 'customize_your_store_assembler_hub_editor_link_click', { source: 'homepage', } ); window.open( `${ ADMIN_URL }site-editor.php`, '_blank' ); return false; } } href="" /> ), } ) } content={ { Object.entries( PATTERN_CATEGORIES ).map( ( [ categoryKey, { label } ], index ) => ( { const categoryUrl = getNewPath( { customizing: true }, `/customize-store/assembler-hub/homepage/${ categoryKey }`, {} ); navigateTo( { url: categoryUrl } ); } } as={ SidebarNavigationItem } withChevron > { capitalize( label ) } ) ) } { notice && ( { __( 'Want more patterns?', 'woocommerce' ) } { createInterpolateElement( notice, { OptInModal: ( { openModal(); } } variant="link" /> ), } ) } { isModalOpen && ( ), }, } ) } checked={ optInDataSharing } onChange={ setIsOptInDataSharing } /> { skipOptIn(); closeModal(); } } variant="link" > { __( 'Cancel', 'woocommerce' ) } { optIn(); if ( isIframe( window ) ) { sendMessageToParent( { type: 'INSTALL_PATTERNS', } ); } else { sendEvent( 'INSTALL_PATTERNS' ); } } } variant="primary" disabled={ ! optInDataSharing } > { __( 'Opt in', 'woocommerce' ) } ) } ) } } /> ); };
{ createInterpolateElement( notice, { OptInModal: ( { openModal(); } } variant="link" /> ), } ) }