/**
* Schema-driven config field renderer, extracted from the former
* WorkflowEditor. Renders one field from an action/trigger `fields` schema.
*
* Supported types: text, number, textarea, checkbox, select, tokens
* (FormTokenField chips), range (RangeControl slider), tone (shared tone
* dropdown with Pro gating) and language (shared language dropdown) — the
* latter four matching the Content Generator module's editor UX.
*/
import { __, _n, sprintf } from '@wordpress/i18n';
import { useState, useEffect } from '@wordpress/element';
import apiFetch from '@wordpress/api-fetch';
import {
SelectControl,
CheckboxControl,
TextControl,
TextareaControl,
FormTokenField,
RangeControl,
} from '../../common/WpComponents';
import { toast } from '../../common/Toast';
import { LANGUAGES, PRO_TONES, toneSelectOptions } from '../../../utils/aiOptions';
import PromptLibraryModal from './PromptLibraryModal';
import { SKILL_LIBRARY } from './skillLibrary';
/** Normalize a tokens value: arrays pass through, legacy comma strings split. */
const toTokens = ( value ) => {
if ( Array.isArray( value ) ) {
return value;
}
if ( typeof value === 'string' && value.trim() !== '' ) {
return value.split( ',' ).map( ( t ) => t.trim() ).filter( Boolean );
}
return [];
};
/** Workflow tokens offered as clickable chips under token-capable fields. */
const TOKEN_HINTS = [
'{topic}',
'{workflow_name}',
'{previous_preview}',
'{previous.link}',
'{event.email}',
'{event.first_name}',
'{event.post_title}',
];
const TokenHints = ( { onInsert } ) => (
{ __( 'Tokens:', 'ai-marketing-expert' ) }
{ TOKEN_HINTS.map( ( t ) => (
) ) }
);
/** Render one config field based on its schema descriptor. */
export const ConfigField = ( {
field,
value,
onChange: rawOnChange,
suggestions = [],
actionType = '',
legacyValue,
} ) => {
const locked = false;
const label = field.label || field.key;
const onChange = rawOnChange;
const val = value === undefined || value === null ? ( field.default ?? '' ) : value;
const [ libraryOpen, setLibraryOpen ] = useState( false );
const [ libraryMode, setLibraryMode ] = useState( 'prompts' );
const [ customSkills, setCustomSkills ] = useState( [] );
useEffect( () => {
if ( field.type !== 'skills' ) {
return;
}
apiFetch( { path: '/aime/v1/workflow-automation/skills' } )
.then( ( res ) => {
if ( Array.isArray( res?.skills ) ) {
const customs = res.skills.filter( ( s ) => !! s.custom || ! s.is_builtin );
setCustomSkills( customs );
}
} )
.catch( () => {} );
}, [ field.type ] );
// Skills field: selection lives in the Skill library popup.
// Sidebar shows a compact summary + browse button, no duplicated list.
if ( field.type === 'skills' ) {
const rawList = Array.isArray( val )
? val
: typeof val === 'string' && val.trim() !== ''
? val.split( ',' ).map( ( s ) => s.trim() ).filter( Boolean )
: [];
const isSkillLocked = () => false;
const toggleSkill = ( entry ) => {
const next = rawList.includes( entry.id )
? rawList.filter( ( id ) => id !== entry.id )
: [ ...rawList, entry.id ];
onChange( next );
};
const openSkills = () => {
setLibraryMode( 'skills' );
setLibraryOpen( true );
};
const allKnownSkills = [ ...SKILL_LIBRARY, ...customSkills ];
const selectedNames = rawList.map( ( id ) => {
const found = allKnownSkills.find( ( s ) => s.id === id );
return ( found ? found.title : id );
} );
return (
{ label }
{ field.help &&
{ field.help }
}
{ selectedNames.length
? sprintf(
_n( '%d skill selected: %s', '%d skills selected: %s', selectedNames.length, 'ai-marketing-expert' ),
selectedNames.length,
selectedNames.join( ', ' )
)
: __( 'No skills selected. Open Skill library to add SEO, readability, image rules.', 'ai-marketing-expert' ) }
setLibraryOpen( false ) }
onUse={ () => {} }
onToggleSkill={ toggleSkill }
/>
);
}
// Apply a prompt picked from the library. Empty fields fill directly;
// existing text is never destroyed silently — confirm to replace, or
// cancel to append below what is already there.
const applyPrompt = ( text ) => {
const current = typeof val === 'string' ? val : '';
if ( current.trim() !== '' && ! window.confirm(
__( 'Replace the existing prompt with this one?', 'ai-marketing-expert' )
) ) {
onChange( `${ current.replace( /\s+$/, '' ) }\n\n${ text }` );
return;
}
onChange( text );
};
if ( field.type === 'checkbox' ) {
return (
onChange( c ) }
help={ field.help }
/>
);
}
if ( field.type === 'tokens' ) {
const tokens = toTokens( val );
return (
! tokens.includes( kw ) ) }
onChange={ ( next ) => onChange( next ) }
placeholder={ __( 'Type and press Enter…', 'ai-marketing-expert' ) }
__experimentalExpandOnFocus
__nextHasNoMarginBottom
/>
);
}
if ( field.type === 'range' ) {
// An explicit 0 is a real value for some ranges (e.g. "no upper bound"),
// so it must not fall through to the default.
const num = Number( val );
const resolved = Number.isFinite( num ) && val !== '' ? num : ( Number( field.default ) || 0 );
return (
onChange( Number.isFinite( Number( v ) ) ? Number( v ) : ( field.min ?? 0 ) ) }
min={ field.min ?? 0 }
max={ field.max ?? 100 }
step={ field.step ?? 1 }
withInputField
help={ field.help }
__nextHasNoMarginBottom
/>
);
}
if ( field.type === 'tone' ) {
return (
onChange( v ) }
help={ field.help }
/>
);
}
if ( field.type === 'language' ) {
return (
onChange( v ) }
help={ field.help }
/>
);
}
if ( field.type === 'select' && field.multiple ) {
const options = ( field.options || [] ).map( ( o ) =>
typeof o === 'string' ? { value: o, label: o } : o
);
// Legacy single-select configs: seed the multi from the old key until
// the user changes the selection (which then writes only the new key).
let effective = Array.isArray( val ) ? val : [];
const legacyRaw =
field.legacy_key && ! effective.length ? legacyValue ?? null : null;
if ( legacyRaw !== null && legacyRaw !== '' && legacyRaw !== 0 && legacyRaw !== undefined ) {
effective = [ legacyRaw ];
}
const selectedVals = effective.map( String );
const selectedLabels = selectedVals.map(
( v ) => options.find( ( o ) => String( o.value ) === String( v ) )?.label ?? v
);
return (
<>
! selectedVals.includes( String( o.value ) ) )
.map( ( o ) => o.label ) }
onChange={ ( nextLabels ) => {
onChange(
nextLabels.map( ( l ) => {
const match = options.find( ( o ) => o.label === l );
return match ? match.value : l;
} )
);
} }
placeholder={ __( 'Type to search, Enter to add…', 'ai-marketing-expert' ) }
__experimentalExpandOnFocus
__nextHasNoMarginBottom
/>
{ field.help && { field.help }
}
>
);
}
if ( field.type === 'select' ) {
const options = ( field.options || [] ).map( ( o ) =>
typeof o === 'string' ? { value: o, label: o } : o
);
// Special case: wp_post_id with token like '{*.post_id}' or '-1' -> normalize to -1 ("Previous step")
let currentVal = val;
if (
field.key === 'wp_post_id' &&
( currentVal === -1 ||
currentVal === '-1' ||
( typeof currentVal === 'string' &&
( currentVal.startsWith( '{' ) || currentVal.includes( 'post_id' ) ) ) )
) {
currentVal = -1;
}
// Entity ID selects (funnel_id, account_id, form_id) should never treat arbitrary strings as selected values.
const isEntitySelect =
field.key === 'funnel_id' || field.key === 'account_id' || field.key === 'form_id';
if (
isEntitySelect &&
typeof currentVal === 'string' &&
! /^\d+$/.test( currentVal ) &&
currentVal !== ''
) {
currentVal = 0;
}
const hasZeroOption = options.some( ( o ) => String( o.value ) === '0' );
const knownOption = options.find( ( o ) => String( o.value ) === String( currentVal ) );
const isEmptyish =
currentVal === '' ||
currentVal === undefined ||
currentVal === null ||
( currentVal === 0 && ! hasZeroOption ) ||
( ! knownOption && isEntitySelect );
// Informative placeholder label when no options exist or nothing is chosen.
let placeholderLabel = __( '— Select —', 'ai-marketing-expert' );
if ( options.length === 0 ) {
if ( field.key === 'account_id' ) {
placeholderLabel = __( '— No accounts connected —', 'ai-marketing-expert' );
} else if ( field.key === 'funnel_id' ) {
placeholderLabel = __( '— No funnels created yet —', 'ai-marketing-expert' );
} else {
placeholderLabel = __( '— None available —', 'ai-marketing-expert' );
}
} else if ( ! field.required ) {
placeholderLabel = __( '— None —', 'ai-marketing-expert' );
}
if ( isEmptyish || ! field.required || ! knownOption ) {
if ( ! options.some( ( o ) => o.value === '' ) ) {
options.unshift( { value: '', label: placeholderLabel } );
}
}
// Only retain unknown value in options if it's a valid numeric ID from a saved workflow
if ( ! knownOption && ! isEmptyish && ! isEntitySelect && /^\d+$/.test( String( currentVal ) ) ) {
options.push( {
value: currentVal,
label: sprintf( __( 'Item #%s', 'ai-marketing-expert' ), currentVal ),
} );
}
const effectiveVal = knownOption && ! isEmptyish ? currentVal : '';
return (
onChange( v ) }
help={ field.help }
/>
);
}
// Append a clicked token at the end of the current value.
const insertToken = ( token ) => {
const current = typeof val === 'string' ? val : '';
onChange( current ? `${ current.replace( /\s+$/, '' ) } ${ token }` : token );
};
if ( field.type === 'textarea' && field.prompt_library ) {
return (
{ label }
onChange( v ) }
help={ field.help }
rows={ 6 }
/>
{ field.token_hints && }
setLibraryOpen( false ) }
onUse={ applyPrompt }
/>
);
}
if ( field.type === 'textarea' ) {
return (
<>
onChange( v ) }
help={ field.help }
/>
{ field.token_hints && }
>
);
}
return (
<>
onChange( field.type === 'number' && v !== '' ? Number( v ) : v ) }
help={ field.help }
/>
{ field.token_hints && }
>
);
};
/** Render a full schema fields list against a config object. */
const ConfigFields = ( { fields, config, onChange, keywordSuggestions = [], tagSuggestions = [], parentActionType = '', stepActionType = '' } ) => {
// Filter fields based on visibility rules. Rules come in two shapes:
// - string markers (legacy third-party serialization), e.g. 'not_parent_ai_brain'
// - declarative objects the server cannot evaluate: { type: 'parent_not'|'parent_is', action }
// Static rules (class_exists / module_active) are already resolved
// server-side into a boolean `visible` flag and dropped fields never arrive.
const visibleFields = ( fields || [] ).filter( ( field ) => {
if ( field.visible === false ) {
return false;
}
const rule = field.visible_rule;
if ( ! rule ) {
return true;
}
if ( typeof rule === 'string' ) {
if ( rule === 'not_parent_ai_brain' ) {
return parentActionType !== 'ai_brain';
}
return true;
}
switch ( rule.type ) {
case 'parent_not':
return parentActionType !== rule.action;
case 'parent_is':
return parentActionType === rule.action;
case 'config_is': {
const current = config?.[ rule.field ] ?? '';
return ( rule.values || [] ).includes( current );
}
case 'config_not': {
const current = config?.[ rule.field ] ?? '';
return ! ( rule.values || [] ).includes( current );
}
default:
return true;
}
} );
return (
<>
{ visibleFields.map( ( field ) => (
onChange( field.key, v ) }
suggestions={
field.type === 'tokens'
? ( field.suggest === 'post_tags' ? tagSuggestions : keywordSuggestions )
: []
}
actionType={ stepActionType || parentActionType }
/>
) ) }
>
);
};
export default ConfigFields;