All files / src/ahuora-design-system/inputs PropertySelection.tsx

92% Statements 23/25
78.78% Branches 26/33
100% Functions 9/9
90% Lines 18/20

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                                                                  354x         81x 5x       81x           81x                                                                                                               68x 68x   64x           706x         64x 756x       64x                 5x 4x 1x       1x           4x   18x                  
import SelectInput from "@/ahuora-design-system/inputs/SelectInput";
import { useFlowsheetUnitOps } from "@/hooks/flowsheetObjects";
import {
  PropertyInfoRead,
  PropertyValueRead,
} from "../../api/apiStore.gen";
 
import { useSimulationObjectPropertySet } from "@/hooks/properties";
import { CircleX, X } from "lucide-react";
import { cn } from "@/lib/utils";
import { SearchSelectInput } from "../ui/search-select-input";
interface ObjectPropertySelectorProps {
  simulationObjectId?: number;
  propertyId: number | undefined;
  onSimulationObjectChange: (simulationObjectId?: number) => void;
  onPropertyChange: (propertyId: number, property?: PropertyValueRead) => void;
  layout?: "horizontal" | "vertical" | "flat";
  children?: React.ReactNode;
  onDelete?: () => void;
  useValueId?: boolean;
}
import { getValuesList } from "../../pages/flowsheet-page/flowsheet/LeftSideBar/Formulas/getValuesList";
 
export function ObjectPropertySelector({
  onSimulationObjectChange,
  onPropertyChange,
  layout,
  propertyId,
  simulationObjectId,
  children,
  onDelete,
  useValueId
}: ObjectPropertySelectorProps) {
  const allOps = useFlowsheetUnitOps()?.map((uo) => ({
    label: uo.componentName || "",
    value: uo.id + "",
  }));
 
  const handleSimulationObjectChange = (val: string) => {
    onSimulationObjectChange(+val);
  };
 
  const layoutStyle =
    layout === "horizontal"
      ? "grid grid-cols-2 gap-2"
      : layout === "flat"
        ? "flex flex-col sm:flex-row items-start sm:items-center gap-4 truncate"
        : "grid grid-cols-1 gap-2";
 
  return (
    <div className={cn(layoutStyle, "w-full relative")}>
      {onDelete && (
        <div className="flex flex-row justify-between">
          <CircleX
            className="cursor-pointer"
            size={17}
            onClick={onDelete}
          />
        </div>
      )}
      {/* Operation selector */}
      <div className="flex-1">
        <SearchSelectInput
          options={allOps ?? []}
          value={String(simulationObjectId ?? "")}
          onValueChange={handleSimulationObjectChange}
          placeholder="Select an Operation"
          searchPlaceholder="Search Operations..."
          ariaLabel="search-select-unitops"
        />
      </div>
      <div className="flex-1">
        {simulationObjectId ? (
          <PropertySelector
            objectID={simulationObjectId}
            propertyId={propertyId}
            onPropertyChange={onPropertyChange}
            layout={layout}
            useValueId={useValueId}
          >
            {children}
          </PropertySelector>
        ) : (
          <div className="flex-1"></div>
        )}
      </div>
    </div>
  );
}
 
function PropertySelector({
  objectID,
  children,
  onPropertyChange,
  propertyId,
  layout,
  useValueId,
}: {
  objectID: number;
  children: React.ReactNode;
  propertyId: number | undefined;
  onPropertyChange: (propertyId: number,propertyInfo:  PropertyInfoRead, property?: PropertyValueRead) => void;
  layout?: "horizontal" | "vertical" | "flat";
  useValueId?: boolean;
}) {
  const properties = useSimulationObjectPropertySet(objectID);
  if (!properties) return null;
  const layoutStyle =
    layout === "horizontal"
      ? "grid grid-cols-2 gap-2"
      : layout === "flat"
        ? "flex-1 flex flex-col sm:flex-row items-start sm:items-center gap-4 truncate w-full"
        : "grid grid-cols-1 gap-2";
 
  const propertyInfoData = properties.ContainedProperties.map((prop) => ({
    label: prop.displayName || "",
    value: prop.id,
  }))
 
  const propertyValueData = properties.ContainedProperties.flatMap(getValuesList)
  const propertyValueDisplayData = propertyValueData.map((val) => ({
    label: val.displayPath || "",
    value: val.propertyValue.id ,
  }));
  return (
    <div className={layoutStyle}>
      {layout !== "flat" ? <p>Property</p> : ""}
      <SelectInput
        ariaLabel="property-mss"
        title="Select a property"
        data={useValueId ? propertyValueDisplayData : propertyInfoData}
        value={propertyId}
        handleChange={(val) => {
          if (useValueId){
            const valueInfo = propertyValueData.find((p) => p.propertyValue.id === +val)
            Iif (!valueInfo) {
              console.error("Could not find property value with id", val);
              return;
            }
            onPropertyChange(
              +val,
              valueInfo!.propertyInfo!,
              valueInfo!.propertyValue!
            )
          } else{
            onPropertyChange(
              val,
              properties.ContainedProperties.find((p) => p.id === +val)!,
            );
          }
        }}
      />
      {propertyId !== undefined && propertyId !== null && children}
    </div>
  );
}