import { useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; import { FeatherAlertTriangle, FeatherCheck, FeatherCheckCircle, FeatherClock, FeatherFlaskConical, FeatherPlay, FeatherRotateCcw, FeatherUndo2, FeatherX, FeatherXCircle, FeatherZap, } from "@subframe/core"; import { BadgeFeedback } from "~/ui/components/BadgeFeedback"; import { Button } from "~/ui/components/Button"; import { Dialog } from "~/ui/components/Dialog"; import { IconButton } from "~/ui/components/IconButton"; import { Tag_Label } from "~/ui/components/Tag_Label"; import { TextInput } from "~/ui/components/TextInput"; import type { AutomationExecutionDetailActionData, AutomationExecutionDetailVM } from "~/services/automation/automation.server"; import { useLocaleFormatters } from "~/locale/format"; import { AutomationStatusBadge, KeyValueBlock, automationStatusLabel, errorMessage, formatDurationMs, localizedValue, nodeIcon, nodeTitle, recordEntries, statusBadgeTone, } from "./automationUi"; /** WF-P12 S03: execution detail with step timeline, run data sidebar and reverse plan. */ export function AutomationExecutionDetailPage({ vm, actionData, submitting, actions, }: { vm: AutomationExecutionDetailVM; actionData?: AutomationExecutionDetailActionData; submitting: boolean; actions: { onReverse: (confirmationPhrase: string) => void; onRetry?: () => void; onResume?: (stepExecutionId: string) => void; }; }): React.JSX.Element { const { t } = useTranslation(); const formatters = useLocaleFormatters(); const [reversePlanOpen, setReversePlanOpen] = useState(false); const [reverseConfirmation, setReverseConfirmation] = useState(""); const succeededSteps = vm.steps.filter((step) => step.status === "success").length; const failedSteps = vm.steps.filter((step) => step.status === "failed").length; // Remaining reverse window, computed once on render (no live ticker). const reverseRemaining = useMemo(() => { if (!vm.execution.canReverse || !vm.execution.reverseWindowExpiresAt) return null; const remainingMs = new Date(vm.execution.reverseWindowExpiresAt).getTime() - Date.now(); if (remainingMs <= 0) return null; return { hours: Math.floor(remainingMs / 3_600_000), minutes: Math.floor((remainingMs % 3_600_000) / 60_000), }; }, [vm.execution.canReverse, vm.execution.reverseWindowExpiresAt]); return (
{t("automation.detail.runFrom", { date: formatters.date(vm.execution.createdAt, { dateStyle: "medium", timeStyle: "short" }) })} {automationStatusLabel(t, vm.execution.status)} {vm.execution.dryRun ? ( } noAction showIcon> {t("automation.executions.kind.dryRun")} ) : null} {vm.canRun && actions.onRetry && !vm.execution.dryRun ? ( ) : null}
{actionData ? ( {t(actionData.ok ? actionData.messageKey : actionData.errorKey, actionData.ok ? { count: actionData.reversedCount } : undefined)} ) : null}
{localizedValue(t, vm.execution.summaryText, "automation.values.unknownSummary")}
{t("automation.detail.trigger")} {nodeTitle(t, vm.execution.triggerKey)}
{t("automation.detail.startedAt")} {vm.execution.startedAt ? formatters.date(vm.execution.startedAt, { dateStyle: "medium", timeStyle: "short" }) : t("automation.values.notAvailable")}
{t("automation.detail.duration")} {formatDurationMs(t, formatters, vm.execution.durationMs)}
{t("automation.sections.steps")} 0 ? "text-body-small-medium-150 font-body-small-medium-150 text-text-feedback-warning" : "text-body-small-medium-150 font-body-small-medium-150 text-text-neutral-primary" } > {t("automation.detail.stepsProgress", { success: formatters.number(succeededSteps), total: formatters.number(vm.steps.length), })}
{vm.execution.errorText ? (
{t("automation.detail.errorTitle")} {errorMessage(t, vm.execution.errorText)}
) : null}
{t("automation.detail.timelineTitle")} {t("automation.detail.stepsCount", { count: vm.steps.length })}
{vm.steps.map((step, index) => { const StepIcon = step.status === "failed" ? FeatherX : step.status === "success" ? FeatherCheck : nodeIcon(step.nodeType); const isLast = index === vm.steps.length - 1; const inputEntries = recordEntries(step.input); const outputEntries = recordEntries(step.output); return (
{!isLast ?
: null}
{t(step.summaryText, { defaultValue: nodeTitle(t, step.nodeType) })} {step.durationMs !== null ? (
{formatDurationMs(t, formatters, step.durationMs)}
) : null} {vm.canRun && actions.onResume && !vm.execution.dryRun && index > 0 ? ( ) : null}
{step.errorText ? (
{t("automation.detail.stepErrorTitle")} {errorMessage(t, step.errorText)}
) : null} {inputEntries.length > 0 || outputEntries.length > 0 ? (
) : null}
); })}
{t("automation.detail.sidebar.title")}
{t("automation.detail.sidebar.result")}
{t("automation.detail.sidebar.stepsSucceeded", { count: succeededSteps })}
{t("automation.detail.sidebar.stepsFailed", { count: failedSteps })}
{t("automation.detail.reverse.windowTitle")}
{t("automation.detail.reverse.windowHint")} {reverseRemaining ? ( <> {t("automation.detail.reverse.remaining", { hours: formatters.number(reverseRemaining.hours), minutes: formatters.number(reverseRemaining.minutes), })} ) : ( {/* A run that was already undone also has canReverse=false - calling that an expired window would be plainly wrong. */} {vm.execution.summaryText === "automation.execution.reversed" ? t("automation.detail.reverse.alreadyReversed") : t("automation.editor.reverse.expired")} )}
{reversePlanOpen ? ( (nextOpen ? undefined : setReversePlanOpen(false))}>
{t("automation.detail.reverse.planTitle")} } aria-label={t("shared.close")} onClick={() => setReversePlanOpen(false)} />
{vm.reversePlan.items.map((item) => (
{localizedValue(t, item.actionKey, "automation.values.unknownAction")} {nodeTitle(t, item.nodeType)}
{item.reversible ? t("automation.editor.reverse.reversible") : t("automation.editor.reverse.notReversible")}
{item.recipients.length > 0 ? (
{t("automation.detail.reverse.recipientsTitle", { count: item.recipients.length })} {item.recipients.map((recipient, index) => ( {recipient.name && recipient.email ? `${recipient.name} ยท ${recipient.email}` : recipient.name ?? recipient.email} ))} {t("automation.detail.reverse.recipientsHint")}
) : null}
))}
setReverseConfirmation(event.target.value)} />
) : null}
); } function SidebarRow({ label, value }: { label: React.ReactNode; value: React.ReactNode }): React.JSX.Element { return (
{label} {value}
); }