/** * Bot List - browse, create, activate/deactivate, duplicate, delete chatbots. */ import { useState, useEffect, useCallback } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { Button, SearchControl } from '@aime/wp-components'; import { edit, trash, copy, seen } from '@wordpress/icons'; import useApi from '../../../../hooks/useApi'; import Card from '../../../common/Card'; import Loader from '../../../common/Loader'; import Notice from '../../../common/Notice'; import ConfirmModal from '../../../common/ConfirmModal'; import { toast } from '../../../common/Toast'; const BotList = ( { onNavigate } ) => { const { get, post, put, del, loading } = useApi(); const [ bots, setBots ] = useState( [] ); const [ search, setSearch ] = useState( '' ); const [ confirmDelete, setConfirmDelete ] = useState( null ); const fetchBots = useCallback( async () => { try { const res = await get( '/chatbot/bots' ); setBots( Array.isArray( res ) ? res : res.items || res.data || [] ); } catch ( e ) { // silent } }, [ get ] ); useEffect( () => { fetchBots(); }, [ fetchBots ] ); const handleToggleActive = async ( bot ) => { try { const newStatus = bot.status === 'active' ? 'inactive' : 'active'; await put( `/chatbot/bots/${ bot.id }`, { status: newStatus } ); toast( bot.status === 'active' ? __( 'Chatbot deactivated.', 'ai-marketing-expert' ) : __( 'Chatbot activated.', 'ai-marketing-expert' ) ); fetchBots(); } catch ( e ) { toast( e.message, 'error' ); } }; const handleDuplicate = async ( id ) => { try { await post( `/chatbot/bots/${ id }/duplicate` ); toast( __( 'Chatbot duplicated.', 'ai-marketing-expert' ) ); fetchBots(); } catch ( e ) { toast( e.message, 'error' ); } }; const handleDelete = async ( id ) => { try { await del( `/chatbot/bots/${ id }` ); toast( __( 'Chatbot deleted.', 'ai-marketing-expert' ) ); setConfirmDelete( null ); fetchBots(); } catch ( e ) { toast( e.message, 'error' ); } }; const filtered = search ? bots.filter( ( b ) => b.name.toLowerCase().includes( search.toLowerCase() ) ) : bots; return (

{ __( 'Chatbots', 'ai-marketing-expert' ) } ({ bots.length })

{ loading && ! bots.length ? ( ) : filtered.length === 0 ? (

{ bots.length === 0 ? __( 'No chatbots yet. Create your first one!', 'ai-marketing-expert' ) : __( 'No chatbots match your search.', 'ai-marketing-expert' ) }

) : ( { filtered.map( ( bot ) => ( ) ) }
{ __( 'Name', 'ai-marketing-expert' ) } { __( 'Status', 'ai-marketing-expert' ) } { __( 'Conversations', 'ai-marketing-expert' ) } { __( 'Active Now', 'ai-marketing-expert' ) } { __( 'Created', 'ai-marketing-expert' ) } { __( 'Actions', 'ai-marketing-expert' ) }
onNavigate( 'edit-bot', { id: bot.id } ) } > { bot.name } { bot.status === 'active' ? __( 'Active', 'ai-marketing-expert' ) : __( 'Inactive', 'ai-marketing-expert' ) } { bot.conversation_count || 0 } { bot.active_count || 0 } { bot.created_at?.split( ' ' )[ 0 ] }
) }
{ confirmDelete && ( handleDelete( confirmDelete ) } onCancel={ () => setConfirmDelete( null ) } /> ) }
); }; export default BotList;