blob: 3235d41c452ff7b2a610f3e704ece0eb69bca27d [file] [log] [blame]
giod0026612025-05-08 13:00:36 +00001import { NodeRect } from "./node-rect";
2import { nodeLabel, MongoDBNode, useStateStore } from "@/lib/state";
3import { useEffect } from "react";
gio5f2f1002025-03-20 18:38:48 +04004import { Handle, Position } from "@xyflow/react";
giof96ffb82025-04-24 09:31:05 +00005import { 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 NodeMongoDB(node: MongoDBNode) {
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
giof96ffb82025-04-24 09:31:05 +000030const schema = z.object({
giod0026612025-05-08 13:00:36 +000031 name: z.string().min(1, "required"),
giof96ffb82025-04-24 09:31:05 +000032});
33
gio08acd3a2025-06-12 12:15:30 +000034export function NodeMongoDBDetails({ node, disabled }: { node: MongoDBNode; disabled?: boolean }) {
35 const { id, data } = node;
giod0026612025-05-08 13:00:36 +000036 const store = useStateStore();
37 const form = useForm<z.infer<typeof schema>>({
38 resolver: zodResolver(schema),
39 mode: "onChange",
40 defaultValues: {
41 name: data.label,
42 },
43 });
44 useEffect(() => {
45 const sub = form.watch(
46 (
47 value: DeepPartial<z.infer<typeof schema>>,
48 { type }: { name?: keyof z.infer<typeof schema> | undefined; type?: EventType | undefined },
49 ) => {
50 if (type !== "change") {
51 return;
52 }
53 store.updateNodeData<"mongodb">(id, {
54 label: value.name,
55 });
56 },
57 );
58 return () => sub.unsubscribe();
59 }, [id, form, store]);
60 return (
61 <>
62 <Form {...form}>
63 <form className="space-y-2">
64 <FormField
65 control={form.control}
66 name="name"
67 render={({ field }) => (
68 <FormItem>
69 <FormControl>
gio3ec94242025-05-16 12:46:57 +000070 <Input placeholder="name" {...field} disabled={disabled} />
giod0026612025-05-08 13:00:36 +000071 </FormControl>
72 <FormMessage />
73 </FormItem>
74 )}
75 />
76 </form>
77 </Form>
78 </>
79 );
80}