import { updateRoute } from "@/lib/clientActions"; import { getHandler, getHosts, getRouteUpstreams } from "@/lib/utils"; import { useState } from "react"; import useSWRMutation from "swr/mutation"; import { toast } from "./ui/use-toast"; import { useFieldArray, useForm } from "react-hook-form"; import { get, set } from "lodash"; import { Dialog, DialogContent } from "./ui/dialog"; import { Form, FormControl, FormField, FormItem, FormLabel } from "./ui/form"; import { Input } from "./ui/input"; import { Button } from "./ui/button"; import { DialogClose } from "@radix-ui/react-dialog"; export function RouteDialog({ route, routesMap, open, onOpenChange }: { route: Route, routesMap: Route[], open: boolean, onOpenChange: (open: boolean) => void }) { let index = routesMap.indexOf(route); const [modifiedRoute, setRoute] = useState(route); const { trigger: pushUpdate } = useSWRMutation(`/api/caddy/routes`, updateRoute, { onError: (err) => { return toast({ title: "Error", description: "There was an error updating the route. " + err.message, variant: "destructive" }); }, onSuccess: () => { toast({ title: "Success", description: "Route updated successfully!", variant: "success" }); } }); let validHosts: any[] = ["google.com", "test.com"]; let validUpstreams: any[] = ["1.1.1.1", "69.126.24.175"]; let handler: string = "reverse_proxy"; if (index != -1) { validHosts = getHosts(route)!; validUpstreams = getRouteUpstreams(route).map((upstream) => upstream.dial); handler = getHandler(route)!; } const form = useForm({ values: { handler: handler, upstreams: validUpstreams ?? [], hosts: validHosts ?? [], } }); const { fields: upstreams, append: appendUpstreams } = useFieldArray({ control: form.control, name: "upstreams", }) const { fields: hosts, append: appendHosts } = useFieldArray({ control: form.control, name: "hosts", }) let onSubmit = (() => { let update = { route: modifiedRoute, index: index, type: index == -1 ? 'PUT' : 'PATCH' } update.route = { "handle": [ { "handler": "subroute", "routes": [ { "handle": [ { "handler": "reverse_proxy", "upstreams": validUpstreams.map((upstream) => ({ "dial": upstream })) } ] } ] } ], "match": [ { "host": validHosts } ], "terminal": true } if (index == -1) update.index = routesMap.length - 1; console.log(update.index) pushUpdate(update); }) let handleChange = (value: any, path: any) => { let updatedRoute = set(modifiedRoute, path, value.target.value); setRoute(updatedRoute); form.setValue(value.target.name, value.target.value) } return (
( Handler handleChange(value, `handle[0].handler`)} /> )} > {upstreams.map((upstream, index) => ( ( Upstream {index + 1} handleChange(value, `handle[0].routes[0].handle[0].upstreams[${index}].dial`)} /> )} /> ))} {hosts.map((host, index) => ( ( Host {index + 1} handleChange(value, `match[0].host[${index}]`)} /> )} /> ))}
) }