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

90% Statements 36/40
40% Branches 4/10
100% Functions 6/6
92.1% Lines 35/38

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                                                522x 522x 522x 522x   522x   522x 522x 522x   522x   522x 522x   522x                         522x     522x 522x   586x   586x   1645x 586x         522x     522x   522x   1044x 1108x     1044x 1044x 347x       586x 586x                           1044x 1044x 727x   727x             1044x     1044x                            
import {
  DirectionEnum,
  SimulationObjectRetrieveRead,
} from "@/api/apiStore.gen";
import {
  useAvailablePortConnections,
  useSplitStreamWithHistory,
  useUpdatePort,
} from "@/hooks/connections";
import { useFlowsheetAccess } from "@/hooks/flowsheetAccess";
import {
  useCurrentObject,
  useFlowsheetObjectsIdMap,
  useObjectsPortsMap,
  useStreamType,
} from "@/hooks/flowsheetObjects";
 
import { Connection } from "./Connection";
 
export function StreamConnectionList({
  selectedObj: propSelectedObj,
}: {
  selectedObj?: SimulationObjectRetrieveRead;
}) {
  const access = useFlowsheetAccess();
  const canMutate = access?.can_edit ?? true;
  const currentObject = useCurrentObject();
  const selectedObj = propSelectedObj || currentObject;
 
  Iif (!selectedObj) return null;
 
  const flowsheetObjectIdMap = useFlowsheetObjectsIdMap();
  const objectsPortsMap = useObjectsPortsMap();
  const streamType = useStreamType();
  const { availableInletPorts, availableOutletPorts } =
    useAvailablePortConnections();
 
  const splitStreamWithHistory = useSplitStreamWithHistory();
  const updatePort = useUpdatePort();
 
  const streamPorts = [
    {
      displayName: "Inlet",
      direction: DirectionEnum.Outlet,
      availablePorts: availableOutletPorts,
    },
    {
      displayName: "Outlet",
      direction: DirectionEnum.Inlet,
      availablePorts: availableInletPorts,
    },
  ];
 
  const selectedObjectPorts = objectsPortsMap.get(selectedObj.id);
 
  function getUnitOpPorts() {
    let portArray: number[] = [];
    selectedObjectPorts?.forEach((port) => {
      if (port) {
        const unitop = flowsheetObjectIdMap.get(port.unitOp);
        if (unitop) {
          const unitopPorts = objectsPortsMap.get(unitop.id);
          if (unitopPorts) {
            const idList = unitopPorts.map((port) => port.id);
            portArray = portArray.concat(idList);
          }
        }
      }
    });
    return portArray;
  }
 
  const unitopPorts = getUnitOpPorts();
 
  return streamPorts.map((streamPort, index) => {
    // try to find the port that the stream is connected to
    const port = selectedObjectPorts?.find(
      (port) => port.direction === streamPort.direction,
    );
    let existingConnection: number | undefined;
    let availablePorts = streamPort.availablePorts;
    availablePorts = availablePorts.filter(
      (port) => !unitopPorts?.includes(port.id),
    );
 
    if (port) {
      existingConnection = port.id;
      availablePorts.unshift(port);
    }
 
    const onConnectionChange = (id: number) => {
      updatePort(id, { stream: selectedObj.id });
    };
 
    const onDisconnect = async () => {
      if (port && port.stream && streamType(port.stream) == "intermediate") {
        await splitStreamWithHistory(port.stream);
      } else if (port) {
        updatePort(port.id, { stream: null });
      }
    };
    const availableArray: OptionsObject[] = [];
    availablePorts.forEach((port) => {
      const unitop = flowsheetObjectIdMap.get(port.unitOp);
      if (unitop) {
        availableArray.push({
          name: `${unitop.componentName!} ${port.displayName}`,
          id: port.id,
        });
      }
    });
 
    const connectedObject = port?.unitOp
      ? flowsheetObjectIdMap.get(port.unitOp)
      : undefined;
    return (
      <Connection
        key={index}
        displayName={streamPort.displayName}
        current={existingConnection}
        currentObject={connectedObject}
        options={availableArray}
        onConnectionChange={onConnectionChange}
        onDisconnect={onDisconnect}
        canMutate={canMutate}
      />
    );
  });
}