All files / src/pages/flowsheet-page/flowsheet/Canvas SimulationObjectGraphic.tsx

96.29% Statements 26/27
72.72% Branches 16/22
100% Functions 5/5
96.15% Lines 25/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198                                                                        8756x     8756x 8756x 8756x                                                                                                                                                                     62x   42780x   8767x 8767x   8767x 8767x 8767x   8767x 8767x 8767x 4776x   8767x 8767x           8767x                   6260x           230x   58x   2219x       8767x                               109x 102x                  
import { NodeProps } from "@xyflow/react";
import { memo, useState } from "react";
import { useSelector } from "react-redux";
import { GraphicObjectRead, SimulationObjectRead } from "@/api/apiStore.gen";
import { useProcessPathHasObject } from "@/hooks/flowsheetObjects";
import { GenericObjectGraphicProps } from "@/hooks/graphicTypes";
import { useSearchParam } from "@/hooks/searchParams";
import { RootState } from "@/store/store";
import { isStream } from "../../../../lib/isStream";
import { PropertiesStatus } from "../PropertiesSidebar/PropertyPanel/PropertiesStatus";
import GroupGraphic from "./Nodes/GroupGraphic";
import LogicBlockGraphic from "./Nodes/LogicBlockGraphic";
import MaterialStreamGraphic from "./Nodes/MaterialStreamGraphic";
import UnitOpGraphic from "./Nodes/UnitOpGraphic";
import { useDynamicHandles } from "./RenderOperations";
 
type BlockGraphicLayoutProps = {
  width: number;
  height: number;
  componentName: string;
  simulationObject: SimulationObjectRead;
  children: React.ReactNode;
  textPadding?: number;
  maxTextWidth?: number;
};
 
export function BlockGraphicLayout({
  width,
  height,
  componentName,
  simulationObject,
  children,
  textPadding = 8,
  maxTextWidth = 150,
}: BlockGraphicLayoutProps) {
  const showStatusDot =
    isStream(simulationObject) ||
    simulationObject.objectType === "decisionNode";
 
  const innerLeft = width / 2 - (componentName.length * 3.5 + 15);
  const textWidth = maxTextWidth - 26;
  const extraLabelHeight = showStatusDot ? 28 : 0;
 
  return (
    <div
      style={{
        position: "relative",
        width,
        height: height + textPadding + extraLabelHeight,
      }}
    >
      <div style={{ width, height, position: "relative" }}>{children}</div>
 
      {showStatusDot ? (
        <div
          style={{
            position: "absolute",
            left: 0,
            top: height + textPadding,
            width: width,
            height: 24,
            pointerEvents: "none",
            userSelect: "none",
            overflow: "visible",
          }}
        >
          <div
            style={{
              position: "absolute",
              left: innerLeft,
              top: 0,
              width: maxTextWidth,
              height: 24,
              overflow: "visible",
            }}
            className="select-none"
          >
            <div style={{ position: "absolute", left: -5, top: -5 }}>
              <div style={{ transform: "scale(0.6)", lineHeight: 0 }}>
                <PropertiesStatus
                  hideSuccess
                  propertySet={simulationObject.unspecifiedProperties}
                />
              </div>
            </div>
 
            <div
              style={{
                position: "absolute",
                left: 14,
                top: -5,
                width: textWidth,
                overflow: "hidden",
                whiteSpace: "nowrap",
                textOverflow: "ellipsis",
                fontFamily: "Inter",
                fontWeight: 500,
                fontSize: 16,
                letterSpacing: -0.2,
                color: "#404040",
                textAlign: "left",
              }}
              title={componentName}
            >
              {componentName}
            </div>
          </div>
        </div>
      ) : null}
    </div>
  );
}
 
interface SimulationObjectGraphicProps {
  id: string;
  position: { x: number; y: number };
  data: {
    simulationObject: SimulationObjectRead;
    graphicObject: GraphicObjectRead;
    label: string;
    hovered?: boolean;
  };
}
 
export const SimulationObjectGraphic = memo(
  (props: NodeProps<SimulationObjectGraphicProps>) => {
    const pointerState = useSelector((state: RootState) => state.pointer);
 
    const simObject = props.data.simulationObject;
    const gObj = props.data.graphicObject;
 
    const nodeWidth = gObj.width ?? 40;
    const nodeHeight = gObj.height ?? 40;
    const labelWidth = 150;
 
    const [objectId] = useSearchParam("object");
    const [mouseOver, setMouseOver] = useState(false);
    const isSpecified = simObject?.unspecifiedProperties.every(
      (set) => set.length === 0,
    );
    const processPathHasObject = useProcessPathHasObject()(simObject.id);
    const { DynamicHandles } = useDynamicHandles({
      simulationObj: simObject,
      graphicObj: gObj,
    });
 
    const renderElement = () => {
      const objProps: GenericObjectGraphicProps = {
        simulationObject: simObject,
        graphicObject: props.data.graphicObject,
        isSelected: objectId == simObject.id + "",
        isHovered: pointerState.state !== "move" && mouseOver,
        disabled: !processPathHasObject,
        highlight: props.data.hovered ?? false,
        isSpecified,
      };
      if (isStream(simObject)) {
        return <MaterialStreamGraphic {...objProps} />;
      }
      switch (simObject.objectType) {
        case "decisionNode":
          return <MaterialStreamGraphic {...objProps} />;
        case "group":
          return <GroupGraphic {...objProps} />;
        case "recycle":
          return <LogicBlockGraphic {...objProps} />;
        default:
          return <UnitOpGraphic {...objProps} />;
      }
    };
 
    return (
      <BlockGraphicLayout
        width={nodeWidth}
        height={nodeHeight}
        componentName={simObject.componentName ?? ""}
        simulationObject={simObject}
        textPadding={8}
        maxTextWidth={labelWidth}
      >
        <div
          style={{
            position: "relative",
            width: nodeWidth,
            height: nodeHeight,
            overflow: "visible",
          }}
          onMouseEnter={() => setMouseOver(true)}
          onMouseLeave={() => setMouseOver(false)}
        >
          {renderElement()}
          <DynamicHandles simulationObj={simObject} graphicObj={gObj} />
        </div>
      </BlockGraphicLayout>
    );
  },
);