/** * Properties of what is selected: a step and its fields, a contact step, an end, a path with * its condition, or (nothing selected) the form itself. Texts are edited in the language chosen in * the top bar: the form's default language, or a translation. */ import React from 'react'; import type { ContactField, Field, FormDocument, FormNode, Text } from '@funnelforms/form-schema'; import { duplicateField, duplicateNode, fieldKind, findNode, isLoading, asksFor, keyClash, moveField, nodeName, removeField, removeNode, setStart, text, } from '../document'; import type { Panel, Recipe } from '../store'; import { BookingTypeField } from './BookingTypes'; import { languageLabel } from './Languages'; import { ChoiceProps, DropdownProps } from './Answers'; import { DateProps, NumberProps, RatingProps, SliderProps, TimeProps } from './RangeProps'; import { RepeatProps } from './RepeatProps'; import { ContactLayoutProps, ContactOptions, ContactTextProps, SalutationProps } from './ContactLook'; import { ContentProps } from './ContentProps'; import { ResultProps } from './ResultProps'; import { FileProps, SignatureProps } from './UploadProps'; import { AddressProps, TextFieldProps } from './TextProps'; import { PhoneCountryProps, PhoneSmsProps } from './PhoneProps'; import { EndProps } from './EndProps'; import { ContactCode, EndCode } from './CustomCode'; import { useAiAsk } from './AiForm'; import { AnswerInsert } from './AnswerInsert'; import { editText } from '../languages'; import { spamProtection } from '../spam'; import { InputIcon } from './IconPicker'; import { HiddenFieldProps, isHidden, PrefillProps } from './Prefill'; import { contactFieldName, stepFieldName } from './catalog'; import { useBuilder, useEnv, ViewContext, type View } from './context'; import { put, TextProp } from './texts'; import { fieldsOf, removeWithNotice } from './Removal'; import { describeCondition, edgeLabel, fieldIndex } from './describe'; import { ActionsSection, kindName } from './Actions'; import { NodeAnalyticsSection, PathsSection } from './Analytics'; import { PredictionNodeSection, PredictionOverview } from './Prediction'; import { HintList, useHints } from './Hints'; import { EdgeInspector, FieldVisibility, NodeLogic } from './Logic'; import { StepVariants } from './Experiments'; import { __, sprintf } from './i18n'; import { Button, IconButton, icons, Notice, Section, TextInput, Toggle } from './ui'; type AnyField = Field | ContactField; /** Changes the document; edits with the same key in a row are one undo step. */ function useChange(): ( recipe: Recipe, key?: string ) => void { return useEnv().store.getState().change; } function fieldIn( doc: FormDocument, nodeId: string, fieldId: string ): AnyField | undefined { const node = findNode( doc, nodeId ); return node && 'fields' in node ? ( node.fields as AnyField[] ).find( ( f ) => f.id === fieldId ) : undefined; } export function Inspector() { const doc = useBuilder( ( s ) => s.doc ); const selection = useBuilder( ( s ) => s.selection ); if ( ! doc ) { return null; } let body: React.ReactNode; const node = selection.node ? findNode( doc, selection.node ) : undefined; if ( selection.edge && doc.edges.some( ( e ) => e.id === selection.edge ) ) { body = ; } else if ( node && selection.field && fieldIn( doc, node.id, selection.field ) ) { body = ; } else if ( node ) { body = ; } else { body = ; } return ( ); } /** While a translation is edited: which language, and what empty fields mean. */ function Translating() { const { ctx, lang } = useEnv(); if ( lang === ctx.locale ) { return null; } return

{ sprintf( __( 'You are translating into %s. Empty fields show the default text to visitors.', 'funnelforms-pro' ), languageLabel( lang ) ) }

; } /** Where the captcha field of a contact step comes from, and why visitors may not see it. */ function CaptchaHint() { const off = useBuilder( ( s ) => false === s.doc?.settings.captcha ); const site = spamProtection(); if ( ! site.captcha ) { return { __( 'No captcha is set up for the website yet, so visitors do not see this field. Add the keys of a captcha service in the settings of Funnelforms, under Entries and spam.', 'funnelforms-pro' ) }; } if ( off ) { return { __( 'This form does without the captcha (Settings, Spam protection), so visitors do not see this field.', 'funnelforms-pro' ) }; } return

{ sprintf( /* translators: %s: name of the captcha service, such as Cloudflare Turnstile */ __( 'Uses the spam protection of the website: %s.', 'funnelforms-pro' ), site.captcha ) }

; } /** In Funnelforms Free: a question of a Pro field type, which Free does not publish. */ function ProField() { return { __( 'This field type is part of Funnelforms Pro. Delete the question to publish the form.', 'funnelforms-pro' ) }; } /* ------------------------------------------------------------------ form */ /** Behaviour of the whole form, in the settings workspace. */ export function FormSettings( { doc }: { doc: FormDocument } ) { const change = useChange(); const open = React.useContext( ViewContext ); const settings = doc.settings; return (
change( ( d ) => void ( d.title = v ), 'title' ) } hint={ __( 'Only you see the name; visitors see the questions.', 'funnelforms-pro' ) } /> change( ( d ) => void ( d.settings.loadingAnimation = v ) ) } /> { /* Pro features: Funnelforms Free shows them only to switch them off. */ } { 'free' !== process.env.FUNNELFORMS_EDITION || settings.savePartialEntries ? ( change( ( d ) => void ( d.settings.savePartialEntries = v ) ) } /> ) : null } { 'free' !== process.env.FUNNELFORMS_EDITION || true === settings.saveProgress ? ( change( ( d ) => void ( d.settings.saveProgress = v ) ) } /> ) : null }

{ __( 'The progress bar, the height of the steps and the transitions are set in the design.', 'funnelforms-pro' ) }{ ' ' }

); } /** * The heading above every step (settings.heading): the same text on all steps, above the * question, like the form title of Funnelforms 3 (fe_title, taken over by the migration). * Switching it off removes the text; undo brings it back. */ function FormHeading( { doc }: { doc: FormDocument } ) { const change = useChange(); const { ctx, lang } = useEnv(); const heading = doc.settings.heading; return ( <> change( ( d ) => { if ( on ) { d.settings.heading = d.settings.heading ?? {}; } else { delete d.settings.heading; } } ) } /> { undefined !== heading ? ( change( ( d ) => put( d.settings, 'heading', lang, v, ctx.locale ), 'settings.heading' ) } /> ) : null } ); } /** * Own texts on the next and back buttons (settings.nextLabel and prevLabel, from global_next_text * and global_prev_text of Funnelforms 3). Empty in every language: the standard texts. */ function ButtonLabels( { doc }: { doc: FormDocument } ) { const change = useChange(); const { ctx, lang } = useEnv(); const write = ( key: 'nextLabel' | 'prevLabel' ) => ( value: string ): void => change( ( d ) => { put( d.settings, key, lang, value, ctx.locale ); if ( Object.values( d.settings[ key ] ?? {} ).every( ( v ) => '' === v.trim() ) ) { d.settings[ key ] = undefined; } }, 'settings.' + key ); return (

{ __( 'Empty: the standard text.', 'funnelforms-pro' ) }

); } /** With nothing selected: the form at a glance and the way to its other workspaces. */ function FormOverview( { doc }: { doc: FormDocument } ) { const change = useChange(); const open = React.useContext( ViewContext ); const steps = doc.nodes.filter( ( n ) => 'end' !== n.type ).length; const questions = doc.nodes.reduce( ( sum, n ) => sum + ( 'step' === n.type ? n.fields.length : 0 ), 0 ); const ends = doc.nodes.filter( ( n ) => 'end' === n.type ).length; const jumps: Array<{ view: View; title: string; text: string; show: boolean }> = [ { view: 'design', title: __( 'Design', 'funnelforms-pro' ), text: __( 'Colours, font and look, with a live preview.', 'funnelforms-pro' ), show: true }, { view: 'actions', title: __( 'After sending', 'funnelforms-pro' ), text: ! doc.nodes.some( ( n ) => 'contact' === n.type ) ? __( 'Add a contact step: mails and webhooks run when visitors send it.', 'funnelforms-pro' ) : doc.actions.length ? sprintf( __( 'Actions: %d, at the contact step.', 'funnelforms-pro' ), doc.actions.length ) : 'free' === process.env.FUNNELFORMS_EDITION ? __( 'Mails to you and your visitors, at the contact step.', 'funnelforms-pro' ) : __( 'Mails, webhooks and connections, at the contact step.', 'funnelforms-pro' ), show: doc.nodes.some( ( n ) => 'contact' === n.type ) || doc.actions.length > 0, }, { view: 'settings', title: __( 'Settings', 'funnelforms-pro' ), text: 'free' === process.env.FUNNELFORMS_EDITION ? __( 'Name, behaviour and the own page of the form.', 'funnelforms-pro' ) : __( 'Languages, calculations, quiz, payment and access.', 'funnelforms-pro' ), show: true }, ]; return ( <>
change( ( d ) => void ( d.title = v ), 'title' ) } hint={ __( 'Only you see the name; visitors see the questions.', 'funnelforms-pro' ) } />
{ __( 'Steps', 'funnelforms-pro' ) }
{ steps }
{ __( 'Questions', 'funnelforms-pro' ) }
{ questions }
{ __( 'Ends', 'funnelforms-pro' ) }
{ ends }

{ __( 'Select a step on the canvas to edit it.', 'funnelforms-pro' ) }

); } /** All hints about the logic of the form, or that everything leads somewhere. */ function LogicCheck( { doc }: { doc: FormDocument } ) { const hints = useHints().filter( ( h ) => 'info' !== h.level ); return (
{ hints.length ? :

{ __( 'Every step leads on and every end can be reached.', 'funnelforms-pro' ) }

}
); } /* ------------------------------------------------------------------ nodes */ function NodeInspector( { doc, node }: { doc: FormDocument; node: FormNode } ) { const { store, readOnly } = useEnv(); const change = useChange(); const select = store.getState().select; const panel = useBuilder( ( s ) => s.panel ); const hints = useHints().filter( ( h ) => h.node === node.id ); const title = 'step' === node.type ? __( 'Step', 'funnelforms-pro' ) : 'contact' === node.type ? __( 'Contact step', 'funnelforms-pro' ) : __( 'End', 'funnelforms-pro' ); const ask = useAiAsk(); const { ctx } = useEnv(); const actions = ( <> { ask ? ( ask( sprintf( __( 'In the step "%s": ', 'funnelforms-pro' ), nodeName( node, ctx.locale ) || title ) ) } /> ) : null } { let id: string | null = null; change( ( d ) => { id = duplicateNode( d, node.id ); } ); if ( id ) { select( { node: id } ); } } } /> { removeWithNotice( store, ( d ) => removeNode( d, node.id ), fieldsOf( doc, node.id ) ); select( {} ); } } /> ); // Hints about the node itself; those about its paths are on the logic panel. const own = hints.filter( ( h ) => ! h.edge && ! h.field && ! LOGIC_CODES.includes( h.code ) ); if ( 'end' === node.type ) { return ( <>
'noEntry' !== h.code ) } compact />
); } const logicCount = hints.filter( ( h ) => 'info' !== h.level && ( h.edge || LOGIC_CODES.includes( h.code ) ) ).length; const single = 'step' === node.type && 1 === node.fields.length ? node.fields[ 0 ] : undefined; const content = ( <>
{ 'step' === node.type && 'free' !== process.env.FUNNELFORMS_EDITION ? : null } { doc.start !== node.id ? ( ) : (

{ __( 'Visitors start here.', 'funnelforms-pro' ) }

) } { 'contact' === node.type ? : null }
{ single && 'step' === node.type ? : } { 'contact' === node.type ? : null } ); const logic = 'contact' === node.type ? ( <> ) : ( ); return ( <>
{ 'logic' === panel ? logic : content }
); } /** Hints that belong on the logic panel of a step. */ const LOGIC_CODES: string[] = [ 'deadEnd', 'noOtherwise', 'contactNoEnd' ]; /** The two panels of a step: what it asks, and where visitors go (or what happens on sending). */ function PanelTabs( { panel, contentLabel, logicLabel, count }: { panel: Panel; contentLabel: string; logicLabel: string; count: number } ) { const show = useEnv().store.getState().showPanel; const tabs: Array<[ Panel, string ]> = [ [ 'content', contentLabel ], [ 'logic', logicLabel ], ]; const onKey = ( e: React.KeyboardEvent ): void => { if ( 'ArrowLeft' === e.key || 'ArrowRight' === e.key ) { e.preventDefault(); const next: Panel = 'content' === panel ? 'logic' : 'content'; show( next ); document.getElementById( 'ffxb-node-tab-' + next )?.focus(); } }; return (
{ tabs.map( ( [ id, label ] ) => ( ) ) }
); } /** How visitors reach an end, and what sending did before they see it. */ function EndArrivals( { doc, node }: { doc: FormDocument; node: Extract } ) { const { store, ctx } = useEnv(); const select = store.getState().select; const incoming = doc.edges.filter( ( e ) => e.to === node.id ); const fields = fieldIndex( doc ); return (
{ 0 === incoming.length ?

{ __( 'No path leads here yet. Choose this end as next step of a contact step or a question, or draw a path on the canvas.', 'funnelforms-pro' ) }

: null }
    { incoming.map( ( e ) => { const from = findNode( doc, e.from ); if ( ! from ) { return null; } const contact = 'contact' === from.type; const run = contact ? doc.actions.filter( ( a ) => ! a.node || a.node === from.id ) : []; return (
  • { contact ? (

    { run.length ? sprintf( __( 'After sending: %s', 'funnelforms-pro' ), run.map( ( a ) => kindName( a.kind ) ).join( ', ' ) ) : __( 'After sending: the entry is saved, nothing else happens.', 'funnelforms-pro' ) }{ ' ' }

    ) : doc.nodes.some( ( n ) => 'contact' === n.type ) ? (

    { __( 'Leaves without sending: no entry, no mails.', 'funnelforms-pro' ) }

    ) : null }
  • ); } ) }
); } function ContactProps( { node }: { node: Extract } ) { const { ctx, lang } = useEnv(); const change = useChange(); const edit = ( key: 'title' | 'description' | 'submitLabel' ) => ( value: string ) => change( ( d ) => { const n = findNode( d, node.id ); if ( n && 'contact' === n.type ) { put( n, key, lang, value, ctx.locale ); } }, `${ node.id }:${ key }` ); return ( <> change( ( d ) => { const n = findNode( d, node.id ); if ( n && 'contact' === n.type ) { n.showBottomBar = v; } }, `${ node.id }:showBottomBar` ) } /> change( ( d ) => { const n = findNode( d, node.id ); if ( n && 'contact' === n.type ) { const layout = n.layout ?? {}; recipe( layout ); if ( Object.keys( layout ).length ) { n.layout = layout; } else { delete n.layout; } } }, `${ node.id }:layout.${ key }` ) } /> ); } /** The fields of a step or contact step, to select, order, copy and remove. */ function FieldList( { node }: { node: Extract } ) { const { store, ctx, readOnly } = useEnv(); const doc = useBuilder( ( s ) => s.doc ) as FormDocument; const change = useChange(); const select = store.getState().select; const fields = node.fields as AnyField[]; const name = ( f: AnyField ): string => ( 'contact' === node.type ? contactFieldName( f.type ) : stepFieldName( fieldKind( f ) ) ); return (
    { fields.map( ( f, i ) => (
  1. ) ) }

{ __( 'Add fields from the list on the left, or drag them onto the step.', 'funnelforms-pro' ) }

); } /* ------------------------------------------------------------------ fields */ function FieldInspector( { doc, node, fieldId }: { doc: FormDocument; node: FormNode; fieldId: string } ) { const field = fieldIn( doc, node.id, fieldId ); const select = useEnv().store.getState().select; if ( ! field || 'end' === node.type ) { return null; } return ( <>
); } function FieldProps( { node, field }: { node: Extract; field: AnyField } ) { const { api, store, ctx, lang, readOnly } = useEnv(); const change = useChange(); const select = store.getState().select; const contact = 'contact' === node.type; // A string, so the selector gives the same value while nothing changes. const clash = useBuilder( ( st ) => { const other = st.doc ? keyClash( st.doc, field.id ) : null; return other ? nodeName( other.node, ctx.locale ) || other.field.key : null; } ); const edit = ( recipe: ( f: AnyField ) => void, key?: string ) => change( ( d ) => { const f = fieldIn( d, node.id, field.id ); if ( f ) { recipe( f ); } }, key ? `${ field.id }:${ key }` : undefined ); const textOf = ( key: string ) => ( value: string ) => edit( ( f ) => put( f, key as keyof AnyField, lang, value, ctx.locale ), key ); const propText = ( key: string ) => ( value: string ) => edit( ( f ) => put( f.props as Record, key, lang, value, ctx.locale ), 'props.' + key ); const props = field.props as Record; const propsEdit = ( recipe: ( p: Record ) => void, key?: string ): void => edit( ( f ) => recipe( f.props as Record ), key ); const actions = ( <> { let id: string | null = null; change( ( d ) => { id = duplicateField( d, node.id, field.id ); } ); if ( id ) { select( { node: node.id, field: id } ); } } } /> { removeWithNotice( store, ( d ) => void removeField( d, node.id, field.id ), asksFor( field.id ) ); select( { node: node.id } ); } } /> ); const hidden = isHidden( field, contact ); const name = hidden ? __( 'Hidden field', 'funnelforms-pro' ) : contact ? contactFieldName( field.type ) : stepFieldName( fieldKind( field ) ); const hasLabel = ! ( contact && [ 'consent', 'checkbox', 'html', 'captcha' ].includes( field.type ) ); return (
{ hasLabel ? : null } { hasLabel && ! contact && 'free' !== process.env.FUNNELFORMS_EDITION ? textOf( 'label' )( `${ editText( field.label, lang, ctx.locale ) } ${ token }`.trim() ) } /> : null } { ! contact && 'description' in field ? : null } { contact && 'text' === field.type ? : null } { 'placeholder' in props && ! hidden ? : null } { contact && ( 'consent' === field.type || 'checkbox' === field.type || 'html' === field.type ) ? ( ) : null } { contact && 'text' === field.type && ! hidden ? void, key?: string ) => void } readOnly={ readOnly } /> : null } { contact && 'salutation' === field.type ? void, key?: string ) => void } readOnly={ readOnly } /> : null } { contact && 'select' === field.type ? void, key?: string ) => void } /> : null } { contact && 'captcha' === field.type ? : null } { contact && api.icons && ! hidden && [ 'name', 'email', 'phone', 'phone_verification', 'text' ].includes( field.type ) ? ( edit( ( f ) => { const p = f.props as Record; if ( icon ) { p.icon = icon; } else { delete p.icon; } }, 'props.icon' ) } /> ) : null } { contact && ( 'phone' === field.type || 'phone_verification' === field.type ) ? ( <> ) : null } { contact && 'email' === field.type ? ( edit( ( f ) => void ( ( f.props as Record ).b2bOnly = v ) ) } /> ) : null } { ! hidden && ! [ 'content', 'captcha', 'html', 'result' ].includes( field.type ) ? ( edit( ( f ) => void ( f.required = v ) ) } /> ) : null } { 'step' === node.type ? : null } { contact ? null : } { clash ? ( { sprintf( __( 'The variable name "%1$s" is used by "%2$s" as well. Calculations, placeholders and integrations then take only one of them; change it under Advanced.', 'funnelforms-pro' ), field.key, clash ) } ) : null }
{ __( 'Advanced', 'funnelforms-pro' ) } edit( ( f ) => void ( f.key = v.replace( /[^A-Za-z0-9_-]/g, '_' ) ), 'key' ) } hint={ __( 'Used in calculations, placeholders and integrations.', 'funnelforms-pro' ) } />
); } type Edit = ( recipe: ( f: AnyField ) => void, key?: string ) => void; function StepFieldProps( { node, field, edit, propText }: { node: FormNode; field: Field; edit: Edit; propText: ( key: string ) => ( value: string ) => void } ) { const set = ( key: string, value: unknown ): void => edit( ( f ) => void ( ( f.props as Record )[ key ] = value ), 'props.' + key ); const num = ( key: string ) => ( value: number | undefined ): void => edit( ( f ) => { const props = f.props as Record; if ( undefined === value ) { // Optional limits are removed; required numbers keep their last value. if ( ! [ 'min', 'max', 'step', 'start', 'maxSizeMb', 'maxFiles', 'minItems', 'maxItems' ].includes( key ) || 'number' === field.type ) { Reflect.deleteProperty( props, key ); } return; } props[ key ] = value; }, 'props.' + key ); switch ( field.type ) { case 'choice': return ; case 'dropdown': return ; case 'text': case 'textarea': return ; case 'number': return ; case 'date': return ; case 'phone': return edit( ( f ) => recipe( f.props as Record ), key ) } />; case 'time': return ; case 'slider': return 'free' === process.env.FUNNELFORMS_EDITION ? : ; case 'rating': return ; case 'content': return ; case 'file': return 'free' === process.env.FUNNELFORMS_EDITION ? : ; case 'signature': return 'free' === process.env.FUNNELFORMS_EDITION ? : ; case 'address': return 'free' === process.env.FUNNELFORMS_EDITION ? : ; case 'booking': return 'free' === process.env.FUNNELFORMS_EDITION ? : set( 'bookingTypeRef', v ) } />; case 'result': return 'free' === process.env.FUNNELFORMS_EDITION ? : ; case 'repeat': if ( 'free' === process.env.FUNNELFORMS_EDITION ) { return ; } return 'step' === node.type ? : null; default: return null; } }