metis-desk/modules/resources/src/web/catalog-editor-dialog.tsx
2026-10-08 16:43:34 +02:00

69 lines
1.5 KiB
TypeScript

import { FormDialog } from "@repo/shadcn-ui/components/form-dialog";
import { useTranslation } from "react-i18next";
import type { CatalogItemDTO } from "../common";
import { CatalogFieldForm } from "./catalog-form";
import { CatalogTemplateForm } from "./catalog-template-form";
export type CatalogEditor = {
kind: "field" | "template";
item?: CatalogItemDTO;
copy?: CatalogItemDTO;
};
function editorTitle(editor: CatalogEditor) {
if (editor.kind === "field") {
return editor.item ? "editField" : "newField";
}
return editor.item ? "editTemplate" : "newTemplate";
}
export function CatalogEditorDialog({
editor,
items,
scope,
csrfToken,
onClose,
}: {
editor: CatalogEditor;
items: readonly CatalogItemDTO[];
scope: string;
csrfToken: string;
onClose: () => void;
}) {
const { t } = useTranslation("resources");
return (
<FormDialog
cancelLabel={t("cancel")}
formActions
onOpenChange={(open) => {
if (!open) {
onClose();
}
}}
open
title={t(editorTitle(editor))}
wide={editor.kind === "template"}
>
{editor.kind === "field" ? (
<CatalogFieldForm
csrfToken={csrfToken}
item={editor.item}
onSaved={() => onClose()}
scope={scope}
/>
) : (
<CatalogTemplateForm
copy={editor.copy}
csrfToken={csrfToken}
item={editor.item}
items={items}
onSaved={() => onClose()}
scope={scope}
/>
)}
</FormDialog>
);
}