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

74.6% Statements 94/126
68.18% Branches 90/132
46.66% Functions 7/15
89.15% Lines 74/83

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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293                                                                            3310x           3310x 3310x     3310x 3871x   3310x 3310x 3310x       574x     3310x 4458x   9930x   1012x                                                                                                             12418x 13240x                                               103x     477x   239x         67503x     67503x 67503x 67503x 67503x 67503x 67503x   67503x 67503x   67503x 67503x     67503x 67503x 135006x 3310x           3310x   3310x       11415x   3310x       13x         13x 130x     13x 13x 13x 13x         5149x       3310x   1x 1x       5149x       3310x       3310x                 2180x           123x   17x 990x             3310x 3310x 3310x 3310x 3310x 3310x 3310x     574x         4458x 559x 6620x   867x             3923x   6778x 923879x 3310x       77433x 87363x 93983x      
import { type Node, NodeProps, NodeResizer } from "@xyflow/react";
import { useCallback, useState } from "react";
import { useSelector } from "react-redux";
import {
  api,
  GraphicObjectRead,
  ObjectTypeEnum,
  SimulationObjectRead,
  useGraphicsObjectsPartialUpdateMutation,
} from "@/api/apiStore.gen";
import { useFlowsheetAccess } from "@/hooks/flowsheetAccess";
import {
  useCurrentGroupId,
  useProcessPathHasObject,
} from "@/hooks/flowsheetObjects";
import { GenericObjectGraphicProps } from "@/hooks/graphicTypes";
import { useSearchParam } from "@/hooks/searchParams";
import { useAppDispatch } from "@/store/hooks";
import { RootState } from "@/store/store";
import { isStreamType } from "../../../../lib/isStream";
import { PropertiesStatus } from "../PropertiesSidebar/PropertyPanel/PropertiesStatus";
import type { ActivePortReconnect } from "./connectionAnchors";
import GroupGraphic from "./Nodes/GroupGraphic";
import LogicBlockGraphic from "./Nodes/LogicBlockGraphic";
import MaterialStreamGraphic from "./Nodes/MaterialStreamGraphic";
import UnitOpGraphic from "./Nodes/UnitOpGraphic";
import { DynamicHandles } 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 =
    isStreamType(simulationObject.objectType) ||
    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>
  );
}
 
type SimulationObjectGraphicNodeData = Record<string, unknown> & {
  simulationObject: SimulationObjectRead;
  graphicObject: GraphicObjectRead;
  label: string;
  hovered?: boolean;
  activeReconnect?: ActivePortReconnect | null;
};
 
type ResizeGeometry = {
  x: number;
  y: number;
  width: number;
  height: number;
};
 
type SimulationObjectGraphicNode = Node<
  SimulationObjectGraphicNodeData,
  "simulationObjectGraphic"
>;
 
const MIN_UNIT_OP_DIMENSION = 30;
 
const isResizableGraphicObject = (simulationObject: SimulationObjectRead) => {
  if (isStreamType(simulationObject.objectType)) return false;
 
  return ![ObjectTypeEnum.DecisionNode, ObjectTypeEnum.Recycle].includes(
    simulationObject.objectType,
  );
};
 
export const SimulationObjectGraphic = (
  props: NodeProps<SimulationObjectGraphicNode>,
) => {
  const dispatch = useAppDispatch();
  const pointerState = useSelector((state: RootState) => state.pointer);
  const currentGroupId = useCurrentGroupId();
  const access = useFlowsheetAccess();
  const canMutate = access?.can_edit ?? true;
  const [updateGraphicObject] = useGraphicsObjectsPartialUpdateMutation();
 
  const simObject = props.data.simulationObject;
  const gObj = props.data.graphicObject;
 
  const nodeWidth = props.width ?? gObj.width ?? 40;
  const nodeHeight = props.height ?? gObj.height ?? 40;
  const labelWidth = 150;
 
  const [objectId] = useSearchParam("object");
  const [mouseOver, setMouseOver] = useState(false);
  const processPathHasObject = useProcessPathHasObject()(simObject.id);
  const effectiveGraphicObject: GraphicObjectRead = {
    ...gObj,
    width: nodeWidth,
    height: nodeHeight,
  };
 
  const isSelected = objectId == simObject.id + "";
  const canResize =
    canMutate &&
    isSelected &&
    pointerState.state !== "move" &&
    processPathHasObject &&
    isResizableGraphicObject(simObject);
 
  const updateGeometryInCache = useCallback(
    (geometry: ResizeGeometry) => {
      Iif (!currentGroupId) return;
 
      dispatch(
        api.util.updateQueryData(
          "graphicsObjectsList",
          { group: currentGroupId },
          (cachedGraphicObjects) => {
            const target = cachedGraphicObjects?.find(
              (graphicObject) => graphicObject.id === gObj.id,
            );
            if (target) {
              target.x = geometry.x;
              target.y = geometry.y;
              target.width = geometry.width;
              target.height = geometry.height;
            }
          },
        ),
      );
    },
    [currentGroupId, dispatch, gObj.id],
  );
 
  const persistGeometry = useCallback(
    (_event: unknown, geometry: ResizeGeometry) => {
      updateGeometryInCache(geometry);
      updateGraphicObject({
        id: gObj.id,
        patchedGraphicObject: geometry,
      });
    },
    [gObj.id, updateGeometryInCache, updateGraphicObject],
  );
 
  const isSpecified = simObject?.unspecifiedProperties.every(
    (set) => set.length === 0,
  );
  const renderElement = () => {
    const objProps: GenericObjectGraphicProps = {
      simulationObject: simObject,
      graphicObject: effectiveGraphicObject,
      isSelected,
      isHovered: pointerState.state !== "move" && mouseOver,
      disabled: !processPathHasObject,
      highlight: props.data.hovered ?? false,
      isSpecified,
    };
    if (isStreamType(simObject.objectType)) {
      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)}
      >
        <NodeResizer
          nodeId={props.id}
          isVisible={canResize}
          minWidth={MIN_UNIT_OP_DIMENSION}
          minHeight={MIN_UNIT_OP_DIMENSION}
          keepAspectRatio={false}
          autoScale
          onResize={(_event, geometry) => updateGeometryInCache(geometry)}
          onResizeEnd={persistGeometry}
        />
        {renderElement()}
        <DynamicHandles
          simulationObj={simObject}
          graphicObj={effectiveGraphicObject}
          activeReconnect={props.data.activeReconnect}
        />
      </div>
    </BlockGraphicLayout>
  );
};