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

86.66% Statements 26/30
75.67% Branches 28/37
92.85% Functions 13/14
84.61% Lines 22/26

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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191                                                                        742x         179x 11x       179x           179x                                                                                 1188x                                                         109x 109x   94x               94x     910x       194x 474x         94x   208x 488x             94x                 11x 11x 38x   11x       11x                                  
import { CircleX, X } from "lucide-react";
import SelectInput from "@/ahuora-design-system/inputs/SelectInput";
import { useFlowsheetUnitOps } from "@/hooks/flowsheetObjects";
 
import { useSimulationObjectPropertySet } from "@/hooks/properties";
import { cn } from "@/lib/utils";
import { PropertyInfoRead, PropertyValueRead } from "../../api/apiStore.gen";
import { SearchSelectInput } from "../ui/search-select-input";
 
type SelectorFilter = (value: {
  propertyInfo: PropertyInfoRead;
  propertyValue: PropertyValueRead;
}) => boolean;
 
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;
  filter?: SelectorFilter;
}
export function ObjectPropertySelector({
  onSimulationObjectChange,
  onPropertyChange,
  layout,
  propertyId,
  simulationObjectId,
  children,
  onDelete,
  useValueId,
  filter,
}: 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}
            filter={filter}
          >
            {children}
          </PropertySelector>
        ) : (
          <div className="flex-1"></div>
        )}
      </div>
    </div>
  );
}
 
function getValuesList(
  property: PropertyInfoRead,
): { propertyInfo: PropertyInfoRead; propertyValue: PropertyValueRead }[] {
  return property.values.map((val) => ({
    propertyInfo: property,
    propertyValue: val,
  }));
}
 
function PropertySelector({
  objectID,
  children,
  onPropertyChange,
  propertyId,
  layout,
  useValueId,
  filter,
}: {
  objectID: number;
  children: React.ReactNode;
  propertyId: number | undefined;
  onPropertyChange: (
    propertyId: number,
    propertyInfo: PropertyInfoRead,
    property?: PropertyValueRead,
  ) => void;
  layout?: "horizontal" | "vertical" | "flat";
  useValueId?: boolean;
  // Optional filter function to filter based on both propertyInfo and propertyValue,
  // E.g if you only want properties that are controlled or enabled.
  filter?: SelectorFilter;
}) {
  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";
 
  // If there is a filter function, only use the property values that pass the filter.
  // otherwise, use all property values.
  const propertyInfoData = properties.ContainedProperties.filter(
    filter
      ? (propertyInfo) =>
          filter({
            propertyInfo: propertyInfo,
            propertyValue: propertyInfo.values[0],
          })
      : (_) => true,
  ).map((prop) => ({
    label: prop.displayName || "",
    value: prop.id,
  }));
 
  const propertyValueData = properties.ContainedProperties.flatMap(
    getValuesList,
  ).filter(filter ? filter : (_) => true);
  const propertyValueDisplayData = propertyValueData.map((val) => ({
    label:
      val.propertyInfo.displayName +
        " " +
        val.propertyValue.indexedSetNames.join(" ") || "",
    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 E{
            onPropertyChange(
              val,
              properties.ContainedProperties.find((p) => p.id === +val)!,
            );
          }
        }}
      />
      {propertyId !== undefined && propertyId !== null && children}
    </div>
  );
}