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

92.1% Statements 35/38
44.44% Branches 4/9
100% Functions 6/6
91.89% Lines 34/37

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                                                370x 370x 370x 370x 370x 370x 370x   370x   370x 370x   370x                         370x     370x 370x   419x   419x   1210x 419x         370x     370x   370x   740x 789x     740x 740x 206x       419x 419x                           740x 740x 491x   491x             740x     740x                            
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!;
  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}
      />
    );
  });
}