All files / src/pages/flowsheet-page/flowsheet/Canvas PropertyHover.tsx

55.1% Statements 27/49
63.04% Branches 29/46
50% Functions 5/10
60.97% Lines 25/41

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                              28x 6x 6x 6x 6x 6x 6x 6x 6x         13x   13x     13x                             13x 28x     13x                     13x 6x           7x 28x 28x 28x     28x 22x         28x           28x                                                                                                                                                        
import { Card, CardContent, CardHeader } from "@/ahuora-design-system/ui/card";
import { Separator } from "@/ahuora-design-system/ui/separator";
import {
  ObjectTypeEnum,
  PropertyInfoRead,
  PropertySetRead,
  SimulationObjectRead,
  useUnitopsSimulationobjectsRetrieveQuery,
} from "@/api/apiStore.gen";
import { memo, useMemo } from "react";
import { useLocalStorage } from "usehooks-ts";
import objects from "@/data/objects.json";
import { GetQuickUnits } from "@/data/UnitsLibrary";
 
function valueFormatter(raw: number | string | null | undefined) {
  if (raw === null || raw === undefined) return "";
  const num = typeof raw === "number" ? raw : Number(raw);
  Iif (Number.isNaN(num)) return "";
  let out: number | string = num;
  const [intPart, decRaw] = num.toString().split(".");
  const dec = decRaw ? decRaw.padEnd(4, "0") : "";
  if (dec !== "" && dec.length > 4) out = +num.toFixed(4);
  Iif (intPart && intPart.length > 6) out = num.toExponential(4);
  return out;
}
 
function PropertyHover({ simulationObject }: { simulationObject: SimulationObjectRead }) {
  const { data: simulationObjectData } =
    useUnitopsSimulationobjectsRetrieveQuery({ id: simulationObject.id });
 
  const properties = simulationObjectData?.properties;
 
  // Streams (Material or Energy) -> use generic hover
  Iif (
    simulationObject.objectType === ObjectTypeEnum.EnergyStream ||
    simulationObject.objectType === ObjectTypeEnum.Stream || simulationObject.objectType === ObjectTypeEnum.AcStream ||
    simulationObject.objectType === ObjectTypeEnum.HumidAirStream
  ) {
    return (
      <RenderStreamHoverGeneric
        simulationObject={simulationObject}
        properties={properties}
        streamName={simulationObject.componentName}
      />
    );
  }
 
  // Generic objects: flat key list from objects.json
  const listOfProperties = properties?.ContainedProperties?.filter((p) =>
    (objects as any)[simulationObject.objectType]?.keyProperties?.includes(p.key),
  );
 
  return (
    <Card className="w-80">
      <CardHeader className="flex flex-row justify-between p-0 items-center">
        <span>{simulationObject.componentName}</span>
      </CardHeader>
      <RenderHover properties={listOfProperties} />
    </Card>
  );
}
 
function RenderHover({ properties }: { properties: PropertyInfoRead[] | undefined }) {
  if (!properties?.length) {
    return (
      <CardContent>
        <small>No properties to display</small>
      </CardContent>
    );
  }
  return properties.map((property, index) => {
    const unit = GetQuickUnits(property.unitType, property.unit);
    const rawValue = property.values?.value as number | string | null | undefined;
    let value = valueFormatter(rawValue);
 
    // Show "unspecified" if the value is missing/empty
    if (value === "") {
      value = "unspecified";
    }
 
    // Decide if the unit should be shown
    const showUnit =
      value !== "unspecified" &&
      unit &&
      unit !== "-" &&
      unit !== "dimensionless" &&
      unit !== "—"; // exclude em dash too
 
    return (
      <CardContent key={property.id}>
        <div className="flex justify-between">
          <small className="w-max">{property.displayName}</small>
          <small className={value === "unspecified" ? "italic text-muted-foreground" : ""}>
            {value}
            {showUnit ? ` ${unit}` : ""}
          </small>
        </div>
        {index !== properties.length - 1 && <Separator className="mt-3" />}
      </CardContent>
    );
  });
}
 
/** ---------- Generic Stream Hover (Material + Energy) ---------- **/
 
type MolarMassKeys = { molar?: string[]; mass?: string[] };
 
function isMolarMassKeys(x: unknown): x is MolarMassKeys {
  return !!x && typeof x === "object" && ("molar" in (x as any) || "mass" in (x as any));
}
 
/**
 * Handles:
 * - Material stream: keyProperties is { molar: string[]; mass: string[] } and respects localStorage "molarOrMass"
 * - Energy stream:   keyProperties is string[]
 */
function RenderStreamHoverGeneric({
  properties,
  streamName,
  simulationObject,
}: {
  simulationObject: SimulationObjectRead;
  properties: PropertySetRead | undefined;
  streamName: string | undefined;
}) {
  // Only used for material streams; harmless for energy streams
  const [molarOrMass] = useLocalStorage<"molar" | "mass">("molarOrMass", "molar");
 
  const listOfProperties = useMemo(() => {
    const keyProps = (objects as any)[simulationObject.objectType]?.keyProperties as
      | string[]
      | MolarMassKeys
      | undefined;
 
    Iif (!keyProps) return [];
 
    // Energy Stream -> flat list of keys (string[])
    Iif (Array.isArray(keyProps)) {
      const set = new Set(keyProps);
      return properties?.ContainedProperties?.filter((p) => set.has(p.key)) ?? [];
    }
 
    // Material Stream -> pick molar or mass keys
    Iif (isMolarMassKeys(keyProps)) {
      const keys = keyProps[molarOrMass] ?? [];
      Iif (!keys.length) return [];
      const set = new Set(keys);
      return properties?.ContainedProperties?.filter((p) => set.has(p.key)) ?? [];
    }
 
    return [];
  }, [properties, simulationObject.objectType, molarOrMass]);
 
  return (
    <Card className="w-80">
      <CardHeader className="flex flex-row justify-between p-0 items-center">
        <span>{streamName}</span>
      </CardHeader>
      <RenderHover properties={listOfProperties} />
    </Card>
  );
}
 
export default memo(PropertyHover);