Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/screens/automation/AutomationExecutionDetailPage.tsx
T

464 lines
25 KiB
TypeScript

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 (
<div className="flex w-full flex-col items-start bg-surface-neutral-primary">
<div className="flex w-full flex-col items-start gap-3 border-b border-solid border-border-default bg-surface-neutral-secondary px-6 py-4 mobile:px-4">
<div className="flex w-full flex-wrap items-center gap-3">
<span className="text-heading- font-heading- text-text-neutral-primary">
{t("automation.detail.runFrom", { date: formatters.date(vm.execution.createdAt, { dateStyle: "medium", timeStyle: "short" }) })}
</span>
<BadgeFeedback farbe={statusBadgeTone(vm.execution.status)}>
{automationStatusLabel(t, vm.execution.status)}
</BadgeFeedback>
{vm.execution.dryRun ? (
<Tag_Label tone="violet-subtle" icon={<FeatherFlaskConical />} noAction showIcon>
{t("automation.executions.kind.dryRun")}
</Tag_Label>
) : null}
{vm.canRun && actions.onRetry && !vm.execution.dryRun ? (
<Button
type="button"
variant="secondary"
size="small"
icon={<FeatherRotateCcw />}
disabled={submitting}
onClick={() => actions.onRetry?.()}
>
{t("automation.detail.retry.button")}
</Button>
) : null}
</div>
{actionData ? (
<BadgeFeedback farbe={actionData.ok ? "subtle-positive" : "subtle-negative"}>
{t(actionData.ok ? actionData.messageKey : actionData.errorKey, actionData.ok ? { count: actionData.reversedCount } : undefined)}
</BadgeFeedback>
) : null}
</div>
<div className="flex w-full items-start mobile:flex-col">
<div className="flex grow shrink-0 basis-0 flex-col items-start gap-6 px-6 py-6 mobile:w-full mobile:flex-none mobile:px-4">
<div className="flex w-full flex-col items-start gap-4 rounded-lg border border-solid border-border-default bg-surface-neutral-secondary px-6 py-5 shadow-sm mobile:px-4">
<div className="flex w-full items-start gap-3">
<div className="flex h-10 w-10 flex-none items-center justify-center rounded-round bg-surface-feedback-subtle-information">
<FeatherZap className="text-heading- font-heading- text-icon-feedback-information" />
</div>
<div className="flex grow shrink-0 basis-0 flex-col items-start gap-0.5">
<span className="text-body-large font-body-large text-text-neutral-primary">
{localizedValue(t, vm.execution.summaryText, "automation.values.unknownSummary")}
</span>
</div>
</div>
<div className="flex w-full flex-wrap items-start gap-x-8 gap-y-4 border-t border-solid border-border-default pt-4">
<div className="flex flex-col items-start gap-1">
<span className="text-body-extra-small-medium-0 font-body-extra-small-medium-0 uppercase tracking-wide text-text-neutral-tertiary">
{t("automation.detail.trigger")}
</span>
<span className="text-body-small-medium-150 font-body-small-medium-150 text-text-neutral-primary">
{nodeTitle(t, vm.execution.triggerKey)}
</span>
</div>
<div className="flex flex-col items-start gap-1">
<span className="text-body-extra-small-medium-0 font-body-extra-small-medium-0 uppercase tracking-wide text-text-neutral-tertiary">
{t("automation.detail.startedAt")}
</span>
<span className="text-body-small-medium-150 font-body-small-medium-150 text-text-neutral-primary">
{vm.execution.startedAt
? formatters.date(vm.execution.startedAt, { dateStyle: "medium", timeStyle: "short" })
: t("automation.values.notAvailable")}
</span>
</div>
<div className="flex flex-col items-start gap-1">
<span className="text-body-extra-small-medium-0 font-body-extra-small-medium-0 uppercase tracking-wide text-text-neutral-tertiary">
{t("automation.detail.duration")}
</span>
<span className="text-body-small-medium-150 font-body-small-medium-150 text-text-neutral-primary">
{formatDurationMs(t, formatters, vm.execution.durationMs)}
</span>
</div>
<div className="flex flex-col items-start gap-1">
<span className="text-body-extra-small-medium-0 font-body-extra-small-medium-0 uppercase tracking-wide text-text-neutral-tertiary">
{t("automation.sections.steps")}
</span>
<span
className={
failedSteps > 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),
})}
</span>
</div>
</div>
</div>
{vm.execution.errorText ? (
<div className="flex w-full items-start gap-3 rounded-md border border-solid border-border-negative-subtle bg-surface-feedback-subtle-negative px-4 py-3">
<FeatherAlertTriangle className="text-body-default-regular-0 font-body-default-regular-0 text-icon-feedback-negative" />
<div className="flex grow shrink-0 basis-0 flex-col items-start gap-0.5">
<span className="text-body-small-semibold-150 font-body-small-semibold-150 text-text-feedback-negative">
{t("automation.detail.errorTitle")}
</span>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">
{errorMessage(t, vm.execution.errorText)}
</span>
</div>
</div>
) : null}
<div className="flex w-full items-center gap-2">
<span className="text-body-default-semibold-150 font-body-default-semibold-150 text-text-neutral-primary">
{t("automation.detail.timelineTitle")}
</span>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">
{t("automation.detail.stepsCount", { count: vm.steps.length })}
</span>
</div>
<div className="flex w-full flex-col items-start">
{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 (
<div key={step.id} className="flex w-full items-start gap-4">
<div className="flex flex-col items-center gap-2 self-stretch">
<div
className={
step.status === "failed"
? "flex h-8 w-8 flex-none items-center justify-center rounded-round bg-surface-feedback-subtle-negative"
: step.status === "success"
? "flex h-8 w-8 flex-none items-center justify-center rounded-round bg-surface-feedback-subtle-positive"
: "flex h-8 w-8 flex-none items-center justify-center rounded-round bg-surface-feedback-subtle-neutral"
}
>
<StepIcon
className={
step.status === "failed"
? "text-body-default-regular-0 font-body-default-regular-0 text-icon-feedback-negative"
: step.status === "success"
? "text-body-default-regular-0 font-body-default-regular-0 text-icon-feedback-positive"
: "text-body-default-regular-0 font-body-default-regular-0 text-icon-neutral-tertiary"
}
/>
</div>
{!isLast ? <div className="flex w-px grow shrink-0 basis-0 flex-col items-center bg-border-default" /> : null}
</div>
<div
className={
step.status === "failed"
? "mb-4 flex grow shrink-0 basis-0 flex-col items-start gap-3 rounded-lg border border-solid border-border-negative-subtle bg-surface-neutral-secondary px-5 py-4 mobile:px-4"
: "mb-4 flex grow shrink-0 basis-0 flex-col items-start gap-3 rounded-lg border border-solid border-border-default bg-surface-neutral-secondary px-5 py-4 mobile:px-4"
}
>
<div className="flex w-full flex-wrap items-center gap-3">
<span className="grow shrink-0 basis-0 text-body-default-medium-150 font-body-default-medium-150 text-text-neutral-primary">
{t(step.summaryText, { defaultValue: nodeTitle(t, step.nodeType) })}
</span>
<AutomationStatusBadge status={step.status} />
{step.durationMs !== null ? (
<div className="flex items-center gap-1.5">
<FeatherClock className="text-body-extra-small-regular-0 font-body-extra-small-regular-0 text-icon-neutral-tertiary" />
<span className="text-body-extra-small-regular-0 font-body-extra-small-regular-0 text-text-neutral-tertiary">
{formatDurationMs(t, formatters, step.durationMs)}
</span>
</div>
) : null}
{vm.canRun && actions.onResume && !vm.execution.dryRun && index > 0 ? (
<Button
type="button"
variant="ghost"
size="small"
icon={<FeatherPlay />}
disabled={submitting}
onClick={() => actions.onResume?.(step.id)}
>
{t("automation.detail.resume.button")}
</Button>
) : null}
</div>
{step.errorText ? (
<div className="flex w-full items-start gap-3 rounded-md border border-solid border-border-negative-subtle bg-surface-feedback-subtle-negative px-4 py-3">
<FeatherAlertTriangle className="text-body-default-regular-0 font-body-default-regular-0 text-icon-feedback-negative" />
<div className="flex grow shrink-0 basis-0 flex-col items-start gap-0.5">
<span className="text-body-small-semibold-150 font-body-small-semibold-150 text-text-feedback-negative">
{t("automation.detail.stepErrorTitle")}
</span>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">
{errorMessage(t, step.errorText)}
</span>
</div>
</div>
) : null}
{inputEntries.length > 0 || outputEntries.length > 0 ? (
<div className="flex w-full items-start gap-4 mobile:flex-col">
<KeyValueBlock label={t("automation.detail.input")} value={step.input} />
<KeyValueBlock label={t("automation.detail.output")} value={step.output} />
</div>
) : null}
</div>
</div>
);
})}
</div>
</div>
<div className="flex w-80 flex-none flex-col items-start gap-6 self-stretch border-l border-solid border-border-default bg-surface-neutral-secondary px-5 py-6 mobile:w-full mobile:self-auto mobile:border-l-0 mobile:border-t">
<div className="flex w-full flex-col items-start gap-4">
<span className="text-body-default-semibold-150 font-body-default-semibold-150 text-text-neutral-primary">
{t("automation.detail.sidebar.title")}
</span>
<div className="flex w-full flex-col items-start gap-3">
<SidebarRow label={t("automation.detail.sidebar.executionId")} value={vm.execution.id} />
<SidebarRow label={t("automation.detail.sidebar.automation")} value={vm.rule.name} />
<SidebarRow
label={t("automation.detail.sidebar.started")}
value={
vm.execution.startedAt
? formatters.date(vm.execution.startedAt, { dateStyle: "medium", timeStyle: "short" })
: t("automation.values.notAvailable")
}
/>
<SidebarRow
label={t("automation.detail.sidebar.ended")}
value={
vm.execution.endedAt
? formatters.date(vm.execution.endedAt, { dateStyle: "medium", timeStyle: "short" })
: t("automation.values.notAvailable")
}
/>
<SidebarRow label={t("automation.detail.sidebar.triggeredBy")} value={nodeTitle(t, vm.execution.triggerKey)} />
</div>
</div>
<div className="flex h-px w-full flex-none flex-col items-center bg-border-default" />
<div className="flex w-full flex-col items-start gap-3">
<span className="text-body-default-semibold-150 font-body-default-semibold-150 text-text-neutral-primary">
{t("automation.detail.sidebar.result")}
</span>
<div className="flex w-full items-center gap-2">
<FeatherCheckCircle className="text-body-default-regular-0 font-body-default-regular-0 text-icon-feedback-positive" />
<span className="grow shrink-0 basis-0 text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">
{t("automation.detail.sidebar.stepsSucceeded", { count: succeededSteps })}
</span>
</div>
<div className="flex w-full items-center gap-2">
<FeatherXCircle className="text-body-default-regular-0 font-body-default-regular-0 text-icon-feedback-negative" />
<span className="grow shrink-0 basis-0 text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">
{t("automation.detail.sidebar.stepsFailed", { count: failedSteps })}
</span>
</div>
</div>
<div className="flex h-px w-full flex-none flex-col items-center bg-border-default" />
<div className="flex w-full flex-col items-start gap-3 rounded-lg border border-solid border-border-default bg-surface-neutral-primary px-4 py-4">
<div className="flex w-full items-center gap-2">
<FeatherClock className="text-body-default-regular-0 font-body-default-regular-0 text-icon-neutral-tertiary" />
<span className="grow shrink-0 basis-0 text-body-small-semibold-150 font-body-small-semibold-150 text-text-neutral-primary">
{t("automation.detail.reverse.windowTitle")}
</span>
</div>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">
{t("automation.detail.reverse.windowHint")}
</span>
{reverseRemaining ? (
<>
<span className="text-body-small-medium-150 font-body-small-medium-150 text-text-feedback-warning">
{t("automation.detail.reverse.remaining", {
hours: formatters.number(reverseRemaining.hours),
minutes: formatters.number(reverseRemaining.minutes),
})}
</span>
<Button
type="button"
className="h-8 w-full flex-none"
variant="secondary"
size="small"
icon={<FeatherUndo2 />}
onClick={() => {
setReverseConfirmation("");
setReversePlanOpen(true);
}}
>
{t("automation.detail.reverse.viewPlan")}
</Button>
</>
) : (
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">
{/* 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")}
</span>
)}
</div>
</div>
</div>
{reversePlanOpen ? (
<Dialog open onOpenChange={(nextOpen: boolean) => (nextOpen ? undefined : setReversePlanOpen(false))}>
<Dialog.Content className="w-[560px] max-w-full gap-0 p-0">
<div className="flex w-full items-center gap-3 border-b border-solid border-border-default px-6 py-4">
<FeatherUndo2 className="text-body-default-regular-0 font-body-default-regular-0 text-icon-neutral-tertiary" />
<span className="grow shrink-0 basis-0 text-body-default-semibold-150 font-body-default-semibold-150 text-text-neutral-primary">
{t("automation.detail.reverse.planTitle")}
</span>
<IconButton variant="ghost" size="small" icon={<FeatherX />} aria-label={t("shared.close")} onClick={() => setReversePlanOpen(false)} />
</div>
<div className="flex w-full flex-col items-start gap-4 px-6 py-5 max-h-[65vh] overflow-y-auto">
<div className="flex w-full flex-col items-start gap-2">
{vm.reversePlan.items.map((item) => (
<div
key={`${item.stepKey}-${item.actionKey}`}
className="flex w-full flex-col gap-2 rounded-md border border-solid border-border-default bg-surface-neutral-primary px-3 py-2"
>
<div className="flex w-full items-center justify-between gap-3">
<div className="flex flex-col items-start">
<span className="text-body-small-medium-150 font-body-small-medium-150 text-text-neutral-primary">
{localizedValue(t, item.actionKey, "automation.values.unknownAction")}
</span>
<span className="text-body-extra-small-regular-0 font-body-extra-small-regular-0 text-text-neutral-tertiary">
{nodeTitle(t, item.nodeType)}
</span>
</div>
<BadgeFeedback farbe={item.reversible ? "subtle-positive" : "subtle-neutral"} size="small">
{item.reversible ? t("automation.editor.reverse.reversible") : t("automation.editor.reverse.notReversible")}
</BadgeFeedback>
</div>
{item.recipients.length > 0 ? (
<div className="flex w-full flex-col gap-1 rounded-md bg-surface-neutral-tertiary px-3 py-2" data-ui-id="ui.automation.execution.reverse.recipients">
<span className="text-body-extra-small-semibold-0 font-body-extra-small-semibold-0 uppercase tracking-wide text-text-neutral-tertiary">
{t("automation.detail.reverse.recipientsTitle", { count: item.recipients.length })}
</span>
{item.recipients.map((recipient, index) => (
<span key={`${recipient.email ?? recipient.name}-${index}`} className="text-body-extra-small-regular-0 font-body-extra-small-regular-0 text-text-neutral-secondary">
{recipient.name && recipient.email ? `${recipient.name} · ${recipient.email}` : recipient.name ?? recipient.email}
</span>
))}
<span className="text-body-extra-small-regular-0 font-body-extra-small-regular-0 text-text-neutral-tertiary">
{t("automation.detail.reverse.recipientsHint")}
</span>
</div>
) : null}
</div>
))}
</div>
<TextInput
label={t("automation.editor.reverse.confirmation")}
helpText={t("automation.detail.reverse.confirmationHint", { phrase: vm.reversePlan.confirmationPhrase })}
showLabel
>
<TextInput.Input value={reverseConfirmation} onChange={(event) => setReverseConfirmation(event.target.value)} />
</TextInput>
</div>
<div className="flex w-full items-center justify-end gap-2 border-t border-solid border-border-default bg-surface-neutral-secondary px-6 py-4">
<Button type="button" variant="tertiary" onClick={() => setReversePlanOpen(false)}>
{t("shared.close")}
</Button>
<Button
type="button"
variant="destructive"
icon={<FeatherUndo2 />}
loading={submitting}
disabled={reverseConfirmation.trim().toLocaleUpperCase() !== vm.reversePlan.confirmationPhrase}
onClick={() => {
actions.onReverse(reverseConfirmation);
setReversePlanOpen(false);
}}
data-ui-id="ui.automation.execution.reverse.confirm"
>
{t("automation.detail.reverse.execute")}
</Button>
</div>
</Dialog.Content>
</Dialog>
) : null}
</div>
);
}
function SidebarRow({ label, value }: { label: React.ReactNode; value: React.ReactNode }): React.JSX.Element {
return (
<div className="flex w-full items-start justify-between gap-3">
<span className="flex-none text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-tertiary">{label}</span>
<span className="break-all text-right text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-primary">{value}</span>
</div>
);
}