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

100% Statements 12/12
100% Branches 9/9
100% Functions 5/5
100% Lines 12/12

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                                    8775x 8775x     8775x 8775x 8775x 867x   8775x       9109x         9109x   9109x                         195x               2x                    
import { PropertyInfoRead } from "../../../../../../api/apiStore.gen";
import { VariableWithUnit } from "./VariableWithUnit";
import { buildRuleValidationKey } from "../../../Diagnostics/ruleValidationKey";
import type { RuleFinding } from "../../../Diagnostics/useRuleValidation";
 
export interface IndexedVariableWithUnitProps {
  property: PropertyInfoRead;
  objectId?: number;
  onUpdateUnit: (unit: string) => void;
  withMSSConnection?: boolean; // should be applied if multi steady state applies
  onUpdateValue: (value: string, id: number) => void;
  deletePropertyFunction?: (property_val_key: string) => void;
  ruleFindingsByKey?: Record<string, RuleFinding[]>;
}
 
 
 
export function IndexedVariableWithUnit(props: IndexedVariableWithUnitProps) {
  const values = props.property.values; // if values is passed, just process the subset of all values
  if (values.id === undefined) {
    // console.error("No id found for property", props.property,"with values", values , "and indexes", props.indexes,"which means there probably is not the same depth of indexes as values, or some other logic issue.")
  }
  const individualNames = props.property.values?.[0]?.indexedSets.length != 0
  const name = individualNames ? props.property.displayName : null
  const sortedValues = [...values].sort(
    (v1, v2) => v1?.indexedSetNames?.join("").localeCompare(v2?.indexedSetNames?.join(""))
  )
  return (
    <div>
      {name}
      {sortedValues.map((value) => {
        const validationKey = buildRuleValidationKey({
          objectId: props.objectId,
          propertyKey: props.property.key,
          propertyValueId: value.id,
        });
        const ruleFindings = props.ruleFindingsByKey?.[validationKey] ?? [];
 
        return (
          <VariableWithUnit
            key={value.id}
            property={props.property}
            displayName={
              individualNames
                ? value.indexedSetNames.join(" ")
                : props.property.displayName + " " + value.indexedSetNames.join(" ")
            }
            objectId={props.objectId}
            value={value}
            ruleFindings={ruleFindings}
            onUpdateValue={(val) => {
              props.onUpdateValue(val, value.id);
            }}
            onUpdateUnit={props.onUpdateUnit}
            withMSSConnection={props.withMSSConnection}
            // Prevent the delete button from showing up if the deletePropertyFunction is not passed
            deletePropertyFunction={
              props.deletePropertyFunction
                ? () => {
                    props.deletePropertyFunction?.(value.indexedSets.join(" "));
                  }
                : undefined
            }
          />
        );
      })}
    </div>
  )
}