|
| 1 | +import cn from "classnames"; |
| 2 | +import { Field, useFormikContext } from "formik"; |
| 3 | +import { useState } from "react"; |
| 4 | +import Select, { type ActionMeta } from "react-select"; |
| 5 | +import type { DNSProvider } from "src/api/backend"; |
| 6 | +import { useDnsProviders } from "src/hooks"; |
| 7 | +import styles from "./DNSProviderFields.module.css"; |
| 8 | + |
| 9 | +interface DNSProviderOption { |
| 10 | + readonly value: string; |
| 11 | + readonly label: string; |
| 12 | + readonly credentials: string; |
| 13 | +} |
| 14 | + |
| 15 | +export function DNSProviderFields() { |
| 16 | + const { values, setFieldValue } = useFormikContext(); |
| 17 | + const { data: dnsProviders, isLoading } = useDnsProviders(); |
| 18 | + const [dnsProviderId, setDnsProviderId] = useState<string | null>(null); |
| 19 | + |
| 20 | + const v: any = values || {}; |
| 21 | + |
| 22 | + const handleChange = (newValue: any, _actionMeta: ActionMeta<DNSProviderOption>) => { |
| 23 | + setFieldValue("dnsProvider", newValue?.value); |
| 24 | + setFieldValue("dnsProviderCredentials", newValue?.credentials); |
| 25 | + setDnsProviderId(newValue?.value); |
| 26 | + }; |
| 27 | + |
| 28 | + const options: DNSProviderOption[] = |
| 29 | + dnsProviders?.map((p: DNSProvider) => ({ |
| 30 | + value: p.id, |
| 31 | + label: p.name, |
| 32 | + credentials: p.credentials, |
| 33 | + })) || []; |
| 34 | + |
| 35 | + return ( |
| 36 | + <div className={styles.dnsChallengeWarning}> |
| 37 | + <p className="text-danger"> |
| 38 | + This section requires some knowledge about Certbot and its DNS plugins. Please consult the respective |
| 39 | + plugins documentation. |
| 40 | + </p> |
| 41 | + |
| 42 | + <Field name="dnsProvider"> |
| 43 | + {({ field }: any) => ( |
| 44 | + <div className="row"> |
| 45 | + <label htmlFor="dnsProvider" className="form-label"> |
| 46 | + DNS Provider |
| 47 | + </label> |
| 48 | + <Select |
| 49 | + name={field.name} |
| 50 | + id="dnsProvider" |
| 51 | + closeMenuOnSelect={true} |
| 52 | + isClearable={false} |
| 53 | + placeholder="Select a Provider..." |
| 54 | + isLoading={isLoading} |
| 55 | + isSearchable |
| 56 | + onChange={handleChange} |
| 57 | + options={options} |
| 58 | + /> |
| 59 | + </div> |
| 60 | + )} |
| 61 | + </Field> |
| 62 | + |
| 63 | + {dnsProviderId ? ( |
| 64 | + <> |
| 65 | + <Field name="dnsProviderCredentials"> |
| 66 | + {({ field }: any) => ( |
| 67 | + <div className="row mt-3"> |
| 68 | + <label htmlFor="dnsProviderCredentials" className="form-label"> |
| 69 | + Credentials File Content |
| 70 | + </label> |
| 71 | + <textarea |
| 72 | + id="dnsProviderCredentials" |
| 73 | + className={cn("form-control", styles.textareaMono)} |
| 74 | + rows={3} |
| 75 | + spellCheck={false} |
| 76 | + value={v.dnsProviderCredentials || ""} |
| 77 | + {...field} |
| 78 | + /> |
| 79 | + <small className="text-muted"> |
| 80 | + This plugin requires a configuration file containing an API token or other |
| 81 | + credentials to your provider |
| 82 | + </small> |
| 83 | + <small className="text-danger"> |
| 84 | + This data will be stored as plaintext in the database and in a file! |
| 85 | + </small> |
| 86 | + </div> |
| 87 | + )} |
| 88 | + </Field> |
| 89 | + <Field name="propagationSeconds"> |
| 90 | + {({ field }: any) => ( |
| 91 | + <div className="row mt-3"> |
| 92 | + <label htmlFor="propagationSeconds" className="form-label"> |
| 93 | + Propagation Seconds |
| 94 | + </label> |
| 95 | + <input |
| 96 | + id="propagationSeconds" |
| 97 | + type="number" |
| 98 | + className="form-control" |
| 99 | + min={0} |
| 100 | + max={600} |
| 101 | + {...field} |
| 102 | + /> |
| 103 | + <small className="text-muted"> |
| 104 | + Leave empty to use the plugins default value. Number of seconds to wait for DNS |
| 105 | + propagation. |
| 106 | + </small> |
| 107 | + </div> |
| 108 | + )} |
| 109 | + </Field> |
| 110 | + </> |
| 111 | + ) : null} |
| 112 | + </div> |
| 113 | + ); |
| 114 | +} |
0 commit comments