import { useMemo } from 'react'; import { useNavigate } from 'react-router'; import { GitBranch, Server } from 'lucide-react'; import { StatusDot, MonoText, Badge } from '@cameleer/design-system'; import { useCorrelationChain } from '../../api/queries/correlation'; import { useAgents } from '../../api/queries/agents'; import type { ExecutionDetail } from '../../components/ExecutionDiagram/types'; import styles from './ExchangeHeader.module.css'; interface ExchangeHeaderProps { detail: ExecutionDetail; onCorrelatedSelect?: (executionId: string, applicationName: string, routeId: string) => void; } type StatusVariant = 'success' | 'error' | 'running' | 'warning'; function statusVariant(s: string): StatusVariant { switch (s) { case 'COMPLETED': return 'success'; case 'FAILED': return 'error'; case 'RUNNING': return 'running'; default: return 'warning'; } } function statusLabel(s: string): string { switch (s) { case 'COMPLETED': return 'OK'; case 'FAILED': return 'ERR'; case 'RUNNING': return 'RUN'; default: return s; } } function formatDuration(ms: number): string { if (ms >= 60_000) return `${(ms / 1000).toFixed(0)}s`; if (ms >= 1000) return `${(ms / 1000).toFixed(2)}s`; return `${ms}ms`; } export function ExchangeHeader({ detail, onCorrelatedSelect }: ExchangeHeaderProps) { const navigate = useNavigate(); const { data: chainResult } = useCorrelationChain(detail.correlationId ?? null); const chain = chainResult?.data; const showChain = chain && chain.length > 1; const attrs = Object.entries(detail.attributes ?? {}); // Look up agent state for icon coloring const { data: agents } = useAgents(undefined, detail.applicationName); const agentState = useMemo(() => { if (!agents || !detail.agentId) return undefined; const agent = (agents as any[]).find((a: any) => a.id === detail.agentId); return agent?.state?.toLowerCase() as 'live' | 'stale' | 'dead' | undefined; }, [agents, detail.agentId]); return (
{/* Exchange info — always shown */}
{attrs.length > 0 && ( <> {attrs.map(([k, v]) => ( ))} )} {detail.agentId && ( <> )} {formatDuration(detail.durationMs)}
{/* Correlation chain */}
Correlated {showChain && (() => { const starts = chain.map((ce: any) => new Date(ce.startTime).getTime()).filter((t: number) => !isNaN(t)); const ends = chain.map((ce: any) => new Date(ce.endTime ?? ce.startTime).getTime() + (ce.durationMs ?? 0)).filter((t: number) => !isNaN(t)); const totalMs = starts.length > 0 && ends.length > 0 ? Math.max(...ends) - Math.min(...starts) : 0; return totalMs > 0 ? Duration: {formatDuration(totalMs)} : null; })()} {showChain ? chain.map((ce: any, i: number) => { const isCurrent = ce.executionId === detail.executionId; const variant = statusVariant(ce.status); const statusCls = variant === 'success' ? styles.chainNodeSuccess : variant === 'error' ? styles.chainNodeError : variant === 'running' ? styles.chainNodeRunning : styles.chainNodeWarning; return ( {i > 0 && } ); }) : ( no correlated exchanges found )}
); }