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

84.61% Statements 11/13
93.33% Branches 28/30
66.66% Functions 4/6
84.61% Lines 11/13

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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167                                                      33x                                                                                     33x                         3761x 3761x 3761x         11283x 11283x       26327x 26327x   26327x                                                                     5x                                                                  
import {
  ContextMenuContent,
  ContextMenuItem,
  ContextMenuSeparator,
  ContextMenuShortcut,
} from "@/ahuora-design-system/ui/context-menu";
import { group } from "console";
import React from "react";
import { isStream, isStreamType } from "../../../../lib/isStream";
import { ObjectTypeEnum } from "../../../../api/apiStore.gen";
 
interface ContextMenuProps {
  //Functions to calls Add more functions here
  //if more functions are needed for canvas context0
  deleteSelectedObject: () => void;
  rotateFlipSelectedObject: (direction: string) => void;
  unGroupCurrentObject: () => void;
  groupSelectedObjects: () => void;
  convertStreamToDecisionNode: (id: number) => void;
  copyObject: () => void;
  pasteObject: () => void;
  //Variables
  objectId: number;
  objectType: ObjectTypeEnum;
  isSelectionVisible?: boolean;
}
//Possible shortcuts will be added in the future
const contextInfo = {
  general: {
    delete: {
      label: "Delete",
      shortcut: "Backspace / Delete",
    },
    copy: {
      label: "Copy",
      shortcut: "CTRL + C",
      ariaLabel: "Copy object action",
    },
    paste: {
      label: "Paste",
      shortcut: "CTRL + V",
      ariaLabel: "Paste object action",
    },
    right: {
      label: "Rotate right",
    },
    left: {
      label: "Rotate left",
    },
    horizontal: {
      label: "Flip horizontally",
    },
    vertical: {
      label: "Flip vertically",
      // isDisabled: (objectType: ObjectTypeEnum) => isStreamType(objectType),
    },
  },
  group: {
    create: {
      label: "Pack",
      ariaLabel: "Merge elements action",
    },
    unpack: {
      label: "Unpack",
      ariaLabel: "Unpack merged element action",
    },
  },
  stream: "Convert into Decision node",
};
 
export const CanvasContextMenu: React.FC<ContextMenuProps> = ({
  deleteSelectedObject,
  rotateFlipSelectedObject,
  unGroupCurrentObject,
  groupSelectedObjects,
  convertStreamToDecisionNode,
  copyObject,
  pasteObject,
  objectType,
  objectId,
  isSelectionVisible,
}) => {
  const groupCondition =
    objectType === "group" && !isSelectionVisible ? "unpack" : "create";
  const objIsStream = isStreamType(objectType);
  return (
    <>
      <ContextMenuContent className="w-64 z-max">
        {Object.entries(contextInfo).map(
          ([contextGroup, contextGroupData], index, array) => {
            const isLastGroup = index === array.length - 1;
            return (
              <React.Fragment key={index}>
                {contextGroup === "general" &&
                  Object.keys(contextGroupData).map((key) => {
                    const subGroupData = contextInfo[contextGroup][key];
                    const isDisabled = subGroupData.isDisabled?.(objectType);
 
                    return (
                      <ContextMenuItem
                        key={key}
                        onClick={
                          key === "delete"
                            ? deleteSelectedObject
                            : key === "copy"
                              ? copyObject
                              : key === "paste"
                                ? pasteObject
                                : () => rotateFlipSelectedObject(key)
                        }
                        aria-label={subGroupData.ariaLabel}
                        disabled={isDisabled}
                        className={
                          isDisabled
                            ? "text-muted-foreground cursor-not-allowed"
                            : ""
                        }
                      >
                        <span>{subGroupData.label}</span>
                        {subGroupData.shortcut && (
                          <ContextMenuShortcut>
                            {subGroupData.shortcut}
                          </ContextMenuShortcut>
                        )}
                      </ContextMenuItem>
                    );
                  })}
                {contextGroup === "group" && (
                  <ContextMenuItem
                    key={groupCondition}
                    onClick={
                      groupCondition === "unpack"
                        ? unGroupCurrentObject
                        : () => groupSelectedObjects()
                    }
                    aria-label={contextGroupData[groupCondition].ariaLabel}
                  >
                    <span>{contextGroupData[groupCondition].label}</span>
                    {contextGroupData[groupCondition].shortcut && (
                      <ContextMenuShortcut>
                        {contextGroupData[groupCondition].shortcut}
                      </ContextMenuShortcut>
                    )}
                  </ContextMenuItem>
                )}
                {contextGroup === "stream" &&
                  objIsStream &&
                  !isSelectionVisible && (
                    <ContextMenuItem
                      key="stream"
                      onClick={() => convertStreamToDecisionNode(objectId)}
                    >
                      <span>{contextInfo["stream"]}</span>
                    </ContextMenuItem>
                  )}
                {!isLastGroup && (objIsStream || contextGroup !== "group") && (
                  <ContextMenuSeparator />
                )}
              </React.Fragment>
            );
          },
        )}
      </ContextMenuContent>
    </>
  );
};