All files / src/pages/flowsheet-page/flowsheet/PropertiesSidebar/LogicBlocks VariantSettings.tsx

67.74% Statements 21/31
66.66% Branches 14/21
50% Functions 1/2
80.95% Lines 17/21

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                      116x 116x   116x 116x   116x 116x     134x   116x       3x             3x 3x 3x 3x       386x     54x         116x 332x      
import { useSearchParams } from "react-router-dom";
import SelectInput from "@/ahuora-design-system/inputs/SelectInput";
import { useUnitopsSimulationobjectsSwitchVariantCreateMutation } from "@/api/apiStore.gen";
import type { UnitOpVariantFamily } from "@/data/unitOpConfigs";
import { useCurrentObject } from "@/hooks/flowsheetObjects";
import { ContentTypes } from "../../LeftSideBar/LeftSideBarTabDefinitions";
 
type VariantSettingsProps = {
  family: UnitOpVariantFamily;
};
 
export function VariantSettings({ family }: VariantSettingsProps) {
  const currentSimulationObject = useCurrentObject();
  const [switchVariant] =
    useUnitopsSimulationobjectsSwitchVariantCreateMutation();
  const [searchParams, setSearchParams] = useSearchParams();
 
  const selectedType = currentSimulationObject?.objectType ?? "";
  const selectData = family.options.map((option) => ({
    value: option.objectType,
    label: option.label,
  }));
 
  async function handleSelectChange(value: string) {
    Iif (!currentSimulationObject || value === selectedType) return;
 
    try {
      const result = await switchVariant({
        id: currentSimulationObject.id,
        switchVariant: {
          target_object_type: value,
        },
      }).unwrap();
 
      const newSearchParams = new URLSearchParams(searchParams);
      newSearchParams.set("object", String(result.simulation_object));
      newSearchParams.set("content", ContentTypes.objectDetails);
      setSearchParams(newSearchParams);
    } catch (error) {
      console.error("Failed to switch model type:", error);
    }
  }
 
  return (
    <SelectInput
      title="Change Model Type"
      value={selectedType}
      data={selectData}
      handleChange={handleSelectChange}
      className={!selectedType ? "border-rose-700" : " "}
    />
  );
}