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

95.34% Statements 41/43
92.59% Branches 25/27
100% Functions 6/6
97.56% Lines 40/41

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                                                      310x 310x 310x 310x 310x 310x 310x 310x 310x 310x           310x     310x       420x 70x 350x 140x 210x 140x 70x 70x     954x     310x 687x     310x     848x             723x   723x 2101x       723x 1970x     717x         15x             4x         3x       1x     848x   848x   349x     848x           2010x                          
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") {
      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.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}
      />
    );
  });
};