blob: e618943b41e81c1ee028c03af81bec4b86e88381 [file] [log] [blame]
gioaba9a962025-04-25 14:19:40 +00001import { v4 as uuidv4 } from "uuid";
gio5f2f1002025-03-20 18:38:48 +04002import { useStateStore, AppNode, GatewayHttpsNode, ServiceNode, nodeLabel, useEnv, nodeIsConnectable } from '@/lib/state';
3import { Handle, Position, useNodes } from '@xyflow/react';
4import { NodeRect } from './node-rect';
gio9b2d4962025-05-07 04:59:39 +00005import { useCallback, useEffect, useMemo } from 'react';
gio5f2f1002025-03-20 18:38:48 +04006import { z } from "zod";
7import { zodResolver } from "@hookform/resolvers/zod";
8import { useForm, EventType, DeepPartial } from 'react-hook-form';
9import { Form, FormControl, FormField, FormItem, FormMessage } from './ui/form';
10import { Input } from './ui/input';
11import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from './ui/select';
gio9b2d4962025-05-07 04:59:39 +000012import { Checkbox } from "./ui/checkbox";
13import { Label } from "./ui/label";
14import { Button } from "./ui/button";
15import { XIcon } from "lucide-react";
gio5f2f1002025-03-20 18:38:48 +040016
17const schema = z.object({
18 network: z.string().min(1, "reqired"),
19 subdomain: z.string().min(1, "required"),
20});
21
22const connectedToSchema = z.object({
23 id: z.string(),
24 portId: z.string(),
25});
26
gio9b2d4962025-05-07 04:59:39 +000027const authEnabledSchema = z.object({
28 enabled: z.boolean(),
29});
30
31const authGroupSchema = z.object({
32 group: z.string(),
33});
34
35const authNoAuthPatternSchema = z.object({
36 noAuthPathPattern: z.string(),
37});
38
gio5f2f1002025-03-20 18:38:48 +040039export function NodeGatewayHttps(node: GatewayHttpsNode) {
40 const { id, selected } = node;
gioaba9a962025-04-25 14:19:40 +000041 const isConnectableNetwork = useMemo(() => nodeIsConnectable(node, "subdomain"), [node]);
gio5f2f1002025-03-20 18:38:48 +040042 const isConnectable = useMemo(() => nodeIsConnectable(node, "https"), [node]);
43 return (
gio1dc800a2025-04-24 17:15:43 +000044 <NodeRect id={id} selected={selected} type={node.type} state={node.data.state}>
gio5f2f1002025-03-20 18:38:48 +040045 {nodeLabel(node)}
46 <Handle
gioaba9a962025-04-25 14:19:40 +000047 type={"source"}
gio9b2d4962025-05-07 04:59:39 +000048 id="subdomain"
gioaba9a962025-04-25 14:19:40 +000049 position={Position.Top}
50 isConnectable={isConnectableNetwork}
gio9b2d4962025-05-07 04:59:39 +000051 isConnectableStart={isConnectableNetwork}
52 isConnectableEnd={isConnectableNetwork}
gioaba9a962025-04-25 14:19:40 +000053 />
54 <Handle
gio5f2f1002025-03-20 18:38:48 +040055 type={"target"}
gio9b2d4962025-05-07 04:59:39 +000056 id="https"
gio5f2f1002025-03-20 18:38:48 +040057 position={Position.Bottom}
58 isConnectable={isConnectable}
gio9b2d4962025-05-07 04:59:39 +000059 isConnectableStart={isConnectable}
60 isConnectableEnd={isConnectable}
gio5f2f1002025-03-20 18:38:48 +040061 />
62 </NodeRect>
63 );
64}
65
66export 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: {
giof96ffb82025-04-24 09:31:05 +000073 network: data.network,
74 subdomain: data.subdomain,
gio5f2f1002025-03-20 18:38:48 +040075 },
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") {
gioaba9a962025-04-25 14:19:40 +000080 let edges = store.edges;
81 if (data.network !== undefined) {
82 edges = edges.filter((e) => {
gioaba9a962025-04-25 14:19:40 +000083 if (e.source === id && e.sourceHandle === "subdomain" && e.target === data.network && e.targetHandle === "subdomain") {
84 return false;
85 } else {
86 return true;
87 }
88 });
gio5f2f1002025-03-20 18:38:48 +040089 }
gioaba9a962025-04-25 14:19:40 +000090 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 });
gio5f2f1002025-03-20 18:38:48 +0400101 } else if (name === "subdomain") {
102 store.updateNodeData<"gateway-https">(id, { subdomain: value.subdomain });
103 }
104 });
105 return () => sub.unsubscribe();
gioaba9a962025-04-25 14:19:40 +0000106 }, [id, data, form, store]);
gio5f2f1002025-03-20 18:38:48 +0400107 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;
128 }, [data]);
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 })
142 }, [nodes, selected]);
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 }) => {
gio5f2f1002025-03-20 18:38:48 +0400145 if (type !== "change") {
146 return;
147 }
148 switch (name) {
149 case "id":
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;
162 case "portId":
163 store.updateNodeData<"gateway-https">(id, {
164 https: {
165 serviceId: value.id,
166 portId: value.portId,
167 }
168 });
169 break;
170 }
171 });
172 return () => sub.unsubscribe();
173 }, [connectedToForm, store, selectable]);
gio9b2d4962025-05-07 04:59:39 +0000174 const authEnabledForm = useForm<z.infer<typeof authEnabledSchema>>({
175 resolver: zodResolver(authEnabledSchema),
176 mode: "onChange",
177 defaultValues: {
178 enabled: data.auth ? data.auth.enabled : false,
179 },
180 });
181 const authGroupForm = useForm<z.infer<typeof authGroupSchema>>({
182 resolver: zodResolver(authGroupSchema),
183 mode: "onSubmit",
184 defaultValues: {
185 group: "",
186 },
187 }); const authNoAuthPatternFrom = useForm<z.infer<typeof authNoAuthPatternSchema>>({
188 resolver: zodResolver(authNoAuthPatternSchema),
189 mode: "onChange",
190 defaultValues: {
191 noAuthPathPattern: "",
192 },
193 });
194 useEffect(() => {
195 const sub = authEnabledForm.watch((value, { name }) => {
196 if (name === "enabled") {
197 store.updateNodeData<"gateway-https">(id, {
198 auth: {
199 ...data.auth,
200 enabled: value.enabled,
201 }
202 })
203 }
204 });
205 return () => sub.unsubscribe();
206 }, [id, data, authEnabledForm, store]);
207 const removeGroup = useCallback((group: string) => {
208 const groups = data?.auth?.groups || [];
209 store.updateNodeData<"gateway-https">(id, {
210 auth: {
211 ...data.auth,
212 groups: groups.filter((g) => g !== group),
213 },
214 });
215 return true;
216 }, [id, data, store]);
217 const onGroupSubmit = useCallback((values: z.infer<typeof authGroupSchema>) => {
218 const groups = data.auth?.groups || [];
219 groups.push(values.group)
220 store.updateNodeData<"gateway-https">(id, {
221 auth: {
222 ...data.auth,
223 groups,
224 },
225 });
226 authGroupForm.reset();
227 }, [id, data, store]);
228 const removeNoAuthPathPattern = useCallback((path: string) => {
229 const noAuthPathPatterns = data?.auth?.noAuthPathPatterns || [];
230 store.updateNodeData<"gateway-https">(id, {
231 auth: {
232 ...data.auth,
233 noAuthPathPatterns: noAuthPathPatterns.filter((p) => p !== path),
234 },
235 });
236 return true;
237 }, [id, data, store]);
238 const onNoAuthPathPatternSubmit = useCallback((values: z.infer<typeof authNoAuthPatternSchema>) => {
239 const noAuthPathPatterns = data.auth?.noAuthPathPatterns || [];
240 noAuthPathPatterns.push(values.noAuthPathPattern)
241 store.updateNodeData<"gateway-https">(id, {
242 auth: {
243 ...data.auth,
244 noAuthPathPatterns,
245 },
246 });
247 authNoAuthPatternFrom.reset();
248 }, [id, data, store]);
gio5f2f1002025-03-20 18:38:48 +0400249 return (
250 <>
251 <Form {...form}>
252 <form className="space-y-2">
gio9b2d4962025-05-07 04:59:39 +0000253 <FormField
gio5f2f1002025-03-20 18:38:48 +0400254 control={form.control}
255 name="network"
256 render={({ field }) => (
257 <FormItem>
258 <Select onValueChange={field.onChange} defaultValue={field.value}>
259 <FormControl>
260 <SelectTrigger>
261 <SelectValue placeholder="Network" />
262 </SelectTrigger>
263 </FormControl>
264 <SelectContent>
265 {env.networks.map((n) => (
266 <SelectItem key={n.name} value={n.domain}>{`${n.name} - ${n.domain}`}</SelectItem>
267 ))}
268 </SelectContent>
269 </Select>
270 <FormMessage />
271 </FormItem>
272 )}
273 />
gio9b2d4962025-05-07 04:59:39 +0000274 <FormField
gio5f2f1002025-03-20 18:38:48 +0400275 control={form.control}
276 name="subdomain"
277 render={({ field }) => (
278 <FormItem>
279 <FormControl>
280 <Input placeholder="subdomain" className="border border-black" {...field} />
281 </FormControl>
282 <FormMessage />
283 </FormItem>
284 )}
285 />
286 </form>
287 </Form>
288 <Form {...connectedToForm}>
289 <form className="space-y-2">
gio9b2d4962025-05-07 04:59:39 +0000290 <FormField
gio5f2f1002025-03-20 18:38:48 +0400291 control={connectedToForm.control}
292 name="id"
293 render={({ field }) => (
294 <FormItem>
295 <Select onValueChange={field.onChange} defaultValue={field.value}>
296 <FormControl>
297 <SelectTrigger>
298 <SelectValue placeholder="Service" />
299 </SelectTrigger>
300 </FormControl>
301 <SelectContent>
302 {selectable.map((n) => (
303 <SelectItem key={n.id} value={n.id}>{nodeLabel(n)}</SelectItem>
304 ))}
305 </SelectContent>
306 </Select>
307 <FormMessage />
308 </FormItem>
309 )}
310 />
gio9b2d4962025-05-07 04:59:39 +0000311 <FormField
gio5f2f1002025-03-20 18:38:48 +0400312 control={connectedToForm.control}
313 name="portId"
314 render={({ field }) => (
315 <FormItem>
316 <Select onValueChange={field.onChange} defaultValue={field.value}>
317 <FormControl>
318 <SelectTrigger>
319 <SelectValue placeholder="Port" />
320 </SelectTrigger>
321 </FormControl>
322 <SelectContent>
323 {selected && selected.data.ports.map((p) => (
324 <SelectItem key={p.id} value={p.id}>{p.name} - {p.value}</SelectItem>
325 ))}
326 </SelectContent>
327 </Select>
328 <FormMessage />
gio9b2d4962025-05-07 04:59:39 +0000329 </FormItem>
gio5f2f1002025-03-20 18:38:48 +0400330 )}
331 />
332 </form>
333 </Form>
gio9b2d4962025-05-07 04:59:39 +0000334 Auth
335 <Form {...authEnabledForm}>
336 <form className="space-y-2">
337 <FormField
338 control={authEnabledForm.control}
339 name="enabled"
340 render={({ field }) => (
341 <FormItem>
342 <Checkbox id="authEnabled" onCheckedChange={field.onChange} checked={field.value} />
343 <Label htmlFor="authEnabled">Enabled</Label>
344 <FormMessage />
345 </FormItem>
346 )}
347 />
348 </form>
349 </Form>
350 {data && data.auth && data.auth.enabled ? (
351 <>
352 Authorized Groups
353 <ul>
354 {(data.auth.groups || []).map((p) => (<li key={p}><Button size={"icon"} variant={"ghost"} onClick={() => removeGroup(p)}><XIcon /></Button> {p}</li>))}
355 </ul>
356 <Form {...authGroupForm}>
357 <form className="flex flex-row space-x-1" onSubmit={authGroupForm.handleSubmit(onGroupSubmit)}>
358 <FormField
359 control={authGroupForm.control}
360 name="group"
361 render={({ field }) => (
362 <FormItem>
363 <FormControl>
364 <Input placeholder="group" className="border border-black" {...field} />
365 </FormControl>
366 <FormMessage />
367 </FormItem>
368 )}
369 />
370 <Button type="submit">Add Group</Button>
371 </form>
372 </Form>
373 Auth optional path patterns
374 <ul>
375 {(data.auth.noAuthPathPatterns || []).map((p) => (<li key={p}><Button size={"icon"} variant={"ghost"} onClick={() => removeNoAuthPathPattern(p)}><XIcon /></Button> {p}</li>))}
376 </ul>
377 <Form {...authNoAuthPatternFrom}>
378 <form className="flex flex-row space-x-1" onSubmit={authNoAuthPatternFrom.handleSubmit(onNoAuthPathPatternSubmit)}>
379 <FormField
380 control={authNoAuthPatternFrom.control}
381 name="noAuthPathPattern"
382 render={({ field }) => (
383 <FormItem>
384 <FormControl>
385 <Input placeholder="group" className="border border-black" {...field} />
386 </FormControl>
387 <FormMessage />
388 </FormItem>
389 )}
390 />
391 <Button type="submit">Add</Button>
392 </form>
393 </Form>
394 </>
395 ) : (<></>)}
gio5f2f1002025-03-20 18:38:48 +0400396 </>
397 );
398}