/** * Articles list - browse, search, filter, bulk actions. */ import { useState, useEffect, useCallback } from '@wordpress/element'; import { __, sprintf } from '@wordpress/i18n'; import { Button, SelectControl, SearchControl, CheckboxControl, TextControl } from '@aime/wp-components'; import { edit, seen, trash, copy, published } from '@wordpress/icons'; import useApi from '../../../hooks/useApi'; import Card from '../../common/Card'; import Loader from '../../common/Loader'; import ConfirmModal from '../../common/ConfirmModal'; import { toast } from '../../common/Toast'; import { ARTICLE_STATUS, ARTICLE_STATUS_LABELS, ARTICLE_STATUS_COLORS, PER_PAGE } from '../../../utils/constants'; const Articles = ( { onNavigate } ) => { const { get, post, del, loading } = useApi(); const [ articles, setArticles ] = useState( [] ); const [ total, setTotal ] = useState( 0 ); const [ page, setPage ] = useState( 1 ); const [ search, setSearch ] = useState( '' ); const [ statusFilter, setStatusFilter ] = useState( '' ); const [ selected, setSelected ] = useState( [] ); const [ confirmDelete, setConfirmDelete ] = useState( null ); const [ scheduleEdits, setScheduleEdits ] = useState( {} ); const [ savingSchedule, setSavingSchedule ] = useState( 0 ); const toDateTimeLocalValue = ( value ) => ( value ? value.replace( ' ', 'T' ).slice( 0, 16 ) : '' ); const formatDateTime = ( value ) => ( value ? value.replace( 'T', ' ' ).slice( 0, 16 ) : '' ); const getErrorMessage = ( error, fallback ) => error?.message || error?.data?.message || fallback; const canScheduleArticle = ( article ) => [ ARTICLE_STATUS.DRAFT, ARTICLE_STATUS.READY, ARTICLE_STATUS.REVIEW, ARTICLE_STATUS.SCHEDULED, ].includes( article.status ); const fetchArticles = useCallback( async () => { try { const params = { page, per_page: PER_PAGE }; if ( search ) params.search = search; if ( statusFilter ) params.status = statusFilter; const res = await get( '/content/articles', params ); setArticles( res.items || res.data || [] ); setTotal( res.total || 0 ); } catch ( e ) { // silent } }, [ get, page, search, statusFilter ] ); useEffect( () => { fetchArticles(); }, [ fetchArticles ] ); const handleDelete = async ( id ) => { try { await del( `/content/articles/${ id }` ); toast( __( 'Article deleted.', 'ai-marketing-expert' ) ); setConfirmDelete( null ); fetchArticles(); } catch ( e ) { toast( getErrorMessage( e, __( 'Could not delete article. Please try again.', 'ai-marketing-expert' ) ), 'error' ); } }; const handleDuplicate = async ( id ) => { try { await post( `/content/articles/${ id }/duplicate` ); toast( __( 'Article duplicated.', 'ai-marketing-expert' ) ); fetchArticles(); } catch ( e ) { toast( getErrorMessage( e, __( 'Could not duplicate article. Please try again.', 'ai-marketing-expert' ) ), 'error' ); } }; const handlePublish = async ( id ) => { try { await post( `/content/articles/${ id }/publish`, { post_status: 'draft' } ); toast( __( 'Article saved as a WordPress draft.', 'ai-marketing-expert' ) ); fetchArticles(); } catch ( e ) { toast( getErrorMessage( e, __( 'Could not send article to WordPress. Please try again.', 'ai-marketing-expert' ) ), 'error' ); } }; const handleScheduleChange = ( id, value ) => { setScheduleEdits( ( prev ) => ( { ...prev, [ id ]: value } ) ); }; const handleScheduleSave = async ( article ) => { const value = scheduleEdits[ article.id ] ?? toDateTimeLocalValue( article.scheduled_publish_at ); if ( ! value ) { toast( __( 'Select a publish date and time.', 'ai-marketing-expert' ), 'error' ); return; } if ( ! canScheduleArticle( article ) ) { toast( __( 'Published articles cannot be scheduled again. Edit the WordPress post date instead.', 'ai-marketing-expert' ), 'error' ); return; } setSavingSchedule( article.id ); try { await post( `/content/articles/${ article.id }/schedule`, { scheduled_publish_at: value } ); toast( __( 'Schedule updated.', 'ai-marketing-expert' ), 'success' ); fetchArticles(); } catch ( e ) { toast( getErrorMessage( e, __( 'Could not update the schedule. Please try again.', 'ai-marketing-expert' ) ), 'error' ); } finally { setSavingSchedule( 0 ); } }; const handleBulkDelete = async () => { if ( ! selected.length ) return; try { await post( '/content/articles/bulk', { action: 'delete', ids: selected } ); toast( __( 'Articles deleted.', 'ai-marketing-expert' ) ); setSelected( [] ); fetchArticles(); } catch ( e ) { toast( getErrorMessage( e, __( 'Could not delete selected articles. Please try again.', 'ai-marketing-expert' ) ), 'error' ); } }; const toggleSelect = ( id ) => { setSelected( ( prev ) => prev.includes( id ) ? prev.filter( ( s ) => s !== id ) : [ ...prev, id ] ); }; const toggleSelectAll = () => { if ( selected.length === articles.length ) { setSelected( [] ); } else { setSelected( articles.map( ( a ) => a.id ) ); } }; const totalPages = Math.ceil( total / PER_PAGE ); const statusOptions = [ { label: __( 'All Statuses', 'ai-marketing-expert' ), value: '' }, ...Object.entries( ARTICLE_STATUS_LABELS ).map( ( [ val, lab ] ) => ( { label: lab, value: val } ) ), ]; return (

{ __( 'Articles', 'ai-marketing-expert' ) } ({ total })

{ /* Bulk actions */ } { selected.length > 0 && (
{ selected.length } { __( 'selected', 'ai-marketing-expert' ) }
) } { /* Filters - same pattern as Subscribers */ }
{ setSearch( v ); setPage( 1 ); } } placeholder={ __( 'Search articles...', 'ai-marketing-expert' ) } className="aime-search" /> { setStatusFilter( v ); setPage( 1 ); } } __nextHasNoMarginBottom />
{ /* Table */ } { loading && ! articles.length ? ( ) : articles.length === 0 ? (

{ __( 'No articles found. Create one to get started!', 'ai-marketing-expert' ) }

) : ( <> { articles.map( ( article ) => ( ) ) }
0 } onChange={ toggleSelectAll } /> { __( 'Title', 'ai-marketing-expert' ) } { __( 'Status', 'ai-marketing-expert' ) } { __( 'Schedule', 'ai-marketing-expert' ) } { __( 'SEO', 'ai-marketing-expert' ) } { __( 'Created', 'ai-marketing-expert' ) } { __( 'Actions', 'ai-marketing-expert' ) }
toggleSelect( article.id ) } /> onNavigate( 'edit-article', { id: article.id } ) } > { article.title || __( 'Untitled', 'ai-marketing-expert' ) } { ARTICLE_STATUS_LABELS[ article.status ] || article.status } { canScheduleArticle( article ) ? (
handleScheduleChange( article.id, value ) } __nextHasNoMarginBottom />
) : ( { article.published_at ? sprintf( __( 'Published %s', 'ai-marketing-expert' ), formatDateTime( article.published_at ) ) : __( 'Already published', 'ai-marketing-expert' ) } ) }
{ article.seo_score || '\u2014' } { article.created_at?.split( ' ' )[ 0 ] }
{ /* Pagination */ } { totalPages > 1 && (
{ page } / { totalPages } ({ total } { __( 'total', 'ai-marketing-expert' ) })
) } ) }
{ /* Confirm delete modal */ } { confirmDelete && ( handleDelete( confirmDelete ) } onCancel={ () => setConfirmDelete( null ) } /> ) }
); }; export default Articles;