blob: 615f7a1c1cf7c9d698f8e477107bbb3b761ec943 [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;
gio6cf8c272025-05-08 09:01:38 +00008 children: React.ReactNode;
gio5f2f1002025-03-20 18:38:48 +04009 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[]>([]);
gioda708652025-04-30 14:57:38 +040019 const [stateClasses, setStateClasses] = useState<string[]>([]);
gio1dc800a2025-04-24 17:15:43 +000020 useEffect(() => {
gioda708652025-04-30 14:57:38 +040021 const classes = ["px-4", "py-2", "rounded-md", "bg-white"];
gio1dc800a2025-04-24 17:15:43 +000022 if (hasFatal) {
23 classes.push("border-red-500");
24 } else if (hasWarning) {
25 classes.push("border-yellow-500");
26 } else {
27 classes.push("border-black");
28 }
29 if (selected) {
30 classes.push("border-2");
31 } else {
32 classes.push("border");
33 }
gio1dc800a2025-04-24 17:15:43 +000034 setClasses(classes);
gio6cf8c272025-05-08 09:01:38 +000035 const stateClasses: string[] = [];
gioda708652025-04-30 14:57:38 +040036 if (state === "processing") {
37 stateClasses.push("bg-yellow-500");
38 stateClasses.push("animate-pulse");
39 } else if (state === "success") {
40 stateClasses.push("bg-green-500");
41 } else if (state === "failure") {
42 stateClasses.push("bg-red-500");
43 } else {
44 stateClasses.push("bg-black");
45 }
46 setStateClasses(stateClasses);
47 }, [selected, hasFatal, hasWarning, state, setClasses, setStateClasses]);
gio5f2f1002025-03-20 18:38:48 +040048 return (
49 <div className={classes.join(" ")}>
gio1dc800a2025-04-24 17:15:43 +000050 <div style={{ position: "absolute", top: "5px", left: "5px" }}>
gio5f2f1002025-03-20 18:38:48 +040051 {Icon(p.type)}
52 </div>
gioda708652025-04-30 14:57:38 +040053 <div style={{ position: "absolute", top: "5px", right: "5px", borderRadius: "50%", width: "5px", height: "5px" }} className={stateClasses.join(" ")}>
54 </div>
gio5f2f1002025-03-20 18:38:48 +040055 {children}
56 </div>
57 )
58
giof8acc612025-04-26 08:20:55 +040059}