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

81.96% Statements 50/61
62.9% Branches 39/62
85.71% Functions 6/7
91.48% Lines 43/47

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                                    76x 57526x 205801x                   3792x     809x 924x 924x                 4601x     2699x 2699x   6068x     1736x   4332x           10x     4601x           2374x     190x 190x     2374x     22285x           22285x 22285x 22285x 22285x         24659x   22285x     29407x     4601x     4601x           4601x   4601x 39x     4601x     22285x       62974x   9138x     2880x 13147x     13147x 2880x             18907x 18907x   18907x      
import { useContext } from "react";
import { useSelector } from "react-redux";
import { StatusIndicator } from "@/ahuora-design-system/ui/ui-status-indicator";
import { useCurrentObjectId, useObjectSchema } from "@/hooks/flowsheetObjects";
import { RootState } from "@/store/store";
import { IndicatorVisibilityContext } from "../../Canvas/FlowsheetSurface";
 
interface PropertiesDictionary {
  [key: string]: any;
}
 
interface PropertiesStatusProps {
  propertySet: PropertiesDictionary | string;
  propertySetGroup?: string;
  compoundValues?: { [key: string]: { value: string | number } };
  hideSuccess?: boolean;
}
 
const selectAnyMutationInProgress = (state: RootState) =>
  Object.values(state.api.mutations).some(
    (mutation) => mutation.status === "pending",
  );
 
function filterPropertiesByGroup(
  propertySet: string[],
  propertySetGroup: string | undefined,
  schema: any,
): string[] {
  // if no  propertyset group (eg. compounds or default) is specified, don't filter
  if (!propertySetGroup) {
    return propertySet;
  }
  // if propertyset group is specified, filter to just the properties in the group
  return propertySet.filter((prop: string) => {
    const propGroup = schema?.properties?.[prop]?.propertySetGroup;
    return propGroup === propertySetGroup;
  });
}
 
function getToolContent(
  data: string[],
  schema: any,
  compoundTotalError = false,
): string {
  let content = "";
  // If data is not empty, process it
  if (data.length > 0 || compoundTotalError) {
    content += "\n";
    data.forEach((prop: string) => {
      // Get display name from schema, or use property key as fallback
      const displayName = schema?.properties?.[prop]?.displayName || prop;
 
      if (prop === "mole_frac_comp") {
        content += `• Compounds\n`;
      } else {
        content += `• ${displayName}\n`;
      }
    });
 
    // Add compound total error message if needed
    if (compoundTotalError) {
      content += `• Total compound fractions must equal to 1!\n`;
    }
  }
  return content;
}
 
function calculateCompoundTotal(
  values: { [key: string]: { value: string | number } } = {},
): number {
  let compoundTotal = 0;
 
  for (const key in values) {
    const value = values[key].value;
    compoundTotal += +value;
  }
 
  return parseFloat(compoundTotal.toFixed(3));
}
 
export const PropertiesStatus: React.FC<PropertiesStatusProps> = ({
  propertySet,
  propertySetGroup,
  compoundValues,
  hideSuccess,
}) => {
  const currentObjectId = useCurrentObjectId();
  const schema = useObjectSchema(currentObjectId);
  const indicatorsVisible = useContext(IndicatorVisibilityContext);
  const hasPendingMutations = useSelector(selectAnyMutationInProgress);
 
  Iif (!indicatorsVisible) return null;
 
  // check compounds total (only relevant if this is compound propertyset group)
  const compoundTotal = calculateCompoundTotal(compoundValues);
  const compoundTotalError =
    propertySetGroup === "composition" &&
    compoundValues &&
    Object.keys(compoundValues).length > 0 &&
    compoundTotal !== 1;
 
  // Regular case with property arrays
  const allProps = Array.isArray(propertySet) ? propertySet : [];
 
  // filter properties according to the specified group
  const filteredProps = filterPropertiesByGroup(
    allProps,
    propertySetGroup,
    schema,
  );
 
  const toggle_key = schema?.propertySetGroups?.[propertySetGroup]?.toggle;
  const groupIsDisabled =
    toggle_key &&
    allProps.find((prop) => prop.key === toggle_key)?.value == false;
 
  // check if we have missing properties or compound total error
  const hasErrors =
    !groupIsDisabled && (filteredProps.length > 0 || compoundTotalError);
 
  const processedContent = getToolContent(
    filteredProps,
    schema,
    compoundTotalError,
  );
 
  Iif ((!hasErrors && hideSuccess) || (!hasErrors && hasPendingMutations))
    return null;
  return (
    <StatusIndicator
      variant={hasErrors ? "error" : "success"}
      size="md"
      tooltip={
        <div aria-label={`${hasErrors ? "alert" : "success"}`}>
          {hasErrors ? (
            <>
              <span className="font-semibold">Undefined parameters:</span>
              <span>{processedContent}</span>
            </>
          ) : (
            "Parameters are defined"
          )}
        </div>
      }
    />
  );
};