-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathDNSProviderFields.tsx
More file actions
145 lines (135 loc) · 4.6 KB
/
DNSProviderFields.tsx
File metadata and controls
145 lines (135 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { IconAlertTriangle } from "@tabler/icons-react";
import CodeEditor from "@uiw/react-textarea-code-editor";
import { Field, useFormikContext } from "formik";
import { useEffect, useState } from "react";
import Select, { type ActionMeta } from "react-select";
import type { DNSProvider } from "src/api/backend";
import { useDnsProviders } from "src/hooks";
import { intl, T } from "src/locale";
import styles from "./DNSProviderFields.module.css";
interface DNSProviderOption {
readonly value: string;
readonly label: string;
readonly credentials: string;
}
interface Props {
showBoundaryBox?: boolean;
/** When true (edit mode), credentials field is for new credentials only; existing credentials are never displayed */
editMode?: boolean;
}
export function DNSProviderFields({ showBoundaryBox = false, editMode = false }: Props) {
const { values, setFieldValue } = useFormikContext();
const { data: dnsProviders, isLoading } = useDnsProviders();
const [dnsProviderId, setDnsProviderId] = useState<string | null>(null);
const v: any = values || {};
const handleChange = (newValue: any, _actionMeta: ActionMeta<DNSProviderOption>) => {
setFieldValue("meta.dnsProvider", newValue?.value);
setFieldValue("meta.dnsProviderCredentials", newValue?.credentials);
setDnsProviderId(newValue?.value);
};
const options: DNSProviderOption[] =
dnsProviders?.map((p: DNSProvider) => ({
value: p.id,
label: p.name,
credentials: p.credentials,
})) || [];
const selectedOption = options.find((o) => o.value === v.meta?.dnsProvider) ?? null;
const showCredentials = dnsProviderId ?? v.meta?.dnsProvider;
// When a provider is selected and credentials are empty, use the template from dns-plugins.json as the value
useEffect(() => {
if (selectedOption && (v.meta?.dnsProviderCredentials ?? "") === "") {
setFieldValue("meta.dnsProviderCredentials", selectedOption.credentials);
}
}, [selectedOption, selectedOption?.credentials, v.meta?.dnsProviderCredentials, setFieldValue]);
return (
<div className={showBoundaryBox ? styles.dnsChallengeWarning : undefined}>
<p className="text-warning">
<IconAlertTriangle size={16} className="me-1" />
<T id="certificates.dns.warning" />
</p>
<Field name="meta.dnsProvider">
{({ field }: any) => (
<div className="row">
<label htmlFor="dnsProvider" className="form-label">
<T id="certificates.dns.provider" />
</label>
<Select
className="react-select-container"
classNamePrefix="react-select"
name={field.name}
id="dnsProvider"
closeMenuOnSelect={true}
isClearable={false}
placeholder={intl.formatMessage({ id: "certificates.dns.provider.placeholder" })}
isLoading={isLoading}
isSearchable
value={selectedOption}
onChange={handleChange}
options={options}
/>
</div>
)}
</Field>
{showCredentials ? (
<>
<Field name="meta.dnsProviderCredentials">
{({ field }: any) => (
<div className="mt-3">
<label htmlFor="dnsProviderCredentials" className="form-label">
<T id={editMode ? "certificates.dns.credentials-update" : "certificates.dns.credentials"} />
</label>
<CodeEditor
language="bash"
id="dnsProviderCredentials"
padding={15}
data-color-mode="dark"
minHeight={130}
indentWidth={2}
style={{
fontFamily:
"ui-monospace,SFMono-Regular,SF Mono,Consolas,Liberation Mono,Menlo,monospace",
borderRadius: "0.3rem",
minHeight: "130px",
backgroundColor: "var(--tblr-bg-surface-dark)",
}}
value={v.meta.dnsProviderCredentials || ""}
{...field}
/>
<div>
<small className="text-muted">
<T id="certificates.dns.credentials-note" />
</small>
</div>
<div>
<small className="text-danger">
<T id="certificates.dns.credentials-warning" />
</small>
</div>
</div>
)}
</Field>
<Field name="meta.propagationSeconds">
{({ field }: any) => (
<div className="mt-3">
<label htmlFor="propagationSeconds" className="form-label">
<T id="certificates.dns.propagation-seconds" />
</label>
<input
id="propagationSeconds"
type="number"
className="form-control"
min={0}
max={7200}
{...field}
/>
<small className="text-muted">
<T id="certificates.dns.propagation-seconds-note" />
</small>
</div>
)}
</Field>
</>
) : null}
</div>
);
}