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

88.88% Statements 40/45
53.33% Branches 8/15
80% Functions 8/10
88.63% Lines 39/44

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                                              401x 401x 401x 401x 401x   401x   401x 401x   401x                         401x     401x 401x 444x 444x 444x 444x 444x 1263x 444x         401x     401x   401x   802x 845x     802x 802x 197x     802x 444x 444x     802x       802x             802x 802x 500x 500x 500x               802x     802x                        
import {
    DirectionEnum,
    SimulationObjectRetrieveRead
} from "@/api/apiStore.gen";
import {
    useAvailablePortConnections,
    useSplitStreamWithHistory,
    useUpdatePort
} from "@/hooks/connections";
import {
    useCurrentObject,
    useFlowsheetObjectsIdMap,
    useObjectsPortsMap,
    useStreamType,
} from "@/hooks/flowsheetObjects";
 
import { Connection } from "./Connection";
 
export function StreamConnectionList({
  selectedObj: propSelectedObj,
}: {
    selectedObj?: SimulationObjectRetrieveRead;
}) {
  const currentObject = useCurrentObject();
  const selectedObj = propSelectedObj || currentObject!;
  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);
      I} 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}
      />
    );
  });
}