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 | 430x 430x 430x 430x 430x 430x 430x 430x 430x 430x 430x 430x 101x 430x 430x 430x 430x 430x 430x 430x 483x 430x 182x 153x 153x 153x 146x 430x 430x 430x 430x 302x 302x 430x 430x 430x 430x 430x 430x 430x 5x 2x 430x 430x | // CompoundListSection.tsx
import { AccordionContent } from "@/ahuora-design-system/ui/accordion";
import {
PropertySetRead,
useCoreSchemapropertysetUpdateCompoundModeUpdateMutation,
useUnitopsSimulationobjectsUpdateCompoundsCreateMutation,
PropertyInfoRead,
useCoreSchemapropertysetNormalizeCompoundValuesCreateMutation,
} from "@/api/apiStore.gen";
import { useSearchParam } from "@/hooks/searchParams";
import {
useFlowsheetObjectsIdMap,
useObjectsPortsMap,
} from "@/hooks/flowsheetObjects";
import { Button } from "@/ahuora-design-system/ui/button";
import { Badge } from "@/ahuora-design-system/ui/badge";
import { ToolTipCover } from "@/ahuora-design-system/ui/tooltip";
import { QuickAddCompoundsModal } from "./QuickAddCompoundsModal";
import { OrderedPropertyList } from "./Properties";
import { useObjectConnectedToRecycle } from "@/hooks/recycleData";
import { toast } from "sonner";
import * as React from "react";
import { ObjectTypeEnum as ObjEnum } from "@/api/apiStore.gen";
export function CompoundListSection(props: {
schema: object;
properties: PropertyInfoRead[];
propertySet: PropertySetRead;
}) {
const [id] = useSearchParam("object");
const simulationObjectPortMap = useObjectsPortsMap();
const objectIdMap = useFlowsheetObjectsIdMap();
const objectConnectedToRecycle = useObjectConnectedToRecycle();
const [updateMode] =
useCoreSchemapropertysetUpdateCompoundModeUpdateMutation();
const [updateCompounds] =
useUnitopsSimulationobjectsUpdateCompoundsCreateMutation();
const [normaliseCompounds] =
useCoreSchemapropertysetNormalizeCompoundValuesCreateMutation();
const { propertySet } = props;
const moleFracComp = props.properties.find(
(prop) => prop.key === "mole_frac_comp",
);
Iif (!moleFracComp) return null;
// Derive the current selection straight from props/server (single source of truth)
const selectedCompounds = React.useMemo<string[]>(
() => Object.keys((moleFracComp).values ?? {}),
[moleFracComp?.values],
);
const { basis, flowType } = (() => {
switch (propertySet.compoundMode) {
case "MolarFraction":
return { basis: "Molar", flowType: "Fraction" as const };
case "MassFraction":
default:
return { basis: "Mass", flowType: "Fraction" as const };
}
})();
const ports = simulationObjectPortMap.get(+id!);
const showAddCompoundButton = (() => {
// decision node
const unitopd = objectIdMap.get(+id!);
Iif (unitopd?.objectType === "decisionNode") return true;
// inlet stream (no outlet)
const outletPort = ports?.find((port) => port.direction === "outlet");
if (!outletPort) return true;
// connected to recycle
if (objectConnectedToRecycle(+id!)) return true;
// connected to translator block
if (outletPort?.unitOp) {
const upstreamOp = objectIdMap.get(outletPort.unitOp);
if (upstreamOp?.objectType === ObjEnum.Translator) return true;
}
return false;
})();
const calculateCompoundTotal = () => {
let compoundTotal = 0;
const values = (moleFracComp as any).values ?? {};
for (const key in values) {
// support both { value } shape and flat number
const v = values[key]?.value ?? values[key];
compoundTotal += +v;
}
return parseFloat(compoundTotal.toFixed(3));
};
const compoundTotal = calculateCompoundTotal();
const compoundTotalError = compoundTotal !== 1;
const noCompounds = selectedCompounds.length === 0;
const switchCompoundMode = (
newBasis: "Molar" | "Mass",
fType: "Fraction" | "Flow",
) => {
const newMode = newBasis + fType;
updateMode({
id: propertySet.id,
updateCompoundMode: { compoundMode: newMode },
});
};
const onClickBasis = () => {
switchCompoundMode(basis === "Molar" ? "Mass" : "Molar", flowType);
};
const deleteCompound = (propertyValueKey: string) => {
const updated = selectedCompounds.filter((c) => c !== propertyValueKey);
updateCompounds({
updateCompound: {
simulationObject: +id!,
compounds: updated,
},
});
// no local state needed; UI will reflect server once mutation settles
};
const normaliseCompoundValues = () => {
normaliseCompounds({ id: propertySet.id })
.unwrap()
.then(() => {
toast.success("Compound values normalised successfully!");
})
.catch((error) => {
toast.error("Failed to normalise compound values:", {
description: error?.data?.message,
});
});
};
return (
<AccordionContent key={id} className="px-0 mt-0 flex flex-col">
{showAddCompoundButton && (
<div className="flex flex-row-reverse gap-2 items-center mx-4">
<QuickAddCompoundsModal
key={`modal-${id}`} // ✅ don't re-mount on server value changes
schema={props.schema}
moleFracComp={moleFracComp as any}
// pass current server truth (the modal may keep a temp local copy while open)
selectedCompounds={selectedCompounds}
/>
{noCompounds ? (
<p className="w-full">Add a compound to get started.</p>
) : (
<div className="flex w-full gap-1 justify-between">
<ToolTipCover
content="Click to toggle between mass and molar basis."
delay={0}
asChild
>
<Button
aria-label="Compound Basis"
variant="secondary"
onClick={onClickBasis}
>
{basis} {flowType}
</Button>
</ToolTipCover>
<ToolTipCover
content={
compoundTotalError
? "Total compound fractions must equal to 1!"
: `Total compound ${
flowType === "Flow" ? "flow" : "fraction"
}.\nClick to normalise values`
}
variant={compoundTotalError ? "error" : "default"}
delay={0}
asChild
>
<Badge
className="pointer-events-auto cursor-pointer"
variant={compoundTotalError ? "destructive" : "secondary"}
size="xs"
tabIndex={0}
onClick={normaliseCompoundValues}
>
Total: {compoundTotal}{" "}
</Badge>
</ToolTipCover>
</div>
)}
</div>
)}
{!noCompounds && (
<OrderedPropertyList
schema={props.schema}
properties={props.properties}
deletePropertyFunction={
showAddCompoundButton ? deleteCompound : undefined
}
/>
)}
</AccordionContent>
);
} |