/** * The result of the Predictive AI in the tab of the editor: the funnel score with what the form * could reach, where visitors leave (with the reasons and the kinds of visitors per step) and the * suggestions by their effect, each applied to the form with one click (PredictionApply). Long * forms show the steps that cost leads first. */ import React, { useMemo, useState } from 'react'; import type { FormDocument } from '@funnelforms/form-schema'; import { localeTag } from '@funnelforms/form-core'; import { funnelScore, isBetter, worthApplying, type Prediction, type PredictionSuggestion, type StoredPrediction } from '../predict'; import { __, sprintf } from './i18n'; import { number, type PredictionMode } from './Prediction'; import { AllDone, ApplyBar, ChangePeek, RevertChoice, Tick } from './PredictionApply'; import { decimal, LONG_FORM, PERSONA_DARK, pointsOf, ScoreRing, stepKind, stepName, stepOrder } from './PredictionStage'; /** A step of a long form is listed at first from this share that leaves. */ const NOTABLE = 0.06; function money( value: number, currency: string ): string { try { return new Intl.NumberFormat( undefined, { style: 'currency', currency, maximumFractionDigits: 0 } ).format( value ); } catch { return `${ Math.round( value ) } ${ currency }`; } } export function when( iso: string, locale?: string ): string { const time = Date.parse( iso ); if ( Number.isNaN( time ) ) { return ''; } try { return new Intl.DateTimeFormat( localeTag( locale ), { dateStyle: 'medium', timeStyle: 'short' } ).format( new Date( time ) ); } catch { return iso.slice( 0, 16 ).replace( 'T', ' ' ); } } function band( drop: number ): 'high' | 'warn' | 'low' { return drop >= 0.3 ? 'high' : drop >= 0.1 ? 'warn' : 'low'; } export interface ResultProps { doc: FormDocument; result: Prediction; stored: StoredPrediction; locale?: string; busy: boolean; canRun: boolean; /** Plays the visitors again. */ onReplay: () => void; /** Shows the prediction on the canvas, with this step selected. */ onCanvas: ( node?: string ) => void; onSetup: () => void; onRun: () => void; /** Applying suggestions and taking them out (usePredictionState). */ apply: Pick; /** Opens publishing with this note. */ onPublish?: ( note: string ) => void; } export function PredictionResult( { doc, result, stored, locale, busy, canRun, onReplay, onCanvas, onSetup, onRun, apply, onPublish }: ResultProps ) { const { model, parameters } = stored; const [ help, setHelp ] = useState( false ); const [ all, setAll ] = useState( false ); const [ open, setOpen ] = useState( null ); // The applied suggestion whose Undo asks how. const [ asking, setAsking ] = useState( null ); const score = funnelScore( result ); const order = useMemo( () => stepOrder( doc ), [ doc ] ); const steps = useMemo( () => { const byId = new Map( result.steps.map( ( s ) => [ s.id, s ] ) ); return order.flatMap( ( n ) => { const s = byId.get( n.id ); return s ? [ { ...s, name: stepName( doc, n ), node: n } ] : []; } ); }, [ order, result, doc ] ); const worst = result.sticking[ 0 ]?.id ?? steps[ steps.length - 1 ]?.id ?? null; const [ chosen, setChosen ] = useState( null ); const selected = steps.find( ( s ) => s.id === chosen ) ?? steps.find( ( s ) => s.id === worst ) ?? steps[ 0 ]; const long = steps.length >= LONG_FORM; const compact = long && ! all; const rows = compact ? steps.filter( ( s, i ) => ( s.dropRate ?? 0 ) >= NOTABLE || s.sticking || i === steps.length - 1 || s.id === selected?.id ) : steps; const recs = worthApplying( result.suggestions ); // More and better leads; the filter only while there are both. const [ filter, setFilter ] = useState<'all' | 'more' | 'better'>( 'all' ); const better = recs.filter( isBetter ); const both = better.length > 0 && better.length < recs.length; const shown = ! both || 'all' === filter ? recs : recs.filter( ( r ) => isBetter( r ) === ( 'better' === filter ) ); const quality = result.quality; const done = result.applied; const appliedAt = new Map( apply.applied.map( ( a ) => [ a.suggestion, a ] ) ); // What the AI said about a change that applied several suggestions at once: each shows its own instruction instead. const shared = new Set( apply.applied.map( ( a ) => a.summary ).filter( ( text, i, all ) => text && all.indexOf( text ) !== i ) ); const leads = result.conversion * 1000; const perfect = 100 === score; const allDone = done.length > 0 && ! recs.length; const verdict = perfect ? __( 'Perfect form', 'funnelforms-pro' ) : score >= 60 ? __( 'Good, with clear potential', 'funnelforms-pro' ) : __( 'Many leads get lost here', 'funnelforms-pro' ); const possible = [ recs.length, `${ decimal( result.best * 100 ) }\u00a0%`, `${ decimal( result.conversion * 100 ) }\u00a0%`, number( Math.round( ( result.best - result.conversion ) * 1000 ) ) ] as const; const verdictSub = perfect ? __( 'As things stand, the AI finds no change that brings noticeably more leads.', 'funnelforms-pro' ) : done.length ? sprintf( __( 'With the remaining %1$d suggestions, %2$s instead of %3$s are possible. That is %4$s more leads per 1,000 visitors.', 'funnelforms-pro' ), ...possible ) : sprintf( __( 'With the %1$d suggestions, %2$s instead of %3$s are possible. That is %4$s more leads per 1,000 visitors.', 'funnelforms-pro' ), ...possible ); // What the suggestions applied add, against the prediction before them. const gained = result.conversion - result.original; const busyApplying = null !== apply.applying; const applyNote = sprintf( __( 'Predictive AI: %d changes', 'funnelforms-pro' ), done.length ); const sticking = result.sticking[ 0 ]; const stickingStep = sticking ? steps.find( ( s ) => s.id === sticking.id ) : undefined; const nodeLabel = ( id: string ): string => { const node = doc.nodes.find( ( n ) => n.id === id ); return node ? stepName( doc, node ) : __( 'Without a title', 'funnelforms-pro' ); }; const continueOf = useMemo( () => new Map( model.steps.map( ( s ) => [ s.node, s ] ) ), [ model ] ); return (
= 90 ? '#3ddbb0' : score >= 60 ? '#a98bff' : '#ffb547' } />
{ __( 'Funnel score', 'funnelforms-pro' ) }

{ verdict }

{ verdictSub }

{ __( 'Conversion rate', 'funnelforms-pro' ) } { `${ decimal( result.conversion * 100 ) }\u00a0%` } { done.length ? ( { sprintf( __( '+%s points from the changes', 'funnelforms-pro' ), pointsOf( Math.max( 0, gained ) ) ) } ) : ( { sprintf( __( 'Range %1$s to %2$s', 'funnelforms-pro' ), `${ decimal( result.low * 100 ) }\u00a0%`, `${ decimal( result.high * 100 ) }\u00a0%` ) } ) }
{ __( 'Leads per 1,000 visitors', 'funnelforms-pro' ) } { number( Math.round( leads ) ) } { done.length ? ( { sprintf( __( '+%s from the changes', 'funnelforms-pro' ), number( Math.max( 0, Math.round( gained * 1000 ) ) ) ) } ) : ( { sprintf( __( 'simulated from %s', 'funnelforms-pro' ), number( result.visitors ) ) } ) }
{ quality ? (
{ __( 'Of them qualified', 'funnelforms-pro' ) } { number( Math.round( leads * quality.share ) ) } { `${ decimal( quality.share * 100, 0 ) }\u00a0% ${ quality.label }` }
) : null }
{ __( 'Value per 1,000 visitors', 'funnelforms-pro' ) } { money( leads * parameters.lead_value, parameters.currency ) } { sprintf( __( 'at %s per lead', 'funnelforms-pro' ), money( parameters.lead_value, parameters.currency ) ) }
{ quality ? null : (
{ __( 'Biggest weakness', 'funnelforms-pro' ) } { stickingStep?.name ?? '–' } { sticking && null !== sticking.dropRate ? sprintf( __( '%s leave here', 'funnelforms-pro' ), `${ decimal( sticking.dropRate * 100, 0 ) }\u00a0%` ) : __( 'No step stands out', 'funnelforms-pro' ) }
) }
{ help ? (
{ __( 'Prediction now', 'funnelforms-pro' ) } { `${ decimal( result.conversion * 100 ) }\u00a0%` } { __( 'with all suggestions', 'funnelforms-pro' ) } { `${ decimal( result.best * 100 ) }\u00a0%` } { __( 'Funnel score', 'funnelforms-pro' ) } { sprintf( __( '%d of 100', 'funnelforms-pro' ), score ) } { sprintf( __( 'The score shows how much of what is possible with your form you already get today. 100 means: the AI finds no change that brings noticeably more leads. The conversion rate comes from the simulation with %s visitors, the effect of each suggestion from a simulation of its own.', 'funnelforms-pro' ), number( result.visitors ) ) }
) : null }
{ [ when( stored.created, locale ), sprintf( __( '%s virtual visitors', 'funnelforms-pro' ), number( result.visitors ) ), __( 'An estimate based on benchmarks and AI, not a promise', 'funnelforms-pro' ) ].filter( Boolean ).join( ' · ' ) }
{ allDone ? ( 0 ? Math.round( 100 * ( result.conversion / result.original - 1 ) ) : 0 } count={ done.length } canRun={ canRun && ! busy } onRun={ onRun } onPublish={ onPublish ? () => onPublish( applyNote ) : undefined } /> ) : null } { result.unknown ?

{ sprintf( __( '%d steps are new since the prediction. Predict again to include them.', 'funnelforms-pro' ), result.unknown ) }

: null }

{ __( 'Where visitors leave', 'funnelforms-pro' ) }

{ sprintf( __( 'of %s visitors reach the step', 'funnelforms-pro' ), number( 10000 ) ) }
{ rows.map( ( s ) => { const reach = result.visitors > 0 ? s.reached / result.visitors : 0; const drop = s.dropRate ?? 0; const b = band( drop ); const on = s.id === selected?.id; return ( ); } ) } { compact && rows.length < steps.length ? ( ) : null } { long && all ? ( ) : null }
{ selected ? (

{ sprintf( __( '%1$s: %2$s leave here', 'funnelforms-pro' ), selected.name, `${ decimal( ( selected.dropRate ?? 0 ) * 100, 0 ) }\u00a0%` ) }

{ selected.reasons.length ? (
    { selected.reasons.map( ( r ) => (
  • { r }
  • ) ) }
) : (

{ __( 'A short step, hardly any hesitation. You lose hardly anyone here.', 'funnelforms-pro' ) }

) } { model.personas.length ? (
{ model.personas.map( ( p, i ) => { const s = continueOf.get( selected.id ); const share = s ? s.continue[ p.id ] ?? s.prior : null; return (
{ p.name } { null === share ? '–' : `${ decimal( share * 100, 0 ) }\u00a0%` }
); } ) } { __( 'Share that finishes this step, per kind of visitor', 'funnelforms-pro' ) }
) : null }
) : null }

{ recs.length ? __( 'Suggestions, sorted by effect', 'funnelforms-pro' ) : done.length ? __( 'Suggestions applied', 'funnelforms-pro' ) : __( 'No suggestions', 'funnelforms-pro' ) }

{ both ? (
{ ( [ [ 'all', sprintf( __( 'All (%d)', 'funnelforms-pro' ), recs.length ) ], [ 'more', sprintf( __( 'More leads (%d)', 'funnelforms-pro' ), recs.length - better.length ) ], [ 'better', sprintf( __( 'Better leads (%d)', 'funnelforms-pro' ), better.length ) ], ] as const ).map( ( [ key, label ] ) => ( ) ) }
) : null }
{ ! recs.length && ! done.length ? (
{ __( 'The AI finds nothing that brings noticeably more leads.', 'funnelforms-pro' ) } { model.insights.length ? (
    { model.insights.map( ( x ) => (
  • { x }
  • ) ) }
) : null }

{ __( 'Next lever: more visitors who fit. Test two headlines in an A/B test once the form has traffic.', 'funnelforms-pro' ) }

) : null } { shown.map( ( r ) => { const i = recs.indexOf( r ); const peek = open === r.id; const working = 1 === apply.applying?.length && apply.applying[ 0 ] === r.id; const failed = apply.applyError?.ids.includes( r.id ) ? apply.applyError.message : null; return (
{ i + 1 }
{ nodeLabel( r.node ) }

{ r.title }

{ isBetter( r ) ? (
{ `+${ decimal( ( r.quality ?? 0 ) * 100, 0 ) }\u00a0%` } { sprintf( __( 'qualified leads, %s points', 'funnelforms-pro' ), signedPoints( r.gain ) ) }
) : (
{ sprintf( __( '+%s points', 'funnelforms-pro' ), pointsOf( r.gain ) ) } { sprintf( __( '+%s leads per 1,000 visitors', 'funnelforms-pro' ), number( Math.round( r.gain * 1000 ) ) ) }
) }

{ r.reason }

{ peek ? : null } { failed ? (

{ failed }

) : null }
{ working ? ( <> { apply.canApply ? ( ) : null }
); } ) } { done.map( ( r ) => { const info = appliedAt.get( r.id ); const how = apply.undoable( r.id ); const failed = apply.applyError?.ids.includes( r.id ) ? apply.applyError.message : null; return (
{ nodeLabel( r.node ) }

{ r.title }

{ isBetter( r ) ? (
{ `+${ decimal( ( r.quality ?? 0 ) * 100, 0 ) }\u00a0%` } { __( 'qualified leads', 'funnelforms-pro' ) }
) : (
{ sprintf( __( '+%s points', 'funnelforms-pro' ), pointsOf( Math.max( 0, r.gain ) ) ) } { sprintf( __( '+%s leads per 1,000 visitors', 'funnelforms-pro' ), number( Math.max( 0, Math.round( r.gain * 1000 ) ) ) ) }
) }
{ failed ? (

{ failed }

) : null } { asking === r.id ? ( { setAsking( null ); apply.revert( r.id, 'before' ); } } onOpen={ () => { setAsking( null ); apply.revert( r.id, 'open' ); } } onCancel={ () => setAsking( null ) } /> ) : null }
{ [ info?.summary && ! shared.has( info.summary ) ? info.summary : r.instruction, info?.at ? when( info.at, locale ) : '' ].filter( Boolean ).join( ' · ' ) } { __( 'Applied in the draft', 'funnelforms-pro' ) }
); } ) }
{ result.personas.length ? (

{ __( 'Your kinds of visitors', 'funnelforms-pro' ) }

{ result.personas.map( ( p, i ) => (
{ p.name } { sprintf( __( '%s of the visitors', 'funnelforms-pro' ), `${ decimal( p.share * 100, 0 ) }\u00a0%` ) }

{ p.description }

{ sprintf( __( '%s become a lead', 'funnelforms-pro' ), null === p.conversion ? '–' : `${ decimal( p.conversion * 100, 0 ) }\u00a0%` ) }
) ) }
) : null } { apply.canApply && recs.length > 1 ? 1 } disabled={ busyApplying } onApply={ () => void apply.apply( recs.map( ( r ) => r.id ) ) } /> : null }
); } /** Points with their sign: −0.4, +1.2. */ function signedPoints( share: number ): string { return ( share < 0 ? '−' : '+' ) + pointsOf( Math.abs( share ) ); } /** More leads or better leads. */ function Kind( { suggestion }: { suggestion: Pick } ) { return isBetter( suggestion ) ? { __( 'Better leads', 'funnelforms-pro' ) } : { __( 'More leads', 'funnelforms-pro' ) }; } /** What a suggestion rests on: a rule of the benchmarks (assumed or measured) or an estimate of the AI. */ function Source( { suggestion, benchmark }: { suggestion: Pick; benchmark: string } ) { const source = suggestion.source; if ( ! source?.text ) { return null; } const origin = 'measured' === source.origin ? __( 'measured', 'funnelforms-pro' ) : 'assumption' === source.origin ? sprintf( __( 'assumption, benchmarks %s', 'funnelforms-pro' ), benchmark ) : __( 'estimate of the AI', 'funnelforms-pro' ); return (

{ source.text } ({ origin })

); } function Chevron( { open }: { open: boolean } ) { return ( ); } function Play() { return ( ); }