/** * Posts - list, filter, and manage social media posts. */ import { useState, useEffect, useCallback } from '@wordpress/element'; import { __, sprintf } from '@wordpress/i18n'; import { Button, SelectControl, SearchControl } from '@aime/wp-components'; 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'; import { SOCIAL_POST_STATUS, SOCIAL_PLATFORMS } from '../../../utils/constants'; const PER_PAGE = 20; const Posts = ( { onNavigate } ) => { const { get, post, del, loading } = useApi(); const [ posts, setPosts ] = 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 fetchPosts = useCallback( async () => { try { const params = new URLSearchParams( { page, per_page: PER_PAGE, _t: Date.now(), ...( search && { search } ), ...( statusFilter && { status: statusFilter } ), } ); const res = await get( `/social/posts?${ params }` ); setPosts( res.items || [] ); setTotal( res.total || 0 ); } catch ( e ) { toast( e.message, 'error' ); } }, [ get, page, search, statusFilter ] ); useEffect( () => { fetchPosts(); }, [ fetchPosts ] ); const totalPages = Math.ceil( total / PER_PAGE ); const handleDelete = async ( postId ) => { try { await del( `/social/posts/${ postId }` ); toast( __( 'Post deleted.', 'ai-marketing-expert' ) ); setConfirmDelete( null ); fetchPosts(); } catch ( e ) { toast( e.message, 'error' ); } }; const handlePublish = async ( postId ) => { try { await post( `/social/posts/${ postId }/publish` ); toast( __( 'Post published!', 'ai-marketing-expert' ) ); fetchPosts(); } catch ( e ) { toast( e.message, 'error' ); } }; const toggleSelect = ( postId ) => { setSelected( ( prev ) => prev.includes( postId ) ? prev.filter( ( id ) => id !== postId ) : [ ...prev, postId ] ); }; const toggleSelectAll = () => { if ( selected.length === posts.length ) { setSelected( [] ); } else { setSelected( posts.map( ( p ) => p.id ) ); } }; const handleBulkAction = async ( action ) => { if ( selected.length === 0 ) { toast( __( 'No posts selected.', 'ai-marketing-expert' ), 'error' ); return; } try { await post( '/social/posts/bulk', { ids: selected, action } ); toast( action === 'delete' ? __( 'Posts deleted.', 'ai-marketing-expert' ) : __( 'Posts published.', 'ai-marketing-expert' ) ); setSelected( [] ); fetchPosts(); } catch ( e ) { toast( e.message, 'error' ); } }; const statusBadge = ( status ) => { const colors = { draft: { bg: '#E0E0E0', color: '#616161' }, approval_pending: { bg: '#FFF8E1', color: '#8A6D00' }, scheduled: { bg: '#E3F2FD', color: '#1565C0' }, publishing: { bg: '#FFF3E0', color: '#E65100' }, published: { bg: '#E8F5E9', color: '#2E7D32' }, failed: { bg: '#FFEBEE', color: '#C62828' }, }; const s = colors[ status ] || colors.draft; return ( { SOCIAL_POST_STATUS[ status ] || status } ); }; const platformBadge = ( platform ) => { const info = SOCIAL_PLATFORMS[ platform ]; if ( ! info ) return platform; return ( { info.icon } { info.label } ); }; return (

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

{ /* Filters */ }
{ setSearch( v ); setPage( 1 ); } } __nextHasNoMarginBottom />
( { label: v, value: k } ) ), ] } onChange={ ( v ) => { setStatusFilter( v ); setPage( 1 ); } } __nextHasNoMarginBottom />
{ selected.length > 0 && (
) }
{ /* Posts Table */ } { loading ? ( ) : posts.length === 0 ? (

{ __( 'No posts found. Create your first social post!', 'ai-marketing-expert' ) }

) : ( <>
{ posts.map( ( p ) => ( ) ) }
0 } onChange={ toggleSelectAll } /> { __( 'Content', 'ai-marketing-expert' ) } { __( 'Platform', 'ai-marketing-expert' ) } { __( 'Status', 'ai-marketing-expert' ) } { __( 'Date', 'ai-marketing-expert' ) } { __( 'Actions', 'ai-marketing-expert' ) }
toggleSelect( p.id ) } />
{ p.content?.substring( 0, 80 ) || '—' } { p.ai_generated ? ( ✨ ) : null }
{ p.account_name && (
{ p.account_name }
) } { p.status === 'failed' && p.error_message && (
{ p.error_message }
) }
{ platformBadge( p.platform ) } { statusBadge( p.status ) } { p.scheduled_at || p.published_at || p.created_at || '—' }
{ p.status !== 'published' && ( ) } { ( p.status === 'draft' || p.status === 'failed' || p.status === 'approval_pending' ) && ( ) }
{ /* Pagination */ } { totalPages > 1 && (
{ page } / { totalPages }
) } ) } { confirmDelete && ( handleDelete( confirmDelete.id ) } onCancel={ () => setConfirmDelete( null ) } /> ) }
); }; export default Posts;