/** * Automations - list view for funnels/automations. */ import { useState, useEffect, useCallback, useRef } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { Button, SearchControl, SelectControl } from '@aime/wp-components'; import useApi from '../../../hooks/useApi'; import useEmailUsage from '../../../hooks/useEmailUsage'; import Card from '../../common/Card'; import Loader from '../../common/Loader'; import Notice from '../../common/Notice'; import { menuUrl } from '../../../utils/menuUrl'; import { formatDate } from '../../../utils/datetime'; const STATUS_OPTIONS = [ { label: __( 'All Statuses', 'ai-marketing-expert' ), value: '' }, { label: __( 'Draft', 'ai-marketing-expert' ), value: 'draft' }, { label: __( 'Published', 'ai-marketing-expert' ), value: 'published' }, ]; const BADGE = { draft: 'warning', published: 'success' }; const TRIGGER_LABELS = { subscriber_created: __( 'New Subscriber', 'ai-marketing-expert' ), subscriber_updated: __( 'Subscriber Updated', 'ai-marketing-expert' ), tag_added: __( 'Tag Added', 'ai-marketing-expert' ), tag_removed: __( 'Tag Removed', 'ai-marketing-expert' ), list_subscribed: __( 'List Subscribed', 'ai-marketing-expert' ), list_unsubscribed: __( 'List Unsubscribed', 'ai-marketing-expert' ), form_submitted: __( 'Form Submitted', 'ai-marketing-expert' ), order_created: __( 'Order Placed', 'ai-marketing-expert' ), order_completed: __( 'Order Completed', 'ai-marketing-expert' ), cart_abandoned: __( 'Cart Abandoned', 'ai-marketing-expert' ), b2b_lead_captured: __( 'B2B Lead Captured', 'ai-marketing-expert' ), lead_captured: __( 'Lead Captured', 'ai-marketing-expert' ), webhook_event: __( 'Webhook Event', 'ai-marketing-expert' ), }; const formatTriggerName = ( trigger ) => { if ( ! trigger ) return __( 'Manual / Custom', 'ai-marketing-expert' ); if ( TRIGGER_LABELS[ trigger ] ) return TRIGGER_LABELS[ trigger ]; return trigger.replace( /_/g, ' ' ).replace( /\b\w/g, ( c ) => c.toUpperCase() ); }; const DEFAULT_COLUMNS = { trigger: false, // Embedded cleanly under Title steps: false, // Embedded cleanly under Title performance: true, // Consolidated performance enrolled: false, active: false, completed: false, completion: false, updated: true, }; const COLUMN_DEFINITIONS = [ { key: 'title', label: __( 'Title & Trigger', 'ai-marketing-expert' ), required: true }, { key: 'status', label: __( 'Status', 'ai-marketing-expert' ), required: true }, { key: 'performance', label: __( 'Performance (Enrolled & Progress)', 'ai-marketing-expert' ) }, { key: 'trigger', label: __( 'Separate Trigger Column', 'ai-marketing-expert' ) }, { key: 'steps', label: __( 'Steps Column', 'ai-marketing-expert' ) }, { key: 'enrolled', label: __( 'Enrolled Count', 'ai-marketing-expert' ) }, { key: 'active', label: __( 'Active in Funnel', 'ai-marketing-expert' ) }, { key: 'completed', label: __( 'Completed Count', 'ai-marketing-expert' ) }, { key: 'completion', label: __( 'Completion Bar', 'ai-marketing-expert' ) }, { key: 'updated', label: __( 'Last Updated', 'ai-marketing-expert' ) }, { key: 'actions', label: __( 'Actions', 'ai-marketing-expert' ), required: true }, ]; const Automations = ( { onNavigate } ) => { const { get, post, put, del, loading, error, clearError } = useApi(); const { usage, refresh: refreshUsage } = useEmailUsage(); const [ automations, setAutomations ] = useState( [] ); const [ search, setSearch ] = useState( '' ); const [ status, setStatus ] = useState( '' ); const [ page, setPage ] = useState( 1 ); const [ total, setTotal ] = useState( 0 ); const [ overview, setOverview ] = useState( null ); const [ cronStatus, setCronStatus ] = useState( null ); // Column customizer state const [ showColMenu, setShowColMenu ] = useState( false ); const colMenuRef = useRef( null ); const [ visibleCols, setVisibleCols ] = useState( () => { try { const saved = localStorage.getItem( 'aime_automations_table_cols_v2' ); if ( saved ) { return { ...DEFAULT_COLUMNS, ...JSON.parse( saved ) }; } } catch ( e ) { /* fallback */ } return DEFAULT_COLUMNS; } ); const toggleColumn = ( colKey ) => { setVisibleCols( ( prev ) => { const updated = { ...prev, [ colKey ]: ! prev[ colKey ] }; try { localStorage.setItem( 'aime_automations_table_cols_v2', JSON.stringify( updated ) ); } catch ( e ) { /* */ } return updated; } ); }; const resetColumnsToDefault = () => { setVisibleCols( DEFAULT_COLUMNS ); try { localStorage.removeItem( 'aime_automations_table_cols_v2' ); } catch ( e ) { /* */ } }; useEffect( () => { const handleClickOutside = ( event ) => { if ( colMenuRef.current && ! colMenuRef.current.contains( event.target ) ) { setShowColMenu( false ); } }; if ( showColMenu ) { document.addEventListener( 'mousedown', handleClickOutside ); } return () => { document.removeEventListener( 'mousedown', handleClickOutside ); }; }, [ showColMenu ] ); const fetchList = useCallback( async () => { const params = new URLSearchParams( { page, per_page: 20 } ); if ( search ) params.set( 'search', search ); if ( status ) params.set( 'status', status ); try { const res = await get( `/email/automations?${ params }` ); setAutomations( res.items || [] ); setTotal( res.total || 0 ); if ( res.overview ) { setOverview( res.overview ); } } catch ( e ) { /* */ } }, [ get, page, search, status ] ); useEffect( () => { fetchList(); }, [ fetchList ] ); useEffect( () => { const fetchCronStatus = async () => { try { const res = await get( '/system/cron-status' ); setCronStatus( res || null ); } catch ( e ) { /* Keep automations usable if status check fails. */ } }; fetchCronStatus(); }, [ get ] ); const handleDuplicate = async ( aid ) => { try { await post( `/email/automations/${ aid }/duplicate` ); fetchList(); refreshUsage(); } catch ( e ) { /* */ } }; const handleDelete = async ( aid ) => { if ( ! window.confirm( __( 'Delete this automation?', 'ai-marketing-expert' ) ) ) return; try { await del( `/email/automations/${ aid }` ); fetchList(); refreshUsage(); } catch ( e ) { /* */ } }; const handleToggleStatus = async ( a ) => { const newStatus = a.status === 'published' ? 'draft' : 'published'; try { await put( `/email/automations/${ a.id }`, { status: newStatus } ); fetchList(); } catch ( e ) { /* */ } }; const totalPages = Math.ceil( total / 20 ); return ( onNavigate( 'automation-editor' ) }> { __( '+ New Automation', 'ai-marketing-expert' ) } } > { error && } { cronStatus?.has_overdue && ( { __( 'Automations may run late because WordPress cron is overdue or missing.', 'ai-marketing-expert' ) }{ ' ' } { __( 'View System Status', 'ai-marketing-expert' ) } } dismissible={ false } /> ) }
⚡
{ overview?.total_automations ?? total }
{ __( 'Total Automations', 'ai-marketing-expert' ) }
👥
{ overview?.total_enrolled ?? 0 }
{ __( 'Total Enrolled', 'ai-marketing-expert' ) }
⏳
{ overview?.total_active ?? 0 }
{ __( 'Active in Funnel', 'ai-marketing-expert' ) }
🎯
{ overview?.total_completed ?? 0 }
{ __( 'Completed Funnels', 'ai-marketing-expert' ) }
{ setSearch( v ); setPage( 1 ); } } placeholder={ __( 'Search automations...', 'ai-marketing-expert' ) } /> { setStatus( v ); setPage( 1 ); } } __nextHasNoMarginBottom />
{ /* Column Settings Dropdown */ }
{ showColMenu && (
{ __( 'Customize Columns', 'ai-marketing-expert' ) }

{ __( 'Select fields to display in this table:', 'ai-marketing-expert' ) }

{ COLUMN_DEFINITIONS.map( ( col ) => { const isChecked = col.required ? true : !! visibleCols[ col.key ]; return ( ); } ) }
{ __( 'Auto-saved', 'ai-marketing-expert' ) }
) }
{ loading && } { ! loading && automations.length === 0 && (

{ __( 'No automations found. Create your first automation!', 'ai-marketing-expert' ) }

) } { ! loading && automations.length > 0 && (
{ visibleCols.trigger && } { visibleCols.steps && } { visibleCols.performance && } { visibleCols.enrolled && } { visibleCols.active && } { visibleCols.completed && } { visibleCols.completion && } { visibleCols.updated && } { automations.map( ( a ) => ( { visibleCols.trigger && ( ) } { visibleCols.steps && ( ) } { visibleCols.performance && ( ) } { visibleCols.enrolled && ( ) } { visibleCols.active && ( ) } { visibleCols.completed && ( ) } { visibleCols.completion && ( ) } { visibleCols.updated && ( ) } ) ) }
{ __( 'Automation', 'ai-marketing-expert' ) }{ __( 'Trigger', 'ai-marketing-expert' ) }{ __( 'Status', 'ai-marketing-expert' ) }{ __( 'Steps', 'ai-marketing-expert' ) }{ __( 'Performance', 'ai-marketing-expert' ) }{ __( 'Enrolled', 'ai-marketing-expert' ) }{ __( 'Active', 'ai-marketing-expert' ) }{ __( 'Completed', 'ai-marketing-expert' ) }{ __( 'Completion', 'ai-marketing-expert' ) }{ __( 'Updated', 'ai-marketing-expert' ) }{ __( 'Actions', 'ai-marketing-expert' ) }
{ formatTriggerName( a.trigger_name ) } { a.sequences_count !== undefined && a.sequences_count !== null && ( • { a.sequences_count } { a.sequences_count === 1 ? __( 'step', 'ai-marketing-expert' ) : __( 'steps', 'ai-marketing-expert' ) } ) }
{ formatTriggerName( a.trigger_name ) } { a.status } { a.sequences_count ?? '\u2014' }
{ a.enrolled_count ?? a.subscribers_count ?? 0 } { __( 'enrolled', 'ai-marketing-expert' ) } { a.completion_rate ?? 0 }%
{ a.active_count ?? 0 } { __( 'active', 'ai-marketing-expert' ) } • { a.completed_count ?? 0 } { __( 'completed', 'ai-marketing-expert' ) }
{ a.enrolled_count ?? a.subscribers_count ?? 0 }{ a.active_count ?? 0 }{ a.completed_count ?? 0 }
{ a.completion_rate ?? 0 }%
{ formatDate( a.updated_at, '\u2014' ) }
) } { totalPages > 1 && (
{ page } / { totalPages }
) }
); }; export default Automations;