import { useEffect, useRef, useState } from 'react'; import { useNavigate, useParams } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { runs } from '../../api'; import type { Scenario, ScenarioRunDetail, ScenarioRunStep, ScenarioRunLog, ScenarioRunStatus, RunStepStatus, LogLevel, } from '../../api'; import { scenarios } from '../../api'; import { AutoRefreshIndicator, Badge, Breadcrumbs, Card, DescriptionList, Notification, Pagination, Search, Table, Timestamp, UuidBadge, type TableColumn, } from '../../ui'; import type { BadgeVariant } from '../../ui'; import { ClipboardList, Activity } from 'lucide-react'; import styles from '../Page.module.css'; const RUN_STATUS_VARIANT: Record = { pending: 'neutral', in_progress: 'info', pass: 'success', fail: 'error', }; const RUN_STATUS_LABEL: Record = { pending: 'Pending', in_progress: 'Running', pass: 'Pass', fail: 'Fail', }; const STEP_STATUS_VARIANT: Record = { waiting: 'neutral', pending: 'neutral', in_progress: 'info', pass: 'success', fail: 'error', cancelled: 'warning', }; const LOG_LEVEL_VARIANT: Record = { log: 'neutral', warn: 'warning', error: 'error', }; const FINAL: ScenarioRunStatus[] = ['pass', 'fail']; export function RunDetailPage() { const { t } = useTranslation(); const { id, runId } = useParams<{ id: string; runId: string }>(); const navigate = useNavigate(); const [scenario, setScenario] = useState(null); const [run, setRun] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [polling, setPolling] = useState(false); const [pulseKey, setPulseKey] = useState(0); const [logPage, setLogPage] = useState(1); const [logSearch, setLogSearch] = useState(''); const [debouncedSearch, setDebouncedSearch] = useState(''); const logSearchRef = useRef(''); const LOG_PAGE_SIZE = 25; const pollRef = useRef | null>(null); const stopPolling = () => { if (pollRef.current) { clearInterval(pollRef.current); pollRef.current = null; setPolling(false); } }; const manualRefresh = () => { if (!id || !runId) return; runs .get(id, runId, logSearchRef.current) .then((r) => { setRun(r); setError(null); setPulseKey((k) => k + 1); }) .catch((err: Error) => setError(err.message)); }; useEffect(() => { const t = setTimeout(() => { setDebouncedSearch(logSearch); logSearchRef.current = logSearch; setLogPage(1); }, 300); return () => clearTimeout(t); }, [logSearch]); useEffect(() => { if (!id || !runId || polling) return; runs .get(id, runId, debouncedSearch) .then(setRun) .catch(() => undefined); }, [id, runId, polling, debouncedSearch]); useEffect(() => { if (!id || !runId) return; let cancelled = false; Promise.all([scenarios.get(id), runs.get(id, runId)]) .then(([sc, r]) => { if (cancelled) return; setScenario(sc); setRun(r); if (!FINAL.includes(r.status)) { setPolling(true); pollRef.current = setInterval(async () => { try { const updated = await runs.get(id, runId, logSearchRef.current); if (cancelled) return; setRun(updated); setPulseKey((k) => k + 1); if (FINAL.includes(updated.status)) stopPolling(); } catch { stopPolling(); } }, 1000); } }) .catch((err: Error) => { if (!cancelled) setError(err.message); }) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; stopPolling(); }; }, [id, runId]); const stepColumns: TableColumn[] = [ { key: 'order', header: t('runs.step_order'), render: (s) => s.order, width: 50 }, { key: 'title', header: t('runs.step_title'), width: 220, render: (s) => s.scenarioStep?.title ?? –, }, { key: 'status', header: t('runs.step_status'), width: 110, render: (s) => ( {s.status.replace('_', ' ')} ), }, { key: 'description', header: t('runs.step_description'), render: (s) => s.description ?? –, }, ]; const logColumns: TableColumn[] = [ { key: 'level', header: t('runs.log_level'), width: 70, render: (l) => {l.level}, }, { key: 'message', header: t('runs.log_message'), render: (l) => l.message }, { key: 'time', header: t('runs.log_time'), width: 160, render: (l) => , }, ]; return (
, onClick: () => navigate('/scenarios') }, { label: scenario?.name ?? `#${id}`, onClick: () => navigate(`/scenarios/${id}`), }, { label: t('runs.title'), icon: , onClick: () => navigate(`/scenarios/${id}/runs`), }, { label: `#${runId}` }, ]} />
{loading &&

{t('runs.loading')}

} {run && ( <> }, { term: t('runs.field_status'), detail: ( {RUN_STATUS_LABEL[run.status]} ), }, { term: t('runs.field_created'), detail: }, { term: t('runs.field_updated'), detail: }, ]} /> {run.stepRuns.length > 0 && (

{t('runs.steps_heading')}

s.id} loading={false} emptyMessage="" /> )} {run.logs.length > 0 && (

{t('runs.logs_heading')}

l.id} loading={false} emptyMessage="" /> {run.logs.length > LOG_PAGE_SIZE && ( )} )} )} ); }