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

100% Statements 16/16
90.9% Branches 10/11
100% Functions 6/6
100% Lines 16/16

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                                                  7503x 7503x 7503x 6568x     6568x               147x               2x 2x                   935x 219x   935x   935x               519x   1436x 1436x 1436x                                    
import { VariableWithUnit, VariableWithUnitProps } from "./VariableWithUnit";
 
 
 
interface PropertyIndex {
  [key: string]: {
    displayName?: string;
    value?: string | number;
  };
}
 
 
export interface IndexedVariableWithUnitProps
  extends Omit<
    VariableWithUnitProps,
    "onUpdateValue" | "deletePropertyFunction"
  > {
  indexes: Record<string, PropertyIndex>;
  onUpdateValue: (value: string, id: number) => void;
  deletePropertyFunction?: (property_val_key: string) => void;
}
 
 
 
export function IndexedVariableWithUnit(props: IndexedVariableWithUnitProps) {
  const indexesArray = Object.keys(props.indexes);
  const values = props.values ?? props.property.values; // if values is passed, just process the subset of all values
  if (indexesArray.length === 0) {
    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.")
    }
    return (
      <VariableWithUnit
        key={values.id}
        property={props.property}
        displayName={props.displayName}
        property_val_key={props.property_val_key}
        values={values}
        onUpdateValue={(val) => {
          props.onUpdateValue(val, values.id);
        }}
        onUpdateUnit={props.onUpdateUnit}
        withMSSConnection={props.withMSSConnection}
        // Prevent the delete button from showing up if the deletePropertyFunction is not passed
        deletePropertyFunction={
          props.deletePropertyFunction
            ? () => {
                if (props.deletePropertyFunction) {
                  props.deletePropertyFunction(props.property_val_key);
                }
              }
            : undefined
        }
      />
    );
  }
 
  // create a new object without the first index
  const objectWithoutIndex0 = Object.fromEntries(
    indexesArray.slice(1).map((index) => [index, props.indexes[index]])
  );
  const displayName = props.displayName ?? props.property.displayName;
 
  return (
    <div className="w-full flex flex-col gap-2">
      <p>{displayName}</p>
      <div
        className="flex flex-col border-l-[1px] pl-2"
        aria-label={`indexedVariable-${displayName}`}
      >
        {Object.entries(values)
          .sort(([key1], [key2]) => key1.localeCompare(key2))
          .map(([key]) => {
            const idxes = Object.values(props.indexes);
            const disName = idxes[0][key]?.displayName || key;
            return (
              <IndexedVariableWithUnit
                key={key}
                property={props.property}
                property_val_key={key}
                displayName={disName}
                indexes={objectWithoutIndex0}
                values={values[key]}
                onUpdateValue={props.onUpdateValue}
                onUpdateUnit={props.onUpdateUnit}
                withMSSConnection={props.withMSSConnection}
                deletePropertyFunction={props.deletePropertyFunction}
              />
            );
          })}
      </div>
    </div>
  );
}