import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { z } from "zod"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectTrigger, SelectValue } from "@/components/ui/select"; import { DeployFormProps } from "./DeployForm"; import { WorkflowNode, WorkflowNodeConfig } from "@/domain/workflow"; import { useWorkflowStore, WorkflowState } from "@/stores/workflow"; import { useShallow } from "zustand/shallow"; import { usePanel } from "./PanelProvider"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "../ui/form"; import { Button } from "../ui/button"; import AccessSelect from "./AccessSelect"; import AccessEditModal from "../access/AccessEditModal"; import { Plus } from "lucide-react"; const selectState = (state: WorkflowState) => ({ updateNode: state.updateNode, getWorkflowOuptutBeforeId: state.getWorkflowOuptutBeforeId, }); const DeployToAliyunCLB = ({ data }: DeployFormProps) => { const { updateNode, getWorkflowOuptutBeforeId } = useWorkflowStore(useShallow(selectState)); const { hidePanel } = usePanel(); const { t } = useTranslation(); const [beforeOutput, setBeforeOutput] = useState([]); useEffect(() => { const rs = getWorkflowOuptutBeforeId(data.id, "certificate"); console.log(rs); setBeforeOutput(rs); }, [data]); const formSchema = z .object({ providerType: z.string(), access: z.string().min(1, t("domain.deployment.form.access.placeholder")), certificate: z.string().min(1), region: z.string().min(1, t("domain.deployment.form.aliyun_clb_region.placeholder")), resourceType: z.union([z.literal("certificate"), z.literal("loadbalancer"), z.literal("listener")], { message: t("domain.deployment.form.aliyun_clb_resource_type.placeholder"), }), loadbalancerId: z.string().optional(), listenerPort: z.string().optional(), }) .refine((data) => (data.resourceType === "loadbalancer" || data.resourceType === "listener" ? !!data.loadbalancerId?.trim() : true), { message: t("domain.deployment.form.aliyun_clb_loadbalancer_id.placeholder"), path: ["loadbalancerId"], }) .refine((data) => (data.resourceType === "listener" ? +data.listenerPort! > 0 && +data.listenerPort! < 65535 : true), { message: t("domain.deployment.form.aliyun_clb_listener_port.placeholder"), path: ["listenerPort"], }); let config: WorkflowNodeConfig = { certificate: "", providerType: "", access: "", region: "", resourceType: "", loadbalancerId: "", listenerPort: "", }; if (data) config = data.config ?? config; const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { providerType: "aliyun-clb", access: config.access as string, certificate: config.certificate as string, region: config.region as string, resourceType: config.resourceType as "loadbalancer" | "listener", loadbalancerId: config.loadbalancerId as string, listenerPort: config.listenerPort as string, }, }); const resouceType = form.watch("resourceType"); 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("domain.deployment.form.access.label")}
{t("common.button.add")} } />
{ form.setValue("access", value); }} providerType="aliyun-clb" />
)} /> ( {t("workflow.common.certificate.label")} )} /> ( {t("domain.deployment.form.aliyun_clb_region.label")} )} /> ( {t("domain.deployment.form.aliyun_clb_resource_type.label")} )} /> ( {t("domain.deployment.form.aliyun_clb_loadbalancer_id.label")} )} /> {resouceType === "listener" && ( ( {t("domain.deployment.form.aliyun_clb_listener_port.label")} )} /> )}
); }; export default DeployToAliyunCLB;