import { zodResolver } from "@hookform/resolvers/zod"; import { Button } from "@repo/shadcn-ui/components/button"; import { Checkbox } from "@repo/shadcn-ui/components/checkbox"; import { FormActions, 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 { SelectWithAction } from "@repo/shadcn-ui/components/select-with-action"; import { Textarea } from "@repo/shadcn-ui/components/textarea"; import { useId, useState } from "react"; import { useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import type { z } from "zod/v4"; import type { CatalogItemDTO } from "../common"; import { AssetDefinitionSchema } from "../common"; import type { CatalogSaved } from "./catalog-form"; import { CatalogError, CatalogFieldForm, useCatalogSave } from "./catalog-form"; import { CatalogHelp } from "./catalog-help"; const TemplateFormSchema = AssetDefinitionSchema.options[1]; type TemplateFormValues = z.infer; export function CatalogTemplateForm({ scope, csrfToken, item, copy, items, onSaved, allowQuickCreate = true, }: { scope: string; csrfToken: string; item?: CatalogItemDTO | undefined; copy?: CatalogItemDTO | undefined; items: readonly CatalogItemDTO[]; onSaved: CatalogSaved; allowQuickCreate?: boolean; }) { const { t } = useTranslation("assets"); const id = useId(); const [creatingField, setCreatingField] = useState(false); const source = item ?? copy; const current = source?.values.kind === "template" ? source.values : undefined; const form = useForm({ resolver: zodResolver(TemplateFormSchema), defaultValues: { kind: "template", name: copy ? t("copyName", { name: copy.values.name }) : (current?.name ?? ""), description: current?.description ?? "", fields: current?.fields.map((field) => ({ ...field })) ?? [], }, }); const mutation = useCatalogSave(scope, csrfToken, onSaved, item?.id); const selected = form.watch("fields"); const fields = items.filter((entry) => entry.values.kind === "field"); const add = (fieldId: string) => { if (fieldId && !selected.some((field) => field.fieldId === fieldId)) { form.setValue("fields", [...selected, { fieldId, required: false }], { shouldDirty: true, shouldValidate: true, }); } }; return ( <>
mutation.mutate(values))}>