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