/** * What can be added: steps by field type, contact steps and ends. A click adds after the * selected node (or on the default path); dragging places it on the canvas or drops a field * type into a node. */ import React, { useState } from 'react'; import { addContact, addEnd, addField, addStep, defaultPlacement, findNode, CONTACT_PALETTE, STEP_FIELD_TYPES, type Placement, type StepKind } from '../document'; import { isPro } from '../edition'; import type { Selection } from '../store'; import { DRAG_TYPE, type PaletteItem } from './Canvas'; import { contactFieldName, stepFieldName } from './catalog'; import { useBuilder, useEnv } from './context'; import { __, sprintf } from './i18n'; import { QuestionList } from './Questions'; import { TypeIcon } from './typeIcons'; import { ProBadge } from './ui'; /** The step types by what visitors do: pick, type in, or the rest. */ const GROUPS: Array<{ id: 'pick' | 'input' | 'more'; title: () => string; types: StepKind[] }> = [ { id: 'pick', title: () => __( 'Pick answers', 'funnelforms-pro' ), types: [ 'choice', 'dropdown', 'rating', 'slider' ] }, { id: 'input', title: () => __( 'Type in', 'funnelforms-pro' ), types: [ 'text', 'textarea', 'email', 'phone', 'url', 'number', 'date', 'time', 'address' ] }, { id: 'more', title: () => __( 'Content and more', 'funnelforms-pro' ), types: [ 'content', 'loading', 'result', 'file', 'signature', 'repeat', 'booking' ] }, ]; function Tile( { item, label, icon, tone, onAdd, disabled }: { item: PaletteItem; label: string; icon: string; tone: string; onAdd: () => void; disabled: boolean } ) { return ( ); } /** A field type of Funnelforms Pro in the palette. */ export function ProTile( { label, icon, tone }: { label: string; icon: string; tone: string } ) { return ( { label } ); } export function Palette() { const { store, ctx, readOnly } = useEnv(); const selection = useBuilder( ( s ) => s.selection ); const selectedNode = useBuilder( ( s ) => ( s.doc && s.selection.node ? findNode( s.doc, s.selection.node ) : undefined ) ); const { change, select } = store.getState(); const [ query, setQuery ] = useState( '' ); const [ tab, setTab ] = useState<'add' | 'questions'>( 'add' ); const placement = ( doc: Parameters[ 0 ] ): Placement => { const node = selection.node ? findNode( doc, selection.node ) : undefined; return node && 'end' !== node.type ? { after: node.id } : defaultPlacement( doc ); }; const add = ( item: PaletteItem ): void => { let created: Selection = {}; change( ( doc ) => { if ( 'step' === item.kind ) { created = { node: addStep( doc, item.type, ctx, placement( doc ) ) }; } else if ( 'contact' === item.kind ) { created = { node: addContact( doc, ctx, placement( doc ) ) }; } else if ( 'end' === item.kind ) { const node = selection.node ? findNode( doc, selection.node ) : undefined; created = { node: addEnd( doc, ctx, node && 'end' !== node.type ? { after: node.id } : {} ) }; } else if ( selection.node ) { const field = addField( doc, selection.node, item.type, ctx ); created = field ? { node: selection.node, field } : selection; } } ); select( created ); }; const fieldTarget = selectedNode && 'end' !== selectedNode.type ? selectedNode : undefined; const needle = query.trim().toLowerCase(); const matches = ( label: string ): boolean => '' === needle || label.toLowerCase().includes( needle ); const tabs = (
); if ( 'questions' === tab ) { return ( ); } const fieldTypes = fieldTarget ? ( 'contact' === fieldTarget.type ? CONTACT_PALETTE : STEP_FIELD_TYPES ).map( ( type ) => ( { type: type as string, label: 'contact' === fieldTarget.type ? contactFieldName( type ) : stepFieldName( type ) } ) ).filter( ( t ) => matches( t.label ) ) : []; const groups = GROUPS.map( ( g ) => ( { ...g, types: g.types.filter( ( type ) => matches( stepFieldName( type ) ) ) } ) ).filter( ( g ) => g.types.length > 0 ); const finish = [ { item: { kind: 'contact' } as PaletteItem, label: __( 'Contact step', 'funnelforms-pro' ), icon: 'contact' }, { item: { kind: 'end' } as PaletteItem, label: __( 'End', 'funnelforms-pro' ), icon: 'end' }, ].filter( ( f ) => matches( f.label ) ); return ( ); }