import { memo, useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { Alert, Button, Form, Input, Radio } from "antd"; import { createSchemaFieldRule } from "antd-zod"; import dayjs from "dayjs"; import { produce } from "immer"; import { z } from "zod"; import { usePanel } from "../PanelProvider"; import { useAntdForm, useZustandShallowSelector } from "@/hooks"; import { type WorkflowNode, type WorkflowNodeConfig } from "@/domain/workflow"; import { useWorkflowStore } from "@/stores/workflow"; import { validCronExpression, getNextCronExecutions } from "@/utils/cron"; export type StartNodeFormProps = { data: WorkflowNode; }; const initFormModel = (): WorkflowNodeConfig => { return { executionMethod: "auto", crontab: "0 0 * * *", }; }; const StartNodeForm = ({ data }: StartNodeFormProps) => { const { t } = useTranslation(); const { updateNode } = useWorkflowStore(useZustandShallowSelector(["updateNode"])); const { hidePanel } = usePanel(); const formSchema = z .object({ executionMethod: z.string({ message: t("workflow_node.start.form.trigger.placeholder") }).min(1, t("workflow_node.start.form.trigger.placeholder")), crontab: z.string().nullish(), }) .superRefine((data, ctx) => { if (data.executionMethod !== "auto") { return; } if (!validCronExpression(data.crontab!)) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: t("workflow_node.start.form.trigger_cron.errmsg.invalid"), path: ["crontab"], }); } }); const formRule = createSchemaFieldRule(formSchema); const { form: formInst, formPending, formProps, } = useAntdForm>({ initialValues: data?.config ?? initFormModel(), onSubmit: async (values) => { await formInst.validateFields(); await updateNode( produce(data, (draft) => { draft.config = { ...values }; draft.validated = true; }) ); hidePanel(); }, }); const [triggerType, setTriggerType] = useState(data?.config?.executionMethod); const [triggerCronLastExecutions, setTriggerCronExecutions] = useState([]); useEffect(() => { setTriggerType(data?.config?.executionMethod); setTriggerCronExecutions(getNextCronExecutions(data?.config?.crontab as string, 5)); }, [data?.config?.executionMethod, data?.config?.crontab]); const handleTriggerTypeChange = (value: string) => { setTriggerType(value); if (value === "auto") { formInst.setFieldValue("crontab", formInst.getFieldValue("crontab") || initFormModel().crontab); } }; const handleTriggerCronChange = (value: string) => { setTriggerCronExecutions(getNextCronExecutions(value, 5)); }; return (
} > handleTriggerTypeChange(e.target.value)}> {t("workflow_node.start.form.trigger.option.auto.label")} {t("workflow_node.start.form.trigger.option.manual.label")}
); }; export default memo(StartNodeForm);