import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Switch } from "@/components/ui/switch"; import { api } from "@/utils/api"; import { zodResolver } from "@hookform/resolvers/zod"; import { AlertTriangle, PenBoxIcon } from "lucide-react"; import { useEffect } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; const hostnameRegex = /^[a-zA-Z0-9][a-zA-Z0-9\.-]*\.[a-zA-Z]{2,}$/; const updateDomain = z.object({ host: z.string().regex(hostnameRegex, { message: "Invalid hostname" }), path: z.string().min(1), port: z .number() .min(1, { message: "Port must be at least 1" }) .max(65535, { message: "Port must be 65535 or below" }), https: z.boolean(), certificateType: z.enum(["letsencrypt", "none"]), }); type UpdateDomain = z.infer; interface Props { domainId: string; } export const UpdateDomain = ({ domainId }: Props) => { const utils = api.useUtils(); const { data, refetch } = api.domain.one.useQuery( { domainId, }, { enabled: !!domainId, }, ); const { mutateAsync, isError, error } = api.domain.update.useMutation(); const form = useForm({ defaultValues: { host: "", https: true, path: "/", port: 3000, certificateType: "none", }, resolver: zodResolver(updateDomain), }); useEffect(() => { if (data) { form.reset({ host: data.host || "", port: data.port || 3000, path: data.path || "/", https: data.https, certificateType: data.certificateType, }); } }, [form, form.reset, data]); const onSubmit = async (data: UpdateDomain) => { await mutateAsync({ domainId, host: data.host, https: data.https, path: data.path, port: data.port, certificateType: data.certificateType, }) .then(async (data) => { toast.success("Domain Updated"); await refetch(); await utils.domain.byApplicationId.invalidate({ applicationId: data?.applicationId, }); await utils.application.readTraefikConfig.invalidate({ applicationId: data?.applicationId, }); }) .catch(() => { toast.error("Error to update the domain"); }); }; return ( Domain In this section you can add custom domains {isError && (
{error?.message}
)}
( Host )} /> { return ( Path ); }} /> { return ( Container Port { field.onChange(Number.parseInt(e.target.value)); }} /> ); }} /> ( Certificate )} /> (
HTTPS Automatically provision SSL Certificate.
)} />
); };