blob: 232fe9f518e2bd1f8fa2d169f99bcbb16972efc6 [file] [log] [blame]
gio5f2f1002025-03-20 18:38:48 +04001import { NodeRect } from './node-rect';
2import { nodeLabel, PostgreSQLNode, useStateStore } from '@/lib/state';
3import { useEffect } from 'react';
4import { Handle, Position } from "@xyflow/react";
5import { z } from "zod";
6import { DeepPartial, EventType, useForm } from 'react-hook-form';
7import { zodResolver } from '@hookform/resolvers/zod';
8import { Form, FormControl, FormField, FormItem, FormMessage } from './ui/form';
9import { Input } from './ui/input';
10
11export 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
30const schema = z.object({
31 name: z.string().min(1, "required"),
32});
33
34export 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}