/** * Start of a new form: a gallery of templates by goal and industry (both at once). It starts with * all templates, those of the site's industry first and recommended ones first, with a search * through texts and synonyms (of all industries, unless the owner chose one), a * thumbnail of the first question, what the template does for leads, a live preview and the * language of the form, an empty form by name, or a draft the AI writes from a description * (Funnelforms Pro). Funnelforms Free shows the templates of Pro locked; they lead to the page * about Pro. Ends in the editor with the new draft. */ import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import type { AiConfig, FormsApi, FormTemplate, IconElements, TemplateCatalog, TemplateCategory, TemplateFeature, TemplateGoal } from '../api'; import type { Device } from '../store'; import { __, sprintf } from './i18n'; import { LucideIcon } from './IconPicker'; import { languageLabel } from './Languages'; import { PreviewDialog } from './Preview'; import { findTheme, themeStyle } from '../themes'; import { AiCreateCard, AiCreateStep } from './AiForm'; import { sampleVars } from './ThemeSample'; import { Button, icons, Notice, ProBadge, ProCard, Spinner, TextInput } from './ui'; import { proUrl } from '../edition'; /** The goal and the industry chosen; null: any. */ type Filter = { goal: TemplateGoal | null; category: TemplateCategory | null }; const ALL: Filter = { goal: null, category: null }; /** What the dialog shows: the gallery, the empty form by name, or the AI form builder. */ type Step = 'gallery' | 'blank' | 'ai'; export function goalLabel( goal: TemplateGoal ): string { switch ( goal ) { case 'quote': return __( 'Quote requests', 'funnelforms-pro' ); case 'calculator': return __( 'Price calculators', 'funnelforms-pro' ); case 'consultation': return __( 'Consultations', 'funnelforms-pro' ); case 'booking': return __( 'Appointments', 'funnelforms-pro' ); case 'application': return __( 'Applications', 'funnelforms-pro' ); case 'quiz': return __( 'Quizzes and product finders', 'funnelforms-pro' ); case 'registration': return __( 'Registrations', 'funnelforms-pro' ); case 'feedback': return __( 'Feedback and surveys', 'funnelforms-pro' ); case 'leadmagnet': return __( 'Lead magnets', 'funnelforms-pro' ); } return goal; } export function categoryLabel( category: TemplateCategory ): string { const labels: Record = { craft: __( 'Trades and construction', 'funnelforms-pro' ), energy: __( 'Energy and heating', 'funnelforms-pro' ), realestate: __( 'Real estate', 'funnelforms-pro' ), finance: __( 'Finance and insurance', 'funnelforms-pro' ), hr: __( 'Recruiting', 'funnelforms-pro' ), health: __( 'Health and care', 'funnelforms-pro' ), legal: __( 'Law and tax', 'funnelforms-pro' ), home: __( 'Home services', 'funnelforms-pro' ), auto: __( 'Cars and mobility', 'funnelforms-pro' ), beauty: __( 'Beauty and wellness', 'funnelforms-pro' ), education: __( 'Education and courses', 'funnelforms-pro' ), coaching: __( 'Coaching and advice', 'funnelforms-pro' ), b2b: __( 'B2B, SaaS and IT', 'funnelforms-pro' ), agency: __( 'Agencies and creatives', 'funnelforms-pro' ), events: __( 'Events, catering and weddings', 'funnelforms-pro' ), ecommerce: __( 'Online shops', 'funnelforms-pro' ), general: __( 'For every business', 'funnelforms-pro' ), }; return labels[ category ] ?? category; } export function featureLabel( feature: TemplateFeature ): string { switch ( feature ) { case 'quiz': return __( 'Quiz', 'funnelforms-pro' ); case 'logic': return __( 'Logic', 'funnelforms-pro' ); case 'calculation': return __( 'Calculation', 'funnelforms-pro' ); case 'booking': return __( 'Booking', 'funnelforms-pro' ); case 'upload': return __( 'File upload', 'funnelforms-pro' ); case 'address': return __( 'Address', 'funnelforms-pro' ); case 'rating': return __( 'Rating', 'funnelforms-pro' ); case 'signature': return __( 'Signature', 'funnelforms-pro' ); case 'price': return __( 'Shows a price', 'funnelforms-pro' ); case 'result': return __( 'Shows a result', 'funnelforms-pro' ); case 'score': return __( 'Lead score', 'funnelforms-pro' ); } return feature; } /** What a card says a template does for leads: the result it shows, the lead score and booking. */ const CONVERSION: TemplateFeature[] = [ 'price', 'result', 'score', 'booking' ]; /** A template of Funnelforms Pro shown locked in Funnelforms Free. */ function locked( template: FormTemplate ): boolean { return 'free' === process.env.FUNNELFORMS_EDITION && true === template.pro; } /** Lower case without accents, so "Gebaude" finds "Gebäude". */ function fold( text: string ): string { return text .normalize( 'NFD' ) .replace( /[̀-ͯ]/g, '' ) .toLowerCase(); } function matches( template: FormTemplate, words: string[] ): boolean { if ( 0 === words.length ) { return true; } const haystack = fold( [ template.title, template.description, template.firstQuestion, goalLabel( template.goal ), categoryLabel( template.category ), ...template.features.map( featureLabel ), ...template.sample.options.map( ( o ) => o.label ), ...( template.keywords ?? [] ), ].join( ' ' ) ); return words.every( ( word ) => haystack.includes( word ) ); } function shows( template: FormTemplate, filter: Filter ): boolean { return ( null === filter.goal || template.goal === filter.goal ) && ( null === filter.category || template.category === filter.category ); } /** * Templates to use before locked ones, those of the site's industry next, recommended ones (rank) * first, otherwise in the order of the catalog. */ function ranked( templates: FormTemplate[], industry?: string | null ): FormTemplate[] { const key = ( t: FormTemplate ): number => ( locked( t ) ? 1e6 : 0 ) + ( industry && t.category !== industry ? 2e5 : 0 ) + ( t.rank ?? 1e5 ); return templates .map( ( t, i ) => ( { t, i } ) ) .sort( ( a, b ) => key( a.t ) - key( b.t ) || a.i - b.i ) .map( ( x ) => x.t ); } /** * ai: the AI form builder of the page (Funnelforms Pro; without it that way is not offered). * Funnelforms Free shows it with the Pro badge. */ export function NewFormDialog( { api, category, ai, onCreated, onClose }: { api: FormsApi; category?: string; ai?: AiConfig | null; onCreated: ( id: number ) => void; onClose: () => void } ) { const ref = useRef( null ); const [ catalog, setCatalog ] = useState( null ); const [ loading, setLoading ] = useState( true ); const [ locale, setLocale ] = useState( '' ); // All templates at the start; those of the site's industry come first. const [ filter, setFilter ] = useState( ALL ); const [ search, setSearch ] = useState( '' ); const [ step, setStep ] = useState( 'gallery' ); const [ title, setTitle ] = useState( '' ); const [ error, setError ] = useState( null ); const [ busy, setBusy ] = useState( false ); const [ previewing, setPreviewing ] = useState( null ); const [ device, setDevice ] = useState( 'desktop' ); useEffect( () => { ref.current?.showModal?.(); }, [] ); useEffect( () => { let current = true; setLoading( true ); api.templates( locale || undefined ).then( ( result ) => { if ( current ) { setCatalog( result ); setLoading( false ); } }, () => { // Without templates the empty form still works. if ( current ) { setCatalog( ( c ) => c ?? { goals: [], categories: [], languages: [], locale: '', templates: [], icons: {} } ); setLoading( false ); } } ); return () => { current = false; }; }, [ api, locale ] ); const words = useMemo( () => fold( search ).split( /\s+/ ).filter( Boolean ), [ search ] ); /** An industry or all templates chosen by the owner, which a search keeps. */ const choose = ( f: Filter ): void => setFilter( f ); const found = useMemo( () => ( catalog?.templates ?? [] ).filter( ( t ) => matches( t, words ) ), [ catalog, words ] ); const shown = ranked( found.filter( ( t ) => shows( t, filter ) ), catalog?.industry ); const filtered = null !== filter.goal || null !== filter.category; // The AI form builder and the empty form, on the unfiltered gallery and whenever no template fits. const starts = null !== catalog && ( ( ! filtered && 0 === words.length ) || 0 === shown.length ); const goals = ( catalog?.goals ?? [] ).filter( ( g ) => ( catalog?.templates ?? [] ).some( ( t ) => t.goal === g ) ); const categories = ( catalog?.categories ?? [] ).filter( ( c ) => ( catalog?.templates ?? [] ).some( ( t ) => t.category === c ) ); // A goal counts within the chosen industry and an industry within the chosen goal. const count = ( f: Filter ): number => found.filter( ( t ) => shows( t, f ) ).length; const iconSet = catalog?.icons ?? {}; const run = async ( work: () => Promise ): Promise => { setBusy( true ); setError( null ); try { onCreated( await work() ); } catch ( e ) { setError( e instanceof Error ? e.message : String( e ) ); setBusy( false ); } }; const use = ( template: FormTemplate ): Promise => run( async () => ( await api.fromTemplate( template.id, locale || undefined ) ).id ); const blank = (): Promise => run( async () => ( await api.create( title.trim(), category ?? '' ) ).id ); const fetchPage = useCallback( () => api.templatePreview( previewing?.id ?? '', locale || undefined ), [ api, previewing, locale ] ); const heading = [ filter.goal ? goalLabel( filter.goal ) : '', filter.category ? categoryLabel( filter.category ) : '' ].filter( Boolean ).join( ' · ' ) || __( 'All templates', 'funnelforms-pro' ); /** A filter button: all templates, or a goal or an industry that is chosen or let go by a click. */ const facet = ( key: string, active: boolean, n: number, label: string, choose: () => void ) => (
  • ); const withAi = 'free' === process.env.FUNNELFORMS_EDITION || ( !! ai && undefined !== api.aiCreate ); let main: React.ReactNode; if ( 'ai' === step && 'free' === process.env.FUNNELFORMS_EDITION ) { main = (
    ); } else if ( 'ai' === step && 'free' !== process.env.FUNNELFORMS_EDITION && ai ) { main = setStep( 'gallery' ) } />; } else if ( 'blank' === step ) { main = (
    { e.preventDefault(); if ( title.trim() ) { void blank(); } } } >

    { __( 'Start from scratch', 'funnelforms-pro' ) }

    { __( 'An empty form with a contact step. Add your own questions in the editor.', 'funnelforms-pro' ) }

    ); } else { main = (
    { null === catalog ? '' : shown.length } { loading && null !== catalog ?
    { null === catalog ? : null } { null !== catalog && 0 === shown.length && ( words.length > 0 || filtered ) ? (
    { __( 'No template matches your search.', 'funnelforms-pro' ) } { __( 'Try another word or start with an empty form.', 'funnelforms-pro' ) }
    ) : null }
      { withAi && starts ? setStep( 'ai' ) } badge={ 'free' === process.env.FUNNELFORMS_EDITION ? : undefined } /> : null } { starts ? (
    • ) : null } { shown.map( ( t ) => ( setPreviewing( t ) } onUse={ () => void use( t ) } /> ) ) }
    ); } const languages = catalog?.languages ?? []; return ( { // The preview is a dialog inside this one; its close reaches here through React. if ( e.target === e.currentTarget ) { onClose(); } } } >

    { __( 'Pick a template made for your industry and goal. Every question, text and calculation can be changed later.', 'funnelforms-pro' ) }

    { 'gallery' !== step ? null : (
    { languages.length > 1 ? ( ) : null }
    ) } { error ? (
    { error }
    ) : null } { main } { previewing ? ( setPreviewing( null ) } actions={ } /> ) : null }
    ); } function TemplateCard( { template: t, icons: set, lang, busy, onPreview, onUse }: { template: FormTemplate; icons: Record; lang: string; busy: boolean; onPreview: () => void; onUse: () => void } ) { const icon = set[ t.icon ]; const lock = locked( t ); // Locked: a teaser that leads to the page about Funnelforms Pro (Free has no document to preview or use). const upgrade = (): void => { window.location.assign( proUrl() ); }; const conversion = CONVERSION.filter( ( f ) => t.features.includes( f ) && ! ( 'result' === f && t.features.includes( 'price' ) ) ); const questions = t.questions ?? t.steps; return (
  • { t.title }

    { lock ? : null }

    { t.description }

    • { goalLabel( t.goal ) }
    • { conversion.map( ( f ) => (
    • { featureLabel( f ) }
    • ) ) }
    • { 1 === questions ? __( '1 question', 'funnelforms-pro' ) : sprintf( /* translators: %d: number of questions the visitor answers */ __( '%d questions', 'funnelforms-pro' ), questions ) }
    { lock ? ( ) : ( <> ) }
  • ); } /** A small drawing of the first step in the template's theme: the question with its answers, rating, slider or text, in the language of the form. */ function Thumbnail( { template: t, icons: set, lang }: { template: FormTemplate; icons: Record; lang: string } ) { const { sample } = t; const theme = findTheme( t.theme ); let answers: React.ReactNode; if ( 'choice' === sample.kind && sample.options.length > 0 ) { const withIcons = sample.options.every( ( o ) => o.icon && set[ o.icon ] ); // Tiles are narrow: long words (Einfamilienhaus) go into rows instead. const tiles = withIcons && sample.options.slice( 0, 3 ).every( ( o ) => o.label.length <= 22 && o.label.split( /[\s-]+/ ).every( ( word ) => word.length <= 11 ) ); answers = ( { sample.options.map( ( o, i ) => { const elements = set[ o.icon ]; return ( { withIcons && elements ? : null } { o.label } ); } ) } ); } else if ( 'rating' === sample.kind ) { const points = 'numbers' === sample.style ? Array.from( { length: Math.min( 11, sample.max + 1 ) }, ( _, i ) => i ) : Array.from( { length: Math.min( 5, sample.max ) }, ( _, i ) => i + 1 ); answers = ( { points.map( ( p ) => ( { 'numbers' === sample.style ? p : '' } ) ) } ); } else if ( 'slider' === sample.kind ) { answers = ( ); } else if ( 'content' === sample.kind ) { answers = ( ); } else { answers = ; } return (