All files / src/pages/flowsheet-page/flowsheet/PropertiesSidebar/PropertyPanel PropertySelector.tsx

84.35% Statements 124/147
83.6% Branches 102/122
57.89% Functions 11/19
90.32% Lines 84/93

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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291                                                                                                  71783x                               8517x 8517x 8517x 8517x 8517x   8517x 5909x 62590x 62590x   62590x   17661x   8517x   8515x     68499x     8515x 13557x             17007x     8515x 8492x   25499x 8515x   9650x     10785x 10785x 8515x 1136x           1x 1x 8515x 9651x 9654x 9654x 8515x 13569x                 255478x 25547x 34062x 25547x       9656x     9656x 8517x 11934x 68122x 51092x       394x                               394x   458x 394x     337x     385x       158x 197x 158x 158x 158x   158x 696x               4939x 652x 178x 652x       696x                                     532x 555x         18x 1619x     2683x       137x 31x   141x             234x 137x             689x 415x         7x                     7x 7x 14x 7x   7x         1x           28x 14x      
import { DropdownMenuTrigger } from "@radix-ui/react-dropdown-menu";
import { ChevronUp, Ellipsis, Search } from "lucide-react";
import { useState } from "react";
import { Button } from "@/ahuora-design-system/ui/button";
import {
  DropdownMenu,
  DropdownMenuContent,
} from "@/ahuora-design-system/ui/dropdown-menu";
import { Input } from "@/ahuora-design-system/ui/input";
import { ScrollArea } from "@/ahuora-design-system/ui/scroll-area";
import { Separator } from "@/ahuora-design-system/ui/separator";
import {
  PropertyInfoRead,
  PropertyValueRead,
  SimulationObjectRead,
  useUnitopsSimulationobjectsRetrieveQuery,
} from "@/api/apiStore.gen";
import {
  useCurrentObject,
  useCurrentObjectId,
  useFlowsheetUnitOps,
  useObjectsPortsMap,
} from "@/hooks/flowsheetObjects";
import { isStream } from "@/lib/isStream";
import { cn } from "@/lib/utils";
 
/**
 * This component is designed to allow the user to select one property from a list of all the available properties in the flowsheet.
 * Rather than making them select the unit op, and then choose one of it's properties, this component will display all the properties
 * in one dialog.
 * Filter functionality is supported, e.g to filter to only the properties that are fixed.
 * Example use case: selecting a property to control, which should be able to be selected from any fixed properties.
 */
 
type Variants = "outline" | "ghost" | "secondary" | "link" | "destructive";
 
type PropertySelectorProps = {
  filter: (property: PropertyInfoRead) => boolean;
  filterValue: (property: PropertyValueRead) => boolean;
  title: string;
  header: string;
  description: string;
  onSelect: (property: PropertyValueRead) => void;
  onDialogOpen?: () => void;
  className?: string;
  children: React.ReactNode;
  variant: Variants;
};
 
export function PropertySelector({
  filter,
  filterValue,
  title,
  header,
  description,
  onSelect,
  onDialogOpen,
  children,
  variant,
  open,
  onOpenChange,
}: PropertySelectorProps & {
  open?: boolean;
  onOpenChange?: (open: boolean) => void;
}) {
  const unitOps = useFlowsheetUnitOps();
  const currentObject = useCurrentObject();
  const currentObjectId = useCurrentObjectId();
  const [showAll, setShowAll] = useState(false);
  const objectsPortsMap = useObjectsPortsMap();
 
  const streamPrevObject = () => {
    Iif (!currentObject || !isStream(currentObject)) return undefined;
    const selectedObjectPorts = objectsPortsMap.get(currentObjectId);
    const inletConnection = selectedObjectPorts?.find(
      (port) => port.direction === "outlet",
    );
    return inletConnection?.unitOp;
  };
 
  const [searchTerm, setSearchTerm] = useState("");
 
  const filteredOps = showAll
    ? unitOps
    : unitOps?.filter((unitOp) => {
        const prevObject = streamPrevObject();
        return unitOp.id === currentObjectId || unitOp.id === prevObject;
      });
  const searchedUnitops = filteredOps?.filter((unitOp) => {
    Iif (searchTerm === "") return true;
    return unitOp.componentName
      .toLowerCase()
      .includes(searchTerm.toLowerCase());
  });
 
  // if children exist, use them as trigger, else a hidden dummy button for programmatic open.
  const trigger = children ? children : <div style={{ display: "none" }} />;
 
  return (
    <DropdownMenu open={open} onOpenChange={onOpenChange}>
      <DropdownMenuTrigger asChild onClick={onDialogOpen}>
        {trigger}
      </DropdownMenuTrigger>
      <DropdownMenuContent side="left" sideOffset={25} className="w-[25em] p-4">
        <div className="flex flex-col gap-2">
          <h3 className="">{title}</h3>
          <p className="text-xs text-faint">
            {header} {description}
          </p>
        </div>
        <div>
          <Input
            type="text"
            placeholder="Search unit operation name"
            startIcon={Search}
            value={searchTerm}
            onChange={(e) => {
              setShowAll(true);
              setSearchTerm(e.target.value);
            }}
          />
          <ScrollArea className={cn(showAll ? "h-[20em]" : "h-fit", "mt-4")}>
            <div className={cn(showAll ? "max-h-screen" : "h-fit")}>
              {searchedUnitops?.map((unitOp) => (
                <AvaliableUnitopProperties
                  filterValue={filterValue}
                  key={unitOp.id}
                  unitOp={unitOp}
                  onSelect={onSelect}
                  filter={filter}
                  title={title}
                  variant={variant}
                />
              ))}
            </div>
          </ScrollArea>
        </div>
        <Button
          variant="ghost"
          size="sm"
          onClick={() => setShowAll(!showAll)}
          className="p-0"
        >
          {showAll ? <ChevronUp /> : <Ellipsis />}{" "}
          {showAll ? "Show only current object" : "Show more"}
        </Button>
      </DropdownMenuContent>
    </DropdownMenu>
  );
}
 
export function AvaliableUnitopProperties({
  unitOp,
  onSelect,
  filter,
  title,
  variant,
  filterValue,
}: {
  onSelect: (property: PropertyValueRead) => void;
  unitOp: SimulationObjectRead;
  filter: (property: PropertyInfoRead) => boolean;
  filterValue: (property: PropertyValueRead) => boolean;
  title: string;
  variant: Variants;
}) {
  const { data: unitOpWithProperties } =
    useUnitopsSimulationobjectsRetrieveQuery({
      id: unitOp.id,
    });
  const propertySet = unitOpWithProperties?.properties;
 
  const availableProperties =
    propertySet?.ContainedProperties.filter(filter) || [];
 
  if (availableProperties.length == 0) {
    return <></>;
  }
 
  return (
    <div className="flex flex-col gap-1">
      <p className="font-normal text-xs">{unitOp.componentName}</p>
      <div
        className="flex flex-1 flex-wrap gap-2"
        aria-label={`property-selector-${title}`}
      >
        {availableProperties.map((propertyInfo) => (
          <AvaliableProperty
            filterValue={filterValue}
            property={propertyInfo}
            key={propertyInfo.id}
            onSelect={onSelect}
            variant={variant}
            unitOpName={unitOp.componentName}
          />
        ))}
      </div>
      <Separator className="my-2" />
    </div>
  );
}
 
export function AvaliableProperty({
  property,
  onSelect,
  variant,
  unitOpName,
  filterValue,
}: {
  property: PropertyInfoRead;
  onSelect: (property: PropertyValueRead) => void;
  variant: Variants;
  unitOpName: string;
  filterValue?: (property: PropertyValueRead) => boolean;
}) {
  // If not an indexed property, just return the button
  if (property.values.length == 0) {
    return <></>;
  }
  if (property.values.length == 1) {
    return (
      <Button
        aria-label={`Control ${property.displayName} ${unitOpName}`}
        variant={variant}
        size="sm"
        className="w-fit font-normal"
        onClick={() => {
          onSelect(property.values[0]);
        }}
      >
        {property.displayName}
      </Button>
    );
  } else {
    return (
      <DropdownMenu>
        <DropdownMenuTrigger asChild>
          <Button
            aria-label={`Control ${property.displayName} ${unitOpName}`}
            variant={variant}
            size="sm"
            className="w-fit font-normal"
          >
            {property.displayName}
          </Button>
        </DropdownMenuTrigger>
        <DropdownMenuContent className="w-[15em] p-4">
          <IndexedVarSelection
            propertyValues={property.values}
            onSelect={onSelect}
            title={property.displayName}
            filterValue={filterValue}
          />
        </DropdownMenuContent>
      </DropdownMenu>
    );
  }
}
 
export function IndexedVarSelection({
  onSelect,
  propertyValues,
  filterValue,
}: {
  propertyValues: PropertyValueRead[];
  onSelect: (property: PropertyValueRead) => void;
  filterValue: (property: PropertyValueRead) => boolean;
}) {
  // Recursive/nested property, display the children
  return (
    <div className="flex flex-col gap-1">
      {propertyValues
        .filter((value) => filterValue(value))
        .map((value) => {
          return (
            <Button
              variant="ghost"
              size="sm"
              className="w-fit font-normal"
              onClick={() => {
                onSelect(value);
              }}
            >
              {value.indexedSetNames.join(" ")}
            </Button>
          );
        })}
    </div>
  );
}