All files / src/pages/flowsheet-page/flowsheet/Canvas FlowsheetCommands.tsx

100% Statements 17/17
58.33% Branches 7/12
100% Functions 6/6
100% Lines 16/16

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                        37x 37x 37x       37x             8396x   4292x 4292x 4292x     6x         6x                 4292x 24623x         24623x                 4292x                                                                                   24623x                     6x                        
import { ObjectTypeEnum } from "@/api/apiStore.gen";
import { RegisterCommand } from "@/commands/CommandProvider";
import { useGraphicsObjects } from "@/hooks/flowsheetObjects";
import { setClick } from "@/store/ClickSlice";
import { useAppDispatch } from "@/store/hooks";
import { RootState } from "@/store/store";
import { defineCommand } from "just-search-it";
import { Fullscreen, Hand, MousePointer } from "lucide-react";
import { useSelector } from "react-redux";
import { useSearchParams } from "react-router-dom";
import { ContentTypes } from "../LeftSideBar/LeftSideBarTabDefinitions";
 
export const ZoomToFit = defineCommand<[], void>("zoomToFit");
export const ZoomReset = defineCommand<[], void>("zoomReset");
export const TogglePointerState = defineCommand<
  [state: "select" | "hand"],
  void
>("togglePointerState");
export const GoToObject = defineCommand<[id: number], void>("goToObject");
 
export function FlowsheetCommands({
  zoomToFit,
  resetZoom,
  togglePointerState,
}) {
  const pointerState = useSelector((state: RootState) => state.pointer);
  //get all objects
  const graphicsObjects = useGraphicsObjects();
  const [_, setParams] = useSearchParams("object");
  const dispatch = useAppDispatch();
  //Change selected object
  function selectObject(objectId: number, parentGroup: string) {
    setParams({
      parentGroup: parentGroup.toString(),
      object: objectId.toString(),
      content: ContentTypes.objectDetails,
    });
    dispatch(
      setClick({
        type: "unit",
        id: objectId,
      }),
    );
  }
 
  //Manage existing flowsheet icon objects
  const displayObjectIcon = (objectType: ObjectTypeEnum) => {
    const image = new URL(
      "/assets/UpdatedIcons/" + objectType + ".svg",
      import.meta.url,
    ).href;
 
    return (
      <div className="bg-white p-1 rounded-md">
        <img src={image} alt={objectType} className="icon-ls" />
      </div>
    );
  };
 
  // TODO: registerCommand for e.g zoom to fit doesn't need to be here.
  // We could just put a useRegisterCommand hook in the file the zoomToFit function is in
  return (
    <>
      <RegisterCommand
        command={ZoomToFit}
        args={[]}
        name="Zoom to Fit"
        description="Zoom to fit the flowsheet"
        group="Display & flowsheet controls"
        icon={<Fullscreen className="icon-ls"/>}
        action={zoomToFit}
        shortcuts={[{ key: "F" }]}
      />
      <RegisterCommand
        command={ZoomReset}
        args={[]}
        name="Zoom Reset (100%)"
        description=""
        group="Display & flowsheet controls"
        icon={<Fullscreen className="icon-ls"/>}
        action={resetZoom}
        shortcuts={[
          { key: "0", ctrlKey: true },
          { key: "0", metaKey: true },
        ]}
      />
      <RegisterCommand
        command={TogglePointerState}
        args={[pointerState.state === "select" ? "hand" : "select"]}
        name={
          pointerState.state === "select" ? "Move Pointer" : "Select Pointer"
        }
        description={
          pointerState.state === "select"
            ? "Allows you to move around the design without accidentally selecting objects."
            : "Allows you to move and select elements in the flowsheet."
        }
        group="Display & flowsheet controls"
        icon={pointerState.state === "select" ? <Hand className="icon-ls"/> : <MousePointer className="icon-ls"/>}
        action={togglePointerState}
        shortcuts={[{ key: pointerState.state === "select" ? "M" : "S" }]}
      />
      {graphicsObjects?.map((unitOp) => {
        return (
          unitOp.group && (
            <RegisterCommand
              key={unitOp.simulationObject.componentName}
              command={GoToObject}
              args={[unitOp.id]}
              name={`Go To ${unitOp.simulationObject.componentName}`}
              description={`Unit Op: ${unitOp.simulationObject.componentName}`}
              icon={displayObjectIcon(unitOp.simulationObject.objectType)}
              group="Go to object"
              action={() =>
                selectObject(
                  unitOp.simulationObject.id,
                  unitOp.group?.toString(),
                )
              }
            />
          )
        );
      })}
    </>
  );
}