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 | 33x 4x 4x 4x 4x 4x 446x 2x 2x 2224x 4x 1x 1x 1x 4x 1x 1x 1x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 2224x 33x 6x 561x 1x 561x | import { Badge } from "@/ahuora-design-system/ui/badge";
import { Checkbox } from "@/ahuora-design-system/ui/checkbox";
import { DataTable } from "@/ahuora-design-system/ui/data-table";
import InfoTable from "@/ahuora-design-system/ui/info-table";
import { Input } from "@/ahuora-design-system/ui/input";
import { ToolTipCover } from "@/ahuora-design-system/ui/tooltip";
import { useProjectId } from "@/hooks/project";
import { ColumnDef, ColumnFilter } from "@tanstack/react-table";
import { CircleCheck, Search, X } from "lucide-react";
import { useState } from "react";
import { useListCompounds } from "@/hooks/compounds.ts";
const CompoundSelectPage = ({ selectedCompounds, setSelectedCompounds }) => {
const flowsheetId = useProjectId();
// Keeps track of table filter
const [filter, setFilter] = useState<ColumnFilter[]>([]);
// Keeps track of currently "selected" compound (sidebar)
const [currentCompound, setCurrentCompound] = useState<string>("");
const compounds = useListCompounds();
const rowSelection = selectedCompounds.reduce(
(acc: { [key: number]: boolean }, selectedCompound: string) => {
const index = compounds.findIndex(compound => compound.name === selectedCompound);
acc[index] = true;
return acc;
},
{},
);
const items: CompoundTableDataType[] = compounds.map((ob, index) => ({
compoundName: ob.name.charAt(0).toUpperCase() + ob.name.slice(1),
compoundID: index,
compoundFormula: ob.formula,
}));
const updateFilter = (event) => {
const newString = event.target.value;
const columnID = "compoundName";
setFilter([{ id: columnID, value: newString }]);
};
/* select a compound */
const selectCompound = (index: number) => {
const toAdd = compounds[index].name;
setSelectedCompounds((prev) => [...prev, toAdd]);
return toAdd;
};
/* unselect a compound */
const unselectCompound = (index: number) => {
const toRemove = compounds[index].name;
// update state
setSelectedCompounds((prev) =>
prev.filter((compound) => compound !== toRemove),
);
return toRemove;
};
const handleSelect = (updater) => {
/* handle the selection of compounds in the table */
/* select 0 or more compounds */
const selectCompounds = (indexes: string[]) => {
Iif (indexes.length === 0) {
return;
}
for (const index of indexes) {
selectCompound(+index);
}
};
/* unselect 0 or more compounds */
const unselectCompounds = (indexes: string[]) => {
if (indexes.length === 0) {
return;
}
for (const index of indexes) {
unselectCompound(+index);
}
};
const selectedItems = updater(rowSelection);
// Get all items in the NEW state but not in the OLD state, and add them to the cache.
const newStateKeys = Object.keys(selectedItems);
const newlyAdded = newStateKeys.filter((key) => !(key in rowSelection));
selectCompounds(newlyAdded);
// Get all the items in the OLD state, but not in the NEW state, and remove them from the cache.
const prevStateKeys = Object.keys(rowSelection);
const newlyRemoved = prevStateKeys.filter((key) => !(key in selectedItems));
unselectCompounds(newlyRemoved);
};
const handleClick = (index: string) => {
setCurrentCompound(compounds[+index].name);
};
return (
<div className="grid grid-cols-2 gap-4 w-[50vw]">
<div className="flex flex-col gap-1">
<div className="h-[15%]">
<Input
type="text"
startIcon={Search}
onChange={updateFilter}
placeholder="Search Compounds"
/>
</div>
<div className="h-[46vh]">
<DataTable
columns={columns}
data={items}
handleSelect={handleSelect}
handleFilterChange={setFilter}
handleClick={handleClick}
columnFilters={filter}
rowSelection={rowSelection}
isLoading={compounds.length === 0}
/>
</div>
</div>
<div className="h-[50.5vh] items-center flex-wrap md:flex-nowrap gap-4">
{/* <CompoundPropertiesTable compound={currentCompound}/> */}
<InfoTable
title="Selected compounds"
icon={<CircleCheck size={20} />}
content={
<div className="flex flex-row flex-wrap gap-2">
{items.map(
(data, index) =>
rowSelection[index] && (
<Badge
variant="secondary"
onClick={() => unselectCompound(index)}
key={index}
size="sm"
>
{data.compoundName}
<X className="w-4 h-4" />
</Badge>
),
)}
</div>
}
/>
</div>
</div>
);
};
type CompoundTableDataType = {
compoundName: string;
compoundID: number;
compoundFormula: string;
};
const columns: ColumnDef<CompoundTableDataType>[] = [
{
id: "select",
header: ({ table }) => (
<Checkbox
checked={
table.getIsAllPageRowsSelected() ||
(table.getIsSomePageRowsSelected() && "indeterminate")
}
onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
aria-label="Select all"
/>
),
cell: ({ row }) => (
<div className="flex">
<Checkbox
checked={row.getIsSelected()}
onCheckedChange={(value) => row.toggleSelected(!!value)}
aria-label="Select row"
/>
</div>
),
enableSorting: false,
enableHiding: false,
size: 0,
},
{
header: "Name",
accessorKey: "compoundName",
filterFn: "includesString",
},
{
header: "Formula",
accessorKey: "compoundFormula",
cell: ({ row }) => {
return (
<ToolTipCover content={`${row.getValue("compoundFormula")}`} asChild>
<div className="text-right mr-2 truncate">
{row.getValue("compoundFormula")}
</div>
</ToolTipCover>
);
},
},
// Commented out to not display the ID for now.
// {
// accessorKey: "compoundID",
// header: () => <div className="text-right mr-2">ID</div>,
// cell: ({ row }) => {
// return <div className="text-right mr-2">{row.getValue("compoundID")}</div>;
// },
// },
];
export default CompoundSelectPage;
|