All files / src/pages/flowsheet-page/flowsheet/LeftSideBar/Formulas FormulaCard.tsx

68.51% Statements 37/54
66.66% Branches 42/63
100% Functions 1/1
82.85% Lines 29/35

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                                        9x                 9x 9x 9x 9x 27x 27x 27x   6x   9x                   33x   9x         33x   9x                   33x                                                             57x 9x         9x     9x   36x   9x   9x           18x 9x     9x 36x 27x   27x 27x      
import { CircleX } from "lucide-react";
import { AutoSelectInput } from "@/ahuora-design-system/ui/auto-select-input";
import { Input } from "@/ahuora-design-system/ui/input";
import {
  PropertyInfoRead,
  useCorePropertyinfoDestroyMutation,
  useCorePropertyinfoPartialUpdateMutation,
} from "@/api/apiStore.gen";
import { useFlowsheetAccess } from "@/hooks/flowsheetAccess";
import { UnitsDropdown } from "../../PropertiesSidebar/PropertyPanel/InputFields/UnitsDropdown";
import {
  canDeleteProperty,
  canEditProperty,
  canEditPropertyFormula,
} from "../../PropertiesSidebar/PropertyPanel/propertyState";
import {
  FormulaInputField,
  type FormulaMentionGroup,
} from "./FormulaInputField";
 
export function FormulaCard({
  property,
  showTop,
  formulaMentionGroups,
}: {
  property: PropertyInfoRead;
  showTop?: boolean;
  formulaMentionGroups?: FormulaMentionGroup[];
}) {
  const [updatePropertyInfo] = useCorePropertyinfoPartialUpdateMutation();
  const [deleteExpression] = useCorePropertyinfoDestroyMutation();
  const access = useFlowsheetAccess();
  const canEditFlowsheet = access?.can_edit ?? true;
  const canEditDetails = canEditFlowsheet && canEditProperty(property);
  const canEditFormula = canEditFlowsheet && canEditPropertyFormula(property);
  const canDelete = canEditFlowsheet && canDeleteProperty(property);
 
  Iif (showTop == undefined) showTop = true;
 
  const onUpdateName = (value: string) => {
    Iif (!canEditDetails) return;
    // backend update
    updatePropertyInfo({
      id: property.id,
      patchedPropertyInfo: {
        displayName: value,
      },
    });
    // TODO: optimistic update
  };
 
  const onDeleteExpression = () => {
    Iif (!canDelete) return;
    deleteExpression({
      id: property.id,
    });
  };
 
  const onUpdateUnit = (unit: string) => {
    Iif (!canEditDetails) return;
    // backend update
    updatePropertyInfo({
      id: property.id,
      patchedPropertyInfo: {
        unit: unit,
      },
    });
    // TODO: optimistic update
  };
 
  return (
    <div className="bg-muted rounded-lg pb-1 min-h-full">
      {showTop && (
        <div className="flex flex-row justify-between p-2 bg-accent items-center gap-2  rounded-t-lg">
          <div className="flex min-w-0 flex-wrap items-center gap-2">
            {!canEditDetails ? (
              <span className="block max-w-500 truncate text-sm font-medium text-primary">
                {property.displayName}
              </span>
            ) : (
              <AutoSelectInput
                aria-label="expression-name"
                textSize="text-sm"
                textColor="text-primary"
                width="max-w-500"
                value={property.displayName}
                onUpdateValue={onUpdateName}
              />
            )}
          </div>
          {canDelete && (
            <CircleX
              size={16}
              className="cursor-pointer"
              aria-label="delete-expression"
              onClick={onDeleteExpression}
            />
          )}
        </div>
      )}
      <div
        aria-label="expression-div"
        className="rounded-md p-2 flex flex-col gap-2"
      >
        <div className="flex flex-col gap-2">
          <FormulaInputField
            property={property}
            propertyValue={property.values[0]}
            disabled={!canEditFormula}
            formulaMentionGroups={formulaMentionGroups}
          />
          <div className="flex flex-row gap-2">
            <Input
              value={
                property.values[0]?.value == undefined
                  ? ""
                  : property.values[0].value
              }
              placeholder="Not calculated"
              disabled
            />
            <UnitsDropdown
              property={property}
              onUpdate={onUpdateUnit}
              disabled={!canEditDetails}
            />
          </div>
        </div>
      </div>
    </div>
  );
}