All files / src/pages/flowsheet-page/flowsheet/Canvas/Nodes GroupGraphic.tsx

44.61% Statements 29/65
62.96% Branches 17/27
12.5% Functions 1/8
47.54% Lines 29/61

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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207                                                    49x 49x 49x   49x 253x 253x 253x 253x   253x 253x 253x   253x 253x     253x   253x   253x 253x 253x 253x   253x   253x 253x   253x           253x             253x 253x   253x                                           253x                                                                                               253x                                                                                                                            
import { useRunCommand } from "just-search-it";
import React, { useCallback, useRef, useState } from "react";
import useImage from "use-image";
import { ModuleMark } from "@/ahuora-design-system/componentIcons/ModuleMark";
import {
  api,
  useGraphicsObjectsPartialUpdateMutation,
} from "@/api/apiStore.gen";
import {
  useCurrentGroupId,
  useSimulationObjectGroup,
} from "@/hooks/flowsheetObjects";
import type { GenericObjectGraphicProps } from "@/hooks/graphicTypes";
import { useAppDispatch } from "@/store/hooks";
import { SwitchGroup } from "../../../../../commands/SwitchCurrentGroup";
import { PropertiesStatus } from "../../PropertiesSidebar/PropertyPanel/PropertiesStatus";
 
import {
  Box,
  getFillColor,
  getStrokeColor,
  NodeLabel,
  Overlay,
  ResizeHandle,
} from "./HelperFunctions.tsx";
 
const MIN_DIMENSION = 80;
const HANDLE_SIZE = 12;
const TEXT_PADDING = 8;
 
const GroupGraphic = (props: GenericObjectGraphicProps) => {
  const dispatch = useAppDispatch();
  const groupId = useCurrentGroupId();
  const simulationObjectGroup = useSimulationObjectGroup();
  const switchGroup = useRunCommand(SwitchGroup);
 
  const gObj = props.graphicObject;
  const width = gObj.width ?? 100;
  const height = gObj.height ?? 100;
 
  const [updateGraphicObject] = useGraphicsObjectsPartialUpdateMutation();
  const [isResizing, setIsResizing] = useState(false);
 
  const canResize =
    props.simulationObject.objectType === "group" && !props.disabled;
  const showResizeHandle =
    canResize && (props.isSelected || props.isHovered || isResizing);
 
  const imageName = props.simulationObject?.objectType;
  const [defaultImage] = useImage(`../../assets/UpdatedIcons/uo_default.svg`);
  const [img] = useImage(`../../assets/UpdatedIcons/${imageName}.svg`);
  const image = img ?? defaultImage;
 
  const isModule = props.simulationObject.objectType === "group";
 
  const fill = getFillColor(props);
  const stroke = getStrokeColor(props, false);
 
  const openGroup = useCallback(() => {
    const group = simulationObjectGroup(props.simulationObject.id);
    Iif (!group) return;
    switchGroup(group.id);
  }, [simulationObjectGroup, props.simulationObject.id, switchGroup]);
 
  const dragRef = useRef<{
    startX: number;
    startY: number;
    startW: number;
    startH: number;
  } | null>(null);
 
  const latestSizeRef = useRef({ w: width, h: height });
  latestSizeRef.current = { w: width, h: height };
 
  const updateSizeInCache = useCallback(
    (nextW: number, nextH: number) => {
      Iif (!groupId) return;
      dispatch(
        api.util.updateQueryData(
          "graphicsObjectsList",
          { group: groupId },
          (cached) => {
            const target = cached?.find(
              (go) => go.simulationObject.id === props.simulationObject.id,
            );
            Iif (target) {
              target.width = nextW;
              target.height = nextH;
            }
          },
        ),
      );
    },
    [dispatch, groupId, props.simulationObject.id],
  );
 
  const onResizeMouseDown = useCallback(
    (e: React.MouseEvent) => {
      e.preventDefault();
      e.stopPropagation();
      Iif (!canResize) return;
 
      setIsResizing(true);
      dragRef.current = {
        startX: e.clientX,
        startY: e.clientY,
        startW: width,
        startH: height,
      };
 
      const onMove = (ev: MouseEvent) => {
        Iif (!dragRef.current) return;
 
        const dx = ev.clientX - dragRef.current.startX;
        const dy = ev.clientY - dragRef.current.startY;
 
        const nextW = Math.max(MIN_DIMENSION, dragRef.current.startW + dx);
        const nextH = Math.max(MIN_DIMENSION, dragRef.current.startH + dy);
 
        latestSizeRef.current = { w: nextW, h: nextH };
        updateSizeInCache(nextW, nextH);
      };
 
      const onUp = async () => {
        setIsResizing(false);
        dragRef.current = null;
 
        window.removeEventListener("mousemove", onMove);
        window.removeEventListener("mouseup", onUp);
 
        const { w, h } = latestSizeRef.current;
 
        await updateGraphicObject({
          id: gObj.id,
          patchedGraphicObject: { width: w, height: h },
        });
      };
 
      window.addEventListener("mousemove", onMove);
      window.addEventListener("mouseup", onUp);
    },
    [canResize, height, updateGraphicObject, updateSizeInCache, width, gObj.id],
  );
 
  return (
    <div
      style={{ width, height, position: "relative" }}
      aria-label={`Module Graphic - ${props.simulationObject.componentName ?? ""}`}
    >
      <Box
        width={width}
        height={height}
        fill={fill}
        stroke={stroke}
        radius={2}
        onDoubleClick={openGroup}
      >
        {image && (
          <img
            src={(image as any).src ?? undefined}
            alt=""
            draggable={false}
            style={{
              width: "50%",
              height: "50%",
              objectFit: "contain",
              pointerEvents: "none",
              userSelect: "none",
            }}
          />
        )}
      </Box>
 
      {isModule && (
        <Overlay left={10} top={10} style={{ transform: "scale(0.9)" }}>
          <div className="text-emerald-600 select-none">
            <ModuleMark />
          </div>
        </Overlay>
      )}
 
      <Overlay right={6} bottom={6} style={{ transform: "scale(0.9)" }}>
        <div className="select-none">
          <PropertiesStatus
            propertySet={props.simulationObject.unspecifiedProperties}
            hideSuccess={true}
          />
        </div>
      </Overlay>
 
      <ResizeHandle
        size={HANDLE_SIZE}
        visible={showResizeHandle}
        onMouseDown={onResizeMouseDown}
      />
 
      <NodeLabel
        text={props.simulationObject.componentName ?? ""}
        width={width}
        y={height + TEXT_PADDING}
      />
    </div>
  );
};
 
export default GroupGraphic;