/**
* Error Log — every workflow run that failed, partly failed, or was skipped,
* newest first, with the step-level reason attached.
*
* The point is that a user never has to guess why a run did nothing: an AI
* timeout, a missing API key, a paused module, or a hit free-plan cap all end
* up here in plain language.
*/
import { useState, useEffect, useCallback } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { apiGet } from '../../../utils/api';
import { toast } from '../../common/Toast';
import Card from '../../common/Card';
import Loader from '../../common/Loader';
import { formatDateTime } from '../../../utils/datetime';
import { Button } from '../../common/WpComponents';
const STATUS_META = {
failed: { label: __( 'Failed', 'ai-marketing-expert' ), color: '#c62828', bg: '#fdecea' },
partial: { label: __( 'Partly failed', 'ai-marketing-expert' ), color: '#ed6c02', bg: '#fff4e5' },
skipped: { label: __( 'Skipped', 'ai-marketing-expert' ), color: '#6c757d', bg: '#f1f2f4' },
};
const TRIGGER_LABELS = {
manual: __( 'Run now', 'ai-marketing-expert' ),
schedule: __( 'Scheduled', 'ai-marketing-expert' ),
event: __( 'Event trigger', 'ai-marketing-expert' ),
};
// Stored as UTC; rendered in the site timezone and format (Settings → General).
const formatDate = ( value ) => formatDateTime( value );
/**
* Turn a raw error into a plain-language hint about what to do next. The
* matching is deliberately loose — provider SDKs word timeouts differently.
*/
const hintFor = ( text ) => {
const t = ( text || '' ).toLowerCase();
if ( t.includes( 'run limit' ) ) {
return __( 'Run limit reached. Runs will resume next cycle.', 'ai-marketing-expert' );
}
if ( t.includes( 'timed out' ) || t.includes( 'timeout' ) || t.includes( 'curl error 28' ) ) {
return __( 'The AI request took too long. Try a shorter article, fewer steps, or a faster model.', 'ai-marketing-expert' );
}
if ( t.includes( 'api key' ) || t.includes( 'unauthorized' ) || t.includes( '401' ) || t.includes( 'invalid_api_key' ) ) {
return __( 'The AI provider rejected the credentials. Check your API key in Settings → AI Connections.', 'ai-marketing-expert' );
}
if ( t.includes( 'rate limit' ) || t.includes( '429' ) || t.includes( 'quota' ) ) {
return __( 'The AI provider is rate limiting or out of quota. Wait and retry, or add a fallback connection.', 'ai-marketing-expert' );
}
if ( t.includes( 'curl' ) || t.includes( 'could not resolve' ) || t.includes( 'connection' ) || t.includes( 'network' ) ) {
return __( 'The site could not reach the AI provider. Check the server network or firewall.', 'ai-marketing-expert' );
}
if ( t.includes( 'no response' ) || t.includes( 'empty response' ) || t.includes( 'invalid result' ) ) {
return __( 'The AI returned nothing usable. Retry, or switch to another model.', 'ai-marketing-expert' );
}
if ( t.includes( 'module' ) && t.includes( 'inactive' ) ) {
return __( 'The module this step writes to is turned off. Enable it under Modules.', 'ai-marketing-expert' );
}
if ( t.includes( 'missing its required' ) || t.includes( 'no topic' ) ) {
return __( 'A step is missing required configuration. Open the workflow and fill it in.', 'ai-marketing-expert' );
}
return '';
};
const ErrorLog = ( { onNavigate } ) => {
const [ loading, setLoading ] = useState( true );
const [ entries, setEntries ] = useState( [] );
const load = useCallback( async () => {
setLoading( true );
try {
const res = await apiGet( '/workflow-automation/logs' );
setEntries( res?.entries || [] );
} catch ( e ) {
toast( e?.message || __( 'Failed to load the error log.', 'ai-marketing-expert' ), 'error' );
} finally {
setLoading( false );
}
}, [] );
useEffect( () => {
load();
}, [ load ] );
// Refresh reuses `load()`, so gate the skeleton on having nothing to show —
// otherwise pressing Refresh tears the log down and rebuilds it.
if ( loading && ! entries.length ) {
return
{ __( 'No failed runs. Every workflow run so far completed successfully.', 'ai-marketing-expert' ) }
{ entry.error }
) } { ( entry.steps || [] ).length > 0 && ({ __( 'What to try:', 'ai-marketing-expert' ) } { topHint }
) }