Skip to content

Commit

Permalink
ddns: allow overriding domains per configuration (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
uubulb authored Jan 30, 2025
1 parent 0ba4182 commit 42b85f7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 17 deletions.
74 changes: 59 additions & 15 deletions src/components/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ const serverFormSchema = z.object({
enable_ddns: asOptionalField(z.boolean()),
ddns_profiles: asOptionalField(z.array(z.number())),
ddns_profiles_raw: asOptionalField(z.string()),
override_ddns_domains: asOptionalField(z.record(z.coerce.number().int(), z.array(z.string()))),
override_ddns_domains_raw: asOptionalField(
z.string().refine(
(val) => {
try {
JSON.parse(val)
return true
} catch (e) {
return false
}
},
{
message: "Invalid JSON string",
},
),
),
})

export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
Expand All @@ -58,6 +74,9 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
defaultValues: {
...data,
ddns_profiles_raw: data.ddns_profiles ? conv.arrToStr(data.ddns_profiles) : undefined,
override_ddns_domains_raw: data.override_ddns_domains
? JSON.stringify(data.override_ddns_domains)
: undefined,
},
resetOptions: {
keepDefaultValues: false,
Expand All @@ -71,6 +90,9 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
values.ddns_profiles = values.ddns_profiles_raw
? conv.strToArr(values.ddns_profiles_raw).map(Number)
: undefined
values.override_ddns_domains = values.override_ddns_domains_raw
? JSON.parse(values.override_ddns_domains_raw)
: undefined
await updateServer(data!.id!, values)
} catch (e) {
console.error(e)
Expand Down Expand Up @@ -124,21 +146,43 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
</FormItem>
)}
/>
<FormField
control={form.control}
name="ddns_profiles_raw"
render={({ field }) => (
<FormItem>
<FormLabel>
{t("DDNSProfiles") + t("SeparateWithComma")}
</FormLabel>
<FormControl>
<Input placeholder="1,2,3" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
{form.watch("enable_ddns") ? (
<>
<FormField
control={form.control}
name="ddns_profiles_raw"
render={({ field }) => (
<FormItem>
<FormLabel>
{t("DDNSProfiles") + t("SeparateWithComma")}
</FormLabel>
<FormControl>
<Input placeholder="1,2,3" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="override_ddns_domains_raw"
render={({ field }) => (
<FormItem>
<FormLabel>
{t("OverrideDDNSDomains")}
</FormLabel>
<FormControl>
<Textarea className="resize-y" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</>
) : (
<></>
)}

<FormField
control={form.control}
name="enable_ddns"
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function asOptionalField<T extends z.ZodTypeAny>(schema: T) {
}

export const conv = {
recordToStr: (rec: Record<string, boolean>) => {
recordToStr: <T>(rec: Record<string, T>) => {
const arr: string[] = []
for (const key in rec) {
arr.push(key)
Expand Down
3 changes: 2 additions & 1 deletion src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,6 @@
"ConfirmBlock": "Confirm Block",
"RejectPassword": "Reject Password Login",
"EmptyText": "Text is empty",
"EmptyNote": "You didn't have any note."
"EmptyNote": "You didn't have any note.",
"OverrideDDNSDomains": "Override DDNS Domains (per configuration)"
}
3 changes: 3 additions & 0 deletions src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ export interface ModelNAT {

export interface ModelNATForm {
domain: string
enabled: boolean
host: string
/** @minLength 1 */
name: string
Expand Down Expand Up @@ -560,6 +561,7 @@ export interface ModelServer {
name: string
/** 管理员可见备注 */
note: string
override_ddns_domains?: Record<string, string[]>
/** 公开备注 */
public_note: string
state: ModelHostState
Expand All @@ -582,6 +584,7 @@ export interface ModelServerForm {
name: string
/** 管理员可见备注 */
note?: string
override_ddns_domains?: Record<string, string[]>
/** 公开备注 */
public_note?: string
}
Expand Down

0 comments on commit 42b85f7

Please sign in to comment.