| gio | aba9a96 | 2025-04-25 14:19:40 +0000 | [diff] [blame] | 1 | import { v4 as uuidv4 } from "uuid"; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 2 | import { useStateStore, AppNode, GatewayHttpsNode, ServiceNode, nodeLabel, useEnv, nodeIsConnectable } from '@/lib/state'; |
| 3 | import { Handle, Position, useNodes } from '@xyflow/react'; |
| 4 | import { NodeRect } from './node-rect'; |
| gio | 9b2d496 | 2025-05-07 04:59:39 +0000 | [diff] [blame] | 5 | import { useCallback, useEffect, useMemo } from 'react'; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 6 | import { z } from "zod"; |
| 7 | import { zodResolver } from "@hookform/resolvers/zod"; |
| 8 | import { useForm, EventType, DeepPartial } from 'react-hook-form'; |
| 9 | import { Form, FormControl, FormField, FormItem, FormMessage } from './ui/form'; |
| 10 | import { Input } from './ui/input'; |
| 11 | import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from './ui/select'; |
| gio | 9b2d496 | 2025-05-07 04:59:39 +0000 | [diff] [blame] | 12 | import { Checkbox } from "./ui/checkbox"; |
| 13 | import { Label } from "./ui/label"; |
| 14 | import { Button } from "./ui/button"; |
| 15 | import { XIcon } from "lucide-react"; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 16 | |
| 17 | const schema = z.object({ |
| 18 | network: z.string().min(1, "reqired"), |
| 19 | subdomain: z.string().min(1, "required"), |
| 20 | }); |
| 21 | |
| 22 | const connectedToSchema = z.object({ |
| 23 | id: z.string(), |
| 24 | portId: z.string(), |
| 25 | }); |
| 26 | |
| gio | 9b2d496 | 2025-05-07 04:59:39 +0000 | [diff] [blame] | 27 | const authEnabledSchema = z.object({ |
| 28 | enabled: z.boolean(), |
| 29 | }); |
| 30 | |
| 31 | const authGroupSchema = z.object({ |
| 32 | group: z.string(), |
| 33 | }); |
| 34 | |
| 35 | const authNoAuthPatternSchema = z.object({ |
| 36 | noAuthPathPattern: z.string(), |
| 37 | }); |
| 38 | |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 39 | export function NodeGatewayHttps(node: GatewayHttpsNode) { |
| 40 | const { id, selected } = node; |
| gio | aba9a96 | 2025-04-25 14:19:40 +0000 | [diff] [blame] | 41 | const isConnectableNetwork = useMemo(() => nodeIsConnectable(node, "subdomain"), [node]); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 42 | const isConnectable = useMemo(() => nodeIsConnectable(node, "https"), [node]); |
| 43 | return ( |
| gio | 1dc800a | 2025-04-24 17:15:43 +0000 | [diff] [blame] | 44 | <NodeRect id={id} selected={selected} type={node.type} state={node.data.state}> |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 45 | {nodeLabel(node)} |
| 46 | <Handle |
| gio | aba9a96 | 2025-04-25 14:19:40 +0000 | [diff] [blame] | 47 | type={"source"} |
| gio | 9b2d496 | 2025-05-07 04:59:39 +0000 | [diff] [blame] | 48 | id="subdomain" |
| gio | aba9a96 | 2025-04-25 14:19:40 +0000 | [diff] [blame] | 49 | position={Position.Top} |
| 50 | isConnectable={isConnectableNetwork} |
| gio | 9b2d496 | 2025-05-07 04:59:39 +0000 | [diff] [blame] | 51 | isConnectableStart={isConnectableNetwork} |
| 52 | isConnectableEnd={isConnectableNetwork} |
| gio | aba9a96 | 2025-04-25 14:19:40 +0000 | [diff] [blame] | 53 | /> |
| 54 | <Handle |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 55 | type={"target"} |
| gio | 9b2d496 | 2025-05-07 04:59:39 +0000 | [diff] [blame] | 56 | id="https" |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 57 | position={Position.Bottom} |
| 58 | isConnectable={isConnectable} |
| gio | 9b2d496 | 2025-05-07 04:59:39 +0000 | [diff] [blame] | 59 | isConnectableStart={isConnectable} |
| 60 | isConnectableEnd={isConnectable} |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 61 | /> |
| 62 | </NodeRect> |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | export function NodeGatewayHttpsDetails({ id, data }: GatewayHttpsNode) { |
| 67 | const store = useStateStore(); |
| 68 | const env = useEnv(); |
| 69 | const form = useForm<z.infer<typeof schema>>({ |
| 70 | resolver: zodResolver(schema), |
| 71 | mode: "onChange", |
| 72 | defaultValues: { |
| gio | f96ffb8 | 2025-04-24 09:31:05 +0000 | [diff] [blame] | 73 | network: data.network, |
| 74 | subdomain: data.subdomain, |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 75 | }, |
| 76 | }); |
| 77 | useEffect(() => { |
| 78 | const sub = form.watch((value: DeepPartial<z.infer<typeof schema>>, { name }: { name?: keyof z.infer<typeof schema> | undefined, type?: EventType | undefined }) => { |
| 79 | if (name === "network") { |
| gio | aba9a96 | 2025-04-25 14:19:40 +0000 | [diff] [blame] | 80 | let edges = store.edges; |
| 81 | if (data.network !== undefined) { |
| 82 | edges = edges.filter((e) => { |
| gio | aba9a96 | 2025-04-25 14:19:40 +0000 | [diff] [blame] | 83 | if (e.source === id && e.sourceHandle === "subdomain" && e.target === data.network && e.targetHandle === "subdomain") { |
| 84 | return false; |
| 85 | } else { |
| 86 | return true; |
| 87 | } |
| 88 | }); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 89 | } |
| gio | aba9a96 | 2025-04-25 14:19:40 +0000 | [diff] [blame] | 90 | if (value.network !== undefined) { |
| 91 | edges = edges.concat({ |
| 92 | id: uuidv4(), |
| 93 | source: id, |
| 94 | sourceHandle: "subdomain", |
| 95 | target: value.network, |
| 96 | targetHandle: "subdomain", |
| 97 | }); |
| 98 | } |
| 99 | store.setEdges(edges); |
| 100 | store.updateNodeData<"gateway-https">(id, { network: value.network }); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 101 | } else if (name === "subdomain") { |
| 102 | store.updateNodeData<"gateway-https">(id, { subdomain: value.subdomain }); |
| 103 | } |
| 104 | }); |
| 105 | return () => sub.unsubscribe(); |
| gio | aba9a96 | 2025-04-25 14:19:40 +0000 | [diff] [blame] | 106 | }, [id, data, form, store]); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 107 | const connectedToForm = useForm<z.infer<typeof connectedToSchema>>({ |
| 108 | resolver: zodResolver(connectedToSchema), |
| 109 | mode: "onChange", |
| 110 | defaultValues: { |
| 111 | id: data.https?.serviceId, |
| 112 | portId: data.https?.portId, |
| 113 | }, |
| 114 | }); |
| 115 | useEffect(() => { |
| 116 | connectedToForm.reset({ |
| 117 | id: data.https?.serviceId, |
| 118 | portId: data.https?.portId, |
| 119 | }); |
| 120 | }, [connectedToForm, data]); |
| 121 | const nodes = useNodes<AppNode>(); |
| 122 | const selected = useMemo(() => { |
| 123 | if (data !== undefined && data.https !== undefined) { |
| 124 | const https = data.https; |
| 125 | return nodes.find((n) => n.id === https.serviceId)! as ServiceNode; |
| 126 | } |
| 127 | return null; |
| gio | 6cf8c27 | 2025-05-08 09:01:38 +0000 | [diff] [blame] | 128 | }, [data, nodes]); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 129 | const selectable = useMemo(() => { |
| 130 | return nodes.filter((n) => { |
| 131 | if (n.id === id) { |
| 132 | return false; |
| 133 | } |
| 134 | if (selected !== null && selected.id === id) { |
| 135 | return true; |
| 136 | } |
| 137 | if (n.type !== "app") { |
| 138 | return false; |
| 139 | } |
| 140 | return n.data && n.data.ports && n.data.ports.length > 0; |
| 141 | }) |
| gio | 6cf8c27 | 2025-05-08 09:01:38 +0000 | [diff] [blame] | 142 | }, [id, nodes, selected]); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 143 | useEffect(() => { |
| 144 | const sub = connectedToForm.watch((value: DeepPartial<z.infer<typeof connectedToSchema>>, { name, type }: { name?: keyof z.infer<typeof connectedToSchema> | undefined, type?: EventType | undefined }) => { |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 145 | if (type !== "change") { |
| 146 | return; |
| 147 | } |
| 148 | switch (name) { |
| gio | 6cf8c27 | 2025-05-08 09:01:38 +0000 | [diff] [blame] | 149 | case "id": { |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 150 | if (!value.id) { |
| 151 | break; |
| 152 | } |
| 153 | const current = store.edges.filter((e) => e.target === id); |
| 154 | const cid = current[0] ? current[0].id : undefined; |
| 155 | store.replaceEdge({ |
| 156 | source: value.id, |
| 157 | sourceHandle: "ports", |
| 158 | target: id, |
| 159 | targetHandle: "https", |
| 160 | }, cid); |
| 161 | break; |
| gio | 6cf8c27 | 2025-05-08 09:01:38 +0000 | [diff] [blame] | 162 | } |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 163 | case "portId": |
| 164 | store.updateNodeData<"gateway-https">(id, { |
| 165 | https: { |
| 166 | serviceId: value.id, |
| 167 | portId: value.portId, |
| 168 | } |
| 169 | }); |
| 170 | break; |
| 171 | } |
| 172 | }); |
| 173 | return () => sub.unsubscribe(); |
| gio | 6cf8c27 | 2025-05-08 09:01:38 +0000 | [diff] [blame] | 174 | }, [id, connectedToForm, store, selectable]); |
| gio | 9b2d496 | 2025-05-07 04:59:39 +0000 | [diff] [blame] | 175 | const authEnabledForm = useForm<z.infer<typeof authEnabledSchema>>({ |
| 176 | resolver: zodResolver(authEnabledSchema), |
| 177 | mode: "onChange", |
| 178 | defaultValues: { |
| 179 | enabled: data.auth ? data.auth.enabled : false, |
| 180 | }, |
| 181 | }); |
| 182 | const authGroupForm = useForm<z.infer<typeof authGroupSchema>>({ |
| 183 | resolver: zodResolver(authGroupSchema), |
| 184 | mode: "onSubmit", |
| 185 | defaultValues: { |
| 186 | group: "", |
| 187 | }, |
| 188 | }); const authNoAuthPatternFrom = useForm<z.infer<typeof authNoAuthPatternSchema>>({ |
| 189 | resolver: zodResolver(authNoAuthPatternSchema), |
| 190 | mode: "onChange", |
| 191 | defaultValues: { |
| 192 | noAuthPathPattern: "", |
| 193 | }, |
| 194 | }); |
| 195 | useEffect(() => { |
| 196 | const sub = authEnabledForm.watch((value, { name }) => { |
| 197 | if (name === "enabled") { |
| 198 | store.updateNodeData<"gateway-https">(id, { |
| 199 | auth: { |
| 200 | ...data.auth, |
| 201 | enabled: value.enabled, |
| 202 | } |
| 203 | }) |
| 204 | } |
| 205 | }); |
| 206 | return () => sub.unsubscribe(); |
| 207 | }, [id, data, authEnabledForm, store]); |
| 208 | const removeGroup = useCallback((group: string) => { |
| 209 | const groups = data?.auth?.groups || []; |
| 210 | store.updateNodeData<"gateway-https">(id, { |
| 211 | auth: { |
| 212 | ...data.auth, |
| 213 | groups: groups.filter((g) => g !== group), |
| 214 | }, |
| 215 | }); |
| 216 | return true; |
| 217 | }, [id, data, store]); |
| 218 | const onGroupSubmit = useCallback((values: z.infer<typeof authGroupSchema>) => { |
| 219 | const groups = data.auth?.groups || []; |
| 220 | groups.push(values.group) |
| 221 | store.updateNodeData<"gateway-https">(id, { |
| 222 | auth: { |
| 223 | ...data.auth, |
| 224 | groups, |
| 225 | }, |
| 226 | }); |
| 227 | authGroupForm.reset(); |
| gio | 6cf8c27 | 2025-05-08 09:01:38 +0000 | [diff] [blame] | 228 | }, [id, data, store, authGroupForm]); |
| gio | 9b2d496 | 2025-05-07 04:59:39 +0000 | [diff] [blame] | 229 | const removeNoAuthPathPattern = useCallback((path: string) => { |
| 230 | const noAuthPathPatterns = data?.auth?.noAuthPathPatterns || []; |
| 231 | store.updateNodeData<"gateway-https">(id, { |
| 232 | auth: { |
| 233 | ...data.auth, |
| 234 | noAuthPathPatterns: noAuthPathPatterns.filter((p) => p !== path), |
| 235 | }, |
| 236 | }); |
| 237 | return true; |
| 238 | }, [id, data, store]); |
| 239 | const onNoAuthPathPatternSubmit = useCallback((values: z.infer<typeof authNoAuthPatternSchema>) => { |
| 240 | const noAuthPathPatterns = data.auth?.noAuthPathPatterns || []; |
| 241 | noAuthPathPatterns.push(values.noAuthPathPattern) |
| 242 | store.updateNodeData<"gateway-https">(id, { |
| 243 | auth: { |
| 244 | ...data.auth, |
| 245 | noAuthPathPatterns, |
| 246 | }, |
| 247 | }); |
| 248 | authNoAuthPatternFrom.reset(); |
| gio | 6cf8c27 | 2025-05-08 09:01:38 +0000 | [diff] [blame] | 249 | }, [id, data, store, authNoAuthPatternFrom]); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 250 | return ( |
| 251 | <> |
| 252 | <Form {...form}> |
| 253 | <form className="space-y-2"> |
| gio | 9b2d496 | 2025-05-07 04:59:39 +0000 | [diff] [blame] | 254 | <FormField |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 255 | control={form.control} |
| 256 | name="network" |
| 257 | render={({ field }) => ( |
| 258 | <FormItem> |
| 259 | <Select onValueChange={field.onChange} defaultValue={field.value}> |
| 260 | <FormControl> |
| 261 | <SelectTrigger> |
| 262 | <SelectValue placeholder="Network" /> |
| 263 | </SelectTrigger> |
| 264 | </FormControl> |
| 265 | <SelectContent> |
| 266 | {env.networks.map((n) => ( |
| 267 | <SelectItem key={n.name} value={n.domain}>{`${n.name} - ${n.domain}`}</SelectItem> |
| 268 | ))} |
| 269 | </SelectContent> |
| 270 | </Select> |
| 271 | <FormMessage /> |
| 272 | </FormItem> |
| 273 | )} |
| 274 | /> |
| gio | 9b2d496 | 2025-05-07 04:59:39 +0000 | [diff] [blame] | 275 | <FormField |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 276 | control={form.control} |
| 277 | name="subdomain" |
| 278 | render={({ field }) => ( |
| 279 | <FormItem> |
| 280 | <FormControl> |
| 281 | <Input placeholder="subdomain" className="border border-black" {...field} /> |
| 282 | </FormControl> |
| 283 | <FormMessage /> |
| 284 | </FormItem> |
| 285 | )} |
| 286 | /> |
| 287 | </form> |
| 288 | </Form> |
| 289 | <Form {...connectedToForm}> |
| 290 | <form className="space-y-2"> |
| gio | 9b2d496 | 2025-05-07 04:59:39 +0000 | [diff] [blame] | 291 | <FormField |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 292 | control={connectedToForm.control} |
| 293 | name="id" |
| 294 | render={({ field }) => ( |
| 295 | <FormItem> |
| 296 | <Select onValueChange={field.onChange} defaultValue={field.value}> |
| 297 | <FormControl> |
| 298 | <SelectTrigger> |
| 299 | <SelectValue placeholder="Service" /> |
| 300 | </SelectTrigger> |
| 301 | </FormControl> |
| 302 | <SelectContent> |
| 303 | {selectable.map((n) => ( |
| 304 | <SelectItem key={n.id} value={n.id}>{nodeLabel(n)}</SelectItem> |
| 305 | ))} |
| 306 | </SelectContent> |
| 307 | </Select> |
| 308 | <FormMessage /> |
| 309 | </FormItem> |
| 310 | )} |
| 311 | /> |
| gio | 9b2d496 | 2025-05-07 04:59:39 +0000 | [diff] [blame] | 312 | <FormField |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 313 | control={connectedToForm.control} |
| 314 | name="portId" |
| 315 | render={({ field }) => ( |
| 316 | <FormItem> |
| 317 | <Select onValueChange={field.onChange} defaultValue={field.value}> |
| 318 | <FormControl> |
| 319 | <SelectTrigger> |
| 320 | <SelectValue placeholder="Port" /> |
| 321 | </SelectTrigger> |
| 322 | </FormControl> |
| 323 | <SelectContent> |
| 324 | {selected && selected.data.ports.map((p) => ( |
| 325 | <SelectItem key={p.id} value={p.id}>{p.name} - {p.value}</SelectItem> |
| 326 | ))} |
| 327 | </SelectContent> |
| 328 | </Select> |
| 329 | <FormMessage /> |
| gio | 9b2d496 | 2025-05-07 04:59:39 +0000 | [diff] [blame] | 330 | </FormItem> |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 331 | )} |
| 332 | /> |
| 333 | </form> |
| 334 | </Form> |
| gio | 9b2d496 | 2025-05-07 04:59:39 +0000 | [diff] [blame] | 335 | Auth |
| 336 | <Form {...authEnabledForm}> |
| 337 | <form className="space-y-2"> |
| 338 | <FormField |
| 339 | control={authEnabledForm.control} |
| 340 | name="enabled" |
| 341 | render={({ field }) => ( |
| 342 | <FormItem> |
| 343 | <Checkbox id="authEnabled" onCheckedChange={field.onChange} checked={field.value} /> |
| 344 | <Label htmlFor="authEnabled">Enabled</Label> |
| 345 | <FormMessage /> |
| 346 | </FormItem> |
| 347 | )} |
| 348 | /> |
| 349 | </form> |
| 350 | </Form> |
| 351 | {data && data.auth && data.auth.enabled ? ( |
| 352 | <> |
| 353 | Authorized Groups |
| 354 | <ul> |
| 355 | {(data.auth.groups || []).map((p) => (<li key={p}><Button size={"icon"} variant={"ghost"} onClick={() => removeGroup(p)}><XIcon /></Button> {p}</li>))} |
| 356 | </ul> |
| 357 | <Form {...authGroupForm}> |
| 358 | <form className="flex flex-row space-x-1" onSubmit={authGroupForm.handleSubmit(onGroupSubmit)}> |
| 359 | <FormField |
| 360 | control={authGroupForm.control} |
| 361 | name="group" |
| 362 | render={({ field }) => ( |
| 363 | <FormItem> |
| 364 | <FormControl> |
| 365 | <Input placeholder="group" className="border border-black" {...field} /> |
| 366 | </FormControl> |
| 367 | <FormMessage /> |
| 368 | </FormItem> |
| 369 | )} |
| 370 | /> |
| 371 | <Button type="submit">Add Group</Button> |
| 372 | </form> |
| 373 | </Form> |
| 374 | Auth optional path patterns |
| 375 | <ul> |
| 376 | {(data.auth.noAuthPathPatterns || []).map((p) => (<li key={p}><Button size={"icon"} variant={"ghost"} onClick={() => removeNoAuthPathPattern(p)}><XIcon /></Button> {p}</li>))} |
| 377 | </ul> |
| 378 | <Form {...authNoAuthPatternFrom}> |
| 379 | <form className="flex flex-row space-x-1" onSubmit={authNoAuthPatternFrom.handleSubmit(onNoAuthPathPatternSubmit)}> |
| 380 | <FormField |
| 381 | control={authNoAuthPatternFrom.control} |
| 382 | name="noAuthPathPattern" |
| 383 | render={({ field }) => ( |
| 384 | <FormItem> |
| 385 | <FormControl> |
| 386 | <Input placeholder="group" className="border border-black" {...field} /> |
| 387 | </FormControl> |
| 388 | <FormMessage /> |
| 389 | </FormItem> |
| 390 | )} |
| 391 | /> |
| 392 | <Button type="submit">Add</Button> |
| 393 | </form> |
| 394 | </Form> |
| 395 | </> |
| 396 | ) : (<></>)} |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 397 | </> |
| 398 | ); |
| 399 | } |