import { forwardRef, memo, useEffect, useImperativeHandle } from "react"; import { useTranslation } from "react-i18next"; import { Link } from "react-router"; import { RightOutlined as RightOutlinedIcon } from "@ant-design/icons"; import { Button, Form, type FormInstance, Input, Select } from "antd"; import { createSchemaFieldRule } from "antd-zod"; import { z } from "zod"; import { notifyChannelsMap } from "@/domain/settings"; import { type WorkflowNodeConfigForNotify } from "@/domain/workflow"; import { useAntdForm, useZustandShallowSelector } from "@/hooks"; import { useNotifyChannelsStore } from "@/stores/notify"; type NotifyNodeConfigFormFieldValues = Partial; export type NotifyNodeConfigFormProps = { className?: string; style?: React.CSSProperties; disabled?: boolean; initialValues?: NotifyNodeConfigFormFieldValues; onValuesChange?: (values: NotifyNodeConfigFormFieldValues) => void; }; export type NotifyNodeConfigFormInstance = { getFieldsValue: () => ReturnType["getFieldsValue"]>; resetFields: FormInstance["resetFields"]; validateFields: FormInstance["validateFields"]; }; const initFormModel = (): NotifyNodeConfigFormFieldValues => { return {}; }; const NotifyNodeConfigForm = forwardRef( ({ className, style, disabled, initialValues, onValuesChange }, ref) => { const { t } = useTranslation(); const { channels, loadedAtOnce: channelsLoadedAtOnce, fetchChannels, } = useNotifyChannelsStore(useZustandShallowSelector(["channels", "loadedAtOnce", "fetchChannels"])); useEffect(() => { fetchChannels(); }, []); const formSchema = z.object({ subject: z .string({ message: t("workflow_node.notify.form.subject.placeholder") }) .min(1, t("workflow_node.notify.form.subject.placeholder")) .max(1000, t("common.errmsg.string_max", { max: 1000 })), message: z .string({ message: t("workflow_node.notify.form.message.placeholder") }) .min(1, t("workflow_node.notify.form.message.placeholder")) .max(1000, t("common.errmsg.string_max", { max: 1000 })), channel: z.string({ message: t("workflow_node.notify.form.channel.placeholder") }).min(1, t("workflow_node.notify.form.channel.placeholder")), }); const formRule = createSchemaFieldRule(formSchema); const { form: formInst, formProps } = useAntdForm({ name: "workflowNodeNotifyConfigForm", initialValues: initialValues ?? initFormModel(), }); const handleFormChange = (_: unknown, values: z.infer) => { onValuesChange?.(values as NotifyNodeConfigFormFieldValues); }; useImperativeHandle(ref, () => { return { getFieldsValue: () => { return formInst.getFieldsValue(true); }, resetFields: (fields) => { return formInst.resetFields(fields as (keyof NotifyNodeConfigFormFieldValues)[]); }, validateFields: (nameList, config) => { return formInst.validateFields(nameList, config); }, } as NotifyNodeConfigFormInstance; }); return (