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

86.11% Statements 31/36
94.87% Branches 74/78
71.42% Functions 5/7
86.11% Lines 31/36

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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261                                                                  6583x     6583x 6583x 6583x 6583x 6583x 6583x   6583x     6583x 6583x 6583x   6583x 6583x   6583x 1509x 955x     554x 554x 41x     554x 554x 554x     6583x 10x     6583x 147x     6583x                               6583x 1x 1x               6583x                                                                                                                                                                                                                                                                                                                            
import DebouncedInput from "@/ahuora-design-system/ui/debounced-input";
import {
    PropertyInfoRead,
    PropertyValueRead,
    useCorePropertyvaluePartialUpdateMutation,
    useCoreRecyclepropertyPartialUpdateMutation
} from "@/api/apiStore.gen";
import { round_to_sf_dp } from "@/functions/roundNumber";
import { getPropertyData } from "@/hooks/properties";
import { Repeat2, Replace, SquareFunction, X } from "lucide-react";
import { useMemo, useState } from "react";
import { Button } from "../../../../../../ahuora-design-system/ui/button";
import { ToolTipCover } from "../../../../../../ahuora-design-system/ui/tooltip";
import { cn } from "../../../../../../lib/utils";
import { useRatingMode } from "../../../LeftSideBar/Scenarios/useCurrentScenario";
import { FormulaInputField } from "../../../LeftSideBar/Formulas/FormulaInputField";
import { CalculateFromTarget, ControlTarget } from "../ControlTarget";
import MSSConnection from "../MSSConnection";
import { UnitsDropdown } from "./UnitsDropdown";
import { ReplaceInfo } from "./VariableWithUnit/ReplaceInfo";
 
export interface VariableWithUnitProps {
  property: PropertyInfoRead;
  displayName?: string;
  onUpdateValue: (value: string) => void;
  onUpdateUnit: (unit: string) => void;
  withMSSConnection?: boolean; // should be applied if multi steady state applies
  deletePropertyFunction?: () => void; // should only be for compounds
  values: PropertyValueRead;
  property_val_key: string;
}
 
export function VariableWithUnit(props: VariableWithUnitProps) {
  const [formulaEnabled, setFormulaEnabled] = useState(
    props.values.formula ?? false
  );
  const withMSSConnection = props.withMSSConnection;
  const deletePropertyFunction = props.deletePropertyFunction;
  const [updateRecycleProperty] = useCoreRecyclepropertyPartialUpdateMutation();
  const [updateValue] = useCorePropertyvaluePartialUpdateMutation();
  const ratingMode = useRatingMode();
  const recycleFixed = props.property.recycleConnection?.fixed ?? false;
 
  const fixed = ![undefined, null, ""].includes(props.values.value);
 
  const { isControlManipulated, isControlSetPoint, displayName, isRecycle } =
    getPropertyData(props.property, props.values, props.displayName);
  const disabled = !props.values.enabled && !isControlSetPoint;
  const isEditable = !disabled || isControlSetPoint || isControlManipulated;
  const isGuess =
    (isEditable && isControlManipulated) || (isRecycle && !isControlSetPoint);
  const showFormula = formulaEnabled && isEditable && !isGuess; // don't show if there is a formula, but this isn't a DoF.
 
  const roundedValue = useMemo(() => {
    if (!fixed) {
      return "";
    }
 
    let value = props.values.value;
    if (props.property.displayName === "Compounds" && isEditable) {
      value = props.values.displayValue;
    }
 
    const propertyValue = value;
    const parsedValue = parseFloat(propertyValue);
    return round_to_sf_dp(parsedValue);
  }, [props.values.displayValue, props.values.value]);
 
  const handleUnitChange = (unit: string) => {
    props.onUpdateUnit(unit);
  };
 
  const handleUpdate = (update: string | number) => {
    props.onUpdateValue(update as string);
  };
 
  const handleShowFormula = () => {
    setFormulaEnabled((state) => {
      Iif (state) {
        // Remove the formula
        updateValue({
          id: props.values.id,
          patchedPropertyValue: {
            formula: "",
          },
        });
        return false;
      }
      return true;
    });
  };
 
  const handleChangeRecycleFixed = () => {
    console.log("update recycle fixed to", !recycleFixed);
    updateRecycleProperty({
      id: props.property.recycleConnection!.id,
      patchedRecycleProperty: {
        fixed: !recycleFixed,
      }
    });
  };
 
  return (
    <div
      className={cn(
        "w-full p-2",
        (isEditable || isRecycle) && "border rounded-lg my-1",
        (isEditable || isRecycle) && (!isGuess || recycleFixed)
          ? "bg-muted"
          : ""
      )}
    >
      <div className="flex flex-row justify-between items-center">
        <div
          className="flex items-center gap-1"
          color={disabled ? "disabled" : "default"}
        >
          <p>{displayName}</p>
 
          {isRecycle && !isControlManipulated && !isControlSetPoint && (
            <ToolTipCover
              content={
                recycleFixed
                  ? "This property is fixed."
                  : "This guess is adjusted by the recycle. Click to fix this property."
              }
            >
              <Repeat2
                size={14}
                className={recycleFixed ? "text-gray-500" : "text-normal"}
                aria-label={`recycle-fix-${displayName}`}
                onClick={handleChangeRecycleFixed}
              />
            </ToolTipCover>
          )}
          {isRecycle &&
            !isControlSetPoint &&
            !isControlManipulated &&
            !recycleFixed && (
              <ToolTipCover content="Replace an existing parameter">
                <ControlTarget
                  disabled={disabled}
                  property={props.property}
                  value={props.values}
                >
                  <Replace
                    size={14}
                    color="hsl(var(--muted-foreground))"
                    aria-label={`recycle-control-${displayName}`}
                  />
                </ControlTarget>
              </ToolTipCover>
            )}
        </div>
        <div className="flex flex-row gap-2">
          {isEditable &&
            !isControlManipulated &&
            !isControlSetPoint &&
            !isGuess &&
            !ratingMode && (
              <ToolTipCover content="Replace DoF" delay={0}>
                <CalculateFromTarget
                  value={props.values}
                  property={props.property}
                />
              </ToolTipCover>
            )}
          {(isEditable || recycleFixed) && !isGuess && !showFormula && (
            <ToolTipCover content="Add constraint" delay={0} asChild>
              <SquareFunction
                size={17}
                className="cursor-pointer"
                onClick={handleShowFormula}
              />
            </ToolTipCover>
          )}
 
          {withMSSConnection && <MSSConnection property={props.property} />}
          {deletePropertyFunction && (
            <X
              className="cursor-pointer hover:opacity-70"
              aria-label={`deleteProperty-${displayName}`}
              onClick={deletePropertyFunction}
            />
          )}
        </div>
      </div>
      <div className="flex flex-col ap-2">
        <div className="flex flex-row gap-2 h-full items-end">
          <div className="flex flex-col w-full">
            {(isEditable || isRecycle) && (
              <DebouncedInput
                onUpdate={handleUpdate}
                value={roundedValue}
                type="number"
                style={{ width: "100%" }}
                isFixed={fixed}
                placeholder={
                  isGuess && !recycleFixed
                    ? "Enter a guess..."
                    : "Enter a value..."
                }
                aria-label={`inputField-${displayName}`}
                className="bg-transparent"
              />
            )}
            {!isRecycle && !isEditable && (
              <ControlTarget
                disabled={disabled}
                value={props.values}
                property={props.property}
              >
                <div>
                  <ToolTipCover
                    asChild
                    content="This is not a parameter, but you can choose one to replace."
                  >
                    <Button
                      aria-label={`Choose a parameter for ${props.property.displayName} to replace`}
                      variant="outline"
                      size="sm"
                      className="w-full font-normal mt-2"
                    >
                      {![undefined, null, ""].includes(roundedValue)
                        ? roundedValue
                        : "Not calculated"}
                    </Button>
                  </ToolTipCover>
                </div>
              </ControlTarget>
            )}
          </div>
          {props.property.displayName !== "Compounds" && (
            <UnitsDropdown
              property={props.property}
              onUpdate={handleUnitChange}
            />
          )}
        </div>
        {!ratingMode && <ReplaceInfo value={props.values} displayName={displayName}/>}
        {showFormula && (
          <div className="flex flex-row items-baseline gap-2">
            <FormulaInputField
              context="specification"
              property={props.property}
              propertyValue={props.values}
            />
            <ToolTipCover content="Remove constraint">
              <X
                color="hsl(var(--muted-foreground))"
                onClick={handleShowFormula}
                aria-label={`remove-constraint-${displayName}`}
              />
            </ToolTipCover>
          </div>
        )}
      </div>
    </div>
  );
}