| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame^] | 1 | import { NodeRect } from './node-rect'; |
| 2 | import { nodeLabel, PostgreSQLNode, useStateStore } from '@/lib/state'; |
| 3 | import { useEffect } from 'react'; |
| 4 | import { Handle, Position } from "@xyflow/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 | |
| 11 | export function NodePostgreSQL(node: PostgreSQLNode) { |
| 12 | const { id, selected } = node; |
| 13 | return ( |
| 14 | <NodeRect id={id} selected={selected} type={node.type}> |
| 15 | <div style={{ padding: '10px 20px' }}> |
| 16 | {nodeLabel(node)} |
| 17 | <Handle |
| 18 | id="env_var" |
| 19 | type={"source"} |
| 20 | position={Position.Top} |
| 21 | isConnectableStart={true} |
| 22 | isConnectableEnd={true} |
| 23 | isConnectable={true} |
| 24 | /> |
| 25 | </div> |
| 26 | </NodeRect> |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | const schema = z.object({ |
| 31 | name: z.string().min(1, "required"), |
| 32 | }); |
| 33 | |
| 34 | export function NodePostgreSQLDetails(node: PostgreSQLNode) { |
| 35 | const { id } = node; |
| 36 | const store = useStateStore(); |
| 37 | const form = useForm<z.infer<typeof schema>>({ |
| 38 | resolver: zodResolver(schema), |
| 39 | mode: "onChange", |
| 40 | defaultValues: { |
| 41 | name: "", |
| 42 | } |
| 43 | }); |
| 44 | useEffect(() => { |
| 45 | const sub = form.watch((value: DeepPartial<z.infer<typeof schema>>, { type }: { name?: keyof z.infer<typeof schema> | undefined, type?: EventType | undefined }) => { |
| 46 | if (type !== "change") { |
| 47 | return; |
| 48 | } |
| 49 | store.updateNodeData<"postgresql">(id, { |
| 50 | label: value.name, |
| 51 | }); |
| 52 | }); |
| 53 | return () => sub.unsubscribe(); |
| 54 | }, [form, store]); |
| 55 | return ( |
| 56 | <> |
| 57 | <Form {...form}> |
| 58 | <form className="space-y-2"> |
| 59 | <FormField |
| 60 | control={form.control} |
| 61 | name="name" |
| 62 | render={({ field }) => ( |
| 63 | <FormItem> |
| 64 | <FormControl> |
| 65 | <Input placeholder="name" className="border border-black" {...field} /> |
| 66 | </FormControl> |
| 67 | <FormMessage /> |
| 68 | </FormItem> |
| 69 | )} |
| 70 | /> |
| 71 | </form> |
| 72 | </Form> |
| 73 | </>); |
| 74 | } |