/** * External dependencies */ import colorStudio from '@automattic/color-studio'; import jetpackAnalytics from '@automattic/jetpack-analytics'; import JetpackLogo from '@automattic/jetpack-components/jetpack-logo'; import getRedirectUrl from '@automattic/jetpack-components/tools/jp-redirect'; import { Modal, Card, Button, ExternalLink, Spinner, // eslint-disable-next-line @wordpress/no-unsafe-wp-apis __experimentalVStack as VStack, // eslint-disable-next-line @wordpress/no-unsafe-wp-apis __experimentalHStack as HStack, } from '@wordpress/components'; import { useState, useCallback, useMemo, createInterpolateElement } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { chevronUp, chevronDown } from '@wordpress/icons'; import * as React from 'react'; /** * Internal dependencies */ import AkismetIcon from '../../src/icons/akismet.tsx'; import MailPoetIcon from '../../src/icons/mailpoet.tsx'; import type { Integration } from '../../src/types/index.ts'; const COLOR_JETPACK = colorStudio.colors[ 'Jetpack Green 40' ]; type IntegrationsModalProps = { isOpen: boolean; onClose: () => void; integrationsData: Integration[]; refreshIntegrations: () => Promise< void >; context?: 'dashboard'; attributes?: unknown; setAttributes?: unknown; }; type IntegrationCardProps = { integration: Integration; refreshIntegrations: () => Promise< void >; isExpanded: boolean; onToggle: () => void; }; const IntegrationCardComponent = ( { integration, refreshIntegrations, isExpanded, onToggle, }: IntegrationCardProps ) => { const { id, title, subtitle, isInstalled, isActive, isConnected, settingsUrl = '', marketingUrl = '', } = integration; const isLoading = typeof isInstalled === 'undefined'; const getIcon = () => { switch ( id ) { case 'akismet': return ; case 'zero-bs-crm': return ; case 'mailpoet': return ; default: return null; } }; const renderStatus = () => { if ( isLoading ) { return ; } if ( ! isInstalled ) { return ( { __( 'Not installed', 'jetpack-forms' ) } ); } if ( ! isActive ) { return ( { __( 'Not activated', 'jetpack-forms' ) } ); } if ( ! isConnected ) { return ( { __( 'Setup required', 'jetpack-forms' ) } ); } return ( { __( 'Connected', 'jetpack-forms' ) } ); }; const renderBody = () => { if ( isLoading ) { return null; } if ( ! isInstalled ) { let message: string; if ( id === 'akismet' ) { message = __( "Add one-click spam protection for your forms with Akismet. Simply install the plugin and you're set.", 'jetpack-forms' ); } else if ( id === 'zero-bs-crm' ) { message = __( 'You can save your form contacts in Jetpack CRM. To get started, please install the plugin.', 'jetpack-forms' ); } else { message = __( 'Add powerful email marketing to your forms with MailPoet. Simply install the plugin to start sending emails.', 'jetpack-forms' ); } return (

{ createInterpolateElement( message, { a: , } ) }

); } if ( ! isActive ) { let message: string; if ( id === 'akismet' ) { message = __( 'Akismet is installed. Just activate the plugin to start blocking spam.', 'jetpack-forms' ); } else if ( id === 'zero-bs-crm' ) { message = __( 'Jetpack CRM is installed. To start saving contacts, simply activate the plugin.', 'jetpack-forms' ); } else { message = __( 'MailPoet is installed. Just activate the plugin to start sending emails.', 'jetpack-forms' ); } return

{ message }

; } if ( ! isConnected ) { return (

{ id === 'akismet' ? createInterpolateElement( __( 'Akismet is active. There is one step left. Please add your Akismet key.', 'jetpack-forms' ), { a: } ) : __( 'Setup is required to complete the integration.', 'jetpack-forms' ) }

); } let message: string; if ( id === 'akismet' ) { message = __( 'Your forms are automatically protected with Akismet.', 'jetpack-forms' ); } else if ( id === 'zero-bs-crm' ) { message = __( 'Jetpack CRM is connected.', 'jetpack-forms' ); } else { message = __( 'MailPoet is connected.', 'jetpack-forms' ); } // Connected state return (

{ message }

{ id === 'akismet' && ( <> | { __( 'Learn about Akismet', 'jetpack-forms' ) } ) }
); }; return ( { getIcon() } { title } { subtitle } { renderStatus() } { isExpanded ? chevronUp : chevronDown } { isExpanded && (
{ renderBody() }
) }
); }; const IntegrationsModal = ( { isOpen, onClose, integrationsData, refreshIntegrations, }: IntegrationsModalProps ) => { const [ expandedCards, setExpandedCards ] = useState< Record< string, boolean > >( {} ); const toggleCard = useCallback( ( id: string ) => { setExpandedCards( prev => { const isExpanding = ! prev[ id ]; if ( isExpanding ) { jetpackAnalytics.tracks.recordEvent( 'jetpack_forms_integrations_card_expand', { card: id, origin: 'dashboard', } ); } return { ...prev, [ id ]: isExpanding }; } ); }, [] ); const filteredIntegrations = useMemo( () => integrationsData.filter( i => [ 'akismet', 'zero-bs-crm', 'mailpoet' ].includes( i.id ) ), [ integrationsData ] ); if ( ! isOpen ) { return null; } return ( { filteredIntegrations.map( integration => ( void } /> ) ) } ); }; export default IntegrationsModal;