All files / src/pages/flowsheet-page/flowsheet/PropertiesSidebar/LogicBlocks RecycleSettings.tsx

75% Statements 18/24
60% Branches 6/10
71.42% Functions 5/7
75% Lines 18/24

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                                      3x 3x     3x 36x 22x           3x 3x 3x   3x 3x 3x   3x       3x 1x     1x                           1x               3x       22x                  
import SelectInput from "@/ahuora-design-system/inputs/SelectInput";
import {
  api,
  useCoreRecycledataPartialUpdateMutation,
} from "@/api/apiStore.gen";
import {
  useCurrentObjectId,
  useFlowsheetUnitOps,
} from "@/hooks/flowsheetObjects";
import { useProjectId } from "@/hooks/project";
import { useObjectRecycleData } from "@/hooks/recycleData";
import { useAppDispatch } from "@/store/hooks";
import { isStream } from "../../../../../lib/isStream";
 
/**
 * Filter out the simulation objects that are not suitable for control
 * @param controlType - whether to filter out fixed or unfixed objects
 */
function useFilteredSimulationObjects() {
  const simulationObjects = useFlowsheetUnitOps();
  Iif (!simulationObjects) {
    return [];
  }
  return simulationObjects.filter((obj) => {
    if (isStream(obj)) {
      return true;
    }
  });
}
 
export function RecycleSettings() {
  const dispatch = useAppDispatch();
  const projectId = useProjectId();
  const simulationObjectId = useCurrentObjectId();
 
  const availableSimulationObjects = useFilteredSimulationObjects();
  const objectRecycleData = useObjectRecycleData()(simulationObjectId);
  const [updateRecycleData] = useCoreRecycledataPartialUpdateMutation();
 
  const selectedTearObjectId = objectRecycleData
    ? objectRecycleData.tearObject
    : undefined;
 
  const handleSelectSimulationObject = (id: string) => {
    Iif (!objectRecycleData) {
      return;
    }
    dispatch(
      api.util.updateQueryData(
        "coreRecycledataList",
        { projectId: projectId },
        (prev) => {
          const prevRecycleData = prev.find(
            (r) => r.id === objectRecycleData.id,
          );
          Iif (prevRecycleData) {
            prevRecycleData.tearObject = +id;
          }
        },
      ),
    );
    updateRecycleData({
      id: objectRecycleData.id,
      patchedRecycleData: {
        tearObject: +id,
      },
    });
  };
 
  return (
    <SelectInput
      title={`Select simulation object`}
      value={selectedTearObjectId ? selectedTearObjectId.toString() : ""}
      data={availableSimulationObjects.map((obj) => ({
        value: obj.id.toString(),
        label: obj.componentName!,
      }))}
      handleChange={handleSelectSimulationObject}
      className={!selectedTearObjectId ? "border-rose-700" : ""}
    />
  );
}