import { Button } from "@repo/shadcn-ui/components/button"; import { Checkbox } from "@repo/shadcn-ui/components/checkbox"; import { EntityAction, EntityListHeader, EntityRowActions, EntityStatus, EntityTable, } from "@repo/shadcn-ui/components/entity-list"; import { FormDialog } from "@repo/shadcn-ui/components/form-dialog"; import { Label } from "@repo/shadcn-ui/components/label"; import { useQuery } from "@tanstack/react-query"; import { useId, useState } from "react"; import { useTranslation } from "react-i18next"; import { interventionTemplateApi, interventionTemplateKeys } from "./template-api"; import { TemplateSteps } from "./template-detail"; import { InterventionTemplateFeedback } from "./template-feedback"; import { InterventionTemplateForm, copyTemplateValues } from "./template-form"; import { TemplateHistory } from "./template-history"; export function InterventionTemplatesPage({ scope = "global", csrfToken, }: { scope?: string; csrfToken: string; }) { const { t, i18n } = useTranslation("companies"); const prefix = useId(); const query = useQuery({ queryKey: interventionTemplateKeys.list(scope), queryFn: () => interventionTemplateApi.list(scope), }); const [editor, setEditor] = useState<{ id?: string; copyId?: string }>(); const [search, setSearch] = useState(""); const [showInactive, setShowInactive] = useState(true); const templates = query.data?.templates ?? []; const selected = templates.find((item) => item.id === editor?.id); const copied = templates.find((item) => item.id === editor?.copyId); const visible = templates.filter( (item) => (showInactive || item.values.active) && item.values.name.toLocaleLowerCase().includes(search.trim().toLocaleLowerCase()) ); return (
{" "} {" "} } description={t( scope === "global" ? "interventionTemplates.scopeGlobal" : "interventionTemplates.scopeCompany" )} locale={i18n.language} title={t("interventionTemplates.title")} /> {query.isPending &&

{t("loading")}

} {query.isError && ( )} {query.data && !query.isError && ( template.values.name, hideable: false, render: (template) => <>{template.values.name}, }, { id: "scope", label: t("interventionTemplates.global"), value: (template) => t( template.companyId ? "interventionTemplates.company" : "interventionTemplates.global" ), render: (template) => ( <> {t( template.companyId ? "interventionTemplates.company" : "interventionTemplates.global" )} ), }, { id: "state", label: t("status"), value: (template) => t(template.values.active ? "active" : "inactive"), render: (template) => ( {t(template.values.active ? "active" : "inactive")} ), }, { id: "steps", label: t("interventions.steps"), value: (template) => template.values.steps.length, render: (template) => (
{t("interventions.steps")} ({template.values.steps.length})
), }, { id: "history", label: t("history"), render: (template) => , }, { id: "actions", label: t("actions"), hideable: false, render: (template) => ( {" "}
{template.companyId === (scope === "global" ? null : scope) && ( setEditor({ id: template.id })}> {t("edit")} )} setEditor({ copyId: template.id })}> {t("interventionTemplates.copy")}
{" "}
), }, ]} emptyMessage={t("interventionTemplates.empty")} filterKey={String(showInactive)} filters={
setShowInactive(value === true)} />
} filtersApplied={!showInactive} items={visible} locale={i18n.language} onClearFilters={() => setShowInactive(true)} onSearchChange={setSearch} search={search} searchPlaceholder={t("interventionTemplates.filter")} title={t("interventionTemplates.title")} /> )} { if (!open) { setEditor(undefined); } }} open={editor !== undefined} title={t(selected ? "interventionTemplates.edit" : "interventionTemplates.new")} wide > {editor && ( setEditor(undefined)} scope={scope} template={selected} /> )}
); }