/** * Get code: every way to use a form. The shortcode, the block, the link * that opens a pop-up, slide-in or bar, and the tag for another website * with the sites allowed to show it (a setting of the draft, saved with * the rest). */ import { useEffect, useId, useState } from '@wordpress/element'; import { __, sprintf } from '@wordpress/i18n'; import { useUi } from '../../runtime'; import { CodeValue, CopyButton } from '../settings/ApiKeyBlock'; import { Fact } from './controls'; import { OVERLAYS } from './placement'; const parseHosts = ( text ) => String( text ) .split( /[\s,]+/ ) .map( ( v ) => v.trim() ) .filter( Boolean ); /** * A titled part of the dialog. * * @param {Object} props * @param {*} props.title Title. * @param {*} props.tip Help for a `?`. * @param {*} props.children Body. * @return {Element} The part. */ function Part( { title, tip, children } ) { const { ui } = useUi(); return (

{ title } { tip && { tip } }

{ children }
); } /** * @param {Object} props * @param {boolean} props.open Shown. * @param {Function} props.onClose Called to close it. * @param {Object} props.form The saved form (`id`, `shortcode`, `embed`, `active`). * @param {string} props.name The draft's name. * @param {string} props.type The draft's type. * @param {Array} props.domains The sites allowed to show it. * @param {Function} props.onDomains Called with the sites. * @return {Element} The dialog. */ export default function GetCodeDialog( { open, onClose, form, name, type, domains = [], onDomains, } ) { const { ui } = useUi(); const hostsId = useId(); const joined = domains.join( ', ' ); const [ text, setText ] = useState( joined ); useEffect( () => { setText( ( now ) => parseHosts( now ).join( ', ' ) === joined ? now : joined ); }, [ joined ] ); const overlay = OVERLAYS.includes( type ); const link = `#mailpuff-${ form.id }`; let description = __( 'Add the Newsletter signup block, paste the shortcode, or use it on another website.', 'mailpuff' ); if ( overlay ) { description = __( 'It shows by itself while it is on. You can also place it in a page.', 'mailpuff' ); } else if ( 'below_post' === type ) { description = __( 'It shows by itself while it is on.', 'mailpuff' ); } return ( ! next && onClose() } title={ __( 'Use this form', 'mailpuff' ) } description={ description } className="grid-cols-[minmax(0,1fr)]" footer={ { __( 'Done', 'mailpuff' ) } } >
{ 'below_post' !== type && ( <>
{ form.shortcode }

{ sprintf( /* translators: %s: a form's name. */ __( 'In the editor, add Newsletter signup and pick ā€œ%sā€.', 'mailpuff' ), name ) }

) } { overlay && (
{ link }
) } { 'below_post' !== type && form.embed && (
{ form.embed }
{ setText( e.target.value ); onDomains( parseHosts( e.target.value ) ); } } className="h-8 text-[13px]" />
{ ! form.active && ( { __( 'Other sites show it once it is on.', 'mailpuff' ) } ) }
) }
); }