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("all"); const [categoryFilter, setCategoryFilter] = useState(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 (
} title={t("automation.templatesPage.title")} subtitle={t("automation.templatesPage.subtitle")} />
) => setQuery(event.target.value)} />
setTab("all")} /> setTab(tab === "shk" ? "all" : "shk")} /> setTab(tab === "own" ? "all" : "own")} />
{categories.length > 0 ? (
setCategoryFilter(null)} /> {categories.map((category) => ( setCategoryFilter(categoryFilter === category ? null : category)} /> ))}
) : null}
{vm.templates.length === 0 ? ( ) : visibleTemplates.length === 0 ? ( ) : (
{visibleTemplates.map((template) => { const TemplateIcon = nodeIcon(template.chain[0]?.nodeType ?? template.category); return (
} />
{localizedValue(t, template.name, "automation.values.unknownTemplate")} {template.description ? localizedValue(t, template.description, "automation.values.unknownTemplateDescription") : t("automation.templatesPage.noDescription")}
{template.isTenantTemplate ? t("automation.templatesPage.tenantTemplate") : t("automation.templatesPage.platformTemplate")}
{/* 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 ? (
{t("automation.templatesPage.usedTimes", { count: template.useCount, formattedCount: formatters.number(template.useCount) })}
) : null} {vm.canManage ? ( ) : null}
); })}
)}
{t("automation.templatesPage.summary", { shown: formatters.number(visibleTemplates.length), total: formatters.number(vm.templates.length), })} {counts.own > 0 ? (
{t("automation.templatesPage.ownCount", { count: counts.own, formattedCount: formatters.number(counts.own) })}
) : null}
); }