/**
* 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' ) }