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

96.07% Statements 49/51
92.85% Branches 26/28
100% Functions 12/12
97.95% Lines 48/49

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                                          37x             294x 294x 294x 294x 294x 294x 294x 294x     294x       294x   294x 1440x 588x 98x 490x       196x 294x 196x 98x 98x     852x     294x 720x     294x     817x       817x 722x   722x 2162x       722x 2042x   722x 718x       817x 14x     817x 2x     2x       817x 2x     817x 1x     817x   817x   430x     817x           2149x                      
import {
    DirectionEnum,
    PortRead,
    SimulationObjectRetrieveRead,
    useUnitopsPortsDestroyMutation,
} from "@/api/apiStore.gen";
import {
    useAddStream,
    useAvailableStreamConnections,
    useSplitStreamWithHistory,
    useUpdatePort
} from "@/hooks/connections";
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 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 } =
    useAvailableStreamConnections();
 
 
    //selectedObjectPorts?.map((port, index) => {
    Iif (!selectedObjectPorts) return null;
 
    const getPortOrder = (port: (typeof selectedObjectPorts)[0]) => {
      if (selectedObj.objectType === "heatExchanger") {
        if (port.key.includes("cold") && port.direction === DirectionEnum.Inlet)
          return 0;
        if (
          port.key.includes("cold") &&
          port.direction === DirectionEnum.Outlet
        )
          return 1;
        if (port.key.includes("hot") && port.direction === DirectionEnum.Inlet)
          return 2;
        if (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.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 () => {
        Iif (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}
        />
      );
    });
  };