"use client";
import { useFormState } from "react-dom";
import { useEffect, useState } from "react";
import {
createL4ProxyHostAction,
deleteL4ProxyHostAction,
updateL4ProxyHostAction,
} from "@/app/(dashboard)/l4-proxy-hosts/actions";
import { INITIAL_ACTION_STATE } from "@/lib/actions";
import type { L4ProxyHost } from "@/lib/models/l4-proxy-hosts";
import { AppDialog } from "@/components/ui/AppDialog";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { Badge } from "@/components/ui/badge";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
import { Textarea } from "@/components/ui/textarea";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import {
Accordion,
AccordionContent,
AccordionItem,
AccordionTrigger,
} from "@/components/ui/accordion";
import { cn } from "@/lib/utils";
import { Globe, Layers, MapPin, Pin } from "lucide-react";
function FormField({
label,
htmlFor,
helperText,
children,
}: {
label: string;
htmlFor: string;
helperText?: string;
children: React.ReactNode;
}) {
return (
{children}
{helperText && (
{helperText}
)}
);
}
function L4HostForm({
formId,
formAction,
state,
initialData,
}: {
formId: string;
formAction: (formData: FormData) => void;
state: { status: string; message?: string };
initialData?: L4ProxyHost | null;
}) {
const [enabled, setEnabled] = useState(initialData?.enabled ?? true);
const [protocol, setProtocol] = useState(initialData?.protocol ?? "tcp");
const [matcherType, setMatcherType] = useState(
initialData?.matcher_type ?? "none"
);
const defaultLbAccordion = initialData?.load_balancer?.enabled
? "load-balancer"
: undefined;
const defaultDnsAccordion = initialData?.dns_resolver?.enabled
? "dns-resolver"
: undefined;
const defaultGeoblockAccordion = initialData?.geoblock?.enabled
? "geoblock"
: undefined;
const defaultUpstreamDnsAccordion =
initialData?.upstream_dns_resolution?.enabled === true
? "upstream-dns"
: undefined;
return (
);
}
export function CreateL4HostDialog({
open,
onClose,
initialData,
}: {
open: boolean;
onClose: () => void;
initialData?: L4ProxyHost | null;
}) {
const [state, formAction] = useFormState(
createL4ProxyHostAction,
INITIAL_ACTION_STATE
);
useEffect(() => {
if (state.status === "success") {
setTimeout(onClose, 1000);
}
}, [state.status, onClose]);
return (
{
(
document.getElementById("create-l4-host-form") as HTMLFormElement
)?.requestSubmit();
}}
>
);
}
export function EditL4HostDialog({
open,
host,
onClose,
}: {
open: boolean;
host: L4ProxyHost;
onClose: () => void;
}) {
const [state, formAction] = useFormState(
updateL4ProxyHostAction.bind(null, host.id),
INITIAL_ACTION_STATE
);
useEffect(() => {
if (state.status === "success") {
setTimeout(onClose, 1000);
}
}, [state.status, onClose]);
return (
{
(
document.getElementById("edit-l4-host-form") as HTMLFormElement
)?.requestSubmit();
}}
>
);
}
export function DeleteL4HostDialog({
open,
host,
onClose,
}: {
open: boolean;
host: L4ProxyHost;
onClose: () => void;
}) {
const [state, formAction] = useFormState(
deleteL4ProxyHostAction.bind(null, host.id),
INITIAL_ACTION_STATE
);
useEffect(() => {
if (state.status === "success") {
setTimeout(onClose, 1000);
}
}, [state.status, onClose]);
return (
{
(
document.getElementById("delete-l4-host-form") as HTMLFormElement
)?.requestSubmit();
}}
>
);
}