All files / src/pages/flowsheet-page/dynamics/results-panel/components ResultMetricStrip.tsx

88.4% Statements 61/69
31.14% Branches 19/61
50% Functions 3/6
88.88% Lines 48/54

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 166 167 168 169 170 171 172 173 174 175 176 177                                          593x                       317x 317x     69x       152x     96x     414x                           194x       951x     317x     317x 317x 317x               2536x     317x   634x 317x     317x 1268x 951x       69x                                     69x 69x     138x 69x 207x 69x     138x 69x 207x 69x       69x 207x 69x     138x 69x 207x 69x     138x 69x 207x 69x     69x 138x 483x     69x     414x                               414x      
import { ArrowRightToLine } from "lucide-react";
import { ToolTipCover } from "@/ahuora-design-system/ui/tooltip";
import type {
  ResultMetricRowTarget,
  ResultMetricTargets,
  ResultStats,
} from "@/api/apiStore.gen";
import { cn } from "@/lib/utils";
import type {
  ResultMetricKey,
  ResultMetricNavigationTarget,
} from "../utils/resultMetricNavigation";
 
type MetricItem = {
  key: ResultMetricKey | "missing";
  label: string;
  value: string;
  target?: ResultMetricRowTarget | null;
};
 
function formatResultMetric(value?: number | null): string {
  return value == null
    ? "Unavailable"
    : Intl.NumberFormat(undefined, {
        maximumFractionDigits: 4,
      }).format(value);
}
 
function metricTargetTooltip(item: MetricItem, unit?: string | null): string {
  if (!item.target) {
    return "";
  }
 
  const valueLabel = `${formatResultMetric(item.target.value)}${unit ? ` ${unit}` : ""}`;
  const rowLabel = `row ${item.target.row_index}`;
 
  if (item.target.match_kind === "closest") {
    return `Go to ${rowLabel}: closest value to ${item.label} (${valueLabel})`;
  }
 
  if (item.target.match_count > 1) {
    return `Go to first matching ${item.label} row: ${rowLabel} of ${item.target.match_count} matches (${valueLabel})`;
  }
 
  return `Go to ${rowLabel}: ${item.label} value (${valueLabel})`;
}
 
function ResultMetricValue({
  item,
  onMetricTargetSelect,
  resultKey,
  resultLabel,
  unit,
}: {
  item: MetricItem;
  onMetricTargetSelect?: (target: ResultMetricNavigationTarget) => void;
  resultKey?: string;
  resultLabel?: string;
  unit?: string | null;
}) {
  if (!item.target || !onMetricTargetSelect || item.key === "missing") {
    return <>{item.value}</>;
  }
 
  return (
    <ToolTipCover asChild content={metricTargetTooltip(item, unit)}>
      <button
        type="button"
        className={cn(
          "group inline-flex max-w-full items-center gap-1 rounded-sm text-left font-medium tabular-nums",
          "transition-colors hover:text-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
        )}
        aria-label={`Go to ${resultLabel ? `${resultLabel} ` : ""}${item.label} row`}
        onClick={() =>
          onMetricTargetSelect({
            resultKey: resultKey ?? "",
            resultLabel: resultLabel ?? resultKey ?? "",
            metricKey: item.key,
            metricLabel: item.label,
            target: item.target!,
            unit,
          })
        }
      >
        <span className="min-w-0 break-words underline-offset-2 group-hover:underline">
          {item.value}
        </span>
        <ArrowRightToLine
          aria-hidden="true"
          className="h-3 w-3 shrink-0 opacity-0 transition-opacity group-hover:opacity-80 group-focus-visible:opacity-80"
        />
      </button>
    </ToolTipCover>
  );
}
 
export function ResultMetricStrip({
  metricTargets,
  onMetricTargetSelect,
  resultKey,
  resultLabel,
  stats,
  unit,
}: {
  metricTargets?: ResultMetricTargets;
  onMetricTargetSelect?: (target: ResultMetricNavigationTarget) => void;
  resultKey?: string;
  resultLabel?: string;
  stats?: ResultStats;
  unit?: string | null;
}) {
  if (!stats) {
    return null;
  }
 
  const metrics: MetricItem[] = [
    {
      key: "mean",
      label: "Mean",
      value: formatResultMetric(stats.mean),
      target: metricTargets?.mean,
    },
    {
      key: "median",
      label: "Median",
      value: formatResultMetric(stats.median),
      target: metricTargets?.median,
    },
    {
      key: "mode",
      label: "Mode",
      value: stats.mode_label,
      target: metricTargets?.mode,
    },
    {
      key: "min",
      label: "Min",
      value: formatResultMetric(stats.min),
      target: metricTargets?.min,
    },
    {
      key: "max",
      label: "Max",
      value: formatResultMetric(stats.max),
      target: metricTargets?.max,
    },
    {
      key: "missing",
      label: "Missing",
      value: `${stats.omitted_count} of ${stats.count + stats.omitted_count}`,
    },
  ];
 
  return (
    <div className="border-b border-border pb-3" aria-label="Result metrics">
      <dl className="grid grid-cols-[repeat(auto-fit,minmax(7.5rem,1fr))] gap-x-4 gap-y-2">
        {metrics.map((item) => (
          <div key={item.key} className="min-w-0">
            <dt className="text-xs leading-none text-muted-foreground">
              {item.label}
            </dt>
            <dd className="mt-1 break-words text-sm font-medium leading-tight tabular-nums">
              <ResultMetricValue
                item={item}
                onMetricTargetSelect={onMetricTargetSelect}
                resultKey={resultKey}
                resultLabel={resultLabel}
                unit={unit}
              />
            </dd>
          </div>
        ))}
      </dl>
    </div>
  );
}