All files / src/commands AddUnitOp.tsx

81.81% Statements 36/44
90% Branches 18/20
60% Functions 3/5
88.23% Lines 30/34

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                            103x   72138x             72138x 72138x 72138x 72138x   79763x 72138x   79763x   72138x   72138x 79x   79x         79x   79x                   79x               219453x     21045x   79763x 72138x   72138x       7625x   87388x     177363x       8250x   125x 125x 125x   7625x           250x          
import { useReactFlow } from "@xyflow/react";
import { defineCommand } from "just-search-it";
import objs from "@/data/unitOpConfigs";
import {
  ObjectTypeEnum,
  useUnitopsSimulationobjectsCreateMutation,
} from "../api/apiStore.gen";
import { useCurrentGroupId } from "../hooks/flowsheetObjects";
import { useFlowsheetId } from "../hooks/project";
import { useSearchParam } from "../hooks/searchParams";
import { isStreamType } from "../lib/isStream";
import { createModel } from "../pages/flowsheet-page/flowsheet/Canvas/FlowsheetFunctions";
import { RegisterCommand } from "./CommandProvider";
 
const AddUnitOp = defineCommand<[ObjectTypeEnum], void>("addUnitOp");
 
const AddUnitOpCommand = ({
  unitOpType,
  canvasDiv,
}: {
  unitOpType: ObjectTypeEnum;
  canvasDiv: React.MutableRefObject<null>;
}) => {
  const [currentGroupId] = useSearchParam("CurrentGroupID");
  const rootGroup = useCurrentGroupId();
  const flowsheetId = useFlowsheetId();
  const { screenToFlowPosition } = useReactFlow();
 
  const image = new URL(
    "/assets/UpdatedIcons/" + unitOpType + ".svg",
    import.meta.url,
  ).href;
 
  const [createObject] = useUnitopsSimulationobjectsCreateMutation();
 
  const action = async () => {
    const pane = canvasDiv.current?.getBoundingClientRect();
 
    const screenPoint = {
      x: pane.left + pane.width / 2,
      y: pane.top + pane.height / 2,
    };
 
    const createPosition = screenToFlowPosition(screenPoint);
 
    const model = createModel(
      createPosition.x,
      createPosition.y,
      unitOpType,
      flowsheetId,
      currentGroupId || rootGroup,
    );
 
    try {
      // Create the object first
      await createObject({
        simulationObject: model,
      }).unwrap();
 
      // Django records the complete factory batch as one edit operation.
    } catch (error) {
      console.error("[AddUnitOp] Failed to create unit operation:", error);
    }
  };
 
  return (
    <RegisterCommand
      command={AddUnitOp}
      args={[unitOpType]}
      name={"Add " + objs[unitOpType].displayType}
      description={
        "Add a " + objs[unitOpType].displayType + " to the flowsheet"
      }
      group="Add Object"
      icon={
        <div className="bg-white p-1 rounded-md">
          <img src={image} alt={unitOpType} className="icon-ls" />
        </div>
      }
      action={action}
    />
  );
};
 
const UnitOpCommands = ({ canvasDiv }) => {
  return (
    <>
      {Object.keys(objs)
        .filter((key) => !isStreamType(key as ObjectTypeEnum))
        .map((key) => (
          <AddUnitOpCommand
            key={key}
            unitOpType={key as ObjectTypeEnum}
            canvasDiv={canvasDiv}
          />
        ))}
    </>
  );
};
 
export { AddUnitOp, AddUnitOpCommand, UnitOpCommands };