Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/routes/app.$tenant/automations.templates._index.tsx
T
2026-07-08 16:15:37 +02:00

74 lines
3.7 KiB
TypeScript

import { Form, Link, redirect, useLoaderData } from "react-router";
import { useTranslation } from "react-i18next";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
import { createAutomationRuleFromTemplateForRoute, loadAutomationTemplateCatalogVM, type AutomationTemplateCatalogVM } from "~/services/automation/automation.server";
import { appPath } from "~/utils/app/CanonicalUrlUtils";
export async function loader({ request, params }: LoaderFunctionArgs) {
return Response.json(await loadAutomationTemplateCatalogVM({ request, params }));
}
export async function action({ request, params }: ActionFunctionArgs) {
const formData = await request.formData();
const intent = String(formData.get("intent") ?? "");
if (intent === "use-template") {
const result = await createAutomationRuleFromTemplateForRoute({
request,
params,
templateId: String(formData.get("templateId") ?? ""),
name: String(formData.get("ruleName") ?? "").trim() || undefined,
});
return redirect(result.editUrl);
}
return redirect(appPath({ tenantSlug: params.tenant ?? "", path: "automations/templates", mode: "host-aware" }));
}
export default function AutomationTemplatesRoute() {
const data = useLoaderData() as AutomationTemplateCatalogVM;
const { t } = useTranslation();
return (
<main className="mx-auto flex w-full max-w-6xl flex-col gap-6 p-6">
<header className="flex flex-col gap-2">
<Link className="text-theme-600 text-sm font-medium" to="../">
{t("automation.templatesPage.back")}
</Link>
<h1 className="text-foreground text-2xl font-semibold">{t("automation.templatesPage.title")}</h1>
<p className="text-muted-foreground text-sm">{t("automation.templatesPage.subtitle")}</p>
</header>
<section className="grid gap-4 md:grid-cols-2">
{data.templates.map((template) => (
<article className="rounded-lg border p-4" key={template.id}>
<div className="flex items-start justify-between gap-3">
<div>
<h2 className="text-foreground text-sm font-semibold">{t(template.name, { defaultValue: template.name })}</h2>
<p className="text-muted-foreground mt-1 text-sm">{template.description ? t(template.description, { defaultValue: template.description }) : t("automation.templatesPage.noDescription")}</p>
</div>
<span className="text-muted-foreground text-xs">{template.isTenantTemplate ? t("automation.templatesPage.tenantTemplate") : t("automation.templatesPage.platformTemplate")}</span>
</div>
<dl className="mt-4 grid grid-cols-2 gap-3 text-xs">
<div>
<dt className="text-muted-foreground">{t("automation.templatesPage.category")}</dt>
<dd className="text-foreground">{template.category}</dd>
</div>
<div>
<dt className="text-muted-foreground">{t("automation.templatesPage.useCount")}</dt>
<dd className="text-foreground">{template.useCount}</dd>
</div>
</dl>
<Form className="mt-4 flex flex-col gap-2" method="post">
<input type="hidden" name="intent" value="use-template" />
<input type="hidden" name="templateId" value={template.id} />
<input className="rounded-md border px-3 py-2 text-sm" name="ruleName" placeholder={t("automation.templatesPage.ruleNamePlaceholder")} />
<button className="bg-foreground text-background rounded-md px-3 py-2 text-sm font-medium" type="submit">
{t("automation.templatesPage.useTemplate")}
</button>
</Form>
</article>
))}
</section>
</main>
);
}