import React, { useMemo } from 'react'; import { WorkflowStatusProps, WorkflowStatusType } from './WorkflowStatusTypes'; import styles from './WorkflowStatus.module.css'; const _STATUS_MAP: Record = { success: 'completed', completed: 'completed', started: 'started', running: 'started', resumed: 'resumed', stopped: 'stopped', failed: 'failed', error: 'failed', }; const extractWorkflowStatus = (logs: any[]): { status: WorkflowStatusType; round: number | null; timestamp: number } => { if (!logs.length) return { status: null, round: null, timestamp: 0 }; const sorted = [...logs].sort((a, b) => (b.timestamp || 0) - (a.timestamp || 0)); for (const log of sorted) { const logStatus = (log.status || '').toLowerCase(); const mapped = _STATUS_MAP[logStatus]; if (mapped) { const roundMatch = (log.message || '').match(/\(?round\s+(\d+)\)?/i); return { status: mapped, round: roundMatch ? parseInt(roundMatch[1], 10) : null, timestamp: log.timestamp || 0 }; } } return { status: null, round: null, timestamp: 0 }; }; const _formatCurrency = (amount?: number): string => { if (amount === undefined || amount === null) return '-'; return `${amount.toFixed(2)} CHF`; }; const WorkflowStatus: React.FC = ({ className = '', logs = [], workflowStatus: workflowStatusFromApi, currentRound: currentRoundFromApi, isRunning, latestStats }) => { // Use workflow status and round from API response, fallback to extracting from logs const workflowStatus = useMemo(() => { if (workflowStatusFromApi) { const mapped = _STATUS_MAP[workflowStatusFromApi.toLowerCase()] || null; return { status: mapped, round: currentRoundFromApi || null, timestamp: Date.now() / 1000 }; } return extractWorkflowStatus(logs); }, [workflowStatusFromApi, currentRoundFromApi, logs]); // Determine if workflow is running (show spinner) // Show spinner if explicitly running OR if status indicates running state const showSpinner = isRunning === true || workflowStatus.status === 'started' || workflowStatus.status === 'resumed'; // Don't render if no status information and no stats (but always show if spinner should be visible) if (!showSpinner && !workflowStatus.status && workflowStatus.round === null && !latestStats) { return null; } return (
{/* Status and Round Badges */}
{showSpinner && (
)} {workflowStatus.status && ( {workflowStatus.status.charAt(0).toUpperCase() + workflowStatus.status.slice(1)} )} {workflowStatus.round !== null && ( Round {workflowStatus.round} )}
{/* Cost Display */} {latestStats && latestStats.priceCHF !== undefined && (
Cost: {_formatCurrency(latestStats.priceCHF)}
)}
); }; export default WorkflowStatus;