blob: a0bd5588404b690c051412022da221b28c42f97b [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 (
gio1dc800a2025-04-24 17:15:43 +000014 <NodeRect id={id} selected={selected} type={node.type} state={node.data.state}>
gio5f2f1002025-03-20 18:38:48 +040015 <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
giof96ffb82025-04-24 09:31:05 +000034export function NodePostgreSQLDetails({ id, data }: PostgreSQLNode) {
gio5f2f1002025-03-20 18:38:48 +040035 const store = useStateStore();
36 const form = useForm<z.infer<typeof schema>>({
37 resolver: zodResolver(schema),
38 mode: "onChange",
39 defaultValues: {
giof96ffb82025-04-24 09:31:05 +000040 name: data.label,
gio5f2f1002025-03-20 18:38:48 +040041 }
42 });
43 useEffect(() => {
44 const sub = form.watch((value: DeepPartial<z.infer<typeof schema>>, { type }: { name?: keyof z.infer<typeof schema> | undefined, type?: EventType | undefined }) => {
45 if (type !== "change") {
46 return;
47 }
48 store.updateNodeData<"postgresql">(id, {
49 label: value.name,
50 });
51 });
52 return () => sub.unsubscribe();
gio6cf8c272025-05-08 09:01:38 +000053 }, [id, form, store]);
gio5f2f1002025-03-20 18:38:48 +040054 return (
55 <>
56 <Form {...form}>
57 <form className="space-y-2">
58 <FormField
59 control={form.control}
60 name="name"
61 render={({ field }) => (
62 <FormItem>
63 <FormControl>
64 <Input placeholder="name" className="border border-black" {...field} />
65 </FormControl>
66 <FormMessage />
67 </FormItem>
68 )}
69 />
70 </form>
71 </Form>
72 </>);
73}