/** * AutomationEditor - visual automation/funnel builder using @xyflow/react. */ import { useState, useEffect, useCallback, useMemo, useRef } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { Button, TextControl, SelectControl, TextareaControl, Spinner, } from '@aime/wp-components'; import { ReactFlow, Background, Controls, MiniMap, Handle, useNodesState, useEdgesState, addEdge, Position, } from '@xyflow/react'; import '@xyflow/react/dist/style.css'; import useApi from '../../../hooks/useApi'; import useSlowWarning from '../../../hooks/useSlowWarning'; import LoadingBtn from '../../common/LoadingBtn'; import AiNotice, { isAiConfigured, aiDisabledTitle } from '../../common/AiNotice'; import Card from '../../common/Card'; import Loader from '../../common/Loader'; import Notice from '../../common/Notice'; import FunnelAnalytics from './FunnelAnalytics'; /* Custom node */ const ICON_MAP = { send_email: '\u2709\uFE0F', add_tag: '\uD83C\uDFF7\uFE0F', remove_tag: '\uD83C\uDFF7\uFE0F', add_to_list: '\uD83D\uDCCB', remove_from_list: '\uD83D\uDCCB', update_contact: '\uD83D\uDC64', webhook: '\uD83C\uDF10', condition: '\u2753', wait: '\u23F3', }; const StepNode = ( { data } ) => (
{ ICON_MAP[ data.actionName ] || '\u26A1' }
{ data.label } { data.actionName }
); const TriggerNode = ( { data } ) => (
{ '\uD83D\uDE80' }
{ __( 'Trigger', 'ai-marketing-expert' ) } { data.triggerKey || '\u2014' }
); const nodeTypes = { step: StepNode, trigger: TriggerNode }; /* Step settings fields by action */ /* Searchable select component */ const SearchableSelect = ( { label, value, options, onChange, placeholder } ) => { const [ search, setSearch ] = useState( '' ); const [ open, setOpen ] = useState( false ); const ref = useRef( null ); const filtered = options.filter( ( o ) => o.label.toLowerCase().includes( search.toLowerCase() ) ); const selected = options.find( ( o ) => String( o.value ) === String( value ) ); /* close on outside click */ useEffect( () => { const handle = ( e ) => { if ( ref.current && ! ref.current.contains( e.target ) ) setOpen( false ); }; document.addEventListener( 'mousedown', handle ); return () => document.removeEventListener( 'mousedown', handle ); }, [] ); return (
{ open && (
setSearch( e.target.value ) } placeholder={ __( 'Type to search...', 'ai-marketing-expert' ) } autoFocus />
    { value && (
  • { onChange( '' ); setOpen( false ); setSearch( '' ); } }> { __( '\u2715 Clear selection', 'ai-marketing-expert' ) }
  • ) } { filtered.length === 0 && (
  • { __( 'No results found', 'ai-marketing-expert' ) }
  • ) } { filtered.map( ( o ) => (
  • { onChange( o.value ); setOpen( false ); setSearch( '' ); } }> { o.label }
  • ) ) }
) }
); }; const StepSettingsFields = ( { actionName, settings, onChange, campaigns, lists, tags } ) => { // Accepts ( key, value ) or a partial object, so a source switch can clear // the other source's fields in one update instead of racing two setState calls. const set = ( key, val ) => onChange( 'object' === typeof key && null !== key ? { ...settings, ...key } : { ...settings, [ key ]: val } ); const campaignOpts = ( campaigns || [] ).map( ( c ) => ( { value: c.id, label: `#${ c.id } \u2014 ${ c.title || c.email_subject || __( 'Untitled', 'ai-marketing-expert' ) }` } ) ); const listOpts = ( lists || [] ).map( ( l ) => ( { value: l.id, label: `#${ l.id } \u2014 ${ l.title }` } ) ); const tagOpts = ( tags || [] ).map( ( t ) => ( { value: t.id, label: `#${ t.id } \u2014 ${ t.title }` } ) ); switch ( actionName ) { case 'send_email': { // Legacy steps have no email_source; infer it from what they stored so // an existing automation keeps sending the same email after upgrade. const emailSource = settings.email_source || ( settings.campaign_id ? 'campaign' : 'plain_text' ); return ( <> { // Clear the other source's fields so only one email can ever be built. if ( 'campaign' === v ) { set( { email_source: v, email_subject: '', email_body: '' } ); } else { set( { email_source: v, campaign_id: '' } ); } } } help={ 'campaign' === emailSource ? __( 'The campaign supplies the subject and body. One email is sent.', 'ai-marketing-expert' ) : __( 'Write the subject and body below. One email is sent.', 'ai-marketing-expert' ) } __nextHasNoMarginBottom /> { 'campaign' === emailSource ? ( set( 'campaign_id', v ) } placeholder={ __( 'Search & select a campaign', 'ai-marketing-expert' ) } /> ) : ( <>
set( 'email_subject', e.target.value ) } placeholder={ __( 'e.g. Welcome to our newsletter!', 'ai-marketing-expert' ) } />
set( 'email_body', v ) } rows={ 8 } __nextHasNoMarginBottom /> ) } ); } case 'add_tag': case 'remove_tag': return tagOpts.length > 0 ? ( set( 'tag_ids', v ? [ v ] : [] ) } placeholder={ __( 'Search & select a tag', 'ai-marketing-expert' ) } /> ) : (
set( 'tag_ids', e.target.value ? [ e.target.value ] : [] ) } placeholder={ __( 'Enter the target tag ID', 'ai-marketing-expert' ) } />
); case 'add_to_list': case 'remove_from_list': return listOpts.length > 0 ? ( set( 'list_ids', v ? [ v ] : [] ) } placeholder={ __( 'Search & select a list', 'ai-marketing-expert' ) } /> ) : (
set( 'list_ids', e.target.value ? [ e.target.value ] : [] ) } placeholder={ __( 'Enter the target list ID', 'ai-marketing-expert' ) } />
); case 'update_contact': return ( <>
{ const field = Object.keys( settings.fields || {} )[ 0 ]; onChange( { fields: field ? { [ field ]: e.target.value } : {} } ); } } placeholder={ __( 'New value for the field', 'ai-marketing-expert' ) } />
); case 'condition': return ( <>
set( 'operator', v ) } __nextHasNoMarginBottom /> set( 'value', e.target.value ) } placeholder={ __( 'Value to compare', 'ai-marketing-expert' ) } /> ); case 'webhook': return ( <>
set( 'url', e.target.value ) } placeholder="https://example.com/webhook" />
); case 'wait': case 'end': return null; default: return (