All files / src/pages/flowsheet-page/flowsheet/PropertiesSidebar/Connections PortsMap.tsx

60.86% Statements 42/69
44% Branches 22/50
45.45% Functions 5/11
78.72% Lines 37/47

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                                        2377x             345x 345x 345x 345x 345x 345x 345x 345x 345x 345x           345x         345x   62x   124x   124x   62x   1166x     432x   345x 769x 345x         979x             781x   781x 781x       781x 2312x     776x         22x             4x         3x       1x     979x   979x   319x 979x                                          
import {
  DirectionEnum,
  SimulationObjectRetrieveRead,
  useUnitopsPortsDestroyMutation,
} from "@/api/apiStore.gen";
import {
  useAddStream,
  useAvailableStreamConnections,
  useSplitStreamWithHistory,
  useUpdatePort,
} from "@/hooks/connections";
import { useFlowsheetAccess } from "@/hooks/flowsheetAccess";
import {
  useFlowsheetObjectsIdMap,
  useObjectsPortsMap,
  useStreamType,
} from "@/hooks/flowsheetObjects";
import { ObjectType } from "../../../../../data/ObjectTypes.gen";
import { Connection } from "./Connection";
 
export const PortsMap = ({
  selectedObj,
  unitConfig,
}: {
  selectedObj: SimulationObjectRetrieveRead;
  unitConfig: ObjectType;
}) => {
  const access = useFlowsheetAccess();
  const canMutate = access?.can_edit ?? true;
  const streamType = useStreamType();
  const splitStreamWithHistory = useSplitStreamWithHistory();
  const updatePort = useUpdatePort();
  const addStream = useAddStream();
  const objectsPortsMap = useObjectsPortsMap();
  const [deletePort] = useUnitopsPortsDestroyMutation();
  const flowsheetObjectIdMap = useFlowsheetObjectsIdMap();
  const selectedObjectPorts = objectsPortsMap.get(selectedObj.id);
 
  const {
    availableInletStreams,
    availableOutletStreams,
    availablePowerStreams,
  } = useAvailableStreamConnections();
 
  //selectedObjectPorts?.map((port, index) => {
  Iif (!selectedObjectPorts) return null;
 
  const getPortOrder = (port: (typeof selectedObjectPorts)[0]) => {
    if (selectedObj.objectType === "heatExchanger") {
      Iif (port.key.includes("cold") && port.direction === DirectionEnum.Inlet)
        return 0;
      Iif (port.key.includes("cold") && port.direction === DirectionEnum.Outlet)
        return 1;
      Iif (port.key.includes("hot") && port.direction === DirectionEnum.Inlet)
        return 2;
      Iif (port.key.includes("hot") && port.direction === DirectionEnum.Outlet)
        return 3;
    }
    // Default order: Inlets first, then Outlets
    return port.direction === DirectionEnum.Inlet ? 0 : 1;
  };
 
  const orderedPorts = [...selectedObjectPorts].sort(
    (a, b) => getPortOrder(a) - getPortOrder(b),
  );
 
  return orderedPorts.map((port, index) => {
    // try to find the stream that the port is connected to
    let availableObjects =
      port.displayName === "Power Stream"
        ? availablePowerStreams
        : port.direction === DirectionEnum.Inlet
          ? availableInletStreams
          : availableOutletStreams;
    let existingConnection: SimulationObjectRetrieveRead | undefined;
    if (port.stream) {
      existingConnection = flowsheetObjectIdMap.get(port.stream);
      // remove already connected streams from the list of available streams
      const unitOpPorts = objectsPortsMap.get(selectedObj.id)!; // attached ports of the selected unit op
      const attachedStreams = unitOpPorts.map((port) => port.stream);
 
      //  const port.StreamType ===
 
      availableObjects = availableObjects.filter(
        (stream) => !attachedStreams.includes(stream.id),
      );
      if (existingConnection) {
        availableObjects.unshift(existingConnection); // add the existing connection to the top of the list
      }
    }
 
    const onConnectionChange = (id: number) => {
      updatePort(port.id, { stream: id });
    };
 
    const onDisconnect = async () => {
      if (port.stream && streamType(port.stream) == "intermediate") {
        await splitStreamWithHistory(port.stream);
      } else {
        updatePort(port.id, { stream: null });
      }
    };
 
    const onStreamAdd = () => {
      addStream(port.id);
    };
 
    const onDelete = () => {
      deletePort({ id: port.id });
    };
 
    const port_config = unitConfig?.ports?.[port.key];
    const canDeletePort =
      port_config?.many &&
      selectedObjectPorts.filter((p) => {
        return p.key === port.key;
      }).length > port_config.minimum;
 
    return (
      <Connection
        key={index}
        displayName={port.displayName}
        current={existingConnection?.id}
        currentObject={existingConnection}
        options={availableObjects.map((stream) => ({
          name: stream.componentName!,
          id: stream.id,
        }))}
        onConnectionChange={onConnectionChange}
        onDisconnect={onDisconnect}
        onAdd={onStreamAdd}
        onDelete={canDeletePort ? onDelete : undefined}
        canMutate={canMutate}
      />
    );
  });
};