import { WorkflowNode, WorkflowNodeConfig } from "@/domain/workflow"; import { zodResolver } from "@hookform/resolvers/zod"; import React, { useEffect } from "react"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { Button } from "../ui/button"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "../ui/form"; import { Input } from "../ui/input"; import { RadioGroup, RadioGroupItem } from "../ui/radio-group"; import { Label } from "../ui/label"; import { useTranslation } from "react-i18next"; import { parseExpression } from "cron-parser"; import { useWorkflowStore, WorkflowState } from "@/providers/workflow"; import { useShallow } from "zustand/shallow"; import { usePanel } from "./PanelProvider"; const formSchema = z .object({ executionMethod: z.string().min(1, "executionMethod is required"), crontab: z.string(), }) .superRefine((data, ctx) => { if (data.executionMethod != "auto") { return; } try { parseExpression(data.crontab); } catch (e) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: "crontab is invalid", path: ["crontab"], }); } }); type StartFormProps = { data: WorkflowNode; }; const i18nPrefix = "workflow.node.start.form"; const selectState = (state: WorkflowState) => ({ updateNode: state.updateNode, }); const StartForm = ({ data }: StartFormProps) => { const { updateNode } = useWorkflowStore(useShallow(selectState)); const { hidePanel } = usePanel(); const { t } = useTranslation(); const [method, setMethod] = React.useState("auto"); useEffect(() => { if (data.config && data.config.executionMethod) { setMethod(data.config.executionMethod as string); } else { setMethod("auto"); } }, [data]); let config: WorkflowNodeConfig = { executionMethod: "auto", crontab: "0 0 * * *", }; if (data) config = data.config ?? config; const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { executionMethod: config.executionMethod as string, crontab: config.crontab as string, }, }); const onSubmit = async (config: z.infer) => { updateNode({ ...data, config: { ...config }, validated: true }); hidePanel(); }; return ( <>
{ e.stopPropagation(); form.handleSubmit(onSubmit)(e); }} className="space-y-8" > ( {t(`${i18nPrefix}.executionMethod.label`)} { setMethod(val); }} className="flex space-x-3" >
)} /> ( )} />
); }; export default StartForm;