/**
* Workflow History — execution log for a single workflow, with per-run
* step outputs (incl. branch context and artifact deep links) and a
* queued re-run action that polls while the run is in flight.
*/
import { useState, useEffect, useCallback, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { apiGet, apiPost } from '../../../utils/api';
import { toast } from '../../common/Toast';
import Card from '../../common/Card';
import Loader from '../../common/Loader';
import LoadingBtn from '../../common/LoadingBtn';
import { formatDateTime } from '../../../utils/datetime';
import { Button } from '../../common/WpComponents';
// Stored as UTC; rendered in the site timezone and format (Settings → General).
const formatDate = ( value ) => formatDateTime( value );
const STATUS_COLOR = {
success: '#2e7d32',
partial: '#ed6c02',
failed: '#c62828',
running: '#0073aa',
queued: '#787c82',
skipped: '#8fa893',
};
const WorkflowHistory = ( { id, onBack } ) => {
const [ loading, setLoading ] = useState( true );
const [ executions, setExecutions ] = useState( [] );
const [ expanded, setExpanded ] = useState( null );
const [ detail, setDetail ] = useState( {} );
const [ running, setRunning ] = useState( false );
const pollRef = useRef( null );
const load = useCallback( async ( { silent = false } = {} ) => {
if ( ! silent ) {
setLoading( true );
}
try {
const res = await apiGet( `/workflow-automation/workflows/${ id }/history` );
setExecutions( res?.executions || [] );
} catch ( e ) {
if ( ! silent ) {
toast( e?.message || __( 'Failed to load history.', 'ai-marketing-expert' ), 'error' );
}
} finally {
if ( ! silent ) {
setLoading( false );
}
}
}, [ id ] );
useEffect( () => {
load();
}, [ load ] );
// Poll every 3s while the newest execution is queued or running.
useEffect( () => {
const newest = executions[ 0 ];
const inFlight = newest && [ 'queued', 'running' ].includes( newest.status );
setRunning( !! inFlight );
if ( inFlight && ! pollRef.current ) {
pollRef.current = setInterval( () => load( { silent: true } ), 3000 );
}
if ( ! inFlight && pollRef.current ) {
clearInterval( pollRef.current );
pollRef.current = null;
// Refresh the expanded detail once the run settles.
setDetail( {} );
}
return () => {
if ( pollRef.current ) {
clearInterval( pollRef.current );
pollRef.current = null;
}
};
}, [ executions, load ] );
const toggle = async ( execId ) => {
if ( expanded === execId ) {
setExpanded( null );
return;
}
setExpanded( execId );
if ( ! detail[ execId ] ) {
try {
const res = await apiGet( `/workflow-automation/executions/${ execId }` );
setDetail( ( prev ) => ( { ...prev, [ execId ]: res?.execution } ) );
} catch ( e ) {
toast( e?.message || __( 'Failed to load run details.', 'ai-marketing-expert' ), 'error' );
}
}
};
const reRun = async () => {
setRunning( true );
try {
const res = await apiPost( `/workflow-automation/workflows/${ id }/run` );
if ( res?.queued ) {
toast( __( 'Run queued…', 'ai-marketing-expert' ), 'info' );
}
load( { silent: true } );
} catch ( e ) {
toast( e?.message || __( 'Run failed.', 'ai-marketing-expert' ), 'error' );
setRunning( false );
}
};
if ( loading ) {
return
{ __( 'This workflow has not run yet.', 'ai-marketing-expert' ) }
{ exec.error }
) } { expanded === exec.id && (