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

97.43% Statements 38/39
90.62% Branches 29/32
100% Functions 7/7
100% Lines 36/36

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                                              17948x 16070x     1878x 2167x 2167x                 17948x   17948x 9301x 9301x   21425x   21425x 6096x   15329x         9301x 21x     17948x           17948x   17948x 394x 394x     17948x     45x           17948x 17948x 17948x   17948x     17948x   17948x           17948x     17948x           17948x 17948x     17948x   17948x           17948x 11481x                                      
import { useContext } from "react";
import { IndicatorVisibilityContext } from "../../Canvas/FlowsheetSurface";
import { StatusIndicator } from "@/ahuora-design-system/ui/ui-status-indicator";
import { useCurrentObjectId, useObjectSchema } from "@/hooks/flowsheetObjects";
 
interface PropertiesDictionary {
  [key: string]: any;
}
 
interface PropertiesStatusProps {
  propertySet: PropertiesDictionary | string;
  propertySetGroup?: string;
  compoundValues?: { [key: string]: { value: string | number } };
  hideSuccess?: boolean;
}
 
 
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);
 
  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,
  );
 
  if (!hasErrors && hideSuccess) 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>
      }
    />
  );
};