From daa5b44f8efaafc838a494a20acde4dd56f42770 Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Wed, 2 Apr 2025 11:28:10 +0800 Subject: [PATCH] refactor(ui): clean code --- .../components/access/AccessFormSSHConfig.tsx | 14 ++++--- .../NotifyChannelEditFormEmailFields.tsx | 14 ++++--- .../workflow/node/ApplyNodeConfigForm.tsx | 37 ++++++++++--------- .../DeployNodeConfigFormAliyunCLBConfig.tsx | 13 ++++--- ...oyNodeConfigFormBaiduCloudAppBLBConfig.tsx | 19 ++++++---- ...eployNodeConfigFormBaiduCloudBLBConfig.tsx | 19 ++++++---- 6 files changed, 67 insertions(+), 49 deletions(-) diff --git a/ui/src/components/access/AccessFormSSHConfig.tsx b/ui/src/components/access/AccessFormSSHConfig.tsx index d455a5fa..6d3615cf 100644 --- a/ui/src/components/access/AccessFormSSHConfig.tsx +++ b/ui/src/components/access/AccessFormSSHConfig.tsx @@ -7,7 +7,7 @@ import { z } from "zod"; import { type AccessConfigForSSH } from "@/domain/access"; import { readFileContent } from "@/utils/file"; -import { validDomainName, validIPv4Address, validIPv6Address } from "@/utils/validators"; +import { validDomainName, validIPv4Address, validIPv6Address, validPortNumber } from "@/utils/validators"; type AccessFormSSHConfigFieldValues = Nullish; @@ -34,11 +34,13 @@ const AccessFormSSHConfig = ({ form: formInst, formName, disabled, initialValues host: z .string({ message: t("access.form.ssh_host.placeholder") }) .refine((v) => validDomainName(v) || validIPv4Address(v) || validIPv6Address(v), t("common.errmsg.host_invalid")), - port: z - .number({ message: t("access.form.ssh_port.placeholder") }) - .int() - .gte(1, t("common.errmsg.port_invalid")) - .lte(65535, t("common.errmsg.port_invalid")), + port: z.preprocess( + (v) => Number(v), + z + .number({ message: t("access.form.ssh_port.placeholder") }) + .int(t("access.form.ssh_port.placeholder")) + .refine((v) => validPortNumber(v), t("common.errmsg.port_invalid")) + ), username: z .string() .min(1, "access.form.ssh_username.placeholder") diff --git a/ui/src/components/notification/NotifyChannelEditFormEmailFields.tsx b/ui/src/components/notification/NotifyChannelEditFormEmailFields.tsx index 615950bb..d7c08c4f 100644 --- a/ui/src/components/notification/NotifyChannelEditFormEmailFields.tsx +++ b/ui/src/components/notification/NotifyChannelEditFormEmailFields.tsx @@ -3,6 +3,8 @@ import { Form, Input, InputNumber, Switch } from "antd"; import { createSchemaFieldRule } from "antd-zod"; import { z } from "zod"; +import { validPortNumber } from "@/utils/validators"; + const NotifyChannelEditFormEmailFields = () => { const { t } = useTranslation(); @@ -11,11 +13,13 @@ const NotifyChannelEditFormEmailFields = () => { .string({ message: t("settings.notification.channel.form.email_smtp_host.placeholder") }) .min(1, t("settings.notification.channel.form.email_smtp_host.placeholder")) .max(256, t("common.errmsg.string_max", { max: 256 })), - smtpPort: z - .number({ message: t("settings.notification.channel.form.email_smtp_port.placeholder") }) - .int() - .gte(1, t("common.errmsg.port_invalid")) - .lte(65535, t("common.errmsg.port_invalid")), + smtpPort: z.preprocess( + (v) => Number(v), + z + .number({ message: t("settings.notification.channel.form.email_smtp_port.placeholder") }) + .int(t("settings.notification.channel.form.email_smtp_port.placeholder")) + .refine((v) => validPortNumber(v), t("common.errmsg.port_invalid")) + ), smtpTLS: z.boolean().nullish(), username: z .string({ message: t("settings.notification.channel.form.email_username.placeholder") }) diff --git a/ui/src/components/workflow/node/ApplyNodeConfigForm.tsx b/ui/src/components/workflow/node/ApplyNodeConfigForm.tsx index 8034c86d..6ea95979 100644 --- a/ui/src/components/workflow/node/ApplyNodeConfigForm.tsx +++ b/ui/src/components/workflow/node/ApplyNodeConfigForm.tsx @@ -114,24 +114,27 @@ const ApplyNodeConfigForm = forwardRef validIPv4Address(e) || validIPv6Address(e) || validDomainName(e)); }, t("common.errmsg.host_invalid")), - dnsPropagationTimeout: z - .union([ - z.number().int().gte(1, t("workflow_node.apply.form.dns_propagation_timeout.placeholder")), - z.string().refine((v) => !v || /^[1-9]\d*$/.test(v), t("workflow_node.apply.form.dns_propagation_timeout.placeholder")), - ]) - .nullish(), - dnsTTL: z - .union([ - z.number().int().gte(1, t("workflow_node.apply.form.dns_ttl.placeholder")), - z.string().refine((v) => !v || /^[1-9]\d*$/.test(v), t("workflow_node.apply.form.dns_ttl.placeholder")), - ]) - .nullish(), + dnsPropagationTimeout: z.preprocess( + (v) => (v == null || v === "" ? undefined : Number(v)), + z + .number() + .int(t("workflow_node.apply.form.dns_propagation_timeout.placeholder")) + .gte(1, t("workflow_node.apply.form.dns_propagation_timeout.placeholder")) + .nullish() + ), + dnsTTL: z.preprocess( + (v) => (v == null || v === "" ? undefined : Number(v)), + z.number().int(t("workflow_node.apply.form.dns_ttl.placeholder")).gte(1, t("workflow_node.apply.form.dns_ttl.placeholder")).nullish() + ), disableFollowCNAME: z.boolean().nullish(), disableARI: z.boolean().nullish(), - skipBeforeExpiryDays: z - .number({ message: t("workflow_node.apply.form.skip_before_expiry_days.placeholder") }) - .int(t("workflow_node.apply.form.skip_before_expiry_days.placeholder")) - .gte(1, t("workflow_node.apply.form.skip_before_expiry_days.placeholder")), + skipBeforeExpiryDays: z.preprocess( + (v) => Number(v), + z + .number({ message: t("workflow_node.apply.form.skip_before_expiry_days.placeholder") }) + .int(t("workflow_node.apply.form.skip_before_expiry_days.placeholder")) + .gte(1, t("workflow_node.apply.form.skip_before_expiry_days.placeholder")) + ), }); const formRule = createSchemaFieldRule(formSchema); const { form: formInst, formProps } = useAntdForm({ @@ -570,7 +573,7 @@ const ApplyNodeConfigForm = forwardRef diff --git a/ui/src/components/workflow/node/DeployNodeConfigFormAliyunCLBConfig.tsx b/ui/src/components/workflow/node/DeployNodeConfigFormAliyunCLBConfig.tsx index 2dd66b42..2c2e43b6 100644 --- a/ui/src/components/workflow/node/DeployNodeConfigFormAliyunCLBConfig.tsx +++ b/ui/src/components/workflow/node/DeployNodeConfigFormAliyunCLBConfig.tsx @@ -10,7 +10,7 @@ type DeployNodeConfigFormAliyunCLBConfigFieldValues = Nullish<{ resourceType: string; region: string; loadbalancerId?: string; - listenerPort?: string | number; + listenerPort?: number; domain?: string; }>; @@ -53,10 +53,13 @@ const DeployNodeConfigFormAliyunCLBConfig = ({ .min(1, t("workflow_node.deploy.form.aliyun_clb_loadbalancer_id.placeholder")) .max(64, t("common.errmsg.string_max", { max: 64 })) .trim(), - listenerPort: z - .union([z.number(), z.string()]) - .refine((v) => fieldResourceType === RESOURCE_TYPE_LISTENER && validPortNumber(v), t("workflow_node.deploy.form.aliyun_clb_listener_port.placeholder")) - .nullish(), + listenerPort: z.preprocess( + (v) => (v == null || v === "" ? undefined : Number(v)), + z + .number() + .nullish() + .refine((v) => fieldResourceType === RESOURCE_TYPE_LISTENER && validPortNumber(v!), t("workflow_node.deploy.form.aliyun_clb_listener_port.placeholder")) + ), domain: z .string() .nullish() diff --git a/ui/src/components/workflow/node/DeployNodeConfigFormBaiduCloudAppBLBConfig.tsx b/ui/src/components/workflow/node/DeployNodeConfigFormBaiduCloudAppBLBConfig.tsx index 157b9a27..7bd40b82 100644 --- a/ui/src/components/workflow/node/DeployNodeConfigFormBaiduCloudAppBLBConfig.tsx +++ b/ui/src/components/workflow/node/DeployNodeConfigFormBaiduCloudAppBLBConfig.tsx @@ -10,7 +10,7 @@ type DeployNodeConfigFormBaiduCloudAppBLBConfigFieldValues = Nullish<{ resourceType: string; region: string; loadbalancerId?: string; - listenerPort?: string | number; + listenerPort?: number; domain?: string; }>; @@ -53,13 +53,16 @@ const DeployNodeConfigFormBaiduCloudAppBLBConfig = ({ .min(1, t("workflow_node.deploy.form.baiducloud_appblb_loadbalancer_id.placeholder")) .max(64, t("common.errmsg.string_max", { max: 64 })) .trim(), - listenerPort: z - .union([z.number(), z.string()]) - .refine( - (v) => fieldResourceType === RESOURCE_TYPE_LISTENER && validPortNumber(v), - t("workflow_node.deploy.form.baiducloud_appblb_listener_port.placeholder") - ) - .nullish(), + listenerPort: z.preprocess( + (v) => (v == null || v === "" ? undefined : Number(v)), + z + .number() + .refine( + (v) => fieldResourceType === RESOURCE_TYPE_LISTENER && validPortNumber(v!), + t("workflow_node.deploy.form.baiducloud_appblb_listener_port.placeholder") + ) + .nullish() + ), domain: z .string() .nullish() diff --git a/ui/src/components/workflow/node/DeployNodeConfigFormBaiduCloudBLBConfig.tsx b/ui/src/components/workflow/node/DeployNodeConfigFormBaiduCloudBLBConfig.tsx index f6ab1750..20bb22f1 100644 --- a/ui/src/components/workflow/node/DeployNodeConfigFormBaiduCloudBLBConfig.tsx +++ b/ui/src/components/workflow/node/DeployNodeConfigFormBaiduCloudBLBConfig.tsx @@ -10,7 +10,7 @@ type DeployNodeConfigFormBaiduCloudBLBConfigFieldValues = Nullish<{ resourceType: string; region: string; loadbalancerId?: string; - listenerPort?: string | number; + listenerPort?: number; domain?: string; }>; @@ -53,13 +53,16 @@ const DeployNodeConfigFormBaiduCloudBLBConfig = ({ .min(1, t("workflow_node.deploy.form.baiducloud_blb_loadbalancer_id.placeholder")) .max(64, t("common.errmsg.string_max", { max: 64 })) .trim(), - listenerPort: z - .union([z.number(), z.string()]) - .refine( - (v) => fieldResourceType === RESOURCE_TYPE_LISTENER && validPortNumber(v), - t("workflow_node.deploy.form.baiducloud_blb_listener_port.placeholder") - ) - .nullish(), + listenerPort: z.preprocess( + (v) => (v == null || v === "" ? undefined : Number(v)), + z + .number() + .nullish() + .refine( + (v) => fieldResourceType === RESOURCE_TYPE_LISTENER && validPortNumber(v!), + t("workflow_node.deploy.form.baiducloud_blb_listener_port.placeholder") + ) + ), domain: z .string() .nullish()