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

191 lines
10 KiB
TypeScript

import { useMemo, useState } from "react";
import { Form as RouterForm } from "react-router";
import { useTranslation } from "react-i18next";
import {
FeatherArrowRight,
FeatherInfo,
FeatherLayoutGrid,
FeatherLayoutTemplate,
FeatherTrendingUp,
FeatherUser,
} from "@subframe/core";
import { Button } from "~/ui/components/Button";
import { EmptyState } from "~/ui/components/EmptyState";
import { IconWithBackground } from "~/ui/components/IconWithBackground";
import { SearchInput } from "~/ui/components/SearchInput";
import { Tag_Label } from "~/ui/components/Tag_Label";
import type { AutomationTemplateCatalogVM } from "~/services/automation/automation.server";
import { useLocaleFormatters } from "~/locale/format";
import { ChainChips, FilterPill, PageHeader, localizedValue, nodeIcon } from "./automationUi";
type TemplateTab = "all" | "shk" | "own";
/** WF-P12 S04: template catalog with tabs, category pills and use-template forms. */
export function AutomationTemplatesPage({ vm }: { vm: AutomationTemplateCatalogVM }): React.JSX.Element {
const { t } = useTranslation();
const formatters = useLocaleFormatters();
const [tab, setTab] = useState<TemplateTab>("all");
const [categoryFilter, setCategoryFilter] = useState<string | null>(null);
const [query, setQuery] = useState("");
const counts = useMemo(
() => ({
all: vm.templates.length,
shk: vm.templates.filter((template) => !template.isTenantTemplate).length,
own: vm.templates.filter((template) => template.isTenantTemplate).length,
}),
[vm.templates]
);
const categories = useMemo(() => [...new Set(vm.templates.map((template) => template.category))].sort(), [vm.templates]);
const visibleTemplates = useMemo(() => {
const needle = query.trim().toLowerCase();
return vm.templates.filter((template) => {
if (tab === "shk" && template.isTenantTemplate) return false;
if (tab === "own" && !template.isTenantTemplate) return false;
if (categoryFilter && template.category !== categoryFilter) return false;
if (needle) {
const haystack = `${localizedValue(t, template.name, "automation.values.unknownTemplate")} ${
template.description ? localizedValue(t, template.description, "automation.values.unknownTemplateDescription") : ""
}`.toLowerCase();
if (!haystack.includes(needle)) return false;
}
return true;
});
}, [vm.templates, tab, categoryFilter, query, t]);
return (
<div className="flex w-full flex-col items-start bg-surface-neutral-primary">
<PageHeader
icon={<FeatherLayoutTemplate className="text-heading- font-heading- text-icon-neutral-tertiary" />}
title={t("automation.templatesPage.title")}
subtitle={t("automation.templatesPage.subtitle")}
/>
<div className="flex w-full items-center gap-3 border-b border-solid border-border-default bg-surface-neutral-secondary px-6 py-3 mobile:flex-col mobile:items-stretch mobile:px-4">
<SearchInput
className="h-8 w-72 flex-none mobile:w-full"
size="small"
hideShortcut
placeholder={t("automation.templatesPage.searchPlaceholder")}
value={query}
onChange={(event: React.ChangeEvent<HTMLInputElement>) => setQuery(event.target.value)}
/>
<div className="flex flex-wrap items-center gap-2">
<FilterPill label={t("automation.templatesPage.tabs.all")} count={counts.all} active={tab === "all"} onClick={() => setTab("all")} />
<FilterPill
label={t("automation.templatesPage.tabs.shk")}
count={counts.shk}
active={tab === "shk"}
onClick={() => setTab(tab === "shk" ? "all" : "shk")}
/>
<FilterPill
label={t("automation.templatesPage.tabs.own")}
count={counts.own}
active={tab === "own"}
onClick={() => setTab(tab === "own" ? "all" : "own")}
/>
</div>
{categories.length > 0 ? (
<div className="flex flex-wrap items-center gap-2">
<FeatherLayoutGrid className="text-body-small-regular-0 font-body-small-regular-0 text-icon-neutral-tertiary" />
<FilterPill label={t("automation.templatesPage.categoriesAll")} active={categoryFilter === null} onClick={() => setCategoryFilter(null)} />
{categories.map((category) => (
<FilterPill
key={category}
label={localizedValue(t, `automation.templatesPage.categories.${category}`, "automation.values.unknownCategory")}
active={categoryFilter === category}
onClick={() => setCategoryFilter(categoryFilter === category ? null : category)}
/>
))}
</div>
) : null}
</div>
<div className="flex w-full flex-col items-start gap-4 px-6 py-6 mobile:px-4">
{vm.templates.length === 0 ? (
<EmptyState outlined title={t("automation.templatesPage.empty.title")} body={t("automation.templatesPage.empty.body")} />
) : visibleTemplates.length === 0 ? (
<EmptyState outlined variant="no-results" title={t("automation.templatesPage.noMatches.title")} body={t("automation.templatesPage.noMatches.body")} />
) : (
<div className="grid w-full grid-cols-2 items-start gap-4 mobile:grid-cols-1">
{visibleTemplates.map((template) => {
const TemplateIcon = nodeIcon(template.chain[0]?.nodeType ?? template.category);
return (
<article
key={template.id}
className="flex flex-col items-start gap-3 self-stretch rounded-md border border-solid border-border-default bg-surface-neutral-secondary px-4 py-4 shadow-sm hover:border-border-strong"
>
<div className="flex w-full items-start gap-3">
<IconWithBackground variant={template.isTenantTemplate ? "neutral" : "information"} icon={<TemplateIcon />} />
<div className="flex grow shrink-0 basis-0 flex-col items-start gap-0.5">
<span className="w-full text-body-default-semibold-150 font-body-default-semibold-150 text-text-neutral-primary">
{localizedValue(t, template.name, "automation.values.unknownTemplate")}
</span>
<span className="line-clamp-2 w-full text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">
{template.description
? localizedValue(t, template.description, "automation.values.unknownTemplateDescription")
: t("automation.templatesPage.noDescription")}
</span>
</div>
<Tag_Label tone={template.isTenantTemplate ? "neutral-subtle" : "sky-subtle"} noAction size="small">
{template.isTenantTemplate ? t("automation.templatesPage.tenantTemplate") : t("automation.templatesPage.platformTemplate")}
</Tag_Label>
</div>
<ChainChips chain={template.chain} />
<div className="flex h-px w-full flex-none flex-col items-center bg-border-default" />
{/* There is no rule -> template back-reference in the schema, so a
usage count of 0 is "not measurable", not "never used". Showing
it anyway made every catalog template read as unused. */}
{template.useCount > 0 ? (
<div className="flex w-full items-center gap-2">
<FeatherTrendingUp className="text-body-small-regular-0 font-body-small-regular-0 text-icon-neutral-tertiary" />
<span className="grow shrink-0 basis-0 text-body-extra-small-regular-0 font-body-extra-small-regular-0 text-text-neutral-tertiary">
{t("automation.templatesPage.usedTimes", { count: template.useCount, formattedCount: formatters.number(template.useCount) })}
</span>
</div>
) : null}
{vm.canManage ? (
<RouterForm method="post" className="flex w-full items-center gap-2 mobile:flex-col mobile:items-stretch">
<input type="hidden" name="intent" value="use-template" />
<input type="hidden" name="templateId" value={template.id} />
<input
className="h-8 grow rounded-sm border border-solid border-border-strong bg-surface-neutral-primary px-3 text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-primary placeholder:text-text-neutral-subtle"
name="ruleName"
placeholder={t("automation.templatesPage.ruleNamePlaceholder")}
aria-label={t("automation.templatesPage.ruleNamePlaceholder")}
/>
<Button type="submit" variant="secondary" size="small" iconRight={<FeatherArrowRight />}>
{t("automation.templatesPage.use")}
</Button>
</RouterForm>
) : null}
</article>
);
})}
</div>
)}
<div className="flex w-full items-center gap-3 pb-4">
<FeatherInfo className="text-body-small-regular-0 font-body-small-regular-0 text-icon-neutral-tertiary" />
<span className="grow shrink-0 basis-0 text-body-extra-small-regular-0 font-body-extra-small-regular-0 text-text-neutral-tertiary">
{t("automation.templatesPage.summary", {
shown: formatters.number(visibleTemplates.length),
total: formatters.number(vm.templates.length),
})}
</span>
{counts.own > 0 ? (
<div className="flex items-center gap-1.5">
<FeatherUser className="text-body-small-regular-0 font-body-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">
{t("automation.templatesPage.ownCount", { count: counts.own, formattedCount: formatters.number(counts.own) })}
</span>
</div>
) : null}
</div>
</div>
</div>
);
}