All files / src/pages/flowsheet-page/flowsheet/PropertiesSidebar/PropertyPackages CompoundsChooser.tsx

76.56% Statements 49/64
78.12% Branches 25/32
55% Functions 11/20
92.5% Lines 37/40

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                                          8x           8x   8x   8x   8x 8x   5x   2x       5x       2x         1x     5x         7x 5x         7x     5x 5x 1x 1x           6x   5x 2x           5x   7x 1x 1x           6x         1x               38x       4x             4x   4x   4x 1x         1x       12x   8x    
import { Minus, MinusCircle, Plus } from "lucide-react";
import React from "react";
import {
  AccessControlledButton as Button,
  AccessControlledInput as Input,
} from "../../../../../ahuora-design-system/ui/accessControlled";
import {
  Tabs,
  TabsContent,
  TabsList,
  TabsTrigger,
} from "../../../../../ahuora-design-system/ui/tabs";
import {
  useCoreCustomCompoundsCreateMutation,
  useCoreCustomCompoundsListQuery,
  useCoreCustomPropertyPackagesAddCompoundCreateMutation,
  useCoreCustomPropertyPackagesRemoveCompoundCreateMutation,
} from "../../../../../api/apiStore.gen";
import { CustomPropertyPackageProperties } from "./CustomPropertyPackageProperties";
import { useCustomPropertyPackage } from "./useCustomPropertyPackage";
 
export function CompoundsChooser({
  customPackageId,
}: {
  customPackageId: number;
}) {
  const [propertyPackage, optimisticUpdate] =
    useCustomPropertyPackage(customPackageId);
  const [addCompound] =
    useCoreCustomPropertyPackagesAddCompoundCreateMutation();
  const [removeCompound] =
    useCoreCustomPropertyPackagesRemoveCompoundCreateMutation();
 
  const { data: allCompounds } = useCoreCustomCompoundsListQuery({});
  const [searchTerm, setSearchTerm] = React.useState("");
  const compounds =
    allCompounds?.filter(
      // Only include compounds that are in the package
      (compound) => propertyPackage?.compounds.some((c) => c === compound.id),
    ) || [];
 
  const otherCompounds =
    allCompounds
      ?.filter(
        // Filter out compounds already in the package
        (compound) =>
          !propertyPackage?.compounds.some((c) => c === compound.id),
      )
      .filter(
        // Filter by search term
        (compound) =>
          compound.name.toLowerCase().includes(searchTerm.toLowerCase()),
      ) || [];
 
  const add = (compoundId: number) => () => {
    addCompound({
      compoundId,
      propertyPackageId: customPackageId,
    });
  };
  const remove = (compoundId: number) => () => {
    removeCompound({
      compoundId,
      propertyPackageId: customPackageId,
    });
  };
 
  return (
    <div>
      <h1> Property Package Compounds</h1>
      {compounds.map((compound) => (
        <div key={compound.id}>
          {compound.name}
          <Button onClick={remove(compound.id)} variant="ghost">
            <MinusCircle></MinusCircle>
          </Button>
        </div>
      ))}
 
      <h2> Other compounds</h2>
      <Input
        type="text"
        placeholder="Search or create"
        aria-label="Search or create compound"
        className="mb-2"
        value={searchTerm}
        onChange={(e) => setSearchTerm(e.target.value)}
        writeAccessOnly={false}
      />
      {otherCompounds.map((compound) => (
        <div key={compound.id}>
          {compound.name}
          <Button onClick={add(compound.id)} variant="ghost">
            <Plus></Plus>
          </Button>
        </div>
      ))}
      {
        // Show add compound option if there is not an exact match in the other compounds
        searchTerm &&
          !otherCompounds?.some(
            (c) => c.name.toLowerCase() === searchTerm.toLowerCase(),
          ) && (
            <AddCompoundButton
              compoundName={searchTerm}
              customPackageId={customPackageId}
            />
          )
      }
    </div>
  );
}
 
export function AddCompoundButton({
  compoundName,
  customPackageId,
}: {
  compoundName: string;
  customPackageId: number;
}) {
  const [createCompound] = useCoreCustomCompoundsCreateMutation();
  const [addCompoundToPackage] =
    useCoreCustomPropertyPackagesAddCompoundCreateMutation();
 
  const addCompound = async () => {
    const compound = await createCompound({
      customCompound: {
        name: compoundName,
      },
    }).unwrap();
    addCompoundToPackage({
      compoundId: compound.id,
      propertyPackageId: customPackageId,
    });
  };
 
  return <Button onClick={addCompound}>Add Compound "{compoundName}"</Button>;
}