mirror of https://github.com/usual2970/certimate
				
				
				
			refactor: clean code
							parent
							
								
									2a68372713
								
							
						
					
					
						commit
						dfc192cb68
					
				|  | @ -75,7 +75,7 @@ func (u *acmeUser) getPrivateKeyPEM() string { | |||
| 	return u.privkey | ||||
| } | ||||
| 
 | ||||
| type AcmeAccountRepository interface { | ||||
| type acmeAccountRepository interface { | ||||
| 	GetByCAAndEmail(ca, email string) (*domain.AcmeAccount, error) | ||||
| 	Save(ca, email, key string, resource *registration.Resource) error | ||||
| } | ||||
|  |  | |||
|  | @ -2,6 +2,7 @@ package applicant | |||
| 
 | ||||
| import ( | ||||
| 	"context" | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"os" | ||||
| 	"strconv" | ||||
|  | @ -13,7 +14,6 @@ import ( | |||
| 	"github.com/go-acme/lego/v4/challenge/dns01" | ||||
| 	"github.com/go-acme/lego/v4/lego" | ||||
| 
 | ||||
| 	"github.com/usual2970/certimate/internal/app" | ||||
| 	"github.com/usual2970/certimate/internal/domain" | ||||
| 	"github.com/usual2970/certimate/internal/repository" | ||||
| ) | ||||
|  | @ -37,11 +37,11 @@ type ApplyCertResult struct { | |||
| 	CSR               string | ||||
| } | ||||
| 
 | ||||
| type applicant interface { | ||||
| type Applicant interface { | ||||
| 	Apply() (*ApplyCertResult, error) | ||||
| } | ||||
| 
 | ||||
| func NewWithApplyNode(node *domain.WorkflowNode) (applicant, error) { | ||||
| func NewWithApplyNode(node *domain.WorkflowNode) (Applicant, error) { | ||||
| 	if node.Type != domain.WorkflowNodeTypeApply { | ||||
| 		return nil, fmt.Errorf("node type is not apply") | ||||
| 	} | ||||
|  | @ -74,14 +74,15 @@ func NewWithApplyNode(node *domain.WorkflowNode) (applicant, error) { | |||
| } | ||||
| 
 | ||||
| func apply(challengeProvider challenge.Provider, applyConfig *applyConfig) (*ApplyCertResult, error) { | ||||
| 	record, _ := app.GetApp().Dao().FindFirstRecordByFilter("settings", "name='sslProvider'") | ||||
| 	settingsRepo := repository.NewSettingsRepository() | ||||
| 	settings, _ := settingsRepo.GetByName(context.Background(), "sslProvider") | ||||
| 
 | ||||
| 	sslProvider := &acmeSSLProviderConfig{ | ||||
| 		Config:   acmeSSLProviderConfigContent{}, | ||||
| 		Provider: defaultSSLProvider, | ||||
| 	} | ||||
| 	if record != nil { | ||||
| 		if err := record.UnmarshalJSONField("content", sslProvider); err != nil { | ||||
| 	if settings != nil { | ||||
| 		if err := json.Unmarshal([]byte(settings.Content), sslProvider); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 	} | ||||
|  |  | |||
|  | @ -2,14 +2,15 @@ package notify | |||
| 
 | ||||
| import ( | ||||
| 	"context" | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 
 | ||||
| 	"golang.org/x/sync/errgroup" | ||||
| 
 | ||||
| 	"github.com/usual2970/certimate/internal/app" | ||||
| 	"github.com/usual2970/certimate/internal/domain" | ||||
| 	"github.com/usual2970/certimate/internal/pkg/core/notifier" | ||||
| 	"github.com/usual2970/certimate/internal/pkg/utils/maps" | ||||
| 	"github.com/usual2970/certimate/internal/repository" | ||||
| ) | ||||
| 
 | ||||
| func SendToAllChannels(subject, message string) error { | ||||
|  | @ -48,13 +49,14 @@ func SendToChannel(subject, message string, channel string, channelConfig map[st | |||
| } | ||||
| 
 | ||||
| func getEnabledNotifiers() ([]notifier.Notifier, error) { | ||||
| 	settings, err := app.GetApp().Dao().FindFirstRecordByFilter("settings", "name='notifyChannels'") | ||||
| 	settingsRepo := repository.NewSettingsRepository() | ||||
| 	settings, err := settingsRepo.GetByName(context.Background(), "notifyChannels") | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("find notifyChannels error: %w", err) | ||||
| 	} | ||||
| 
 | ||||
| 	rs := make(map[string]map[string]any) | ||||
| 	if err := settings.UnmarshalJSONField("content", &rs); err != nil { | ||||
| 	if err := json.Unmarshal([]byte(settings.Content), &rs); err != nil { | ||||
| 		return nil, fmt.Errorf("unmarshal notifyChannels error: %w", err) | ||||
| 	} | ||||
| 
 | ||||
|  |  | |||
|  | @ -4,7 +4,6 @@ import { createSchemaFieldRule } from "antd-zod"; | |||
| import { z } from "zod"; | ||||
| 
 | ||||
| import { type AccessConfigForACMEHttpReq } from "@/domain/access"; | ||||
| import { useAntdForm } from "@/hooks"; | ||||
| 
 | ||||
| type AccessEditFormACMEHttpReqConfigFieldValues = Partial<AccessConfigForACMEHttpReq>; | ||||
| 
 | ||||
|  | @ -41,17 +40,13 @@ const AccessEditFormACMEHttpReqConfig = ({ form, formName, disabled, initialValu | |||
|       .nullish(), | ||||
|   }); | ||||
|   const formRule = createSchemaFieldRule(formSchema); | ||||
|   const { form: formInst, formProps } = useAntdForm<z.infer<typeof formSchema>>({ | ||||
|     form: form, | ||||
|     initialValues: initialValues ?? initFormModel(), | ||||
|   }); | ||||
| 
 | ||||
|   const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => { | ||||
|     onValuesChange?.(values as AccessEditFormACMEHttpReqConfigFieldValues); | ||||
|   }; | ||||
| 
 | ||||
|   return ( | ||||
|     <Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|     <Form form={form} disabled={disabled} initialValues={initialValues ?? initFormModel()} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|       <Form.Item | ||||
|         name="endpoint" | ||||
|         label={t("access.form.acmehttpreq_endpoint.label")} | ||||
|  |  | |||
|  | @ -4,7 +4,6 @@ import { createSchemaFieldRule } from "antd-zod"; | |||
| import { z } from "zod"; | ||||
| 
 | ||||
| import { type AccessConfigForAWS } from "@/domain/access"; | ||||
| import { useAntdForm } from "@/hooks"; | ||||
| 
 | ||||
| type AccessEditFormAWSConfigFieldValues = Partial<AccessConfigForAWS>; | ||||
| 
 | ||||
|  | @ -53,17 +52,13 @@ const AccessEditFormAWSConfig = ({ form, formName, disabled, initialValues, onVa | |||
|       .nullish(), | ||||
|   }); | ||||
|   const formRule = createSchemaFieldRule(formSchema); | ||||
|   const { form: formInst, formProps } = useAntdForm<z.infer<typeof formSchema>>({ | ||||
|     form: form, | ||||
|     initialValues: initialValues ?? initFormModel(), | ||||
|   }); | ||||
| 
 | ||||
|   const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => { | ||||
|     onValuesChange?.(values as AccessEditFormAWSConfigFieldValues); | ||||
|   }; | ||||
| 
 | ||||
|   return ( | ||||
|     <Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|     <Form form={form} disabled={disabled} initialValues={initialValues ?? initFormModel()} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|       <Form.Item | ||||
|         name="accessKeyId" | ||||
|         label={t("access.form.aws_access_key_id.label")} | ||||
|  |  | |||
|  | @ -4,7 +4,6 @@ import { createSchemaFieldRule } from "antd-zod"; | |||
| import { z } from "zod"; | ||||
| 
 | ||||
| import { type AccessConfigForAliyun } from "@/domain/access"; | ||||
| import { useAntdForm } from "@/hooks"; | ||||
| 
 | ||||
| type AccessEditFormAliyunConfigFieldValues = Partial<AccessConfigForAliyun>; | ||||
| 
 | ||||
|  | @ -39,17 +38,13 @@ const AccessEditFormAliyunConfig = ({ form, formName, disabled, initialValues, o | |||
|       .trim(), | ||||
|   }); | ||||
|   const formRule = createSchemaFieldRule(formSchema); | ||||
|   const { form: formInst, formProps } = useAntdForm<z.infer<typeof formSchema>>({ | ||||
|     form: form, | ||||
|     initialValues: initialValues ?? initFormModel(), | ||||
|   }); | ||||
| 
 | ||||
|   const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => { | ||||
|     onValuesChange?.(values as AccessEditFormAliyunConfigFieldValues); | ||||
|   }; | ||||
| 
 | ||||
|   return ( | ||||
|     <Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|     <Form form={form} disabled={disabled} initialValues={initialValues ?? initFormModel()} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|       <Form.Item | ||||
|         name="accessKeyId" | ||||
|         label={t("access.form.aliyun_access_key_id.label")} | ||||
|  |  | |||
|  | @ -4,7 +4,6 @@ import { createSchemaFieldRule } from "antd-zod"; | |||
| import { z } from "zod"; | ||||
| 
 | ||||
| import { type AccessConfigForBaiduCloud } from "@/domain/access"; | ||||
| import { useAntdForm } from "@/hooks"; | ||||
| 
 | ||||
| type AccessEditFormBaiduCloudConfigFieldValues = Partial<AccessConfigForBaiduCloud>; | ||||
| 
 | ||||
|  | @ -39,17 +38,13 @@ const AccessEditFormBaiduCloudConfig = ({ form, formName, disabled, initialValue | |||
|       .trim(), | ||||
|   }); | ||||
|   const formRule = createSchemaFieldRule(formSchema); | ||||
|   const { form: formInst, formProps } = useAntdForm<z.infer<typeof formSchema>>({ | ||||
|     form: form, | ||||
|     initialValues: initialValues ?? initFormModel(), | ||||
|   }); | ||||
| 
 | ||||
|   const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => { | ||||
|     onValuesChange?.(values as AccessEditFormBaiduCloudConfigFieldValues); | ||||
|   }; | ||||
| 
 | ||||
|   return ( | ||||
|     <Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|     <Form form={form} disabled={disabled} initialValues={initialValues ?? initFormModel()} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|       <Form.Item | ||||
|         name="accessKeyId" | ||||
|         label={t("access.form.baiducloud_access_key_id.label")} | ||||
|  |  | |||
|  | @ -4,7 +4,6 @@ import { createSchemaFieldRule } from "antd-zod"; | |||
| import { z } from "zod"; | ||||
| 
 | ||||
| import { type AccessConfigForBytePlus } from "@/domain/access"; | ||||
| import { useAntdForm } from "@/hooks"; | ||||
| 
 | ||||
| type AccessEditFormBytePlusConfigFieldValues = Partial<AccessConfigForBytePlus>; | ||||
| 
 | ||||
|  | @ -39,17 +38,13 @@ const AccessEditFormBytePlusConfig = ({ form, formName, disabled, initialValues, | |||
|       .trim(), | ||||
|   }); | ||||
|   const formRule = createSchemaFieldRule(formSchema); | ||||
|   const { form: formInst, formProps } = useAntdForm<z.infer<typeof formSchema>>({ | ||||
|     form: form, | ||||
|     initialValues: initialValues ?? initFormModel(), | ||||
|   }); | ||||
| 
 | ||||
|   const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => { | ||||
|     onValuesChange?.(values as AccessEditFormBytePlusConfigFieldValues); | ||||
|   }; | ||||
| 
 | ||||
|   return ( | ||||
|     <Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|     <Form form={form} disabled={disabled} initialValues={initialValues ?? initFormModel()} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|       <Form.Item | ||||
|         name="accessKey" | ||||
|         label={t("access.form.byteplus_access_key.label")} | ||||
|  |  | |||
|  | @ -4,7 +4,6 @@ import { createSchemaFieldRule } from "antd-zod"; | |||
| import { z } from "zod"; | ||||
| 
 | ||||
| import { type AccessConfigForCloudflare } from "@/domain/access"; | ||||
| import { useAntdForm } from "@/hooks"; | ||||
| 
 | ||||
| type AccessEditFormCloudflareConfigFieldValues = Partial<AccessConfigForCloudflare>; | ||||
| 
 | ||||
|  | @ -33,17 +32,13 @@ const AccessEditFormCloudflareConfig = ({ form, formName, disabled, initialValue | |||
|       .trim(), | ||||
|   }); | ||||
|   const formRule = createSchemaFieldRule(formSchema); | ||||
|   const { form: formInst, formProps } = useAntdForm<z.infer<typeof formSchema>>({ | ||||
|     form: form, | ||||
|     initialValues: initialValues ?? initFormModel(), | ||||
|   }); | ||||
| 
 | ||||
|   const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => { | ||||
|     onValuesChange?.(values as AccessEditFormCloudflareConfigFieldValues); | ||||
|   }; | ||||
| 
 | ||||
|   return ( | ||||
|     <Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|     <Form form={form} disabled={disabled} initialValues={initialValues ?? initFormModel()} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|       <Form.Item | ||||
|         name="dnsApiToken" | ||||
|         label={t("access.form.cloudflare_dns_api_token.label")} | ||||
|  |  | |||
|  | @ -4,7 +4,6 @@ import { createSchemaFieldRule } from "antd-zod"; | |||
| import { z } from "zod"; | ||||
| 
 | ||||
| import { type AccessConfigForDogeCloud } from "@/domain/access"; | ||||
| import { useAntdForm } from "@/hooks"; | ||||
| 
 | ||||
| type AccessEditFormDogeCloudConfigFieldValues = Partial<AccessConfigForDogeCloud>; | ||||
| 
 | ||||
|  | @ -39,17 +38,13 @@ const AccessEditFormDogeCloudConfig = ({ form, formName, disabled, initialValues | |||
|       .trim(), | ||||
|   }); | ||||
|   const formRule = createSchemaFieldRule(formSchema); | ||||
|   const { form: formInst, formProps } = useAntdForm<z.infer<typeof formSchema>>({ | ||||
|     form: form, | ||||
|     initialValues: initialValues ?? initFormModel(), | ||||
|   }); | ||||
| 
 | ||||
|   const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => { | ||||
|     onValuesChange?.(values as AccessEditFormDogeCloudConfigFieldValues); | ||||
|   }; | ||||
| 
 | ||||
|   return ( | ||||
|     <Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|     <Form form={form} disabled={disabled} initialValues={initialValues ?? initFormModel()} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|       <Form.Item | ||||
|         name="accessKey" | ||||
|         label={t("access.form.dogecloud_access_key.label")} | ||||
|  |  | |||
|  | @ -4,7 +4,6 @@ import { createSchemaFieldRule } from "antd-zod"; | |||
| import { z } from "zod"; | ||||
| 
 | ||||
| import { type AccessConfigForGoDaddy } from "@/domain/access"; | ||||
| import { useAntdForm } from "@/hooks"; | ||||
| 
 | ||||
| type AccessEditFormGoDaddyConfigFieldValues = Partial<AccessConfigForGoDaddy>; | ||||
| 
 | ||||
|  | @ -39,17 +38,13 @@ const AccessEditFormGoDaddyConfig = ({ form, formName, disabled, initialValues, | |||
|       .trim(), | ||||
|   }); | ||||
|   const formRule = createSchemaFieldRule(formSchema); | ||||
|   const { form: formInst, formProps } = useAntdForm<z.infer<typeof formSchema>>({ | ||||
|     form: form, | ||||
|     initialValues: initialValues ?? initFormModel(), | ||||
|   }); | ||||
| 
 | ||||
|   const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => { | ||||
|     onValuesChange?.(values as AccessEditFormGoDaddyConfigFieldValues); | ||||
|   }; | ||||
| 
 | ||||
|   return ( | ||||
|     <Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|     <Form form={form} disabled={disabled} initialValues={initialValues ?? initFormModel()} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|       <Form.Item | ||||
|         name="apiKey" | ||||
|         label={t("access.form.godaddy_api_key.label")} | ||||
|  |  | |||
|  | @ -4,7 +4,6 @@ import { createSchemaFieldRule } from "antd-zod"; | |||
| import { z } from "zod"; | ||||
| 
 | ||||
| import { type AccessConfigForHuaweiCloud } from "@/domain/access"; | ||||
| import { useAntdForm } from "@/hooks"; | ||||
| 
 | ||||
| type AccessEditFormHuaweiCloudConfigFieldValues = Partial<AccessConfigForHuaweiCloud>; | ||||
| 
 | ||||
|  | @ -46,17 +45,13 @@ const AccessEditFormHuaweiCloudConfig = ({ form, formName, disabled, initialValu | |||
|       .nullish(), | ||||
|   }); | ||||
|   const formRule = createSchemaFieldRule(formSchema); | ||||
|   const { form: formInst, formProps } = useAntdForm<z.infer<typeof formSchema>>({ | ||||
|     form: form, | ||||
|     initialValues: initialValues ?? initFormModel(), | ||||
|   }); | ||||
| 
 | ||||
|   const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => { | ||||
|     onValuesChange?.(values as AccessEditFormHuaweiCloudConfigFieldValues); | ||||
|   }; | ||||
| 
 | ||||
|   return ( | ||||
|     <Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|     <Form form={form} disabled={disabled} initialValues={initialValues ?? initFormModel()} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|       <Form.Item | ||||
|         name="accessKeyId" | ||||
|         label={t("access.form.huaweicloud_access_key_id.label")} | ||||
|  |  | |||
|  | @ -8,7 +8,6 @@ import { createSchemaFieldRule } from "antd-zod"; | |||
| import { z } from "zod"; | ||||
| 
 | ||||
| import { type AccessConfigForKubernetes } from "@/domain/access"; | ||||
| import { useAntdForm } from "@/hooks"; | ||||
| import { readFileContent } from "@/utils/file"; | ||||
| 
 | ||||
| type AccessEditFormKubernetesConfigFieldValues = Partial<AccessConfigForKubernetes>; | ||||
|  | @ -36,14 +35,11 @@ const AccessEditFormKubernetesConfig = ({ form, formName, disabled, initialValue | |||
|       .nullish(), | ||||
|   }); | ||||
|   const formRule = createSchemaFieldRule(formSchema); | ||||
|   const { form: formInst, formProps } = useAntdForm<z.infer<typeof formSchema>>({ | ||||
|     form: form, | ||||
|     initialValues: initialValues ?? initFormModel(), | ||||
|   }); | ||||
| 
 | ||||
|   const [kubeFileList, setKubeFileList] = useState<UploadFile[]>([]); | ||||
|   const fieldKubeConfig = Form.useWatch("kubeConfig", form); | ||||
|   const [fieldKubeFileList, setFieldKubeFileList] = useState<UploadFile[]>([]); | ||||
|   useDeepCompareEffect(() => { | ||||
|     setKubeFileList(initialValues?.kubeConfig?.trim() ? [{ uid: "-1", name: "kubeconfig", status: "done" }] : []); | ||||
|     setFieldKubeFileList(initialValues?.kubeConfig?.trim() ? [{ uid: "-1", name: "kubeconfig", status: "done" }] : []); | ||||
|   }, [initialValues]); | ||||
| 
 | ||||
|   const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => { | ||||
|  | @ -52,31 +48,26 @@ const AccessEditFormKubernetesConfig = ({ form, formName, disabled, initialValue | |||
| 
 | ||||
|   const handleKubeFileChange: UploadProps["onChange"] = async ({ file }) => { | ||||
|     if (file && file.status !== "removed") { | ||||
|       formInst.setFieldValue("kubeConfig", await readFileContent(file.originFileObj ?? (file as unknown as File))); | ||||
|       setKubeFileList([file]); | ||||
|       form.setFieldValue("kubeConfig", await readFileContent(file.originFileObj ?? (file as unknown as File))); | ||||
|       setFieldKubeFileList([file]); | ||||
|     } else { | ||||
|       formInst.setFieldValue("kubeConfig", ""); | ||||
|       setKubeFileList([]); | ||||
|       form.setFieldValue("kubeConfig", ""); | ||||
|       setFieldKubeFileList([]); | ||||
|     } | ||||
| 
 | ||||
|     flushSync(() => onValuesChange?.(formInst.getFieldsValue(true))); | ||||
|     flushSync(() => onValuesChange?.(form.getFieldsValue(true))); | ||||
|   }; | ||||
| 
 | ||||
|   return ( | ||||
|     <Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|     <Form form={form} disabled={disabled} initialValues={initialValues ?? initFormModel()} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|       <Form.Item name="kubeConfig" noStyle rules={[formRule]}> | ||||
|         <Input.TextArea | ||||
|           autoComplete="new-password" | ||||
|           hidden | ||||
|           placeholder={t("access.form.k8s_kubeconfig.placeholder")} | ||||
|           value={formInst.getFieldValue("kubeConfig")} | ||||
|         /> | ||||
|         <Input.TextArea autoComplete="new-password" hidden placeholder={t("access.form.k8s_kubeconfig.placeholder")} value={fieldKubeConfig} /> | ||||
|       </Form.Item> | ||||
|       <Form.Item | ||||
|         label={t("access.form.k8s_kubeconfig.label")} | ||||
|         tooltip={<span dangerouslySetInnerHTML={{ __html: t("access.form.k8s_kubeconfig.tooltip") }}></span>} | ||||
|       > | ||||
|         <Upload beforeUpload={() => false} fileList={kubeFileList} maxCount={1} onChange={handleKubeFileChange}> | ||||
|         <Upload beforeUpload={() => false} fileList={fieldKubeFileList} maxCount={1} onChange={handleKubeFileChange}> | ||||
|           <Button icon={<UploadOutlinedIcon />}>{t("access.form.k8s_kubeconfig.upload")}</Button> | ||||
|         </Upload> | ||||
|       </Form.Item> | ||||
|  |  | |||
|  | @ -1,7 +1,6 @@ | |||
| import { Form, type FormInstance } from "antd"; | ||||
| 
 | ||||
| import { type AccessConfigForLocal } from "@/domain/access"; | ||||
| import { useAntdForm } from "@/hooks"; | ||||
| 
 | ||||
| type AccessEditFormLocalConfigFieldValues = Partial<AccessConfigForLocal>; | ||||
| 
 | ||||
|  | @ -18,16 +17,20 @@ const initFormModel = (): AccessEditFormLocalConfigFieldValues => { | |||
| }; | ||||
| 
 | ||||
| const AccessEditFormLocalConfig = ({ form, formName, disabled, initialValues, onValuesChange }: AccessEditFormLocalConfigProps) => { | ||||
|   const { form: formInst, formProps } = useAntdForm({ | ||||
|     form: form, | ||||
|     initialValues: initialValues ?? initFormModel(), | ||||
|   }); | ||||
| 
 | ||||
|   const handleFormChange = (_: unknown, values: unknown) => { | ||||
|     onValuesChange?.(values as AccessEditFormLocalConfigFieldValues); | ||||
|   }; | ||||
| 
 | ||||
|   return <Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}></Form>; | ||||
|   return ( | ||||
|     <Form | ||||
|       form={form} | ||||
|       disabled={disabled} | ||||
|       initialValues={initialValues ?? initFormModel()} | ||||
|       layout="vertical" | ||||
|       name={formName} | ||||
|       onValuesChange={handleFormChange} | ||||
|     ></Form> | ||||
|   ); | ||||
| }; | ||||
| 
 | ||||
| export default AccessEditFormLocalConfig; | ||||
|  |  | |||
|  | @ -4,7 +4,6 @@ import { createSchemaFieldRule } from "antd-zod"; | |||
| import { z } from "zod"; | ||||
| 
 | ||||
| import { type AccessConfigForNameDotCom } from "@/domain/access"; | ||||
| import { useAntdForm } from "@/hooks"; | ||||
| 
 | ||||
| type AccessEditFormNameDotComConfigFieldValues = Partial<AccessConfigForNameDotCom>; | ||||
| 
 | ||||
|  | @ -39,17 +38,13 @@ const AccessEditFormNameDotComConfig = ({ form, formName, disabled, initialValue | |||
|       .trim(), | ||||
|   }); | ||||
|   const formRule = createSchemaFieldRule(formSchema); | ||||
|   const { form: formInst, formProps } = useAntdForm<z.infer<typeof formSchema>>({ | ||||
|     form: form, | ||||
|     initialValues: initialValues ?? initFormModel(), | ||||
|   }); | ||||
| 
 | ||||
|   const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => { | ||||
|     onValuesChange?.(values as AccessEditFormNameDotComConfigFieldValues); | ||||
|   }; | ||||
| 
 | ||||
|   return ( | ||||
|     <Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|     <Form form={form} disabled={disabled} initialValues={initialValues ?? initFormModel()} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|       <Form.Item | ||||
|         name="username" | ||||
|         label={t("access.form.namedotcom_username.label")} | ||||
|  |  | |||
|  | @ -4,7 +4,6 @@ import { createSchemaFieldRule } from "antd-zod"; | |||
| import { z } from "zod"; | ||||
| 
 | ||||
| import { type AccessConfigForNameSilo } from "@/domain/access"; | ||||
| import { useAntdForm } from "@/hooks"; | ||||
| 
 | ||||
| type AccessEditFormNameSiloConfigFieldValues = Partial<AccessConfigForNameSilo>; | ||||
| 
 | ||||
|  | @ -33,17 +32,13 @@ const AccessEditFormNameSiloConfig = ({ form, formName, disabled, initialValues, | |||
|       .trim(), | ||||
|   }); | ||||
|   const formRule = createSchemaFieldRule(formSchema); | ||||
|   const { form: formInst, formProps } = useAntdForm<z.infer<typeof formSchema>>({ | ||||
|     form: form, | ||||
|     initialValues: initialValues ?? initFormModel(), | ||||
|   }); | ||||
| 
 | ||||
|   const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => { | ||||
|     onValuesChange?.(values as AccessEditFormNameSiloConfigFieldValues); | ||||
|   }; | ||||
| 
 | ||||
|   return ( | ||||
|     <Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|     <Form form={form} disabled={disabled} initialValues={initialValues ?? initFormModel()} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|       <Form.Item | ||||
|         name="apiKey" | ||||
|         label={t("access.form.namesilo_api_key.label")} | ||||
|  |  | |||
|  | @ -4,7 +4,6 @@ import { createSchemaFieldRule } from "antd-zod"; | |||
| import { z } from "zod"; | ||||
| 
 | ||||
| import { type AccessConfigForPowerDNS } from "@/domain/access"; | ||||
| import { useAntdForm } from "@/hooks"; | ||||
| 
 | ||||
| type AccessEditFormPowerDNSConfigFieldValues = Partial<AccessConfigForPowerDNS>; | ||||
| 
 | ||||
|  | @ -35,17 +34,13 @@ const AccessEditFormPowerDNSConfig = ({ form, formName, disabled, initialValues, | |||
|       .trim(), | ||||
|   }); | ||||
|   const formRule = createSchemaFieldRule(formSchema); | ||||
|   const { form: formInst, formProps } = useAntdForm<z.infer<typeof formSchema>>({ | ||||
|     form: form, | ||||
|     initialValues: initialValues ?? initFormModel(), | ||||
|   }); | ||||
| 
 | ||||
|   const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => { | ||||
|     onValuesChange?.(values as AccessEditFormPowerDNSConfigFieldValues); | ||||
|   }; | ||||
| 
 | ||||
|   return ( | ||||
|     <Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|     <Form form={form} disabled={disabled} initialValues={initialValues ?? initFormModel()} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|       <Form.Item | ||||
|         name="apiUrl" | ||||
|         label={t("access.form.powerdns_api_url.label")} | ||||
|  |  | |||
|  | @ -4,7 +4,6 @@ import { createSchemaFieldRule } from "antd-zod"; | |||
| import { z } from "zod"; | ||||
| 
 | ||||
| import { type AccessConfigForQiniu } from "@/domain/access"; | ||||
| import { useAntdForm } from "@/hooks"; | ||||
| 
 | ||||
| type AccessEditFormQiniuConfigFieldValues = Partial<AccessConfigForQiniu>; | ||||
| 
 | ||||
|  | @ -39,17 +38,13 @@ const AccessEditFormQiniuConfig = ({ form, formName, disabled, initialValues, on | |||
|       .trim(), | ||||
|   }); | ||||
|   const formRule = createSchemaFieldRule(formSchema); | ||||
|   const { form: formInst, formProps } = useAntdForm<z.infer<typeof formSchema>>({ | ||||
|     form: form, | ||||
|     initialValues: initialValues ?? initFormModel(), | ||||
|   }); | ||||
| 
 | ||||
|   const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => { | ||||
|     onValuesChange?.(values as AccessEditFormQiniuConfigFieldValues); | ||||
|   }; | ||||
| 
 | ||||
|   return ( | ||||
|     <Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|     <Form form={form} disabled={disabled} initialValues={initialValues ?? initFormModel()} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|       <Form.Item | ||||
|         name="accessKey" | ||||
|         label={t("access.form.qiniu_access_key.label")} | ||||
|  |  | |||
|  | @ -8,7 +8,6 @@ import { createSchemaFieldRule } from "antd-zod"; | |||
| import { z } from "zod"; | ||||
| 
 | ||||
| import { type AccessConfigForSSH } from "@/domain/access"; | ||||
| import { useAntdForm } from "@/hooks"; | ||||
| import { readFileContent } from "@/utils/file"; | ||||
| import { validDomainName, validIPv4Address, validIPv6Address } from "@/utils/validators"; | ||||
| 
 | ||||
|  | @ -55,14 +54,11 @@ const AccessEditFormSSHConfig = ({ form, formName, disabled, initialValues, onVa | |||
|       .refine((v) => !v || form.getFieldValue("key"), t("access.form.ssh_key.placeholder")), | ||||
|   }); | ||||
|   const formRule = createSchemaFieldRule(formSchema); | ||||
|   const { form: formInst, formProps } = useAntdForm<z.infer<typeof formSchema>>({ | ||||
|     form: form, | ||||
|     initialValues: initialValues ?? initFormModel(), | ||||
|   }); | ||||
| 
 | ||||
|   const [keyFileList, setKeyFileList] = useState<UploadFile[]>([]); | ||||
|   const fieldKey = Form.useWatch("key", form); | ||||
|   const [fieldKeyFileList, setFieldKeyFileList] = useState<UploadFile[]>([]); | ||||
|   useDeepCompareEffect(() => { | ||||
|     setKeyFileList(initialValues?.key?.trim() ? [{ uid: "-1", name: "sshkey", status: "done" }] : []); | ||||
|     setFieldKeyFileList(initialValues?.key?.trim() ? [{ uid: "-1", name: "sshkey", status: "done" }] : []); | ||||
|   }, [initialValues]); | ||||
| 
 | ||||
|   const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => { | ||||
|  | @ -71,18 +67,18 @@ const AccessEditFormSSHConfig = ({ form, formName, disabled, initialValues, onVa | |||
| 
 | ||||
|   const handleKeyFileChange: UploadProps["onChange"] = async ({ file }) => { | ||||
|     if (file && file.status !== "removed") { | ||||
|       formInst.setFieldValue("key", await readFileContent(file.originFileObj ?? (file as unknown as File))); | ||||
|       setKeyFileList([file]); | ||||
|       form.setFieldValue("key", await readFileContent(file.originFileObj ?? (file as unknown as File))); | ||||
|       setFieldKeyFileList([file]); | ||||
|     } else { | ||||
|       formInst.setFieldValue("key", ""); | ||||
|       setKeyFileList([]); | ||||
|       form.setFieldValue("key", ""); | ||||
|       setFieldKeyFileList([]); | ||||
|     } | ||||
| 
 | ||||
|     flushSync(() => onValuesChange?.(formInst.getFieldsValue(true))); | ||||
|     flushSync(() => onValuesChange?.(form.getFieldsValue(true))); | ||||
|   }; | ||||
| 
 | ||||
|   return ( | ||||
|     <Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|     <Form form={form} disabled={disabled} initialValues={initialValues ?? initFormModel()} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|       <div className="flex space-x-2"> | ||||
|         <div className="w-2/3"> | ||||
|           <Form.Item name="host" label={t("access.form.ssh_host.label")} rules={[formRule]}> | ||||
|  | @ -119,10 +115,10 @@ const AccessEditFormSSHConfig = ({ form, formName, disabled, initialValues, onVa | |||
|       <div className="flex space-x-2"> | ||||
|         <div className="w-1/2"> | ||||
|           <Form.Item name="key" noStyle rules={[formRule]}> | ||||
|             <Input.TextArea autoComplete="new-password" hidden placeholder={t("access.form.ssh_key.placeholder")} value={formInst.getFieldValue("key")} /> | ||||
|             <Input.TextArea autoComplete="new-password" hidden placeholder={t("access.form.ssh_key.placeholder")} value={fieldKey} /> | ||||
|           </Form.Item> | ||||
|           <Form.Item label={t("access.form.ssh_key.label")} tooltip={<span dangerouslySetInnerHTML={{ __html: t("access.form.ssh_key.tooltip") }}></span>}> | ||||
|             <Upload beforeUpload={() => false} fileList={keyFileList} maxCount={1} onChange={handleKeyFileChange}> | ||||
|             <Upload beforeUpload={() => false} fileList={fieldKeyFileList} maxCount={1} onChange={handleKeyFileChange}> | ||||
|               <Button icon={<UploadOutlinedIcon />}>{t("access.form.ssh_key.upload")}</Button> | ||||
|             </Upload> | ||||
|           </Form.Item> | ||||
|  |  | |||
|  | @ -4,7 +4,6 @@ import { createSchemaFieldRule } from "antd-zod"; | |||
| import { z } from "zod"; | ||||
| 
 | ||||
| import { type AccessConfigForTencentCloud } from "@/domain/access"; | ||||
| import { useAntdForm } from "@/hooks"; | ||||
| 
 | ||||
| type AccessEditFormTencentCloudConfigFieldValues = Partial<AccessConfigForTencentCloud>; | ||||
| 
 | ||||
|  | @ -39,17 +38,13 @@ const AccessEditFormTencentCloudConfig = ({ form, formName, disabled, initialVal | |||
|       .trim(), | ||||
|   }); | ||||
|   const formRule = createSchemaFieldRule(formSchema); | ||||
|   const { form: formInst, formProps } = useAntdForm<z.infer<typeof formSchema>>({ | ||||
|     form: form, | ||||
|     initialValues: initialValues ?? initFormModel(), | ||||
|   }); | ||||
| 
 | ||||
|   const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => { | ||||
|     onValuesChange?.(values as AccessEditFormTencentCloudConfigFieldValues); | ||||
|   }; | ||||
| 
 | ||||
|   return ( | ||||
|     <Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|     <Form form={form} disabled={disabled} initialValues={initialValues ?? initFormModel()} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|       <Form.Item | ||||
|         name="secretId" | ||||
|         label={t("access.form.tencentcloud_secret_id.label")} | ||||
|  |  | |||
|  | @ -4,7 +4,6 @@ import { createSchemaFieldRule } from "antd-zod"; | |||
| import { z } from "zod"; | ||||
| 
 | ||||
| import { type AccessConfigForVolcEngine } from "@/domain/access"; | ||||
| import { useAntdForm } from "@/hooks"; | ||||
| 
 | ||||
| type AccessEditFormVolcEngineConfigFieldValues = Partial<AccessConfigForVolcEngine>; | ||||
| 
 | ||||
|  | @ -39,17 +38,13 @@ const AccessEditFormVolcEngineConfig = ({ form, formName, disabled, initialValue | |||
|       .trim(), | ||||
|   }); | ||||
|   const formRule = createSchemaFieldRule(formSchema); | ||||
|   const { form: formInst, formProps } = useAntdForm<z.infer<typeof formSchema>>({ | ||||
|     form: form, | ||||
|     initialValues: initialValues ?? initFormModel(), | ||||
|   }); | ||||
| 
 | ||||
|   const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => { | ||||
|     onValuesChange?.(values as AccessEditFormVolcEngineConfigFieldValues); | ||||
|   }; | ||||
| 
 | ||||
|   return ( | ||||
|     <Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|     <Form form={form} disabled={disabled} initialValues={initialValues ?? initFormModel()} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|       <Form.Item | ||||
|         name="accessKeyId" | ||||
|         label={t("access.form.volcengine_access_key_id.label")} | ||||
|  |  | |||
|  | @ -4,7 +4,6 @@ import { createSchemaFieldRule } from "antd-zod"; | |||
| import { z } from "zod"; | ||||
| 
 | ||||
| import { type AccessConfigForWebhook } from "@/domain/access"; | ||||
| import { useAntdForm } from "@/hooks"; | ||||
| 
 | ||||
| type AccessEditFormWebhookConfigFieldValues = Partial<AccessConfigForWebhook>; | ||||
| 
 | ||||
|  | @ -29,17 +28,13 @@ const AccessEditFormWebhookConfig = ({ form, formName, disabled, initialValues, | |||
|     url: z.string({ message: t("access.form.webhook_url.placeholder") }).url(t("common.errmsg.url_invalid")), | ||||
|   }); | ||||
|   const formRule = createSchemaFieldRule(formSchema); | ||||
|   const { form: formInst, formProps } = useAntdForm<z.infer<typeof formSchema>>({ | ||||
|     form: form, | ||||
|     initialValues: initialValues ?? initFormModel(), | ||||
|   }); | ||||
| 
 | ||||
|   const handleFormChange = (_: unknown, values: z.infer<typeof formSchema>) => { | ||||
|     onValuesChange?.(values as AccessEditFormWebhookConfigFieldValues); | ||||
|   }; | ||||
| 
 | ||||
|   return ( | ||||
|     <Form {...formProps} form={formInst} disabled={disabled} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|     <Form form={form} disabled={disabled} initialValues={initialValues ?? initFormModel()} layout="vertical" name={formName} onValuesChange={handleFormChange}> | ||||
|       <Form.Item name="url" label={t("access.form.webhook_url.label")} rules={[formRule]}> | ||||
|         <Input placeholder={t("access.form.webhook_url.placeholder")} /> | ||||
|       </Form.Item> | ||||
|  |  | |||
|  | @ -36,24 +36,24 @@ const useAntdForm = <T extends NonNullable<unknown> = any>({ form, initialValues | |||
|       return; | ||||
|     } | ||||
| 
 | ||||
|     let temp: Promise<Partial<T>>; | ||||
|     let p: Promise<Partial<T>>; | ||||
|     if (typeof initialValues === "function") { | ||||
|       temp = Promise.resolve(initialValues()); | ||||
|       p = Promise.resolve(initialValues()); | ||||
|     } else { | ||||
|       temp = Promise.resolve(initialValues); | ||||
|       p = Promise.resolve(initialValues); | ||||
|     } | ||||
| 
 | ||||
|     temp.then((temp) => { | ||||
|     p.then((res) => { | ||||
|       if (!unmounted) { | ||||
|         type FieldName = Parameters<FormInstance<T>["getFieldValue"]>[0]; | ||||
|         type FieldsValue = Parameters<FormInstance<T>["setFieldsValue"]>[0]; | ||||
| 
 | ||||
|         const obj = { ...temp }; | ||||
|         Object.keys(temp).forEach((key) => { | ||||
|           obj[key as keyof T] = formInst!.isFieldTouched(key as FieldName) ? formInst!.getFieldValue(key as FieldName) : temp[key as keyof T]; | ||||
|         const obj = { ...res }; | ||||
|         Object.keys(res).forEach((key) => { | ||||
|           obj[key as keyof T] = formInst!.isFieldTouched(key as FieldName) ? formInst!.getFieldValue(key as FieldName) : res[key as keyof T]; | ||||
|         }); | ||||
| 
 | ||||
|         setFormInitialValues(temp); | ||||
|         setFormInitialValues(res); | ||||
|         formInst!.setFieldsValue(obj as FieldsValue); | ||||
|       } | ||||
|     }); | ||||
|  |  | |||
|  | @ -17,12 +17,12 @@ const useTriggerElement = (trigger: React.ReactNode, options?: UseTriggerElement | |||
|       return null; | ||||
|     } | ||||
| 
 | ||||
|     const temp = isValidElement(trigger) ? trigger : createElement(Fragment, null, trigger); | ||||
|     return cloneElement(temp, { | ||||
|       ...temp.props, | ||||
|     const el = isValidElement(trigger) ? trigger : createElement(Fragment, null, trigger); | ||||
|     return cloneElement(el, { | ||||
|       ...el.props, | ||||
|       onClick: (e: MouseEvent) => { | ||||
|         onClick?.(e); | ||||
|         temp.props?.onClick?.(e); | ||||
|         el.props?.onClick?.(e); | ||||
|       }, | ||||
|     }); | ||||
|   }, [trigger, onClick]); | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	 Fu Diwei
						Fu Diwei