/** * The list of forms (Funnelforms → Forms) in the frame of the other admin pages: key figures of * the last 30 days, the forms by status with search and category, per form its entries, views * and conversion, and the actions per form. */ import React, { useCallback, useEffect, useRef, useState } from 'react'; import type { AiConfig, FormFigure, FormFigures, FormList, FormsApi, FormStatus, FormSummary, ImportedForm } from '../api'; import { ApiError } from '../api'; import { change, relativeTime } from '../entries'; import { Badge, Button, icons, Notice, Spinner } from './ui'; import { EmptyState, Frame, FrameButton, Icon, Kpi, Pill, Spark } from './Frame'; import { NewFormDialog } from './NewForm'; import { StatsDialog } from './Stats'; import { __, sprintf } from './i18n'; type Tab = 'all' | FormStatus; /** A share as the plugin's pages write it: 37.4 %. */ function percent( rate: number ): string { return ( Math.round( 1000 * rate ) / 10 ).toLocaleString( undefined, { minimumFractionDigits: 1, maximumFractionDigits: 1 } ) + ' %'; } /** * The list of forms. The head offers the two imports by the names the menu gives them: Import * Funnelforms (export files of Funnelforms; its page when importOwn is given, else the file * picker right here) and Import from other plugins (importUrl). */ export function FormsList( { api, onOpen, importUrl, importOwn, ai }: { api: FormsApi; onOpen: ( id: number ) => void; importUrl?: string; importOwn?: string; ai?: AiConfig | null } ) { const [ tab, setTab ] = useState( 'all' ); const [ search, setSearch ] = useState( '' ); const [ category, setCategory ] = useState( null ); const [ page, setPage ] = useState( 1 ); const [ data, setData ] = useState( null ); const [ figures, setFigures ] = useState( null ); const [ error, setError ] = useState( null ); const [ notice, setNotice ] = useState<{ tone: 'success' | 'warning' | 'danger'; text: string } | null>( null ); const [ creating, setCreating ] = useState( false ); const [ busy, setBusy ] = useState( false ); const [ statsFor, setStatsFor ] = useState( null ); const fileInput = useRef( null ); const perPage = 20; const reload = useCallback( async () => { setError( null ); try { setData( await api.list( { search, status: 'all' === tab ? undefined : tab, category: category ?? undefined, page, perPage, } ) ); } catch ( e ) { setError( e instanceof Error ? e.message : String( e ) ); } }, [ api, search, tab, category, page ] ); useEffect( () => { const timer = setTimeout( () => void reload(), search ? 250 : 0 ); return () => clearTimeout( timer ); }, [ reload, search ] ); const loadFigures = useCallback( async () => { if ( ! api.formFigures ) { return; } try { setFigures( await api.formFigures() ); } catch { // The list works without them. } }, [ api ] ); useEffect( () => { void loadFigures(); }, [ loadFigures ] ); const act = async ( work: () => Promise, done?: string ): Promise => { setBusy( true ); try { await work(); if ( done ) { setNotice( { tone: 'success', text: done } ); } await reload(); void loadFigures(); } catch ( e ) { setNotice( { tone: 'danger', text: e instanceof ApiError || e instanceof Error ? e.message : String( e ) } ); } finally { setBusy( false ); } }; const download = async ( form: FormSummary ): Promise => { const data = await api.exportForm( form.id, 'draft' ); const url = URL.createObjectURL( new Blob( [ JSON.stringify( data, null, 2 ) ], { type: 'application/json' } ) ); const link = document.createElement( 'a' ); link.href = url; link.download = ( form.title || 'form' ).replace( /[^\p{L}\p{N}-]+/gu, '-' ) + '.funnelforms.json'; link.click(); setTimeout( () => URL.revokeObjectURL( url ), 1000 ); }; const upload = async ( file: File ): Promise => { setBusy( true ); try { const result: ImportedForm[] = await api.importFile( file, file.name ); // Leads of Funnelforms 4 go into forms imported before; the import page reports them in detail. const forms = result.filter( ( f ) => ! f.entries && ! f.errors?.includes( 'ff4_form_missing' ) ); const ok = forms.filter( ( f ) => null !== f.id ).length; const failed = forms.length - ok; const missing = result.find( ( f ) => f.errors?.includes( 'ff4_form_missing' ) ); setNotice( missing ? { tone: 'warning', text: sprintf( /* translators: title of the form in Funnelforms 4, or the file name */ __( '%s: the form of these leads is not imported yet. Import the forms file of Funnelforms 4 first, then this file again.', 'funnelforms-pro' ), missing.title || file.name ) } : 0 === forms.length && result.some( ( f ) => f.entries ) ? { tone: 'success', text: __( 'The leads were imported. Imported leads send no e-mails or webhooks.', 'funnelforms-pro' ) } : failed ? { tone: 'warning', text: sprintf( __( '%1$d forms imported as drafts, %2$d could not be converted.', 'funnelforms-pro' ), ok, failed ) } : { tone: 'success', text: sprintf( __( '%d forms imported as drafts. Check them and publish.', 'funnelforms-pro' ), ok ) } ); setTab( 'draft' ); await reload(); void loadFigures(); } catch ( e ) { setNotice( { tone: 'danger', text: e instanceof Error ? e.message : String( e ) } ); } finally { setBusy( false ); if ( fileInput.current ) { fileInput.current.value = ''; } } }; const counts = figures?.forms; const tabs: Array<{ id: Tab; label: string; count?: number }> = [ { id: 'all', label: __( 'All', 'funnelforms-pro' ), count: counts?.total }, { id: 'published', label: __( 'Published', 'funnelforms-pro' ), count: counts?.published }, { id: 'draft', label: __( 'Drafts', 'funnelforms-pro' ), count: counts?.draft }, { id: 'trashed', label: __( 'Trash', 'funnelforms-pro' ), count: counts?.trashed }, ]; const f = figures?.figures ?? null; const trend = f ? change( f.entries, f.entriesBefore ) : null; const measured = null !== f; return ( { importOwn ? ( { __( 'Import Funnelforms', 'funnelforms-pro' ) } ) : ( fileInput.current?.click() } disabled={ busy } title={ __( 'Forms exported from Funnelforms 5 or 4 (.json) or Funnelforms 3 (.zip)', 'funnelforms-pro' ) }> { __( 'Import Funnelforms', 'funnelforms-pro' ) } ) } { importUrl ? ( { __( 'Import from other plugins', 'funnelforms-pro' ) } ) : null } { const file = e.target.files?.[ 0 ]; if ( file ) { void upload( file ); } } } /> setCreating( true ) }> { __( 'New form', 'funnelforms-pro' ) } } > { notice ? ( setNotice( null ) } /> }> { notice.text } ) : null } { figures ? (
{ f ? ( <> 0 ? '+' : '' ) + trend.toLocaleString() + ' %' ) } tone={ null === trend || 0 === trend ? '' : trend > 0 ? 'up' : 'down' } /> ) : null }
) : null }
{ tabs.map( ( t ) => ( ) ) }
{ data && data.categories.length > 0 ? ( ) : null }
{ error ? (
void reload() }>{ __( 'Try again', 'funnelforms-pro' ) } }> { error }
) : null } { ! data && ! error ? : null } { data && 0 === data.forms.length ? ( 'trashed' === tab && ! search && ! category ? ( { __( 'Forms you move to the trash wait here until you restore or delete them.', 'funnelforms-pro' ) } ) : search || category || 'all' !== tab ? ( { __( 'Try another search or status.', 'funnelforms-pro' ) } ) : ( setCreating( true ) }> { __( 'New form', 'funnelforms-pro' ) } } > { __( 'Pick what the form should do and start from a template, or start from scratch.', 'funnelforms-pro' ) } ) ) : null } { data && data.forms.length > 0 ? (
{ __( 'Form', 'funnelforms-pro' ) } { measured ? ( <> { __( 'Entries', 'funnelforms-pro' ) } { __( 'Views', 'funnelforms-pro' ) } { __( 'Conversion', 'funnelforms-pro' ) } ) : null } { __( 'Actions', 'funnelforms-pro' ) }
    { data.forms.map( ( form ) => ( onOpen( form.id ) } onStats={ null !== form.entries && null !== form.currentRevision ? () => setStatsFor( form ) : undefined } onDuplicate={ () => void act( () => api.duplicate( form.id ), __( 'Copy created as a draft.', 'funnelforms-pro' ) ) } onExport={ () => void act( () => download( form ) ) } onTrash={ () => void act( () => api.remove( form.id ), __( 'Moved to the trash.', 'funnelforms-pro' ) ) } onRestore={ () => void act( () => api.update( form.id, { status: null === form.currentRevision ? 'draft' : 'published' } ), __( 'Restored.', 'funnelforms-pro' ) ) } onDelete={ () => void act( () => api.remove( form.id, true ), __( 'Deleted.', 'funnelforms-pro' ) ) } /> ) ) }
) : null } { data && data.total > perPage ? (
) : null }
{ statsFor ? setStatsFor( null ) } /> : null } { creating ? ( setCreating( false ) } onCreated={ ( id ) => { setCreating( false ); onOpen( id ); } } /> ) : null } ); } /** The time of a form's last change, as people say it. */ function changedAt( form: FormSummary ): string { const when = form.draftUpdatedAt ?? form.updatedAt; return when ? relativeTime( when.replace( ' ', 'T' ) + ( /[zZ+]/.test( when ) ? '' : 'Z' ) ) : ''; } /** * One form. figure: its figures of the last 30 days, null without any, undefined when the user * may not see them. */ function FormRow( props: { form: FormSummary; figure?: FormFigure | null; onOpen: () => void; onStats?: () => void; onDuplicate: () => void; onExport: () => void; onTrash: () => void; onRestore: () => void; onDelete: () => void } ) { const { form, figure } = props; const [ copied, setCopied ] = useState( false ); const trashed = 'trashed' === form.status; const changed = changedAt( form ); const copy = async (): Promise => { try { await navigator.clipboard.writeText( form.shortcode ); setCopied( true ); setTimeout( () => setCopied( false ), 1500 ); } catch { // The shortcode stays visible to copy by hand. } }; const rate = figure && null !== figure.rate ? percent( figure.rate ) : '–'; return (
  • { trashed ? ( { form.title } ) : ( ) }
    { form.category ? { form.category } : null } { null !== form.entries && undefined === figure ? { sprintf( __( '%d entries', 'funnelforms-pro' ), form.entries.toLocaleString() ) } : null } { changed ? { sprintf( __( 'Last change: %s', 'funnelforms-pro' ), changed ) } : null } { form.lockedBy ? { sprintf( __( '%s is editing', 'funnelforms-pro' ), form.lockedBy.name ) } : null }
    { undefined === figure ? null : ( <>
    { figure ? figure.entries.toLocaleString() : '–' } { figure && figure.series.some( ( n ) => n > 0 ) ? : null }
    { figure && figure.measured ? figure.views.toLocaleString() : '–' }
    { rate }
    ) }
    { trashed ? ( <> { __( 'Restore', 'funnelforms-pro' ) } { __( 'Delete permanently', 'funnelforms-pro' ) } ) : ( <> { __( 'Edit', 'funnelforms-pro' ) } { props.onStats ? : null } ) }
  • ); } /** The status of a form in the list, in the look of the plugin's pages. */ function FormStatusPill( { form }: { form: FormSummary } ) { if ( 'trashed' === form.status ) { return { __( 'In the trash', 'funnelforms-pro' ) }; } if ( 'published' === form.status ) { if ( form.hasDraft ) { return { __( 'Published, with changes', 'funnelforms-pro' ) }; } return 'legacy' === form.renderer ? { __( 'Live on Funnelforms 3', 'funnelforms-pro' ) } : { __( 'Published', 'funnelforms-pro' ) }; } return { __( 'Draft', 'funnelforms-pro' ) }; } /** The status of a form, as the editor's top bar shows it. */ export function StatusBadge( { form }: { form: FormSummary } ) { if ( 'trashed' === form.status ) { return { __( 'In the trash', 'funnelforms-pro' ) }; } if ( 'published' === form.status ) { if ( form.hasDraft ) { return { __( 'Published, with changes', 'funnelforms-pro' ) }; } // A form from 3.x the pages still show with Funnelforms 3: changes published here do not show there yet. return 'legacy' === form.renderer ? { __( 'Live on Funnelforms 3', 'funnelforms-pro' ) } : { __( 'Published', 'funnelforms-pro' ) }; } return { __( 'Draft', 'funnelforms-pro' ) }; }