/** * Library modal — single-purpose popup, no tabs. * * UX contract: * - mode="prompts": browse pre-made prompts, "Use" inserts text via onUse(). * Entries tagged with the current step's action type list first * under "Recommended", rest under "All prompts". * - mode="skills": browse reusable skill blocks, click toggles selection * via onToggleSkill() and keeps modal open for multi-select. * - Search filters current list only. Closing without choosing changes * nothing for prompts; skill toggles apply instantly. */ import { useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import apiFetch from '@wordpress/api-fetch'; import { Modal, SearchControl } from '../../common/WpComponents'; import { toast } from '../../common/Toast'; import { splitPromptsForAction } from './promptLibrary'; import { SKILL_LIBRARY } from './skillLibrary'; /** One selectable prompt card. */ const PromptCard = ( { entry, onUse } ) => ( ); const SkillCard = ( { entry, active, onToggle } ) => ( ); const PromptLibraryModal = ( { open, actionType = '', mode = 'prompts', skills = [], customSkills = [], activeSkillIds = [], onClose, onUse, onToggleSkill, } ) => { const [ query, setQuery ] = useState( '' ); const [ showAddCustom, setShowAddCustom ] = useState( false ); const [ newTitle, setNewTitle ] = useState( '' ); const [ newDesc, setNewDesc ] = useState( '' ); const [ newInstructions, setNewInstructions ] = useState( '' ); const [ savingCustom, setSavingCustom ] = useState( false ); const [ createdSkills, setCreatedSkills ] = useState( [] ); const isSkills = mode === 'skills'; if ( ! open ) { return null; } const isSkillPro = () => false; const handleSkillToggle = ( entry ) => { onToggleSkill && onToggleSkill( entry ); }; const handleCreateCustomSkill = () => { if ( ! newTitle.trim() || ! newInstructions.trim() ) { toast( __( 'Title and instructions are required.', 'ai-marketing-expert' ), 'error' ); return; } setSavingCustom( true ); apiFetch( { path: '/aime/v1/workflow-automation/skills', method: 'POST', data: { title: newTitle.trim(), description: newDesc.trim(), instructions: newInstructions.trim(), }, } ) .then( ( res ) => { setSavingCustom( false ); if ( res?.id ) { const entry = { id: res.id, title: newTitle.trim(), description: newDesc.trim(), instructions: newInstructions.trim(), custom: true, }; setCreatedSkills( ( prev ) => [ ...prev, entry ] ); onToggleSkill && onToggleSkill( entry ); setNewTitle( '' ); setNewDesc( '' ); setNewInstructions( '' ); setShowAddCustom( false ); toast( __( 'Custom skill created and selected!', 'ai-marketing-expert' ), 'success' ); } } ) .catch( ( err ) => { setSavingCustom( false ); toast( err?.message || __( 'Could not create skill.', 'ai-marketing-expert' ), 'error' ); } ); }; const needle = query.trim().toLowerCase(); const matchesPrompt = ( entry ) => ! needle || entry.title.toLowerCase().includes( needle ) || ( entry.text || '' ).toLowerCase().includes( needle ); const matchesSkill = ( entry ) => ! needle || entry.title.toLowerCase().includes( needle ) || ( entry.description || '' ).toLowerCase().includes( needle ); const { recommended, others } = splitPromptsForAction( actionType ); const rec = recommended.filter( matchesPrompt ); const rest = others.filter( matchesPrompt ); const nothingFound = ! rec.length && ! rest.length; const pick = ( entry ) => { onUse( entry.text, entry ); setQuery( '' ); onClose(); }; const allSkills = [ ...SKILL_LIBRARY, ...( skills || [] ), ...( customSkills || [] ), ...createdSkills ]; const seen = new Set(); const dedupedSkills = allSkills.filter( ( s ) => { if ( ! s || ! s.id || seen.has( s.id ) ) { return false; } seen.add( s.id ); return true; } ); const filteredSkills = dedupedSkills.filter( matchesSkill ); return ( { setQuery( '' ); onClose(); } } className="aime-wf-promptlib" overlayClassName="aime-modal-overlay" > { ! isSkills && ( <>

{ __( 'Pick a professionally written starting point, then edit it freely. Skip this dialog to keep writing your own.', 'ai-marketing-expert' ) }

{ !! rec.length && ( <>

{ __( 'Recommended for this step', 'ai-marketing-expert' ) }

{ rec.map( ( entry ) => ( ) ) } ) } { !! rest.length && ( <> { !! rec.length && (

{ __( 'All prompts', 'ai-marketing-expert' ) }

) } { rest.map( ( entry ) => ( ) ) } ) } { nothingFound && (

{ __( 'No prompts match your search.', 'ai-marketing-expert' ) }

) }
) } { isSkills && ( <>

{ __( 'Skills are reusable rule blocks merged into the Brain prompt. Toggle 2–4 per workflow.', 'ai-marketing-expert' ) }

{ showAddCustom && (

{ __( 'Create Custom Brain Skill', 'ai-marketing-expert' ) }

setNewTitle( e.target.value ) } style={ { width: '100%', marginBottom: '8px', padding: '6px 8px', borderRadius: '4px', border: '1px solid #ccc' } } /> setNewDesc( e.target.value ) } style={ { width: '100%', marginBottom: '8px', padding: '6px 8px', borderRadius: '4px', border: '1px solid #ccc' } } />