blob: 94b7fac99ce7c6f7c1b3b0cf962f6e0f8545da42 [file] [log] [blame]
gio5f2f1002025-03-20 18:38:48 +04001import { NodeType, useNodeMessages } from "@/lib/state";
2import { Icon } from "./icon";
gio1dc800a2025-04-24 17:15:43 +00003import { useEffect, useState } from "react";
gio5f2f1002025-03-20 18:38:48 +04004
5export type Props = {
giod0026612025-05-08 13:00:36 +00006 id: string;
7 selected?: boolean;
8 children: React.ReactNode;
9 type: NodeType;
gio818da4e2025-05-12 14:45:35 +000010 state?: string | null;
gio5f2f1002025-03-20 18:38:48 +040011};
12
13export function NodeRect(p: Props) {
giod0026612025-05-08 13:00:36 +000014 const { id, selected, children, state } = p;
15 const messages = useNodeMessages(id);
16 const hasFatal = messages.some((m) => m.type === "FATAL");
17 const hasWarning = messages.some((m) => m.type === "WARNING");
18 const [classes, setClasses] = useState<string[]>([]);
19 const [stateClasses, setStateClasses] = useState<string[]>([]);
20 useEffect(() => {
21 const classes = ["px-4", "py-2", "rounded-md", "bg-white"];
giodc2dfcf2025-05-16 09:41:04 +000022 classes.push("border");
23 if (selected) {
24 classes.push("shadow-xl");
25 }
giod0026612025-05-08 13:00:36 +000026 if (hasFatal) {
27 classes.push("border-red-500");
28 } else if (hasWarning) {
29 classes.push("border-yellow-500");
30 } else {
31 classes.push("border-black");
32 }
giod0026612025-05-08 13:00:36 +000033 setClasses(classes);
34 const stateClasses: string[] = [];
35 if (state === "processing") {
36 stateClasses.push("bg-yellow-500");
37 stateClasses.push("animate-pulse");
38 } else if (state === "success") {
39 stateClasses.push("bg-green-500");
40 } else if (state === "failure") {
41 stateClasses.push("bg-red-500");
42 } else {
43 stateClasses.push("bg-black");
44 }
45 setStateClasses(stateClasses);
46 }, [selected, hasFatal, hasWarning, state, setClasses, setStateClasses]);
47 return (
48 <div className={classes.join(" ")}>
gio0b4002c2025-05-11 15:48:51 +000049 <div style={{ position: "absolute", top: "5px", left: "5px" }}>
50 <Icon type={p.type} />
51 </div>
giod0026612025-05-08 13:00:36 +000052 <div
53 style={{
54 position: "absolute",
55 top: "5px",
56 right: "5px",
57 borderRadius: "50%",
58 width: "5px",
59 height: "5px",
60 }}
61 className={stateClasses.join(" ")}
62 ></div>
63 {children}
64 </div>
65 );
giof8acc612025-04-26 08:20:55 +040066}