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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | 33x 6161x 6161x 6161x 6161x 6161x 6161x 6161x 6161x 6161x 151x 151x 151x 6161x 10x 10x 26x 10x 10x 10x 10x 6161x 161x 161x 161x 156x 1x 155x 155x 156x 2x 2x 6161x 6067x 76x 18x 1002x 1002x 1002x 5943x 1002x 11886x 1002x 5943x 1002x 5943x 1002x 224x 5719x 1190x 1190x 835x 355x 355x 238x 238x 218x 355x 757x 757x 757x 757x 1190x 1190x 1190x 1190x 1190x 1190x 1190x 487x 482x 5077x 5x 6582x 1190x 220x 101x 101x 80x 80x 80x 101x 1190x 1190x 6582x 1190x 355x 1190x 1190x 6253x 1190x 1190x 6582x 1190x | import {
api,
PropertyInfoRead,
useCorePropertyinfoPartialUpdateMutation,
PropertySetRead,
useCorePropertyvaluePartialUpdateMutation,
PropertyValueRead,
} from "@/api/apiStore.gen";
import { useAppDispatch } from "@/store/hooks";
import { toast } from "sonner";
import { useCurrentObject } from "@/hooks/flowsheetObjects";
import {
PropertyTextField
} from "./InputFields/PropertyTextField";
import {
AccordionContent,
AccordionItem,
AccordionTrigger,
} from "@/ahuora-design-system/ui/accordion";
import { PropertiesStatus } from "./PropertiesStatus";
import { ObjectType, PropertySetType } from "@/data/ObjectTypes.gen";
import objs from "@/data/objects.json";
import { CompoundListSection } from "./CompoundList";
import { Separator } from "@/ahuora-design-system/ui/separator";
import React, { useMemo } from "react";
import { KeySquare } from "lucide-react";
import { infoMessages } from "../../InfoData";
import { isStream } from "@/lib/isStream";
import { IndexedVariableWithUnit } from "./InputFields/IndexedVariableWithUnit";
import { PropertyCheckbox } from "./InputFields/PropertyCheckbox";
import { PropertyDropdown } from "./InputFields/PropertyDropdown";
import { IndexedNumericArgWithUnit } from "./InputFields/IndexedNumericArgWithUnit";
const objects = objs as Record<string, ObjectType>;
interface PropertyProps {
property: PropertyValueRead;
isOperatingVariable?: boolean;
withMSSConnection?: boolean;
deletePropertyFunction?: (property_val_key: string) => void;
}
type MutationResult = ReturnType<
typeof useCorePropertyinfoPartialUpdateMutation
>[0];
export function Property({
property,
withMSSConnection,
deletePropertyFunction,
}: PropertyProps) {
const [updateValue] = useCorePropertyvaluePartialUpdateMutation();
const [updateProperty] = useCorePropertyinfoPartialUpdateMutation();
const currentObject = useCurrentObject();
const printToast = true; // TODO: Make this a prop
const dispatch = useAppDispatch();
const currentObjectType = currentObject?.objectType!;
const objectSchema = objects[currentObjectType];
const propertySchema = objectSchema?.properties[property.key];
const onUpdateIndexedValue = (value: string, id: number) => {
// this currently does this for ALL property info's values. We need to only have this != 0 check for compounds.
Iif (value === "0" && property.key === "mole_frac_comp") {
updateValue({
id: id,
patchedPropertyValue: {
value: "",
},
});
toast.error("Cannot set value to 0", {
description:
"This will be available in future updates. Please set a non-zero value for now :)",
});
} else {
updateBackendWithToast(() => {
return updateValue({
id: id,
patchedPropertyValue: {
value: value,
},
});
}, value);
}
};
const onUpdateUnit = (unit: string) => {
// Optimistically update the property
dispatch(
api.util.updateQueryData(
"unitopsSimulationobjectsRetrieve",
{ id: currentObject!.id },
(cachedSimulationObject) => {
const cachedProperty =
cachedSimulationObject.properties.ContainedProperties.find(
(prop) => prop.id === property.id,
);
if (cachedProperty) {
cachedProperty.unit = unit;
}
},
),
);
updateBackendWithToast(() => {
return updateProperty({
id: property.id,
patchedPropertyInfo: {
unit: unit,
},
});
}, unit);
};
const updateBackendWithToast = (
fn: () => ReturnType<MutationResult>,
updatedValue: string | number | boolean,
) => {
// Check for efficiency values > 1 in dimensionless units
const isEfficiencyProperty =
property.key === "efficiency" ||
property.key === "isentropicEfficiency" ||
property.key === "adiabaticEfficiency";
const isHighEfficiency =
isEfficiencyProperty &&
property.unit === "dimensionless" &&
(typeof updatedValue === "number" || typeof updatedValue === "string") &&
parseFloat(String(updatedValue)) > 1;
// Update the backend
fn()
.unwrap()
.then((data) => {
// If efficiency is high, show a warning toast after a slight delay
if (isHighEfficiency) {
toast.warning("High efficiency value detected", {
description:
"You're using decimal format, which means your efficiency is over 100%.",
duration: 7000, // Keep it visible longer
});
} else if (printToast) {
toast.success(
`Successfully updated ${property.displayName} to ${updatedValue}`,
);
}
return;
})
.catch((error) => {
// TODO: Add error handling in future to correctly display this error
console.error(
`Failed to update ${property.displayName} to ${updatedValue}: ${error.data.error}`,
);
return;
});
};
switch (property.type) {
case "numeric": {
return (
<IndexedVariableWithUnit
property={property}
indexes={property.indexed_sets}
onUpdateValue={onUpdateIndexedValue}
onUpdateUnit={onUpdateUnit}
withMSSConnection={withMSSConnection}
deletePropertyFunction={deletePropertyFunction}
/>
);
}
case "checkbox": {
return (
<PropertyCheckbox
property={property}
onUpdateValue={onUpdateIndexedValue}
/>
);
}
case "text": {
return (
<PropertyTextField
property={property}
onUpdateValue={onUpdateIndexedValue}
/>
);
}
case "dropdown": {
return (
<PropertyDropdown
property={property}
onUpdateValue={onUpdateIndexedValue}
schema={propertySchema}
></PropertyDropdown>
)
}
case "numeric_arg": {
// Same rendering as numeric, but without DoF controls. Our NumericArg components do not show formulas/replacements.
return (
<IndexedNumericArgWithUnit
property={property}
indexes={property.indexed_sets}
onUpdateValue={onUpdateIndexedValue}
onUpdateUnit={onUpdateUnit}
/>
);
}
default: {
return <>Default Type</>;
}
}
}
export interface OrderedPropertyListProps {
properties: PropertyInfoRead[];
schema: PropertySetType;
deletePropertyFunction?: (property_val_key: string) => void;
}
export function OrderedPropertyList(props: OrderedPropertyListProps) {
// With no calculation modes, we don't really want to order the property list, other than splitting out operating variables and reactive properties.
const isComposition = props.schema.type === "composition" ? true : false;
const withMSSConnection = props.schema.type === "composition" ? false : true;
const mapFn = (property, index) => {
return (
<Property
key={index}
property={property}
withMSSConnection={withMSSConnection}
deletePropertyFunction={props.deletePropertyFunction}
/>
);
};
const isOperatingVariable = (prop: PropertyValueRead) => {
return (
prop.controlManipulated !== null ||
props.schema.stateVars?.includes(prop.key)
// && prop.controlSetPoint === null
);
};
const operatingVariables = props.properties.filter((prop) =>
isOperatingVariable(prop),
);
const reactiveProperties = props.properties.filter(
(prop) => !isOperatingVariable(prop),
);
return (
<div className=" mx-4">
{isComposition ? (
<div className="mt-2">
{operatingVariables.map((property, index) => mapFn(property, index))}
</div>
) : (
<div className="flex flex-col gap-4">
<div className="flex flex-col gap-2">
<div className="gap-2">
{operatingVariables.map((property, index) =>
mapFn(property, index),
)}
</div>
</div>
<div className="flex flex-col gap-1">
<div>
{reactiveProperties.map((property, index) =>
mapFn(property, index),
)}
</div>
</div>
</div>
)}
</div>
);
}
export type PropertySetComponentProps = {
schema: PropertySetType;
properties: PropertyInfoRead[];
propertySet: PropertySetRead;
};
function manageCompoundDisplay(
isStream: boolean,
compositionData: PropertyInfoRead | undefined,
) {
const compounds: PropertyValueRead | undefined = compositionData?.values;
if (!compounds || !isStream) {
return infoMessages.error;
}
let total: number = 0;
Object?.keys(compounds).forEach((key) => {
const compValue = compounds[key]?.value;
if (compValue || compValue != undefined) {
total += +compValue;
}
});
return total === 1 ? infoMessages.success : infoMessages.error;
}
export function Properties() {
const currentObj = useCurrentObject();
const objectSchema = objects[currentObj!.objectType!];
const propertySetGroups = objectSchema.propertySetGroups!;
return Object.keys(propertySetGroups).map((groupKey) => (
<PropertySetGroup key={groupKey} groupKey={groupKey} />
));
}
function PropertySetGroup({ groupKey }) {
const currentObj = useCurrentObject();
const propertySet = currentObj!.properties;
const objectSchema = objects[currentObj!.objectType!];
const propertySetGroups = objectSchema.propertySetGroups!;
const schema = propertySetGroups[groupKey];
const propertyInfos = useMemo(() => {
if (Object.keys(objectSchema?.properties).length !== 0) {
return propertySet.ContainedProperties.filter(
(prop) =>{
return objectSchema?.properties[prop.key]?.propertySetGroup == groupKey
},
);
}
// If there are no properties defined in the group schema, it must be something
// like machineLearning block where there are custom properties
// TODO: Improve how machineLearningBlock is handled (so that we can still have custom properties as well as the ML properties)
return propertySet.ContainedProperties;
}, [groupKey, objectSchema?.properties, propertySet.ContainedProperties]);
const moleFracComp = propertyInfos.find((p) => p.key === "mole_frac_comp");
// Convert compound values to a simpler format for validation
//uses useMemo to avoid recalculating when inputs haven't changed
// only processes data when viewing composition tab
const compoundValues = useMemo(() => {
if (groupKey !== "composition" || !moleFracComp?.values) return undefined;
// convert to the format expected by PropertiesStatus
const result: { [key: string]: { value: string | number } } = {};
Object.entries(moleFracComp.values).forEach(([key, valueObj]) => {
// skip the id property and any non-object values
Iif (key === "id" || !valueObj || typeof valueObj !== "object") return;
// only include if it has a value property
if ("value" in valueObj) {
result[key] = { value: valueObj.value };
}
});
return result;
}, [moleFracComp?.values, groupKey]);
// Showing property set status
let statusInfo = propertySet.unspecifiedProperties;
const compInfo = manageCompoundDisplay(
isStream(currentObj),
propertyInfos.find((property) => property.key === "mole_frac_comp"),
);
if (groupKey === "composition" && isStream(currentObj)) {
statusInfo = compInfo === infoMessages.success ? [] : ["mole_frac_comp"];
}
Iif (compInfo === infoMessages.success && groupKey !== "composition") {
// Remove "Compounds" if compInfo is success
statusInfo = propertySet.unspecifiedProperties?.filter(
(prop) => prop !== "Compounds" && prop !== "mole_frac_comp",
);
}
// Enable/Disable additional properties
const toggleProperty = propertyInfos.find(
(prop) => prop.key === schema.toggle,
);
const toggleEnabled =
!toggleProperty || toggleProperty?.values?.value == true;
const otherProperties = propertyInfos.filter(
(prop) => prop.key !== schema.toggle,
);
return (
<AccordionItem value={groupKey}>
<AccordionTrigger
variant="default"
statusIcon={
<PropertiesStatus
propertySet={statusInfo}
propertySetGroup={groupKey}
compoundValues={compoundValues}
/>
}
>
{schema.displayName}
</AccordionTrigger>
<AccordionContent className="p-0">
{toggleProperty && (
<div className="p-2">
<Property property={toggleProperty} />
</div>
)}
{toggleEnabled &&
(schema.type === "composition" ? (
<CompoundListSection
schema={schema}
properties={otherProperties}
propertySet={propertySet}
/>
) : currentObj!.objectType! === "specificationBlock" ? (
<SpecificationBlockProperties
schema={schema}
properties={otherProperties}
propertySet={propertySet}
/>
) : (
<OrderedPropertyList properties={otherProperties} schema={schema} />
))}
</AccordionContent>
</AccordionItem>
);
}
|