blob: ad0bee79dd956aff5049994ca53d22b2768cd5c3 [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 = {
6 id: string;
7 selected?: boolean;
8 children: any;
9 type: NodeType;
gio1dc800a2025-04-24 17:15:43 +000010 state: string | null;
gio5f2f1002025-03-20 18:38:48 +040011};
12
13export function NodeRect(p: Props) {
gio1dc800a2025-04-24 17:15:43 +000014 const { id, selected, children, state } = p;
gio5f2f1002025-03-20 18:38:48 +040015 const messages = useNodeMessages(id);
16 const hasFatal = messages.some((m) => m.type === "FATAL");
17 const hasWarning = messages.some((m) => m.type === "WARNING");
gio1dc800a2025-04-24 17:15:43 +000018 const [classes, setClasses] = useState<string[]>([]);
19 useEffect(() => {
20 const classes = ["px-4", "py-2", "rounded-md", "bg-white"];
21 if (hasFatal) {
22 classes.push("border-red-500");
23 } else if (hasWarning) {
24 classes.push("border-yellow-500");
25 } else {
26 classes.push("border-black");
27 }
28 if (selected) {
29 classes.push("border-2");
30 } else {
31 classes.push("border");
32 }
33 if (state === "running") {
34 classes.push("animate-pulse");
35 }
36 setClasses(classes);
37 }, [selected, hasFatal, hasWarning, state, setClasses]);
gio5f2f1002025-03-20 18:38:48 +040038 return (
39 <div className={classes.join(" ")}>
gio1dc800a2025-04-24 17:15:43 +000040 <div style={{ position: "absolute", top: "5px", left: "5px" }}>
gio5f2f1002025-03-20 18:38:48 +040041 {Icon(p.type)}
42 </div>
43 {children}
44 </div>
45 )
46
47}