| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 1 | import { v4 as uuidv4 } from "uuid"; |
| 2 | import { NodeRect } from './node-rect'; |
| 3 | import { useStateStore, ServiceNode, ServiceTypes, nodeLabel, BoundEnvVar, AppState, nodeIsConnectable } from '@/lib/state'; |
| 4 | import { KeyboardEvent, FocusEvent, useCallback, useEffect, useMemo, useState } from 'react'; |
| 5 | import { z } from "zod"; |
| 6 | import { DeepPartial, EventType, useForm } from 'react-hook-form'; |
| 7 | import { zodResolver } from '@hookform/resolvers/zod'; |
| 8 | import { Form, FormControl, FormField, FormItem, FormMessage } from './ui/form'; |
| 9 | import { Input } from './ui/input'; |
| 10 | import { Button } from './ui/button'; |
| 11 | import { Handle, Position } from "@xyflow/react"; |
| 12 | import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./ui/select"; |
| 13 | import { EditIcon } from "lucide-react"; |
| 14 | import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "./ui/tooltip"; |
| 15 | |
| 16 | export function NodeApp(node: ServiceNode) { |
| 17 | const { id, selected } = node; |
| 18 | const isConnectablePorts = useMemo(() => nodeIsConnectable(node, "ports"), [node]); |
| 19 | const isConnectableRepository = useMemo(() => nodeIsConnectable(node, "repository"), [node]); |
| 20 | return ( |
| 21 | <NodeRect id={id} selected={selected} type={node.type}> |
| 22 | <div style={{ padding: '10px 20px' }}> |
| 23 | {nodeLabel(node)} |
| 24 | <Handle |
| 25 | id="repository" |
| 26 | type={"target"} |
| 27 | position={Position.Left} |
| 28 | isConnectableStart={isConnectableRepository} |
| 29 | isConnectableEnd={isConnectableRepository} |
| 30 | isConnectable={isConnectableRepository} |
| 31 | /> |
| 32 | <Handle |
| 33 | id="ports" |
| 34 | type={"source"} |
| 35 | position={Position.Top} |
| 36 | isConnectableStart={isConnectablePorts} |
| 37 | isConnectableEnd={isConnectablePorts} |
| 38 | isConnectable={isConnectablePorts} |
| 39 | /> |
| 40 | <Handle |
| 41 | id="env_var" |
| 42 | type={"target"} |
| 43 | position={Position.Bottom} |
| 44 | isConnectableStart={true} |
| 45 | isConnectableEnd={true} |
| 46 | isConnectable={true} |
| 47 | /> |
| 48 | </div> |
| 49 | </NodeRect> |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | const schema = z.object({ |
| 54 | name: z.string().min(1, "requried"), |
| 55 | type: z.enum(ServiceTypes), |
| 56 | }); |
| 57 | |
| 58 | const portSchema = z.object({ |
| 59 | name: z.string().min(1, "required"), |
| 60 | value: z.coerce.number().gt(0, "can not be negative"), |
| 61 | }); |
| 62 | |
| 63 | export function NodeAppDetails({ id, data }: ServiceNode) { |
| 64 | const store = useStateStore(); |
| 65 | const form = useForm<z.infer<typeof schema>>({ |
| 66 | resolver: zodResolver(schema), |
| 67 | mode: "onChange", |
| 68 | defaultValues: { |
| 69 | name: data.label, |
| 70 | type: data.type, |
| 71 | } |
| 72 | }); |
| 73 | const portForm = useForm<z.infer<typeof portSchema>>({ |
| 74 | resolver: zodResolver(portSchema), |
| 75 | mode: "onSubmit", |
| 76 | defaultValues: { |
| 77 | name: "", |
| 78 | value: 0, |
| 79 | } |
| 80 | }); |
| 81 | const onSubmit = useCallback((values: z.infer<typeof portSchema>) => { |
| 82 | store.updateNodeData<"app">(id, { |
| 83 | ports: (data.ports || []).concat({ |
| 84 | id: uuidv4(), |
| 85 | name: values.name, |
| 86 | value: values.value, |
| 87 | }) |
| 88 | }); |
| 89 | portForm.reset(); |
| 90 | }, [data, portForm]); |
| 91 | useEffect(() => { |
| 92 | const sub = form.watch((value: DeepPartial<z.infer<typeof schema>>, { name, type }: { name?: keyof z.infer<typeof schema> | undefined, type?: EventType | undefined }) => { |
| 93 | console.log({ name, type }); |
| 94 | if (type !== "change") { |
| 95 | return; |
| 96 | } |
| 97 | switch (name) { |
| 98 | case "name": |
| 99 | if (!value.name) { |
| 100 | break; |
| 101 | } |
| 102 | store.updateNodeData<"app">(id, { |
| 103 | label: value.name, |
| 104 | }); |
| 105 | break; |
| 106 | case "type": |
| 107 | if (!value.type) { |
| 108 | break; |
| 109 | } |
| 110 | store.updateNodeData<"app">(id, { |
| 111 | type: value.type, |
| 112 | }) |
| 113 | break; |
| 114 | } |
| 115 | }); |
| 116 | return () => sub.unsubscribe(); |
| 117 | }, [form, store]); |
| 118 | const focus = useCallback((field: any, name: string) => { |
| 119 | return (e: HTMLElement | null) => { |
| 120 | field.ref(e); |
| 121 | if (e != null && name === data.activeField) { |
| 122 | console.log(e); |
| 123 | e.focus(); |
| 124 | store.updateNodeData(id, { |
| 125 | activeField: undefined, |
| 126 | }); |
| 127 | } |
| 128 | } |
| 129 | }, [data, store]); |
| 130 | const [typeProps, setTypeProps] = useState({}); |
| 131 | useEffect(() => { |
| 132 | if (data.activeField === "type") { |
| 133 | setTypeProps({ |
| 134 | open: true, |
| 135 | onOpenChange: () => store.updateNodeData(id, { activeField: undefined }), |
| 136 | }); |
| 137 | } else { |
| 138 | setTypeProps({}); |
| 139 | } |
| 140 | }, [store, data, setTypeProps]); |
| 141 | const editAlias = useCallback((e: BoundEnvVar) => { |
| 142 | return () => { |
| 143 | store.updateNodeData(id, { |
| 144 | ...data, |
| 145 | envVars: data.envVars!.map((o) => { |
| 146 | if (o.id !== e.id) { |
| 147 | return o; |
| 148 | } else return { |
| 149 | ...o, |
| 150 | isEditting: true, |
| 151 | } |
| 152 | }), |
| 153 | }); |
| 154 | }; |
| 155 | }, [id, data, store]); |
| 156 | const saveAlias = (e: BoundEnvVar, value: string, store: AppState) => { |
| 157 | store.updateNodeData(id, { |
| 158 | ...data, |
| 159 | envVars: data.envVars!.map((o) => { |
| 160 | if (o.id !== e.id) { |
| 161 | return o; |
| 162 | } |
| 163 | if (value) { |
| 164 | return { |
| 165 | ...o, |
| 166 | isEditting: false, |
| 167 | alias: value.toUpperCase(), |
| 168 | } |
| 169 | } |
| 170 | console.log(o); |
| 171 | if ("alias" in o) { |
| 172 | const { alias: tmp, ...rest } = o; |
| 173 | console.log(rest); |
| 174 | return { |
| 175 | ...rest, |
| 176 | isEditting: false, |
| 177 | }; |
| 178 | } |
| 179 | return { |
| 180 | ...o, |
| 181 | isEditting: false, |
| 182 | }; |
| 183 | }), |
| 184 | }); |
| 185 | }; |
| 186 | const saveAliasOnEnter = useCallback((e: BoundEnvVar) => { |
| 187 | return (event: KeyboardEvent<HTMLInputElement>) => { |
| 188 | if (event.key === "Enter") { |
| 189 | event.preventDefault(); |
| 190 | saveAlias(e, event.currentTarget.value, store); |
| 191 | } |
| 192 | } |
| 193 | }, [id, data, store]); |
| 194 | const saveAliasOnBlur = useCallback((e: BoundEnvVar) => { |
| 195 | return (event: FocusEvent<HTMLInputElement>) => { |
| 196 | saveAlias(e, event.currentTarget.value, store); |
| 197 | } |
| 198 | }, [id, data, store]); |
| 199 | return ( |
| 200 | <> |
| 201 | <Form {...form}> |
| 202 | <form> |
| 203 | <FormField |
| 204 | control={form.control} |
| 205 | name="name" |
| 206 | render={({ field }) => ( |
| 207 | <FormItem> |
| 208 | <FormControl> |
| 209 | <Input placeholder="name" className="border border-black" {...field} ref={focus(field, "name")} /> |
| 210 | </FormControl> |
| 211 | <FormMessage /> |
| 212 | </FormItem> |
| 213 | )} |
| 214 | /> |
| 215 | <FormField |
| 216 | control={form.control} |
| 217 | name="type" |
| 218 | render={({ field }) => ( |
| 219 | <FormItem> |
| 220 | <Select onValueChange={field.onChange} defaultValue={field.value} {...typeProps}> |
| 221 | <FormControl> |
| 222 | <SelectTrigger> |
| 223 | <SelectValue placeholder="Runtime" /> |
| 224 | </SelectTrigger> |
| 225 | </FormControl> |
| 226 | <SelectContent> |
| 227 | {ServiceTypes.map((t) => ( |
| 228 | <SelectItem key={t} value={t}>{t}</SelectItem> |
| 229 | ))} |
| 230 | </SelectContent> |
| 231 | </Select> |
| 232 | <FormMessage /> |
| 233 | </FormItem> |
| 234 | )} |
| 235 | /> |
| 236 | </form> |
| 237 | </Form> |
| 238 | Ports |
| 239 | <ul> |
| 240 | {data && data.ports && data.ports.map((p) => (<li key={p.id}>{p.name} - {p.value}</li>))} |
| 241 | </ul> |
| 242 | <Form {...portForm}> |
| 243 | <form className="flex flex-row space-x-1" onSubmit={portForm.handleSubmit(onSubmit)}> |
| 244 | <FormField |
| 245 | control={portForm.control} |
| 246 | name="name" |
| 247 | render={({ field }) => ( |
| 248 | <FormItem> |
| 249 | <FormControl> |
| 250 | <Input placeholder="name" className="border border-black" {...field} /> |
| 251 | </FormControl> |
| 252 | <FormMessage /> |
| 253 | </FormItem> |
| 254 | )} |
| 255 | /> |
| 256 | <FormField |
| 257 | control={portForm.control} |
| 258 | name="value" |
| 259 | render={({ field }) => ( |
| 260 | <FormItem> |
| 261 | <FormControl> |
| 262 | <Input placeholder="value" className="border border-black" {...field} /> |
| 263 | </FormControl> |
| 264 | <FormMessage /> |
| 265 | </FormItem> |
| 266 | )} |
| 267 | /> |
| 268 | <Button type="submit">Add Port</Button> |
| 269 | </form> |
| 270 | </Form> |
| 271 | Env Vars |
| 272 | <ul> |
| 273 | {data && data.envVars && data.envVars.map((v) => { |
| 274 | if ("name" in v) { |
| 275 | const value = "alias" in v ? v.alias : v.name; |
| 276 | if (v.isEditting) { |
| 277 | return (<li key={v.id}><Input type="text" className="border border-black" defaultValue={value} onKeyUp={saveAliasOnEnter(v)} onBlur={saveAliasOnBlur(v)} autoFocus={true} /></li>); |
| 278 | } |
| 279 | return ( |
| 280 | <li key={v.id} onClick={editAlias(v)}> |
| 281 | <TooltipProvider> |
| 282 | <Tooltip> |
| 283 | <TooltipTrigger> |
| 284 | <Button size={"icon"} variant={"ghost"}><EditIcon /></Button> |
| 285 | {value} |
| 286 | </TooltipTrigger> |
| 287 | <TooltipContent> |
| 288 | {v.name} |
| 289 | </TooltipContent> |
| 290 | </Tooltip> |
| 291 | </TooltipProvider> |
| 292 | </li> |
| 293 | ); |
| 294 | } |
| 295 | })} |
| 296 | </ul> |
| 297 | </>); |
| 298 | } |