216 lines
12 KiB
TypeScript
216 lines
12 KiB
TypeScript
import { Form, Link, useActionData, useLoaderData } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
|
|
import { handleAutomationEditorAction, loadAutomationEditorVM, type AutomationEditorActionData, type AutomationEditorVM } from "~/services/automation/automation.server";
|
|
|
|
export async function loader({ request, params }: LoaderFunctionArgs) {
|
|
return Response.json(await loadAutomationEditorVM({ request, params }));
|
|
}
|
|
|
|
export async function action({ request, params }: ActionFunctionArgs) {
|
|
return Response.json(await handleAutomationEditorAction({ request, params }));
|
|
}
|
|
|
|
export default function AutomationEditorRoute() {
|
|
const data = useLoaderData() as AutomationEditorVM;
|
|
const actionData = useActionData() as AutomationEditorActionData | undefined;
|
|
const { t } = useTranslation();
|
|
const lastStepKey = data.flow.steps[data.flow.steps.length - 1]?.stepKey ?? "";
|
|
const firstStepKey = data.flow.steps[0]?.stepKey ?? "";
|
|
const secondStepKey = data.flow.steps[1]?.stepKey ?? "";
|
|
|
|
return (
|
|
<main className="mx-auto flex w-full max-w-7xl flex-col gap-6 p-6">
|
|
<header className="flex flex-col gap-3 md:flex-row md:items-start md:justify-between">
|
|
<div className="flex flex-col gap-2">
|
|
<Link className="text-theme-600 text-sm font-medium" to={data.rule.executionsUrl}>
|
|
{t("automation.editor.backToExecutions")}
|
|
</Link>
|
|
<h1 className="text-foreground text-2xl font-semibold">{data.rule.name}</h1>
|
|
<p className="text-muted-foreground text-sm">{data.rule.description ?? t("automation.editor.noDescription")}</p>
|
|
</div>
|
|
<div className="flex flex-wrap gap-2">
|
|
<Form method="post">
|
|
<button className="rounded-md border px-3 py-2 text-sm font-medium" name="intent" value="auto-layout" type="submit">
|
|
{t("automation.editor.actions.autoLayout")}
|
|
</button>
|
|
</Form>
|
|
<Form method="post">
|
|
<button className="rounded-md border px-3 py-2 text-sm font-medium" name="intent" value="undo" type="submit" disabled={data.flow.history.past.length === 0}>
|
|
{t("automation.editor.actions.undo")}
|
|
</button>
|
|
</Form>
|
|
<Form method="post">
|
|
<button className="rounded-md border px-3 py-2 text-sm font-medium" name="intent" value="redo" type="submit" disabled={data.flow.history.future.length === 0}>
|
|
{t("automation.editor.actions.redo")}
|
|
</button>
|
|
</Form>
|
|
<Form method="post">
|
|
<button className="bg-foreground text-background rounded-md px-3 py-2 text-sm font-medium" name="intent" value="activate" type="submit" disabled={!data.validation.canActivate}>
|
|
{t("automation.editor.actions.activate")}
|
|
</button>
|
|
</Form>
|
|
<Form method="post">
|
|
<button className="rounded-md border px-3 py-2 text-sm font-medium" name="intent" value="pause" type="submit">
|
|
{t("automation.editor.actions.pause")}
|
|
</button>
|
|
</Form>
|
|
</div>
|
|
</header>
|
|
|
|
{actionData ? (
|
|
<section className={`rounded-lg border px-4 py-3 text-sm ${actionData.ok ? "border-emerald-200 bg-emerald-50 text-emerald-900" : "border-red-200 bg-red-50 text-red-900"}`}>
|
|
{t(actionData.ok ? actionData.messageKey : actionData.errorKey)}
|
|
{actionData.ok === false && actionData.detail ? <span className="ml-1">{t(actionData.detail)}</span> : null}
|
|
</section>
|
|
) : null}
|
|
|
|
<section className="grid gap-4 lg:grid-cols-[280px_1fr_320px]">
|
|
<aside className="flex flex-col gap-3 rounded-lg border p-4">
|
|
<h2 className="text-foreground text-sm font-semibold">{t("automation.editor.catalog.title")}</h2>
|
|
<div className="flex flex-col gap-2">
|
|
{data.catalog.map((node) => (
|
|
<Form className="rounded-md border px-3 py-2" key={node.key} method="post">
|
|
<input type="hidden" name="intent" value="add-node" />
|
|
<input type="hidden" name="nodeType" value={node.key} />
|
|
<input type="hidden" name="parentStepKey" value={lastStepKey} />
|
|
<button className="flex w-full flex-col text-left" type="submit">
|
|
<span className="text-foreground text-sm font-medium">{t(node.titleKey)}</span>
|
|
<span className="text-muted-foreground text-xs">{node.group}</span>
|
|
</button>
|
|
</Form>
|
|
))}
|
|
</div>
|
|
</aside>
|
|
|
|
<section className="flex min-h-[520px] flex-col gap-3 rounded-lg border p-4">
|
|
<div className="flex items-center justify-between gap-3">
|
|
<h2 className="text-foreground text-sm font-semibold">{t("automation.editor.graph.title")}</h2>
|
|
<span className="text-muted-foreground text-xs">{t("automation.editor.graph.status", { count: data.flow.steps.length })}</span>
|
|
</div>
|
|
<div className="grid gap-3 md:grid-cols-2">
|
|
{data.flow.steps.map((step) => (
|
|
<article className="rounded-lg border px-4 py-3" key={step.stepKey}>
|
|
<div className="flex items-start justify-between gap-3">
|
|
<div>
|
|
<h3 className="text-foreground text-sm font-semibold">{t(step.summaryKey, { defaultValue: step.nodeType })}</h3>
|
|
<p className="text-muted-foreground text-xs">{step.stepKey}</p>
|
|
</div>
|
|
<span className="text-muted-foreground text-xs">{step.kind}</span>
|
|
</div>
|
|
<dl className="mt-3 grid grid-cols-2 gap-2 text-xs">
|
|
<div>
|
|
<dt className="text-muted-foreground">{t("automation.editor.graph.parents")}</dt>
|
|
<dd className="text-foreground">{step.parentStepIds.join(", ") || t("automation.values.notAvailable")}</dd>
|
|
</div>
|
|
<div>
|
|
<dt className="text-muted-foreground">{t("automation.editor.graph.position")}</dt>
|
|
<dd className="text-foreground">
|
|
{step.position.x}/{step.position.y}
|
|
</dd>
|
|
</div>
|
|
</dl>
|
|
<div className="mt-3 flex flex-wrap gap-2">
|
|
<Form method="post">
|
|
<input type="hidden" name="stepKey" value={step.stepKey} />
|
|
<button className="text-theme-600 text-xs font-medium" name="intent" value="duplicate-node" type="submit">
|
|
{t("automation.editor.actions.duplicate")}
|
|
</button>
|
|
</Form>
|
|
<Form method="post">
|
|
<input type="hidden" name="stepKey" value={step.stepKey} />
|
|
<button className="text-red-600 text-xs font-medium" name="intent" value="delete-node" type="submit">
|
|
{t("automation.editor.actions.delete")}
|
|
</button>
|
|
</Form>
|
|
</div>
|
|
</article>
|
|
))}
|
|
</div>
|
|
</section>
|
|
|
|
<aside className="flex flex-col gap-4">
|
|
<section className="rounded-lg border p-4">
|
|
<h2 className="text-foreground text-sm font-semibold">{t("automation.editor.validation.title")}</h2>
|
|
{data.validation.issues.length === 0 ? (
|
|
<p className="text-muted-foreground mt-2 text-sm">{t("automation.editor.validation.clean")}</p>
|
|
) : (
|
|
<ul className="mt-2 flex flex-col gap-2">
|
|
{data.validation.issues.map((issue, index) => (
|
|
<li className="text-sm text-red-700" key={`${issue.code}-${issue.stepKey ?? "flow"}-${index}`}>
|
|
{t(issue.messageKey)}
|
|
{issue.detail ? <span className="text-muted-foreground ml-1">{issue.detail}</span> : null}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
</section>
|
|
|
|
<section className="rounded-lg border p-4">
|
|
<h2 className="text-foreground text-sm font-semibold">{t("automation.editor.connections.title")}</h2>
|
|
<Form className="mt-3 flex flex-col gap-2" method="post">
|
|
<select className="rounded-md border px-3 py-2 text-sm" name="fromStepKey" defaultValue={firstStepKey}>
|
|
{data.flow.steps.map((step) => (
|
|
<option key={step.stepKey} value={step.stepKey}>
|
|
{step.stepKey}
|
|
</option>
|
|
))}
|
|
</select>
|
|
<select className="rounded-md border px-3 py-2 text-sm" name="toStepKey" defaultValue={secondStepKey}>
|
|
{data.flow.steps.map((step) => (
|
|
<option key={step.stepKey} value={step.stepKey}>
|
|
{step.stepKey}
|
|
</option>
|
|
))}
|
|
</select>
|
|
<div className="flex gap-2">
|
|
<button className="rounded-md border px-3 py-2 text-sm font-medium" name="intent" value="connect" type="submit">
|
|
{t("automation.editor.actions.connect")}
|
|
</button>
|
|
<button className="rounded-md border px-3 py-2 text-sm font-medium" name="intent" value="disconnect" type="submit">
|
|
{t("automation.editor.actions.disconnect")}
|
|
</button>
|
|
</div>
|
|
</Form>
|
|
</section>
|
|
|
|
<section className="rounded-lg border p-4">
|
|
<h2 className="text-foreground text-sm font-semibold">{t("automation.editor.dryRun.title")}</h2>
|
|
<Form className="mt-3 flex flex-col gap-2" method="post">
|
|
<input className="rounded-md border px-3 py-2 text-sm" min="0" name="matchedRecords" type="number" defaultValue="0" />
|
|
<input className="rounded-md border px-3 py-2 text-sm" name="confirmationPhrase" placeholder={t("automation.editor.dryRun.confirmationPhrase")} />
|
|
<button className="rounded-md border px-3 py-2 text-sm font-medium" name="intent" value="dry-run" type="submit">
|
|
{t("automation.editor.actions.dryRun")}
|
|
</button>
|
|
</Form>
|
|
{actionData?.dryRunPreview ? (
|
|
<div className="mt-3 rounded-md bg-muted/40 p-3 text-sm">
|
|
<p className="font-medium">{t("automation.editor.dryRun.records", { count: actionData.dryRunPreview.matchedRecords })}</p>
|
|
<ul className="mt-2 flex flex-col gap-1">
|
|
{actionData.dryRunPreview.steps.map((step) => (
|
|
<li key={step.stepKey}>{t(step.effectKey, { node: step.nodeType })}</li>
|
|
))}
|
|
</ul>
|
|
</div>
|
|
) : null}
|
|
</section>
|
|
|
|
<section className="rounded-lg border p-4">
|
|
<h2 className="text-foreground text-sm font-semibold">{t("automation.editor.templates.title")}</h2>
|
|
<Form className="mt-3 flex flex-col gap-2" method="post">
|
|
<input className="rounded-md border px-3 py-2 text-sm" name="templateName" placeholder={t("automation.editor.templates.namePlaceholder")} />
|
|
<input type="hidden" name="category" value="operations" />
|
|
<button className="rounded-md border px-3 py-2 text-sm font-medium" name="intent" value="create-template" type="submit">
|
|
{t("automation.editor.actions.createTemplate")}
|
|
</button>
|
|
</Form>
|
|
<Link className="text-theme-600 mt-3 block text-sm font-medium" to={data.templatesUrl}>
|
|
{t("automation.editor.templates.openCatalog")}
|
|
</Link>
|
|
</section>
|
|
</aside>
|
|
</section>
|
|
</main>
|
|
);
|
|
}
|