Skip to content

Commit 42b85f7

Browse files
authored
ddns: allow overriding domains per configuration (#111)
1 parent 0ba4182 commit 42b85f7

File tree

4 files changed

+65
-17
lines changed

4 files changed

+65
-17
lines changed

src/components/server.tsx

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ const serverFormSchema = z.object({
4949
enable_ddns: asOptionalField(z.boolean()),
5050
ddns_profiles: asOptionalField(z.array(z.number())),
5151
ddns_profiles_raw: asOptionalField(z.string()),
52+
override_ddns_domains: asOptionalField(z.record(z.coerce.number().int(), z.array(z.string()))),
53+
override_ddns_domains_raw: asOptionalField(
54+
z.string().refine(
55+
(val) => {
56+
try {
57+
JSON.parse(val)
58+
return true
59+
} catch (e) {
60+
return false
61+
}
62+
},
63+
{
64+
message: "Invalid JSON string",
65+
},
66+
),
67+
),
5268
})
5369

5470
export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
@@ -58,6 +74,9 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
5874
defaultValues: {
5975
...data,
6076
ddns_profiles_raw: data.ddns_profiles ? conv.arrToStr(data.ddns_profiles) : undefined,
77+
override_ddns_domains_raw: data.override_ddns_domains
78+
? JSON.stringify(data.override_ddns_domains)
79+
: undefined,
6180
},
6281
resetOptions: {
6382
keepDefaultValues: false,
@@ -71,6 +90,9 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
7190
values.ddns_profiles = values.ddns_profiles_raw
7291
? conv.strToArr(values.ddns_profiles_raw).map(Number)
7392
: undefined
93+
values.override_ddns_domains = values.override_ddns_domains_raw
94+
? JSON.parse(values.override_ddns_domains_raw)
95+
: undefined
7496
await updateServer(data!.id!, values)
7597
} catch (e) {
7698
console.error(e)
@@ -124,21 +146,43 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
124146
</FormItem>
125147
)}
126148
/>
127-
<FormField
128-
control={form.control}
129-
name="ddns_profiles_raw"
130-
render={({ field }) => (
131-
<FormItem>
132-
<FormLabel>
133-
{t("DDNSProfiles") + t("SeparateWithComma")}
134-
</FormLabel>
135-
<FormControl>
136-
<Input placeholder="1,2,3" {...field} />
137-
</FormControl>
138-
<FormMessage />
139-
</FormItem>
140-
)}
141-
/>
149+
{form.watch("enable_ddns") ? (
150+
<>
151+
<FormField
152+
control={form.control}
153+
name="ddns_profiles_raw"
154+
render={({ field }) => (
155+
<FormItem>
156+
<FormLabel>
157+
{t("DDNSProfiles") + t("SeparateWithComma")}
158+
</FormLabel>
159+
<FormControl>
160+
<Input placeholder="1,2,3" {...field} />
161+
</FormControl>
162+
<FormMessage />
163+
</FormItem>
164+
)}
165+
/>
166+
<FormField
167+
control={form.control}
168+
name="override_ddns_domains_raw"
169+
render={({ field }) => (
170+
<FormItem>
171+
<FormLabel>
172+
{t("OverrideDDNSDomains")}
173+
</FormLabel>
174+
<FormControl>
175+
<Textarea className="resize-y" {...field} />
176+
</FormControl>
177+
<FormMessage />
178+
</FormItem>
179+
)}
180+
/>
181+
</>
182+
) : (
183+
<></>
184+
)}
185+
142186
<FormField
143187
control={form.control}
144188
name="enable_ddns"

src/lib/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function asOptionalField<T extends z.ZodTypeAny>(schema: T) {
1717
}
1818

1919
export const conv = {
20-
recordToStr: (rec: Record<string, boolean>) => {
20+
recordToStr: <T>(rec: Record<string, T>) => {
2121
const arr: string[] = []
2222
for (const key in rec) {
2323
arr.push(key)

src/locales/en/translation.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,6 @@
179179
"ConfirmBlock": "Confirm Block",
180180
"RejectPassword": "Reject Password Login",
181181
"EmptyText": "Text is empty",
182-
"EmptyNote": "You didn't have any note."
182+
"EmptyNote": "You didn't have any note.",
183+
"OverrideDDNSDomains": "Override DDNS Domains (per configuration)"
183184
}

src/types/api.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ export interface ModelNAT {
411411

412412
export interface ModelNATForm {
413413
domain: string
414+
enabled: boolean
414415
host: string
415416
/** @minLength 1 */
416417
name: string
@@ -560,6 +561,7 @@ export interface ModelServer {
560561
name: string
561562
/** 管理员可见备注 */
562563
note: string
564+
override_ddns_domains?: Record<string, string[]>
563565
/** 公开备注 */
564566
public_note: string
565567
state: ModelHostState
@@ -582,6 +584,7 @@ export interface ModelServerForm {
582584
name: string
583585
/** 管理员可见备注 */
584586
note?: string
587+
override_ddns_domains?: Record<string, string[]>
585588
/** 公开备注 */
586589
public_note?: string
587590
}

0 commit comments

Comments
 (0)