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

89.47% Statements 17/19
80% Branches 4/5
100% Functions 8/8
89.47% Lines 17/19

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                                        49x       81x 81x 81x   81x 1x       1x 1x               81x 69x     81x       567x 577x     77x     33x       1x           81x                  
import { useState } from "react";
import type { FormulaTemplates } from "@/data/formulaTemplates.d.ts";
import formulaTemplates from "@/data/formulaTemplates.json";
import { Button } from "../../../../../ahuora-design-system/ui/button";
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "../../../../../ahuora-design-system/ui/dialog";
import { useUnitopsSimulationobjectsAddCustomPropertyTemplateCreateMutation } from "../../../../../api/apiStore.gen";
import {
  useCurrentObject,
  useCurrentObjectId,
} from "../../../../../hooks/flowsheetObjects";
 
formulaTemplates as unknown as FormulaTemplates;
 
export function FormulaTemplates() {
  const [addTemplate] =
    useUnitopsSimulationobjectsAddCustomPropertyTemplateCreateMutation();
  const [open, setOpen] = useState(false);
  const currentObject = useCurrentObject();
 
  const onAddTemplate = (templateKey: string) => {
    Iif (!currentObject) {
      console.error("No current object found");
      return;
    }
    setOpen(false);
    addTemplate({
      id: currentObject.id,
      addPropertyTemplate: {
        templateKey: templateKey,
      },
    });
  };
 
  const propertyKeys = currentObject?.properties.ContainedProperties.map(
    (property) => property.key,
  );
 
  const additionalMetrics = Object.entries(formulaTemplates)
    .filter(
      // Filter to only include templates that have all required properties
      ([key, template]) =>
        template.required_properties.every(
          (prop) => propertyKeys?.includes(prop),
          // And have not already been added to the model.
        ) &&
        template.fields.every((field) => !propertyKeys?.includes(field.key)),
    )
    .map(([key, template]) => (
      <Button
        key={key}
        variant="outline"
        className="text-wrap mr-2 mb-2"
        onClick={() => onAddTemplate(key)}
      >
        {template.name}
      </Button>
    ));
 
  return (
    additionalMetrics.length != 0 && (
      <div>
        <div className="py-2">Additional Metrics</div>
        <div>{additionalMetrics}</div>
      </div>
    )
  );
}