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

89.23% Statements 58/65
84.61% Branches 33/39
72.22% Functions 13/18
94.73% Lines 54/57

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                                                                120606x 120606x   120606x 120606x       33x 4035x   4035x 176x                   33x 5141x   5141x 5141x     5141x       5141x 1x         5141x 5141x 27305x 127435x   27305x 27220x                           33x     40202x               40202x               33x 5611x 5611x 5611x 5611x 5611x                     5611x           20101x   119825x         28083x     20101x 20101x       20101x       20101x           5611x 5210x 20468x 20468x 20468x 20468x 20468x 20468x   20468x     20101x   20101x   20101x       20101x         20101x       20101x       20101x                          
import {
  GraphicObject,
  SimulationObjectRead,
  GraphicObjectRead,
  useGraphicsgroupingsConnectionsRetrieveQuery,
  PortRead,
  DirectionEnum,
  ObjectTypeEnum,
} from "@/api/apiStore.gen";
import { SimulationObjectGraphic } from "./SimulationObjectGraphic";
import {
  useObjectsGraphicObjectsMap,
  useProcessPathHasObject,
  useCurrentGroupObjects,
  useCurrentGroupId,
  useGraphicsObjects,
} from "@/hooks/flowsheetObjects";
import {
  ConnectionInfo,
  ConnectedObjectInfo,
  ConnectionGraphic,
  LogicConnectionGraphic,
} from "@/pages/flowsheet-page/flowsheet/Canvas/ConnectionGraphic";
import { useObjectsPortsMap } from "@/hooks/flowsheetObjects";
import { useState } from "react";
import { useRecycleData } from "@/hooks/recycleData";
import { FileSpreadsheet } from "lucide-react";
 
class SkPoint {
  X: number;
  Y: number;
  constructor(
    public x: number,
    public y: number,
  ) {
    this.X = x;
    this.Y = y;
  }
}
 
export const RecycleConnections = () => {
  const recycleData = useRecycleData();
 
  return recycleData?.map((recycleConnection, index) => {
    return (
      <LogicConnectionGraphic
        key={index}
        fromObjectId={recycleConnection.simulationObject}
        toObjectId={recycleConnection.tearObject}
      />
    );
  });
};
 
export const UnitOpBlock = () => {
  const graphicsObjects = useGraphicsObjects();
 
  const [hoveredObjects, setHoveredObjects] = useState<number[]>([]);
  const currentGroupObjects = useCurrentGroupObjects();
 
  // Add object to hovered objects list
  const addHoveredObject = (id: number) => {
    setHoveredObjects((prev) => [...prev, id]);
  };
 
  const removeAllHoveredObjects = () => {
    hoveredObjects.forEach((id) => {
      setHoveredObjects((prev) => prev.filter((item) => item !== id));
    });
  };
 
  Iif (!currentGroupObjects) return [];
  return currentGroupObjects.map((unitop) => {
    const graphicObject = graphicsObjects?.find(
      (obj) => obj.simulationObject.id === unitop.id,
    );
    if (!graphicObject || !graphicObject.visible) return null;
    return (
      <SimulationObjectGraphic
        key={unitop.id}
        simulationObject={unitop}
        graphicObject={graphicObject}
        hovered={hoveredObjects.includes(unitop.id)}
        addHoveredObject={addHoveredObject}
        removeAllHoveredObjects={removeAllHoveredObjects}
        hoveredObjects={hoveredObjects}
      />
    );
  });
};
 
const parseGraphicObject = (
  obj: GraphicObject | undefined,
): ConnectedObjectInfo => {
  Iif (!obj) {
    return {
      position: new SkPoint(0, 0),
      size: new SkPoint(0, 0),
      rotation: 0,
      is_flipped: false,
    };
  }
  return {
    position: new SkPoint(+obj.x, +obj.y),
    size: new SkPoint(+(obj.width || 0), +(obj.height || 0)),
    rotation: +(obj.rotation || 0),
    is_flipped: obj.flipped || false,
  };
};
 
export const ConnectorLines = () => {
  const objectsPortsMap = useObjectsPortsMap();
  const objectsGraphicObjectsMap = useObjectsGraphicObjectsMap();
  const processPathHasObject = useProcessPathHasObject();
  const currentGroup = useCurrentGroupId();
  const { data: connections } = useGraphicsgroupingsConnectionsRetrieveQuery(
    { group: currentGroup },
    { skip: !currentGroup || currentGroup <= 0 },
  );
 
  interface PortAccumulator {
    inlets: string[];
    outlets: string[];
    //  power_outlets:string[];
  }
 
const getConnectionInfo = (
  unitopId: number,
  portId: number,
  portDirection: DirectionEnum,
): ConnectionInfo => {
  // Use the connections array to find streams for this unitop and direction
  const filteredConnections = (connections || []).filter(
    (c) =>
      c.unitOp === unitopId &&
      c.direction === (portDirection === DirectionEnum.Inlet ? "inlet" : "outlet")
  );
 
  // List all unique portIds for this unitop and direction (one per connection)
  const connectedPortIds = filteredConnections.map((c) => c.port).sort(); // sort so the order of connections doesn't change based on the streams.
 
  // This port's index among connected ports (for spacing)
  const portIndex = connectedPortIds.indexOf(portId);
  const totalConnections = connectedPortIds.length;
 
  // Evenly spaced Y, or center if not found
  const yPos =
    totalConnections > 0 && portIndex !== -1
      ? (portIndex + 0.5) / totalConnections
      : 0.5;
 
  return portDirection === DirectionEnum.Inlet
    ? { location: new SkPoint(0, yPos), direction: DirectionEnum.Inlet }
    : { location: new SkPoint(1, yPos), direction: DirectionEnum.Outlet };
};
 
 
  if (!connections) return [];
  return connections.map((connection) => {
    const streamId = connection.stream;
    const unitOpId = connection.unitOp;
    const portId = connection.port;
    const isOnProcessPath = processPathHasObject(streamId);
    const unitopGraphic = objectsGraphicObjectsMap.get(unitOpId);
    const streamGraphic = objectsGraphicObjectsMap.get(streamId);
 
    if (!unitopGraphic || !streamGraphic) return null; // couldn't find the graphic object, it is probably still being fetched
 
    const isEnergy =
      streamGraphic.simulationObject.objectType === ObjectTypeEnum.EnergyStream;
 
    const type = connection.direction;
    const startCon =
      type === "outlet"
        ? getConnectionInfo(unitOpId, portId, connection.direction)
        : { location: new SkPoint(1, 0.5), direction: "outlet" };
    const endCon =
      type === "inlet"
        ? getConnectionInfo(unitOpId, portId, connection.direction)
        : { location: new SkPoint(0, 0.5), direction: "inlet" };
 
    const startOp =
      type === "outlet"
        ? parseGraphicObject(unitopGraphic)
        : parseGraphicObject(streamGraphic);
    const endOp =
      type === "inlet"
        ? parseGraphicObject(unitopGraphic)
        : parseGraphicObject(streamGraphic);
 
    return (
      <ConnectionGraphic
        key={unitOpId + "-" + streamId}
        startOp={startOp}
        startCon={startCon}
        endOp={endOp}
        endCon={endCon}
        isEnergy={isEnergy}
        showArrow={false} //!isIntermediate ---> currently written false for testign purposes
        disabled={!isOnProcessPath}
      />
    );
  });
};