blob: a568bc02bad36df1210dc7c6d08c516136a916c4 [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(() => {
giof8acc612025-04-26 08:20:55 +040020 const classes = ["px-4", "py-2", "rounded-md"];
gio1dc800a2025-04-24 17:15:43 +000021 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 }
giof8acc612025-04-26 08:20:55 +040033 if (state === "processing") {
gio1dc800a2025-04-24 17:15:43 +000034 classes.push("animate-pulse");
giof8acc612025-04-26 08:20:55 +040035 } else if (state === "success") {
36 classes.push("bg-green-500");
37 } else if (state === "failure") {
38 classes.push("bg-red-500");
39 } else {
40 classes.push("bg-white");
gio1dc800a2025-04-24 17:15:43 +000041 }
42 setClasses(classes);
43 }, [selected, hasFatal, hasWarning, state, setClasses]);
gio5f2f1002025-03-20 18:38:48 +040044 return (
45 <div className={classes.join(" ")}>
gio1dc800a2025-04-24 17:15:43 +000046 <div style={{ position: "absolute", top: "5px", left: "5px" }}>
gio5f2f1002025-03-20 18:38:48 +040047 {Icon(p.type)}
48 </div>
49 {children}
50 </div>
51 )
52
giof8acc612025-04-26 08:20:55 +040053}