From e53749e16e339d54151cee553177824f4d7b883d Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Tue, 22 Oct 2024 12:20:13 +0800 Subject: [PATCH] refactor: clean code --- .../components/certimate/AccessAliyunForm.tsx | 6 +- ui/src/components/certimate/AccessAwsForm.tsx | 6 +- .../certimate/AccessCloudflareForm.tsx | 6 +- .../components/certimate/AccessEditDialog.tsx | 11 +- .../certimate/AccessGodaddyForm.tsx | 6 +- .../components/certimate/AccessGroupList.tsx | 6 +- .../certimate/AccessHttpreqForm.tsx | 6 +- .../certimate/AccessHuaweicloudForm.tsx | 6 +- .../certimate/AccessKubernetesForm.tsx | 6 +- .../components/certimate/AccessLocalForm.tsx | 6 +- .../certimate/AccessNamesiloForm.tsx | 6 +- .../components/certimate/AccessPdnsForm.tsx | 6 +- .../components/certimate/AccessQiniuForm.tsx | 6 +- ui/src/components/certimate/AccessSSHForm.tsx | 6 +- .../certimate/AccessTencentForm.tsx | 6 +- .../certimate/AccessWebhookForm.tsx | 6 +- ui/src/components/certimate/DeployList.tsx | 146 ++++++++---------- ui/src/domain/access.ts | 104 +++++-------- ui/src/pages/access/Access.tsx | 6 +- ui/src/pages/domains/Edit.tsx | 4 +- ui/src/providers/config/index.tsx | 8 +- 21 files changed, 167 insertions(+), 202 deletions(-) diff --git a/ui/src/components/certimate/AccessAliyunForm.tsx b/ui/src/components/certimate/AccessAliyunForm.tsx index 2b1d818c..ad54a95e 100644 --- a/ui/src/components/certimate/AccessAliyunForm.tsx +++ b/ui/src/components/certimate/AccessAliyunForm.tsx @@ -8,7 +8,7 @@ import { Button } from "@/components/ui/button"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { PbErrorData } from "@/domain/base"; -import { Access, AliyunConfig, accessFormType, getUsageByConfigType } from "@/domain/access"; +import { accessProvidersMap, accessTypeFormSchema, type Access, type AliyunConfig } from "@/domain/access"; import { save } from "@/repository/access"; import { useConfigContext } from "@/providers/config"; @@ -27,7 +27,7 @@ const AccessAliyunForm = ({ data, op, onAfterReq }: AccessAliyunFormProps) => { .string() .min(1, "access.authorization.form.name.placeholder") .max(64, t("common.errmsg.string_max", { max: 64 })), - configType: accessFormType, + configType: accessTypeFormSchema, accessKeyId: z .string() .min(1, "access.authorization.form.access_key_id.placeholder") @@ -60,7 +60,7 @@ const AccessAliyunForm = ({ data, op, onAfterReq }: AccessAliyunFormProps) => { id: data.id as string, name: data.name, configType: data.configType, - usage: getUsageByConfigType(data.configType), + usage: accessProvidersMap.get(data.configType)!.usage, config: { accessKeyId: data.accessKeyId, accessKeySecret: data.accessSecretId, diff --git a/ui/src/components/certimate/AccessAwsForm.tsx b/ui/src/components/certimate/AccessAwsForm.tsx index 94a4aedb..7b69a77f 100644 --- a/ui/src/components/certimate/AccessAwsForm.tsx +++ b/ui/src/components/certimate/AccessAwsForm.tsx @@ -8,7 +8,7 @@ import { Input } from "@/components/ui/input"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Button } from "@/components/ui/button"; import { PbErrorData } from "@/domain/base"; -import { Access, accessFormType, AwsConfig, getUsageByConfigType } from "@/domain/access"; +import { Access, accessProvidersMap, accessTypeFormSchema, type AwsConfig } from "@/domain/access"; import { save } from "@/repository/access"; import { useConfigContext } from "@/providers/config"; @@ -27,7 +27,7 @@ const AccessAwsForm = ({ data, op, onAfterReq }: AccessAwsFormProps) => { .string() .min(1, "access.authorization.form.name.placeholder") .max(64, t("common.errmsg.string_max", { max: 64 })), - configType: accessFormType, + configType: accessTypeFormSchema, region: z .string() .min(1, "access.authorization.form.region.placeholder") @@ -72,7 +72,7 @@ const AccessAwsForm = ({ data, op, onAfterReq }: AccessAwsFormProps) => { id: data.id as string, name: data.name, configType: data.configType, - usage: getUsageByConfigType(data.configType), + usage: accessProvidersMap.get(data.configType)!.usage, config: { region: data.region, accessKeyId: data.accessKeyId, diff --git a/ui/src/components/certimate/AccessCloudflareForm.tsx b/ui/src/components/certimate/AccessCloudflareForm.tsx index 0f156eee..a03c39b1 100644 --- a/ui/src/components/certimate/AccessCloudflareForm.tsx +++ b/ui/src/components/certimate/AccessCloudflareForm.tsx @@ -8,7 +8,7 @@ import { Button } from "@/components/ui/button"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { PbErrorData } from "@/domain/base"; -import { Access, accessFormType, CloudflareConfig, getUsageByConfigType } from "@/domain/access"; +import { accessProvidersMap, accessTypeFormSchema, type Access, type CloudflareConfig } from "@/domain/access"; import { save } from "@/repository/access"; import { useConfigContext } from "@/providers/config"; @@ -27,7 +27,7 @@ const AccessCloudflareForm = ({ data, op, onAfterReq }: AccessCloudflareFormProp .string() .min(1, "access.authorization.form.name.placeholder") .max(64, t("common.errmsg.string_max", { max: 64 })), - configType: accessFormType, + configType: accessTypeFormSchema, dnsApiToken: z .string() .min(1, "access.authorization.form.cloud_dns_api_token.placeholder") @@ -54,7 +54,7 @@ const AccessCloudflareForm = ({ data, op, onAfterReq }: AccessCloudflareFormProp id: data.id as string, name: data.name, configType: data.configType, - usage: getUsageByConfigType(data.configType), + usage: accessProvidersMap.get(data.configType)!.usage, config: { dnsApiToken: data.dnsApiToken, }, diff --git a/ui/src/components/certimate/AccessEditDialog.tsx b/ui/src/components/certimate/AccessEditDialog.tsx index c44a7f52..d93230f7 100644 --- a/ui/src/components/certimate/AccessEditDialog.tsx +++ b/ui/src/components/certimate/AccessEditDialog.tsx @@ -20,7 +20,7 @@ import AccessLocalForm from "./AccessLocalForm"; import AccessSSHForm from "./AccessSSHForm"; import AccessWebhookForm from "./AccessWebhookForm"; import AccessKubernetesForm from "./AccessKubernetesForm"; -import { Access, accessTypeMap } from "@/domain/access"; +import { Access, accessProvidersMap } from "@/domain/access"; type AccessEditProps = { op: "add" | "edit" | "copy"; @@ -30,10 +30,9 @@ type AccessEditProps = { }; const AccessEditDialog = ({ trigger, op, data, className }: AccessEditProps) => { - const [open, setOpen] = useState(false); const { t } = useTranslation(); - const typeKeys = Array.from(accessTypeMap.keys()); + const [open, setOpen] = useState(false); const [configType, setConfigType] = useState(data?.configType || ""); @@ -232,11 +231,11 @@ const AccessEditDialog = ({ trigger, op, data, className }: AccessEditProps) => {t("access.authorization.form.type.list")} - {typeKeys.map((key) => ( + {Array.from(accessProvidersMap.entries()).map(([key, provider]) => (
- -
{t(accessTypeMap.get(key)?.[0] || "")}
+ +
{t(provider.name)}
))} diff --git a/ui/src/components/certimate/AccessGodaddyForm.tsx b/ui/src/components/certimate/AccessGodaddyForm.tsx index 39325fff..4c8b4eca 100644 --- a/ui/src/components/certimate/AccessGodaddyForm.tsx +++ b/ui/src/components/certimate/AccessGodaddyForm.tsx @@ -8,7 +8,7 @@ import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { PbErrorData } from "@/domain/base"; -import { Access, accessFormType, getUsageByConfigType, GodaddyConfig } from "@/domain/access"; +import { accessProvidersMap, accessTypeFormSchema, type Access, type GodaddyConfig } from "@/domain/access"; import { save } from "@/repository/access"; import { useConfigContext } from "@/providers/config"; @@ -27,7 +27,7 @@ const AccessGodaddyForm = ({ data, op, onAfterReq }: AccessGodaddyFormProps) => .string() .min(1, "access.authorization.form.name.placeholder") .max(64, t("common.errmsg.string_max", { max: 64 })), - configType: accessFormType, + configType: accessTypeFormSchema, apiKey: z .string() .min(1, "access.authorization.form.godaddy_api_key.placeholder") @@ -60,7 +60,7 @@ const AccessGodaddyForm = ({ data, op, onAfterReq }: AccessGodaddyFormProps) => id: data.id as string, name: data.name, configType: data.configType, - usage: getUsageByConfigType(data.configType), + usage: accessProvidersMap.get(data.configType)!.usage, config: { apiKey: data.apiKey, apiSecret: data.apiSecret, diff --git a/ui/src/components/certimate/AccessGroupList.tsx b/ui/src/components/certimate/AccessGroupList.tsx index 704b49f5..e07344ca 100644 --- a/ui/src/components/certimate/AccessGroupList.tsx +++ b/ui/src/components/certimate/AccessGroupList.tsx @@ -19,7 +19,7 @@ import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } import { ScrollArea } from "@/components/ui/scroll-area"; import { useToast } from "@/components/ui/use-toast"; import AccessGroupEdit from "./AccessGroupEdit"; -import { getProviderInfo } from "@/domain/access"; +import { accessProvidersMap } from "@/domain/access"; import { getErrMessage } from "@/lib/error"; import { useConfigContext } from "@/providers/config"; import { remove } from "@/repository/access_group"; @@ -86,11 +86,11 @@ const AccessGroupList = () => {
- provider + provider
{access.name}
-
{getProviderInfo(access.configType)![0]}
+
{accessProvidersMap.get(access.configType)!.name}
diff --git a/ui/src/components/certimate/AccessHttpreqForm.tsx b/ui/src/components/certimate/AccessHttpreqForm.tsx index b8527fbb..bf398170 100644 --- a/ui/src/components/certimate/AccessHttpreqForm.tsx +++ b/ui/src/components/certimate/AccessHttpreqForm.tsx @@ -8,7 +8,7 @@ import { Button } from "@/components/ui/button"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { PbErrorData } from "@/domain/base"; -import { Access, HttpreqConfig, accessFormType, getUsageByConfigType } from "@/domain/access"; +import { accessProvidersMap, accessTypeFormSchema, type Access, type HttpreqConfig } from "@/domain/access"; import { save } from "@/repository/access"; import { useConfigContext } from "@/providers/config"; @@ -27,7 +27,7 @@ const AccessHttpreqForm = ({ data, op, onAfterReq }: AccessHttpreqFormProps) => .string() .min(1, "access.authorization.form.name.placeholder") .max(64, t("common.errmsg.string_max", { max: 64 })), - configType: accessFormType, + configType: accessTypeFormSchema, endpoint: z.string().url("common.errmsg.url_invalid"), mode: z.enum(["RAW", ""]), username: z @@ -66,7 +66,7 @@ const AccessHttpreqForm = ({ data, op, onAfterReq }: AccessHttpreqFormProps) => id: data.id as string, name: data.name, configType: data.configType, - usage: getUsageByConfigType(data.configType), + usage: accessProvidersMap.get(data.configType)!.usage, config: { endpoint: data.endpoint, mode: data.mode, diff --git a/ui/src/components/certimate/AccessHuaweicloudForm.tsx b/ui/src/components/certimate/AccessHuaweicloudForm.tsx index d70bd994..af07611a 100644 --- a/ui/src/components/certimate/AccessHuaweicloudForm.tsx +++ b/ui/src/components/certimate/AccessHuaweicloudForm.tsx @@ -8,7 +8,7 @@ import { Input } from "@/components/ui/input"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Button } from "@/components/ui/button"; import { PbErrorData } from "@/domain/base"; -import { Access, accessFormType, HuaweiCloudConfig, getUsageByConfigType } from "@/domain/access"; +import { accessProvidersMap, accessTypeFormSchema, type Access, type HuaweiCloudConfig } from "@/domain/access"; import { save } from "@/repository/access"; import { useConfigContext } from "@/providers/config"; @@ -27,7 +27,7 @@ const AccessHuaweiCloudForm = ({ data, op, onAfterReq }: AccessHuaweiCloudFormPr .string() .min(1, "access.authorization.form.name.placeholder") .max(64, t("common.errmsg.string_max", { max: 64 })), - configType: accessFormType, + configType: accessTypeFormSchema, region: z .string() .min(1, "access.authorization.form.region.placeholder") @@ -66,7 +66,7 @@ const AccessHuaweiCloudForm = ({ data, op, onAfterReq }: AccessHuaweiCloudFormPr id: data.id as string, name: data.name, configType: data.configType, - usage: getUsageByConfigType(data.configType), + usage: accessProvidersMap.get(data.configType)!.usage, config: { region: data.region, accessKeyId: data.accessKeyId, diff --git a/ui/src/components/certimate/AccessKubernetesForm.tsx b/ui/src/components/certimate/AccessKubernetesForm.tsx index aa23089a..bb84c89c 100644 --- a/ui/src/components/certimate/AccessKubernetesForm.tsx +++ b/ui/src/components/certimate/AccessKubernetesForm.tsx @@ -5,12 +5,12 @@ import { z } from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { ClientResponseError } from "pocketbase"; -import { Access, accessFormType, getUsageByConfigType, KubernetesConfig } from "@/domain/access"; import { Button } from "@/components/ui/button"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { readFileContent } from "@/lib/file"; import { PbErrorData } from "@/domain/base"; +import { accessProvidersMap, accessTypeFormSchema, type Access, type KubernetesConfig } from "@/domain/access"; import { save } from "@/repository/access"; import { useConfigContext } from "@/providers/config"; @@ -34,7 +34,7 @@ const AccessKubernetesForm = ({ data, op, onAfterReq }: AccessKubernetesFormProp .string() .min(1, "access.authorization.form.name.placeholder") .max(64, t("common.errmsg.string_max", { max: 64 })), - configType: accessFormType, + configType: accessTypeFormSchema, kubeConfig: z .string() .min(1, "access.authorization.form.k8s_kubeconfig.placeholder") @@ -64,7 +64,7 @@ const AccessKubernetesForm = ({ data, op, onAfterReq }: AccessKubernetesFormProp id: data.id as string, name: data.name, configType: data.configType, - usage: getUsageByConfigType(data.configType), + usage: accessProvidersMap.get(data.configType)!.usage, config: { kubeConfig: data.kubeConfig, }, diff --git a/ui/src/components/certimate/AccessLocalForm.tsx b/ui/src/components/certimate/AccessLocalForm.tsx index 5c08a257..23184b66 100644 --- a/ui/src/components/certimate/AccessLocalForm.tsx +++ b/ui/src/components/certimate/AccessLocalForm.tsx @@ -8,7 +8,7 @@ import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from " import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { PbErrorData } from "@/domain/base"; -import { Access, accessFormType, getUsageByConfigType } from "@/domain/access"; +import { accessProvidersMap, accessTypeFormSchema, type Access } from "@/domain/access"; import { save } from "@/repository/access"; import { useConfigContext } from "@/providers/config"; @@ -28,7 +28,7 @@ const AccessLocalForm = ({ data, op, onAfterReq }: AccessLocalFormProps) => { .string() .min(1, "access.authorization.form.name.placeholder") .max(64, t("common.errmsg.string_max", { max: 64 })), - configType: accessFormType, + configType: accessTypeFormSchema, }); const form = useForm>({ @@ -45,7 +45,7 @@ const AccessLocalForm = ({ data, op, onAfterReq }: AccessLocalFormProps) => { id: data.id as string, name: data.name, configType: data.configType, - usage: getUsageByConfigType(data.configType), + usage: accessProvidersMap.get(data.configType)!.usage, config: {}, }; diff --git a/ui/src/components/certimate/AccessNamesiloForm.tsx b/ui/src/components/certimate/AccessNamesiloForm.tsx index c01dbf32..ee8df30f 100644 --- a/ui/src/components/certimate/AccessNamesiloForm.tsx +++ b/ui/src/components/certimate/AccessNamesiloForm.tsx @@ -8,7 +8,7 @@ import { Button } from "@/components/ui/button"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { PbErrorData } from "@/domain/base"; -import { Access, accessFormType, getUsageByConfigType, NamesiloConfig } from "@/domain/access"; +import { accessProvidersMap, accessTypeFormSchema, type Access, type NamesiloConfig } from "@/domain/access"; import { save } from "@/repository/access"; import { useConfigContext } from "@/providers/config"; @@ -27,7 +27,7 @@ const AccessNamesiloForm = ({ data, op, onAfterReq }: AccessNamesiloFormProps) = .string() .min(1, "access.authorization.form.name.placeholder") .max(64, t("common.errmsg.string_max", { max: 64 })), - configType: accessFormType, + configType: accessTypeFormSchema, apiKey: z .string() .min(1, "access.authorization.form.namesilo_api_key.placeholder") @@ -54,7 +54,7 @@ const AccessNamesiloForm = ({ data, op, onAfterReq }: AccessNamesiloFormProps) = id: data.id as string, name: data.name, configType: data.configType, - usage: getUsageByConfigType(data.configType), + usage: accessProvidersMap.get(data.configType)!.usage, config: { apiKey: data.apiKey, }, diff --git a/ui/src/components/certimate/AccessPdnsForm.tsx b/ui/src/components/certimate/AccessPdnsForm.tsx index 37a00f9c..f527fc0c 100644 --- a/ui/src/components/certimate/AccessPdnsForm.tsx +++ b/ui/src/components/certimate/AccessPdnsForm.tsx @@ -8,7 +8,7 @@ import { Button } from "@/components/ui/button"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { PbErrorData } from "@/domain/base"; -import { Access, PdnsConfig, accessFormType, getUsageByConfigType } from "@/domain/access"; +import { accessProvidersMap, accessTypeFormSchema, type Access, type PdnsConfig } from "@/domain/access"; import { save } from "@/repository/access"; import { useConfigContext } from "@/providers/config"; @@ -27,7 +27,7 @@ const AccessPdnsForm = ({ data, op, onAfterReq }: AccessPdnsFormProps) => { .string() .min(1, "access.authorization.form.name.placeholder") .max(64, t("common.errmsg.string_max", { max: 64 })), - configType: accessFormType, + configType: accessTypeFormSchema, apiUrl: z.string().url("common.errmsg.url_invalid"), apiKey: z .string() @@ -57,7 +57,7 @@ const AccessPdnsForm = ({ data, op, onAfterReq }: AccessPdnsFormProps) => { id: data.id as string, name: data.name, configType: data.configType, - usage: getUsageByConfigType(data.configType), + usage: accessProvidersMap.get(data.configType)!.usage, config: { apiUrl: data.apiUrl, apiKey: data.apiKey, diff --git a/ui/src/components/certimate/AccessQiniuForm.tsx b/ui/src/components/certimate/AccessQiniuForm.tsx index 8d73bf00..bf9d93c4 100644 --- a/ui/src/components/certimate/AccessQiniuForm.tsx +++ b/ui/src/components/certimate/AccessQiniuForm.tsx @@ -8,7 +8,7 @@ import { Button } from "@/components/ui/button"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { PbErrorData } from "@/domain/base"; -import { Access, accessFormType, getUsageByConfigType, QiniuConfig } from "@/domain/access"; +import { accessProvidersMap, accessTypeFormSchema, type Access, type QiniuConfig } from "@/domain/access"; import { save } from "@/repository/access"; import { useConfigContext } from "@/providers/config"; @@ -27,7 +27,7 @@ const AccessQiniuForm = ({ data, op, onAfterReq }: AccessQiniuFormProps) => { .string() .min(1, "access.authorization.form.name.placeholder") .max(64, t("common.errmsg.string_max", { max: 64 })), - configType: accessFormType, + configType: accessTypeFormSchema, accessKey: z.string().min(1, "access.authorization.form.access_key.placeholder").max(64), secretKey: z.string().min(1, "access.authorization.form.secret_key.placeholder").max(64), }); @@ -54,7 +54,7 @@ const AccessQiniuForm = ({ data, op, onAfterReq }: AccessQiniuFormProps) => { id: data.id as string, name: data.name, configType: data.configType, - usage: getUsageByConfigType(data.configType), + usage: accessProvidersMap.get(data.configType)!.usage, config: { accessKey: data.accessKey, secretKey: data.secretKey, diff --git a/ui/src/components/certimate/AccessSSHForm.tsx b/ui/src/components/certimate/AccessSSHForm.tsx index 024c2554..440c585c 100644 --- a/ui/src/components/certimate/AccessSSHForm.tsx +++ b/ui/src/components/certimate/AccessSSHForm.tsx @@ -6,7 +6,6 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { Plus } from "lucide-react"; import { ClientResponseError } from "pocketbase"; -import { Access, accessFormType, getUsageByConfigType, SSHConfig } from "@/domain/access"; import { Button } from "@/components/ui/button"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; @@ -15,6 +14,7 @@ import AccessGroupEdit from "./AccessGroupEdit"; import { readFileContent } from "@/lib/file"; import { cn } from "@/lib/utils"; import { PbErrorData } from "@/domain/base"; +import { accessProvidersMap, accessTypeFormSchema, type Access, type SSHConfig } from "@/domain/access"; import { save } from "@/repository/access"; import { updateById } from "@/repository/access_group"; import { useConfigContext } from "@/providers/config"; @@ -50,7 +50,7 @@ const AccessSSHForm = ({ data, op, onAfterReq }: AccessSSHFormProps) => { .string() .min(1, "access.authorization.form.name.placeholder") .max(64, t("common.errmsg.string_max", { max: 64 })), - configType: accessFormType, + configType: accessTypeFormSchema, host: z.string().refine( (str) => { return ipReg.test(str) || domainReg.test(str); @@ -119,7 +119,7 @@ const AccessSSHForm = ({ data, op, onAfterReq }: AccessSSHFormProps) => { id: data.id as string, name: data.name, configType: data.configType, - usage: getUsageByConfigType(data.configType), + usage: accessProvidersMap.get(data.configType)!.usage, group: group, config: { host: data.host, diff --git a/ui/src/components/certimate/AccessTencentForm.tsx b/ui/src/components/certimate/AccessTencentForm.tsx index a3aa55d3..40037d1e 100644 --- a/ui/src/components/certimate/AccessTencentForm.tsx +++ b/ui/src/components/certimate/AccessTencentForm.tsx @@ -8,7 +8,7 @@ import { Button } from "@/components/ui/button"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { PbErrorData } from "@/domain/base"; -import { Access, accessFormType, getUsageByConfigType, TencentConfig } from "@/domain/access"; +import { accessProvidersMap, accessTypeFormSchema, type Access, type TencentConfig } from "@/domain/access"; import { save } from "@/repository/access"; import { useConfigContext } from "@/providers/config"; @@ -27,7 +27,7 @@ const AccessTencentForm = ({ data, op, onAfterReq }: AccessTencentFormProps) => .string() .min(1, "access.authorization.form.name.placeholder") .max(64, t("common.errmsg.string_max", { max: 64 })), - configType: accessFormType, + configType: accessTypeFormSchema, secretId: z .string() .min(1, "access.authorization.form.secret_id.placeholder") @@ -60,7 +60,7 @@ const AccessTencentForm = ({ data, op, onAfterReq }: AccessTencentFormProps) => id: data.id as string, name: data.name, configType: data.configType, - usage: getUsageByConfigType(data.configType), + usage: accessProvidersMap.get(data.configType)!.usage, config: { secretId: data.secretId, secretKey: data.secretKey, diff --git a/ui/src/components/certimate/AccessWebhookForm.tsx b/ui/src/components/certimate/AccessWebhookForm.tsx index 40f8f56d..1b8824d5 100644 --- a/ui/src/components/certimate/AccessWebhookForm.tsx +++ b/ui/src/components/certimate/AccessWebhookForm.tsx @@ -8,7 +8,7 @@ import { Button } from "@/components/ui/button"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { PbErrorData } from "@/domain/base"; -import { Access, accessFormType, getUsageByConfigType, WebhookConfig } from "@/domain/access"; +import { Access, accessProvidersMap, accessTypeFormSchema, WebhookConfig } from "@/domain/access"; import { save } from "@/repository/access"; import { useConfigContext } from "@/providers/config"; @@ -27,7 +27,7 @@ const AccessWebhookForm = ({ data, op, onAfterReq }: AccessWebhookFormProps) => .string() .min(1, "access.authorization.form.name.placeholder") .max(64, t("common.errmsg.string_max", { max: 64 })), - configType: accessFormType, + configType: accessTypeFormSchema, url: z.string().url("common.errmsg.url_invalid"), }); @@ -51,7 +51,7 @@ const AccessWebhookForm = ({ data, op, onAfterReq }: AccessWebhookFormProps) => id: data.id as string, name: data.name, configType: data.configType, - usage: getUsageByConfigType(data.configType), + usage: accessProvidersMap.get(data.configType)!.usage, config: { url: data.url, }, diff --git a/ui/src/components/certimate/DeployList.tsx b/ui/src/components/certimate/DeployList.tsx index 8f68bafe..dc459a32 100644 --- a/ui/src/components/certimate/DeployList.tsx +++ b/ui/src/components/certimate/DeployList.tsx @@ -16,8 +16,8 @@ import { Textarea } from "@/components/ui/textarea"; import AccessEditDialog from "./AccessEditDialog"; import KVList from "./KVList"; import { DeployConfig, KVType, targetTypeKeys, targetTypeMap } from "@/domain/domain"; -import { accessTypeMap } from "@/domain/access"; -import { useConfig } from "@/providers/config"; +import { accessProvidersMap } from "@/domain/access"; +import { useConfigContext } from "@/providers/config"; type DeployEditContextProps = { deploy: DeployConfig; @@ -32,6 +32,68 @@ export const useDeployEditContext = () => { return useContext(DeployEditContext); }; +type DeployItemProps = { + item: DeployConfig; + onDelete: () => void; + onSave: (deploy: DeployConfig) => void; +}; + +const DeployItem = ({ item, onDelete, onSave }: DeployItemProps) => { + const { + config: { accesses }, + } = useConfigContext(); + const { t } = useTranslation(); + + const access = accesses.find((access) => access.id === item.access); + + const getTypeIcon = () => { + if (!access) { + return ""; + } + + return accessProvidersMap.get(access.configType)?.icon || ""; + }; + + const getTypeName = () => { + if (!access) { + return ""; + } + + return t(accessProvidersMap.get(access.configType)?.name || ""); + }; + + return ( +
+
+
+ +
+
+
{getTypeName()}
+
{access?.name}
+
+
+
+ } + deployConfig={item} + onSave={(deploy: DeployConfig) => { + onSave(deploy); + }} + /> + + { + onDelete(); + }} + /> +
+
+ ); +}; + type DeployListProps = { deploys: DeployConfig[]; onChange: (deploys: DeployConfig[]) => void; @@ -129,80 +191,6 @@ const DeployList = ({ deploys, onChange }: DeployListProps) => { export default DeployList; -type DeployItemProps = { - item: DeployConfig; - onDelete: () => void; - onSave: (deploy: DeployConfig) => void; -}; - -const DeployItem = ({ item, onDelete, onSave }: DeployItemProps) => { - const { - config: { accesses }, - } = useConfig(); - const { t } = useTranslation(); - - const access = accesses.find((access) => access.id === item.access); - - const getTypeIcon = () => { - if (!access) { - return ""; - } - - const accessType = accessTypeMap.get(access.configType); - - if (accessType) { - return accessType[1]; - } - - return ""; - }; - - const getTypeName = () => { - if (!access) { - return ""; - } - - const accessType = targetTypeMap.get(item.type); - - if (accessType) { - return t(accessType[0]); - } - - return ""; - }; - - return ( -
-
-
- -
-
-
{getTypeName()}
-
{access?.name}
-
-
-
- } - deployConfig={item} - onSave={(deploy: DeployConfig) => { - onSave(deploy); - }} - /> - - { - onDelete(); - }} - /> -
-
- ); -}; - type DeployEditDialogProps = { trigger: React.ReactNode; deployConfig?: DeployConfig; @@ -212,7 +200,7 @@ type DeployEditDialogProps = { const DeployEditDialog = ({ trigger, deployConfig, onSave }: DeployEditDialogProps) => { const { config: { accesses }, - } = useConfig(); + } = useConfigContext(); const [deployType, setDeployType] = useState(); @@ -317,8 +305,8 @@ const DeployEditDialog = ({ trigger, deployConfig, onSave }: DeployEditDialogPro @@ -391,7 +379,7 @@ const DeployEditDialog = ({ trigger, deployConfig, onSave }: DeployEditDialogPro {targetAccesses.map((item) => (
- +
{item.name}
diff --git a/ui/src/domain/access.ts b/ui/src/domain/access.ts index 988374b8..eaabfbd2 100644 --- a/ui/src/domain/access.ts +++ b/ui/src/domain/access.ts @@ -1,53 +1,58 @@ import { z } from "zod"; -export const accessTypeMap: Map = new Map([ - ["aliyun", ["common.provider.aliyun", "/imgs/providers/aliyun.svg"]], - ["tencent", ["common.provider.tencent", "/imgs/providers/tencent.svg"]], - ["huaweicloud", ["common.provider.huaweicloud", "/imgs/providers/huaweicloud.svg"]], - ["qiniu", ["common.provider.qiniu", "/imgs/providers/qiniu.svg"]], - ["aws", ["common.provider.aws", "/imgs/providers/aws.svg"]], - ["cloudflare", ["common.provider.cloudflare", "/imgs/providers/cloudflare.svg"]], - ["namesilo", ["common.provider.namesilo", "/imgs/providers/namesilo.svg"]], - ["godaddy", ["common.provider.godaddy", "/imgs/providers/godaddy.svg"]], - ["pdns", ["common.provider.pdns", "/imgs/providers/pdns.svg"]], - ["httpreq", ["common.provider.httpreq", "/imgs/providers/httpreq.svg"]], - ["local", ["common.provider.local", "/imgs/providers/local.svg"]], - ["ssh", ["common.provider.ssh", "/imgs/providers/ssh.svg"]], - ["webhook", ["common.provider.webhook", "/imgs/providers/webhook.svg"]], - ["k8s", ["common.provider.kubernetes", "/imgs/providers/k8s.svg"]], -]); +type AccessUsages = "apply" | "deploy" | "all"; -export const getProviderInfo = (t: string) => { - return accessTypeMap.get(t); +type AccessProvider = { + type: string; + name: string; + icon: string; + usage: AccessUsages; }; -export const accessFormType = z.union( +export const accessProvidersMap: Map = new Map( [ - z.literal("aliyun"), - z.literal("tencent"), - z.literal("huaweicloud"), - z.literal("qiniu"), - z.literal("aws"), - z.literal("cloudflare"), - z.literal("namesilo"), - z.literal("godaddy"), - z.literal("pdns"), - z.literal("httpreq"), - z.literal("local"), - z.literal("ssh"), - z.literal("webhook"), - z.literal("k8s"), + ["aliyun", "common.provider.aliyun", "/imgs/providers/aliyun.svg", "all"], + ["tencent", "common.provider.tencent", "/imgs/providers/tencent.svg", "all"], + ["huaweicloud", "common.provider.huaweicloud", "/imgs/providers/huaweicloud.svg", "all"], + ["qiniu", "common.provider.qiniu", "/imgs/providers/qiniu.svg", "deploy"], + ["aws", "common.provider.aws", "/imgs/providers/aws.svg", "apply"], + ["cloudflare", "common.provider.cloudflare", "/imgs/providers/cloudflare.svg", "apply"], + ["namesilo", "common.provider.namesilo", "/imgs/providers/namesilo.svg", "apply"], + ["godaddy", "common.provider.godaddy", "/imgs/providers/godaddy.svg", "apply"], + ["pdns", "common.provider.pdns", "/imgs/providers/pdns.svg", "apply"], + ["httpreq", "common.provider.httpreq", "/imgs/providers/httpreq.svg", "apply"], + ["local", "common.provider.local", "/imgs/providers/local.svg", "deploy"], + ["ssh", "common.provider.ssh", "/imgs/providers/ssh.svg", "deploy"], + ["webhook", "common.provider.webhook", "/imgs/providers/webhook.svg", "deploy"], + ["k8s", "common.provider.kubernetes", "/imgs/providers/k8s.svg", "deploy"], + ].map(([type, name, icon, usage]) => [type, { type, name, icon, usage: usage as AccessUsages }]) +); + +export const accessTypeFormSchema = z.union( + [ + z.literal("aliyun" as string), + z.literal("tencent" as string), + z.literal("huaweicloud" as string), + z.literal("qiniu" as string), + z.literal("aws" as string), + z.literal("cloudflare" as string), + z.literal("namesilo" as string), + z.literal("godaddy" as string), + z.literal("pdns" as string), + z.literal("httpreq" as string), + z.literal("local" as string), + z.literal("ssh" as string), + z.literal("webhook" as string), + z.literal("k8s" as string), ], { message: "access.authorization.form.type.placeholder" } ); -type AccessUsage = "apply" | "deploy" | "all"; - export type Access = { id: string; name: string; configType: string; - usage: AccessUsage; + usage: AccessUsages; group?: string; config: | AliyunConfig @@ -141,30 +146,3 @@ export type WebhookConfig = { export type KubernetesConfig = { kubeConfig: string; }; - -export const getUsageByConfigType = (configType: string): AccessUsage => { - switch (configType) { - case "aliyun": - case "tencent": - case "huaweicloud": - return "all"; - - case "qiniu": - case "local": - case "ssh": - case "webhook": - case "k8s": - return "deploy"; - - case "aws": - case "cloudflare": - case "namesilo": - case "godaddy": - case "pdns": - case "httpreq": - return "apply"; - - default: - return "all"; - } -}; diff --git a/ui/src/pages/access/Access.tsx b/ui/src/pages/access/Access.tsx index ac3b6549..6fb3cd66 100644 --- a/ui/src/pages/access/Access.tsx +++ b/ui/src/pages/access/Access.tsx @@ -21,7 +21,7 @@ import AccessGroupEdit from "@/components/certimate/AccessGroupEdit"; import AccessGroupList from "@/components/certimate/AccessGroupList"; import XPagination from "@/components/certimate/XPagination"; import { convertZulu2Beijing } from "@/lib/time"; -import { Access as AccessType, accessTypeMap } from "@/domain/access"; +import { Access as AccessType, accessProvidersMap } from "@/domain/access"; import { remove } from "@/repository/access"; import { useConfigContext } from "@/providers/config"; @@ -119,8 +119,8 @@ const Access = () => { >
{access.name}
- -
{t(accessTypeMap.get(access.configType)?.[0] || "")}
+ +
{t(accessProvidersMap.get(access.configType)?.name || "")}
{access.created && convertZulu2Beijing(access.created)}
diff --git a/ui/src/pages/domains/Edit.tsx b/ui/src/pages/domains/Edit.tsx index 66e66a30..fbd57444 100644 --- a/ui/src/pages/domains/Edit.tsx +++ b/ui/src/pages/domains/Edit.tsx @@ -21,7 +21,7 @@ import EmailsEdit from "@/components/certimate/EmailsEdit"; import StringList from "@/components/certimate/StringList"; import { cn } from "@/lib/utils"; import { PbErrorData } from "@/domain/base"; -import { accessTypeMap } from "@/domain/access"; +import { accessProvidersMap } from "@/domain/access"; import { EmailsSetting } from "@/domain/settings"; import { DeployConfig, Domain } from "@/domain/domain"; import { save, get } from "@/repository/domains"; @@ -330,7 +330,7 @@ const Edit = () => { .map((item) => (
- +
{item.name}
diff --git a/ui/src/providers/config/index.tsx b/ui/src/providers/config/index.tsx index 67455c49..0b98c129 100644 --- a/ui/src/providers/config/index.tsx +++ b/ui/src/providers/config/index.tsx @@ -16,10 +16,10 @@ export type ConfigData = { export type ConfigContext = { config: ConfigData; - deleteAccess: (id: string) => void; + setEmails: (email: Setting) => void; addAccess: (access: Access) => void; updateAccess: (access: Access) => void; - setEmails: (email: Setting) => void; + deleteAccess: (id: string) => void; setAccessGroups: (accessGroups: AccessGroup[]) => void; reloadAccessGroups: () => void; }; @@ -96,10 +96,10 @@ export const ConfigProvider = ({ children }: ConfigProviderProps) => { emails: config.emails, accessGroups: config.accessGroups, }, - deleteAccess, - addAccess, setEmails, + addAccess, updateAccess, + deleteAccess, setAccessGroups, reloadAccessGroups, }}