/** * Applying suggestions of the Predictive AI to the form with one click (tab of the Predictive AI): * the step as it is and as it will be (See the change), the bar to apply all open ones, the note * once all are applied, and taking one out again. The AI form builder makes the change with the suggestion's * instruction, like the AI bar; it lands in the draft and goes live with the next publishing. */ import React from 'react'; import type { FormDocument, FormNode } from '@funnelforms/form-schema'; import { text } from '../document'; import type { PredictionSuggestion } from '../predict'; import { contactFieldName, stepFieldName } from './catalog'; import { __, sprintf } from './i18n'; import { stepName } from './PredictionStage'; /** Fields shown in the small picture of a step at most. */ const PREVIEW_FIELDS = 5; /** Answers shown of a choice at most. */ const PREVIEW_TILES = 4; interface PreviewField { id: string; label: string; required: boolean; /** The answers of a choice; an input box without. */ tiles?: string[]; placeholder: string; tone?: 'bad' | 'good'; hint?: string; } interface PreviewSide { title: string; fields: PreviewField[]; /** The text of the send button of a contact step. */ button?: string; /** Fields not shown. */ more: number; } function plain( html: string ): string { return html .replace( /<[^>]*>/g, ' ' ) .replace( /\s+/g, ' ' ) .trim(); } /** The fields of a step as the small picture shows them: what a visitor fills in. */ function fieldsOf( node: FormNode, locale: string ): PreviewField[] { if ( 'end' === node.type ) { return []; } if ( 'contact' === node.type ) { return node.fields .filter( ( f ) => ! [ 'captcha', 'html', 'consent', 'phone_verification' ].includes( f.type ) ) .map( ( f ) => { const placeholder = ( f.props as { placeholder?: Record } ).placeholder; return { id: f.id, label: plain( text( f.label, locale ) ) || contactFieldName( f.type ), required: f.required, placeholder: placeholder ? text( placeholder, locale ) : '' }; } ); } return node.fields .filter( ( f ) => ! [ 'content', 'html', 'result', 'captcha' ].includes( f.type ) ) .map( ( f ) => { const props = f.props as { placeholder?: Record }; const options = 'choice' === f.type || 'dropdown' === f.type ? f.options.map( ( o ) => plain( text( o.label, locale ) ) || o.value ) : undefined; return { id: f.id, label: plain( text( f.label, locale ) ) || stepFieldName( f.type ), required: f.required, tiles: options ? [ ...options.slice( 0, PREVIEW_TILES ), ...( options.length > PREVIEW_TILES ? [ '…' ] : [] ) ] : undefined, placeholder: props.placeholder ? text( props.placeholder, locale ) : stepFieldName( f.type ), }; } ); } /** The picture of a step: the first fields, the field a suggestion is about among them. */ function sideOf( doc: FormDocument, node: FormNode, fields: PreviewField[], locale: string, first?: string ): PreviewSide { const hit = first ? fields.find( ( f ) => f.id === first ) : undefined; const shown = hit && fields.indexOf( hit ) >= PREVIEW_FIELDS ? [ hit, ...fields.filter( ( f ) => f !== hit ).slice( 0, PREVIEW_FIELDS - 1 ) ] : fields.slice( 0, PREVIEW_FIELDS ); return { title: stepName( doc, node ), fields: shown, button: 'contact' === node.type ? plain( text( node.submitLabel, locale ) ) || __( 'Send', 'funnelforms-pro' ) : undefined, more: Math.max( 0, fields.length - shown.length ), }; } /** * The step as it is and as it will be. Making a field optional and removing one are drawn as they * will be; for other changes the AI decides the details, so the instruction stands there instead. */ export function sidesOf( doc: FormDocument, suggestion: Pick, locale: string ): { before: PreviewSide; after: PreviewSide | null } | null { const node = doc.nodes.find( ( n ) => n.id === suggestion.node ); if ( ! node ) { return null; } const fields = fieldsOf( node, locale ); const field = suggestion.field ? fields.find( ( f ) => f.id === suggestion.field ) : undefined; const id = field?.id; const marked = fields.map( ( f ) => ( f.id === id ? { ...f, tone: 'bad' as const, hint: 'remove_field' === suggestion.kind ? __( 'Is removed', 'funnelforms-pro' ) : f.required ? __( 'Required', 'funnelforms-pro' ) : undefined } : f ) ); const before = sideOf( doc, node, marked, locale, id ); if ( id && 'optional_field' === suggestion.kind ) { return { before, after: sideOf( doc, node, fields.map( ( f ) => ( f.id === id ? { ...f, required: false, tone: 'good' as const, hint: __( 'Optional', 'funnelforms-pro' ) } : f ) ), locale, id ) }; } if ( id && 'remove_field' === suggestion.kind ) { return { before, after: sideOf( doc, node, fields.filter( ( f ) => f.id !== id ), locale ) }; } return { before, after: null }; } function Side( { label, side, after }: { label: string; side: PreviewSide; after?: boolean } ) { return (
{ label }
{ side.title ? { side.title } : null } { side.fields.map( ( f ) => (
{ f.label } { f.required ? * : null } { f.tiles ? ( { f.tiles.map( ( t, i ) => ( { t } ) ) } ) : ( { f.placeholder } ) } { f.hint ? { f.hint } : null }
) ) } { side.more ? { sprintf( __( '+%d more fields', 'funnelforms-pro' ), side.more ) } : null } { side.button ? { side.button } : null }
); } /** See the change: the step now and after applying, and what applying changes. */ export function ChangePeek( { doc, suggestion, locale, rate }: { doc: FormDocument; suggestion: PredictionSuggestion; locale: string; rate: string } ) { const sides = sidesOf( doc, suggestion, locale ); return (
{ sides ? (
{ sides.after ? ( ) : (
{ __( 'After applying', 'funnelforms-pro' ) }

{ suggestion.instruction }

{ __( 'The AI works out the details when it puts the change into your form.', 'funnelforms-pro' ) }
) }
) : null }
{ __( 'What "Apply change" does to your form', 'funnelforms-pro' ) } { suggestion.instruction } { rate } { __( 'Lands in the draft and can be undone at any time. Live only once you publish.', 'funnelforms-pro' ) }
); } /** The bar under the suggestions: all open ones into the form at once. */ export function ApplyBar( { count, points, busy, disabled, onApply }: { count: number; points: string; busy: boolean; disabled: boolean; onApply: () => void } ) { return (
{ busy ? sprintf( __( 'The AI applies %d suggestions to your form. This can take up to a minute.', 'funnelforms-pro' ), count ) : sprintf( __( '%1$d suggestions open · together about +%2$s points conversion rate', 'funnelforms-pro' ), count, points ) }
); } /** Once every suggestion is in the form: what it brings and how it goes live. */ export function AllDone( { more, count, onPublish, onRun, canRun }: { more: number; count: number; onPublish?: () => void; onRun: () => void; canRun: boolean } ) { return (
{ more > 0 ? sprintf( __( 'All suggestions applied: probably %s more leads.', 'funnelforms-pro' ), `${ more }\u00a0%` ) : __( 'All suggestions applied.', 'funnelforms-pro' ) } { sprintf( __( 'The %d changes are in the draft. Visitors see them once you publish.', 'funnelforms-pro' ), count ) }
{ onPublish ? ( ) : null }
); } /** * Undo when the change is not simply the last one: since then the form changed (the state * before it comes back, and with it what came after), or it was made on an earlier visit. */ export function RevertChoice( { how, onBefore, onOpen, onCancel }: { how: 'before' | null; onBefore: () => void; onOpen: () => void; onCancel: () => void } ) { return (

{ 'before' === how ? __( 'You changed the form since. Undo brings back the form as it was before this suggestion, without your changes after it (Ctrl+Z brings them back).', 'funnelforms-pro' ) : __( 'This change is already saved in the draft. Change the step back on the canvas, or only count the suggestion as open again.', 'funnelforms-pro' ) }

{ 'before' === how ? ( ) : null }
); } export function Tick( { size = 14 }: { size?: number } ) { return ( ); } function Arrow() { return ( ); } export function Sparkle() { return ( ); }