import { Button } from "@repo/shadcn-ui/components/button"; import { Checkbox } from "@repo/shadcn-ui/components/checkbox"; import { FormDialog } from "@repo/shadcn-ui/components/form-dialog"; import { Input } from "@repo/shadcn-ui/components/input"; 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 { TemplateHistory, TemplateSteps } from "./template-detail"; import { InterventionTemplateForm, copyTemplateValues } from "./template-form"; import { InterventionTemplateFeedback } from "./template-help"; export function InterventionTemplatesPage({ scope = "global", csrfToken, }: { scope?: string; csrfToken: string; }) { const { t } = 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 (

{t("interventionTemplates.title")}

{t( scope === "global" ? "interventionTemplates.scopeGlobal" : "interventionTemplates.scopeCompany" )}

setSearch(event.target.value)} placeholder={t("interventionTemplates.filter")} value={search} />
setShowInactive(value === true)} />
{query.isPending &&

{t("loading")}

} {query.isError && ( )} {query.data && !query.isError && ( <> {!visible.length &&

{t("interventionTemplates.empty")}

} {visible.map((template) => (

{template.values.name}

{t( template.companyId ? "interventionTemplates.company" : "interventionTemplates.global" )}{" "} ยท {t(template.values.active ? "active" : "inactive")}

{template.companyId === (scope === "global" ? null : scope) && ( )}
))} )} { if (!open) { setEditor(undefined); } }} open={editor !== undefined} title={t(selected ? "interventionTemplates.edit" : "interventionTemplates.new")} wide > {editor && ( setEditor(undefined)} scope={scope} template={selected} /> )}
); }