All files / src/commands AddUnitOp.tsx

95.65% Statements 22/23
85.71% Branches 6/7
100% Functions 5/5
95.65% Lines 22/23

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                                                33x   33x                     199352x 199352x 199352x 199352x   199352x         199352x   199352x 19x 19x 19x               19x   19x         19x 19x                 19x             199352x                                     33x 3761x     218138x   199333x                          
import {
  ObjectTypeEnum,
  useUnitopsSimulationobjectsCreateMutation,
} from "../api/apiStore.gen";
import { useCurrentGroupId } from "../hooks/flowsheetObjects";
import { useProjectId } from "../hooks/project";
import { useSearchParam } from "../hooks/searchParams";
import {
  canvasToFlowsheetCoords,
  createModel,
} from "../pages/flowsheet-page/flowsheet/Canvas/FlowsheetFunctions";
import objs from "@/data/objects.json";
import { RegisterCommand } from "./CommandProvider";
import { isStreamType } from "../lib/isStream";
import { defineCommand } from "just-search-it";
import { useUndoRedoStore } from "../hooks/useUndoRedoStore";
import { createAddCommand } from "../utils/commandCreators";
 
interface CanvasState {
  stageX: number;
  stageY: number;
  scale: number;
}
 
const AddUnitOp = defineCommand<[ObjectTypeEnum], void>("addUnitOp");
 
const AddUnitOpCommand = ({
  unitOpType,
  state,
  width,
  height,
}: {
  unitOpType: ObjectTypeEnum;
  state: CanvasState;
  width: number;
  height: number;
}) => {
  const [currentGroupId] = useSearchParam("CurrentGroupID");
  const rootGroup = useCurrentGroupId();
  const flowsheetId = useProjectId();
  const { addHistory } = useUndoRedoStore();
 
  const image = new URL(
    "/assets/UpdatedIcons/" + unitOpType + ".svg",
    import.meta.url,
  ).href;
 
  const [createObject] = useUnitopsSimulationobjectsCreateMutation();
 
  const action = async () => {
    const x = canvasToFlowsheetCoords(width / 2, state.stageX, state.scale);
    const y = canvasToFlowsheetCoords(height / 2, state.stageY, state.scale);
    const model = createModel(
      x,
      y,
      unitOpType,
      flowsheetId,
      currentGroupId || rootGroup,
    );
 
    try {
      // Create the object first
      const response = await createObject({
        simulationObject: model,
      }).unwrap();
 
      // CRITICAL FIX: Track the ADD operation for undo/redo
      if (response?.id) {
        const addCommand = createAddCommand({
          objectId: response.id,
          graphicObjectId: response.graphics_object?.id || response.id,
          objectType: unitOpType,
          simulationObject: response,
          position: { x, y },
          groupId: currentGroupId ? parseInt(currentGroupId) : rootGroup,
        });
 
        addHistory(addCommand);
      }
    } 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="w-6 h-6" />
        </div>
      }
      action={action}
    />
  );
};
 
const UnitOpCommands = ({ state, width, height }) => {
  return (
    <>
      {Object.keys(objs)
        .filter((key) => !isStreamType(key as ObjectTypeEnum))
        .map((key) => (
          <AddUnitOpCommand
            key={key}
            unitOpType={key as ObjectTypeEnum}
            state={state}
            width={width}
            height={height}
          />
        ))}
    </>
  );
};
 
export { AddUnitOpCommand, UnitOpCommands, AddUnitOp };