/** * The tab of the Predictive AI in the form editor (Funnelforms Pro), between Settings and Embed, in * three steps: describe the visitors (Audience), watch the virtual visitors go through the form as * it is in the editor (Simulation), and read the funnel score with the suggestions (Result). * It shares the prediction with the canvas mode (usePredictionState), so a new analysis shows on * the canvas at once and the canvas mode reads the same last prediction. The page Funnelforms → * Predictive AI stays the overview across all forms. */ import React, { useCallback, useEffect, useMemo, useState } from 'react'; import type { FormDocument } from '@funnelforms/form-schema'; import type { FormsApi, PredictionQuota } from '../api'; import { DEFAULT_VISITORS, funnelScore, INDUSTRIES, worthApplying, type PredictionParameters } from '../predict'; import { useBuilder } from './context'; import { __, sprintf } from './i18n'; import { defaultParameters, industryNames, NotReady, number, predictionPrivacyText, type PredictionMode } from './Prediction'; import { EarlierList, EarlierView, useEarlier, type CurrentEntry } from './PredictionHistory'; import { PredictionResult } from './PredictionResult'; import { Check, countChips, PredictionStage, stepKind, stepName, stepOrder } from './PredictionStage'; import { Notice, Spinner } from './ui'; type Phase = 'setup' | 'sim' | 'result'; /** Where the setup starts the clock of the stage; the visitors only, to watch them again. */ const VISITORS_PHASE = 5.4; export function PredictionWorkspace( { api, formId, mode, locale, onCanvas, onPublish }: { api: FormsApi; formId: number; mode: PredictionMode; locale?: string; onCanvas: ( node?: string ) => void; onPublish?: ( note: string ) => void } ) { const doc = useBuilder( ( s ) => s.doc ); const [ parameters, setParameters ] = useState( () => mode.stored?.parameters ?? defaultParameters() ); // undefined while it loads; null when the server could not be reached. const [ quota, setQuota ] = useState( undefined ); // Null: the result once there is one, the setup before. const [ phase, setPhase ] = useState( null ); const [ sim, setSim ] = useState<{ from: number; waiting: boolean; key: number }>( { from: 0, waiting: false, key: 0 } ); const [ progress, setProgress ] = useState( 0 ); const loadQuota = useCallback( () => { if ( ! api.predictionQuota ) { setQuota( null ); return; } api.predictionQuota() .then( setQuota ) .catch( () => setQuota( null ) ); }, [ api ] ); useEffect( loadQuota, [ loadQuota ] ); // The description of the visitors of the last prediction, once it is loaded. const last = mode.stored; useEffect( () => { if ( last ) { setParameters( last.parameters ); } }, [ last ] ); const steps = doc ? doc.nodes.filter( ( n ) => 'end' !== n.type ) : []; const empty = quota?.included === true && quota.remaining <= 0; const canRun = mode.ready && !! doc && steps.length > 0 && ! mode.busy && ! empty; // The result of the newest prediction only; during a new analysis the old one is not shown on the stage. const fresh = mode.result && last && mode.resultOf === last ? mode.result : null; const shown: Phase = phase ?? ( fresh ? 'result' : 'setup' ); // Earlier predictions with the real figures; one of them opened instead of the current one. const history = useEarlier( api, formId, last?.created ?? null ); const [ earlier, setEarlier ] = useState( null ); useEffect( () => setEarlier( null ), [ last ] ); const opened = null === earlier ? null : history?.history[ earlier ] ?? null; const current: CurrentEntry | null = fresh && last ? { created: last.created, score: funnelScore( fresh ), conversion: fresh.conversion, open: worthApplying( fresh.suggestions ).length } : null; const list = ( { setEarlier( i ); if ( null === i && fresh ) { setPhase( 'result' ); } } } /> ); const hasEarlier = !! history?.history.length; const run = async (): Promise => { setSim( ( s ) => ( { from: 0, waiting: true, key: s.key + 1 } ) ); setProgress( 0 ); setPhase( 'sim' ); const ok = await mode.run( parameters, DEFAULT_VISITORS ); loadQuota(); if ( ok ) { setSim( ( s ) => ( { ...s, waiting: false } ) ); } else { setPhase( 'setup' ); } }; const replay = (): void => { setSim( ( s ) => ( { from: VISITORS_PHASE, waiting: false, key: s.key + 1 } ) ); setPhase( 'sim' ); }; if ( ! doc ) { return null; } return (
{ mode.ready ? null : } { mode.error ? { mode.error } : null } { mode.loading ? : null } { ! steps.length ? { __( 'This form has no steps yet. Add steps in the form builder first.', 'funnelforms-pro' ) } : null } { empty ? { __( 'Your license has no analyses left this month.', 'funnelforms-pro' ) } : null } { opened && 'sim' !== shown ? setEarlier( null ) } /> : null } { 'setup' === shown && ! opened ? void run() } quota={ quota } history={ hasEarlier || current ? list : null } /> : null } { 'sim' === shown ? ( setPhase( 'result' ) } /> ) : null } { 'result' === shown && fresh && last && ! opened ? setPhase( 'setup' ) } onRun={ () => void run() } apply={ mode } onPublish={ onPublish } /> : null } { 'result' === shown && ! fresh && ! mode.loading ? : null } { ( 'result' === shown || opened ) && hasEarlier ? (

{ __( 'History', 'funnelforms-pro' ) }

{ __( 'Earlier predictions with the real figures after them', 'funnelforms-pro' ) }
{ list }
) : null }
); } /** The three steps of the tab and the analyses left, under the top bar. */ function Stepper( { phase, progress, hasResult, running, onPhase, quota }: { phase: Phase; progress: number; hasResult: boolean; running: boolean; onPhase: ( p: Phase ) => void; quota: PredictionQuota | null | undefined } ) { const at = 'setup' === phase ? 0 : 'sim' === phase ? 1 : 2; const items: Array<[ Phase, string, string ]> = [ [ 'setup', __( 'Audience', 'funnelforms-pro' ), __( 'Who comes to your form?', 'funnelforms-pro' ) ], [ 'sim', __( 'Simulation', 'funnelforms-pro' ), sprintf( __( '%s virtual visitors', 'funnelforms-pro' ), number( DEFAULT_VISITORS ) ) ], [ 'result', __( 'Result', 'funnelforms-pro' ), __( 'Score and suggestions', 'funnelforms-pro' ) ], ]; return (
    { items.map( ( [ id, label, sub ], i ) => { const done = i < at; const active = i === at; // The simulation is no place to go to; the result only with one. const link = ! active && ! running && 'sim' !== id && ( 'result' !== id || hasResult ); const fill = 0 === i ? ( at > 0 ? 100 : 0 ) : 'sim' === phase ? Math.round( progress * 100 ) : at > 1 ? 100 : 0; return (
  1. { i < 2 ? (
  2. ); } ) }
{ quota && quota.included ? (
) : null }
); } function Setup( { doc, parameters: p, onChange, canRun, busy, onRun, quota, history }: { doc: FormDocument; parameters: PredictionParameters; onChange: ( p: PredictionParameters ) => void; canRun: boolean; busy: boolean; onRun: () => void; quota: PredictionQuota | null | undefined; history: React.ReactNode } ) { const [ explain, setExplain ] = useState( false ); const [ more, setMore ] = useState( false ); const set = ( key: K, value: PredictionParameters[ K ] ): void => onChange( { ...p, [ key ]: value } ); const names = industryNames(); const order = useMemo( () => stepOrder( doc ), [ doc ] ); const chips = useMemo( () => countChips( doc ), [ doc ] ); const intents: Array<[ PredictionParameters[ 'intent' ], string, string ]> = [ [ 'research', __( 'Just looking around', 'funnelforms-pro' ), __( 'Reads first, decides later', 'funnelforms-pro' ) ], [ 'compare', __( 'Comparing offers', 'funnelforms-pro' ), __( 'Interested, checks several providers', 'funnelforms-pro' ) ], [ 'ready', __( 'Ready to buy', 'funnelforms-pro' ), __( 'Looking for a provider now', 'funnelforms-pro' ) ], ]; const sources: Array<[ PredictionParameters[ 'traffic' ], string ]> = [ [ 'search_ads', __( 'Search ads', 'funnelforms-pro' ) ], [ 'social_ads', __( 'Social media ads', 'funnelforms-pro' ) ], [ 'organic', __( 'Search results and own website', 'funnelforms-pro' ) ], [ 'email', __( 'Newsletter and e-mail', 'funnelforms-pro' ) ], [ 'direct', __( 'Direct, recommendations', 'funnelforms-pro' ) ], [ 'mixed', __( 'Mixed', 'funnelforms-pro' ) ], ]; const audiences: Array<[ PredictionParameters[ 'audience' ], string ]> = [ [ 'b2c', __( 'Private customers', 'funnelforms-pro' ) ], [ 'b2b', __( 'Business customers', 'funnelforms-pro' ) ], [ 'mixed', __( 'Both', 'funnelforms-pro' ) ], ]; const mobile = p.mobile_share; const shownMobile = mobile ?? 60; const line = [ audiences.find( ( a ) => a[ 0 ] === p.audience )?.[ 1 ], intents.find( ( a ) => a[ 0 ] === p.intent )?.[ 1 ], sources.find( ( a ) => a[ 0 ] === p.traffic )?.[ 1 ], null === mobile ? __( 'usual share on phones', 'funnelforms-pro' ) : sprintf( __( '%s on phones', 'funnelforms-pro' ), `${ mobile }\u00a0%` ), ] .filter( Boolean ) .join( ' · ' ); const how: Array<[ string, string ]> = [ [ __( 'Read the form', 'funnelforms-pro' ), __( 'Steps, fields, required fields, logic and calculations, exactly as they are in the editor.', 'funnelforms-pro' ) ], [ __( 'Understand the visitors', 'funnelforms-pro' ), __( 'From your description the AI builds kinds of visitors with their own patience, intent and device.', 'funnelforms-pro' ) ], [ sprintf( __( 'Send %s visitors', 'funnelforms-pro' ), number( DEFAULT_VISITORS ) ), __( 'Every visitor goes through your real logic and decides at every field by benchmarks.', 'funnelforms-pro' ) ], [ __( 'Work out the suggestions', 'funnelforms-pro' ), __( 'Every improvement is simulated again with the same visitors. That is where the points on each suggestion come from.', 'funnelforms-pro' ) ], ]; const faq: Array<[ string, string ]> = [ [ __( 'What is the funnel score?', 'funnelforms-pro' ), __( 'How much of what is possible with your form you already get today. 100 means: the AI finds nothing that brings noticeably more leads.', 'funnelforms-pro' ) ], [ __( 'How exact is it?', 'funnelforms-pro' ), __( 'An estimate with a range, not a promise. Once real visitors come, the statistics put the real figures beside it.', 'funnelforms-pro' ) ], [ __( 'What is sent to the AI?', 'funnelforms-pro' ), __( 'Only the structure of your form and what you enter on this page. No entries, no visitor data.', 'funnelforms-pro' ) ], ]; return (
{ __( 'Predictive AI', 'funnelforms-pro' ) }

{ sprintf( __( 'Test your form with %s virtual visitors before real ones come.', 'funnelforms-pro' ), number( DEFAULT_VISITORS ) ) }

{ __( 'The AI sends your audience through your real steps and logic and shows where leads get lost and what would help.', 'funnelforms-pro' ) }

{ explain ? (

{ __( 'How the Predictive AI works it out', 'funnelforms-pro' ) }

{ __( 'This can take up to a minute.', 'funnelforms-pro' ) }
    { how.map( ( [ title, body ], i ) => (
  1. { i + 1 } { title } { body }
  2. ) ) }
{ faq.map( ( [ q, a ] ) => (
{ q } { a }
) ) }
) : null }

{ __( 'Who comes to your form?', 'funnelforms-pro' ) }

{ 'other' === p.industry ? ( ) : null }
{ __( 'How ready to buy are they?', 'funnelforms-pro' ) }
{ intents.map( ( [ id, label, sub ] ) => ( ) ) }
{ __( 'Where do they come from?', 'funnelforms-pro' ) }
{ sources.map( ( [ id, label ] ) => ( ) ) }
{ __( 'Phone or desktop?', 'funnelforms-pro' ) } set( 'mobile_share', Number( e.target.value ) ) } /> { null === mobile ? ( __( 'Not set: the AI takes the usual share of the source.', 'funnelforms-pro' ) ) : ( ) }