blob: 292fd1e40ee8e1069ede1bb7937825512b55eca8 [file] [log] [blame]
giod0026612025-05-08 13:00:36 +00001import { NodeRect } from "./node-rect";
2import { nodeLabel, PostgreSQLNode, useStateStore } from "@/lib/state";
3import { useEffect } from "react";
gio5f2f1002025-03-20 18:38:48 +04004import { Handle, Position } from "@xyflow/react";
5import { z } from "zod";
giod0026612025-05-08 13:00:36 +00006import { 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";
gio5f2f1002025-03-20 18:38:48 +040010
11export function NodePostgreSQL(node: PostgreSQLNode) {
giod0026612025-05-08 13:00:36 +000012 const { id, selected } = node;
13 return (
14 <NodeRect id={id} selected={selected} type={node.type} state={node.data.state}>
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 );
gio5f2f1002025-03-20 18:38:48 +040028}
29
30const schema = z.object({
giod0026612025-05-08 13:00:36 +000031 name: z.string().min(1, "required"),
gio5f2f1002025-03-20 18:38:48 +040032});
33
giof96ffb82025-04-24 09:31:05 +000034export function NodePostgreSQLDetails({ id, data }: PostgreSQLNode) {
giod0026612025-05-08 13:00:36 +000035 const store = useStateStore();
36 const form = useForm<z.infer<typeof schema>>({
37 resolver: zodResolver(schema),
38 mode: "onChange",
39 defaultValues: {
40 name: data.label,
41 },
42 });
43 useEffect(() => {
44 const sub = form.watch(
45 (
46 value: DeepPartial<z.infer<typeof schema>>,
47 { type }: { name?: keyof z.infer<typeof schema> | undefined; type?: EventType | undefined },
48 ) => {
49 if (type !== "change") {
50 return;
51 }
52 store.updateNodeData<"postgresql">(id, {
53 label: value.name,
54 });
55 },
56 );
57 return () => sub.unsubscribe();
58 }, [id, form, store]);
59 return (
60 <>
61 <Form {...form}>
62 <form className="space-y-2">
63 <FormField
64 control={form.control}
65 name="name"
66 render={({ field }) => (
67 <FormItem>
68 <FormControl>
giofcefd7c2025-05-13 08:01:07 +000069 <Input placeholder="name" {...field} />
giod0026612025-05-08 13:00:36 +000070 </FormControl>
71 <FormMessage />
72 </FormItem>
73 )}
74 />
75 </form>
76 </Form>
77 </>
78 );
79}