blob: f1128a843686caed0068a96ae67dec80fc567ffa [file] [log] [blame]
gio3fb133d2025-06-13 07:20:24 +00001import { useState, useEffect } from "react";
gio8e74dc02025-06-13 10:19:26 +00002import { nodeLabel, useStateStore } from "@/lib/state";
gio3fb133d2025-06-13 07:20:24 +00003import { Icon } from "./icon";
4import { Input } from "./ui/input";
gio69148322025-06-19 23:16:12 +04005import { AppNode } from "config";
gio3fb133d2025-06-13 07:20:24 +00006
7export function Name({
8 node,
9 disabled,
10 editing,
11}: {
12 node: AppNode;
13 disabled?: boolean;
14 editing?: boolean;
15}): React.ReactNode {
16 const { id, data } = node;
17 const store = useStateStore();
18 const [isEditing, setIsEditing] = useState(false);
19 useEffect(() => {
20 if (data.label === "") {
21 setIsEditing(true);
22 }
23 }, [data.label, disabled]);
gio8e74dc02025-06-13 10:19:26 +000024 if (node.type === "github" || node.type === "gateway-https" || node.type === "gateway-tcp") {
25 return (
26 <div className="w-full flex flex-row gap-1 items-center">
gio69148322025-06-19 23:16:12 +040027 <Icon node={node} />
gioefc9a4a2025-07-06 16:22:10 +000028 <h4 className="w-full cursor-text select-none hover:outline-solid hover:outline-2 hover:outline-gray-200">
gio8e74dc02025-06-13 10:19:26 +000029 {nodeLabel(node)}
gioefc9a4a2025-07-06 16:22:10 +000030 </h4>
gio8e74dc02025-06-13 10:19:26 +000031 </div>
32 );
33 }
gio3fb133d2025-06-13 07:20:24 +000034 return (
35 <div className="w-full flex flex-row gap-1 items-center">
gio69148322025-06-19 23:16:12 +040036 <Icon node={node} />
gio3fb133d2025-06-13 07:20:24 +000037 {isEditing || editing ? (
38 <Input
39 placeholder="Name"
40 className="w-full"
41 value={data.label}
42 onChange={(e) => store.updateNodeData(id, { label: e.target.value })}
43 onBlur={() => {
44 if (data.label !== "") {
45 setIsEditing(false);
46 }
47 }}
48 disabled={disabled}
49 />
50 ) : (
gioefc9a4a2025-07-06 16:22:10 +000051 <h4
52 className="w-full cursor-text select-none hover:outline-solid hover:outline-2 hover:outline-gray-200"
gio3fb133d2025-06-13 07:20:24 +000053 onClick={() => setIsEditing(true)}
54 >
55 {data.label}
gioefc9a4a2025-07-06 16:22:10 +000056 </h4>
gio3fb133d2025-06-13 07:20:24 +000057 )}
58 </div>
59 );
60}