All files / src/pages/flowsheet-page/flowsheet/PropertiesSidebar/PropertyPanel/InputFields/VariableWithUnit ReplaceInfo.tsx

86.66% Statements 13/15
84.21% Branches 16/19
100% Functions 0/0
86.66% Lines 13/15

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                                            15738x 15738x   126x               174x                 15438x                     176x 176x 176x             2x                                                                                         129x 129x 129x             2x                                                                    
import { X } from "lucide-react";
import { useSearchParams } from "react-router-dom";
import {
  PropertyValueRead,
  useCoreControlvaluesDestroyMutation,
} from "@/api/apiStore.gen";
import { ToolTipCover } from "../../../../../../../ahuora-design-system/ui/tooltip";
import { ReverseReplace } from "../../../../../../../assets/ReverseReplace";
 
type ReplaceInfoProps = {
  value: PropertyValueRead;
  displayName: string;
  onChangeDof?: () => void;
  canMutate?: boolean;
};
 
export function ReplaceInfo({
  value,
  displayName,
  onChangeDof,
  canMutate = true,
}: ReplaceInfoProps) {
  const isControlManipulated = value.controlManipulated;
  const isControlSetPoint = value.controlSetPoint;
  if (isControlManipulated) {
    return (
      <ReplacedByInfo
        value={value}
        displayName={displayName}
        canMutate={canMutate}
      />
    );
  } else if (isControlSetPoint) {
    return (
      <ReplacingInfo
        value={value}
        displayName={displayName}
        onChangeDof={onChangeDof}
        canMutate={canMutate}
      />
    );
  } else {
    return null;
  }
}
 
// When this property is replacing another property
function ReplacingInfo({
  value,
  displayName,
  onChangeDof,
  canMutate,
}: ReplaceInfoProps) {
  const [searchParams, setSearchParams] = useSearchParams();
  const [deleteControl] = useCoreControlvaluesDestroyMutation();
  const objectId = +(searchParams.get("object") || 0);
 
  const handleControlSetPointObjectClick = () => {
    setSearchParams({ object: value.controlSetPointId });
  };
 
  const handleRemoveControl = () => {
    deleteControl({
      id: value.controlSetPoint!,
    });
  };
 
  return (
    <div className="flex flex-row text-success">
      <div className="flex flex-row gap-2 pt-1 flex-grow text-success">
        <ReverseReplace size={14} fill="hsl(var(--muted-foreground))" />
        <small className="font-light">
          Replacing {value.controlSetPointName}
          {value.controlSetPointId != objectId && <small> at:</small>}
        </small>
        {value.controlSetPointId != objectId && (
          <small
            onClick={handleControlSetPointObjectClick}
            style={{ cursor: "pointer", textDecoration: "underline" }}
          >
            {value.controlSetPointObject}
          </small>
        )}
        {canMutate && (
          <small
            className="cursor-pointer bold underline"
            onClick={onChangeDof}
          >
            Change
          </small>
        )}
      </div>
      {canMutate && (
        <ToolTipCover content="Click to remove parameter replacement.">
          <X
            color="hsl(var(--muted-foreground))"
            onClick={handleRemoveControl}
            aria-label={`setpoint-${displayName}`}
          ></X>
        </ToolTipCover>
      )}
    </div>
  );
}
 
// When this property is replaced by another property
function ReplacedByInfo({ value, displayName, canMutate }: ReplaceInfoProps) {
  const [searchParams, setSearchParams] = useSearchParams();
  const [deleteControl] = useCoreControlvaluesDestroyMutation();
  const objectId = +(searchParams.get("object") || 0);
 
  const handleControlManipulatedObjectClick = () => {
    setSearchParams({ object: value.controlManipulatedId });
  };
 
  const handleRemoveControl = () => {
    deleteControl({
      id: value.controlManipulated!,
    });
  };
 
  return (
    <div className="flex flex-row text-success">
      <div className="flex flex-row gap-2 flex-grow text-success">
        <small className="font-light">
          {" "}
          Replaced by {value.controlManipulatedName}
          {value.controlManipulatedId != objectId && <small> at:</small>}
        </small>
        {value.controlManipulatedId != objectId && (
          <small
            onClick={handleControlManipulatedObjectClick}
            style={{ cursor: "pointer", textDecoration: "underline" }}
          >
            {value.controlManipulatedObject}
          </small>
        )}
      </div>
      {canMutate && (
        <ToolTipCover content="Click to remove replacement.">
          <X
            color="hsl(var(--muted-foreground))"
            onClick={handleRemoveControl}
            aria-label={`controlled-${displayName}`}
          ></X>
        </ToolTipCover>
      )}
    </div>
  );
}