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

73.07% Statements 57/78
56.56% Branches 56/99
38.46% Functions 5/13
89.13% Lines 41/46

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 192 193 194 195 196 197 198 199 200 201 202                                                    618x                     154x 154x         154x 20x 184x     154x             184x 30x                       214x   154x   308x 154x             770x 100x                             854x 770x             616x           182x                                             76x 16x   60x               60x 40x   578x       81x       142x   41x   81x 60x           202x     100x 45x     60x       20x 67x           20x                     224x 195x 60x 240x      
import { CircleX } 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 { useAccessControlledState } from "../ui/accessControlled";
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 { lacksWriteAccess } = useAccessControlledState({});
  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">
          <button
            type="button"
            aria-label="Remove property selection"
            disabled={lacksWriteAccess}
            onClick={onDelete}
            className="disabled:cursor-not-allowed disabled:opacity-50"
          >
            <CircleX className="cursor-pointer" size={17} />
          </button>
        </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"
          disabled={lacksWriteAccess}
        />
      </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);
  Iif (!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,
            );
            if (!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>
  );
}