All files / src/pages/flowsheet-page/flowsheet/PropertiesSidebar/PropertyPackages PropertyPackage.tsx

95.45% Statements 21/22
90.9% Branches 10/11
100% Functions 9/9
95.45% Lines 21/22

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                                                    641x 641x 641x 641x           641x     641x   5670x       6x         6x 6x   6x 6x             6x                 1x             641x       641x             5139x                                               579x   579x       579x     526x                  
import SelectInput from "@/ahuora-design-system/inputs/SelectInput";
import { Label } from "@/ahuora-design-system/ui/label";
import {
  api,
  SimulationObjectPropertyPackagesRead,
  useCoreCustomPropertyPackagesListQuery,
  usePropertyPackagesSimulationObjectPropertyPackagesListQuery,
  usePropertyPackagesSimulationObjectPropertyPackagesPartialUpdateMutation,
} from "@/api/apiStore.gen";
import allPropertyPackages from "@/data/propertyPackages.json";
import { useCurrentObject } from "@/hooks/flowsheetObjects";
import { useAppDispatch } from "@/store/hooks";
import { FeatureFlag, useFeatureFlag } from "../../../../../lib/featureFlags";
import { CustomPackageChooser } from "./CustomPackageChooser";
import { CustomPackageDialog } from "./CustomPackageDialog";
 
/**
 * a single property package slot
 */
type PropertyPackageSlotProps = {
  simulationObjectPackage: SimulationObjectPropertyPackagesRead;
};
function PropertyPackageSlot({
  simulationObjectPackage,
}: PropertyPackageSlotProps) {
  const [updateUnitOpPropertyPackage] =
    usePropertyPackagesSimulationObjectPropertyPackagesPartialUpdateMutation();
  const currentSelectedName = simulationObjectPackage.propertyPackage;
  const dispatch = useAppDispatch();
  const unitOp = useCurrentObject();
 
  // We use feature flags to determine wheather to show the custom property package option in the dropdown,
  //  but we want to keep the option to open the modal to view the custom property package for existing users
  // who have already set a property package to custom.
 
  const [customPropertyPackagesEnabled] = useFeatureFlag(
    FeatureFlag.CUSTOM_PROPERTY_PACKAGES,
  );
  const availablePropertyPackages = customPropertyPackagesEnabled
    ? allPropertyPackages
    : allPropertyPackages.filter((pkg) => pkg.key !== "custom");
 
  function onChange(pkgKey: string) {
    // optimistic update
    dispatch(
      api.util.updateQueryData(
        "propertyPackagesSimulationObjectPropertyPackagesList",
        { simulationObject: unitOp!.id },
        (cachedPropertyPackages) => {
          const cachedPackage = cachedPropertyPackages!.find(
            (pkg) => pkg.id === simulationObjectPackage.id,
          );
          if (cachedPackage) {
            cachedPackage.propertyPackage = pkgKey;
          }
        },
      ),
    );
 
    //Update in backend
    updateUnitOpPropertyPackage({
      id: simulationObjectPackage.id,
      patchedSimulationObjectPropertyPackages: {
        propertyPackage: pkgKey,
      },
    });
  }
 
  function onCustomPackageChange(newCustomPackageId: number) {
    updateUnitOpPropertyPackage({
      id: simulationObjectPackage.id,
      patchedSimulationObjectPropertyPackages: {
        customPackage: newCustomPackageId,
      },
    });
  }
  Iif (!simulationObjectPackage) {
    return "Loading...";
  }
 
  return (
    <div className="flex flex-col gap-3">
      {simulationObjectPackage.name && (
        <Label variant="light">{simulationObjectPackage.name}</Label>
      )}
      <SelectInput
        title="Select property package"
        data={availablePropertyPackages.map((pkg) => ({
          label: pkg.name,
          value: pkg.key,
        }))}
        value={currentSelectedName}
        handleChange={onChange}
      />
      {currentSelectedName === "custom" && (
        <CustomPackageChooser
          customPackageId={simulationObjectPackage.customPackage}
          onCustomPackageChange={onCustomPackageChange}
        />
      )}
      {currentSelectedName === "custom" &&
        simulationObjectPackage.customPackage && (
          <CustomPackageDialog
            propertyPackageId={simulationObjectPackage.customPackage}
          ></CustomPackageDialog>
        )}
    </div>
  );
}
 
function PropertyPackageSettings() {
  const selectedObject = useCurrentObject();
  const { data: selectedObjectPropertyPackages } =
    usePropertyPackagesSimulationObjectPropertyPackagesListQuery({
      simulationObject: selectedObject!.id!,
    });
 
  return (
    <div className="flex flex-col gap-3">
      {selectedObjectPropertyPackages?.map((pkg) => {
        return (
          <PropertyPackageSlot key={pkg.id} simulationObjectPackage={pkg} />
        );
      })}
    </div>
  );
}
 
export default PropertyPackageSettings;