All files / src/pages/flowsheet-page/economics/results-panel/charts ChartCard.tsx

75.32% Statements 58/77
41.07% Branches 23/56
36.36% Functions 4/11
83.72% Lines 36/43

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                                        8x                 8x 8x 16x   8x   16x     8x       8x       16x                 24x               16x 24x 24x 24x 8x 88x 24x 8x   32x 56x       8x             6x   2x   4x   2x       4x 6x       12x             6x     6x   12x 1x   5x 10x 11x                       15x      
import type { ReactNode } from "react";
import { Badge } from "@/ahuora-design-system/ui/badge";
import { Button } from "@/ahuora-design-system/ui/button";
import { cn } from "@/lib/utils";
import { formatAmount } from "../../shared/model/economicsFormatters";
import type {
  DrillbackHandler,
  ParsedChartDataset,
  SourceRow,
  WarningRef,
} from "../model/types";
import {
  chartSourcePoints,
  chartTitle,
  visibleChartWarnings,
} from "./chartData";
import { cashFlowRenderingMetadata } from "./chartFormatters";
import { chartCardDefinition } from "./chartRenderers";
import { CHART_CASH_FLOW_NPV } from "./constants";
 
export function ChartCard({
  dataset,
  onDrillback,
  controls,
}: {
  dataset: ParsedChartDataset;
  onDrillback: DrillbackHandler;
  controls?: ReactNode;
}) {
  const isCashFlowChart = dataset.chart_key === CHART_CASH_FLOW_NPV;
  const chartDefinition = chartCardDefinition(dataset);
  const warningRefs = visibleChartWarnings(dataset);
  const paybackYears =
    cashFlowRenderingMetadata(dataset.rendering_metadata)?.payback_years ??
    null;
  const title = chartTitle(dataset);
  return (
    <section
      className={cn(
        "rounded-md border bg-background p-3",
        chartDefinition.wide && "xl:col-span-2",
      )}
      aria-label={`Chart ${title}`}
    >
      <div className="mb-3 flex items-start justify-between gap-3">
        <div className="min-w-0">
          <h5 className="text-sm font-semibold">{title}</h5>
          {isCashFlowChart && paybackYears != null && (
            <div
              className="mt-1 text-xs text-muted-foreground"
              aria-label="Cash-flow payback marker"
            >
              Payback {formatAmount(paybackYears)} years
            </div>
          )}
        </div>
        <div className="flex shrink-0 flex-wrap items-center justify-end gap-2">
          {controls}
          {warningRefs.length > 0 && (
            <Badge variant="secondary" size="xs" borderRadius="round">
              {warningRefs.length} warning
              {warningRefs.length === 1 ? "" : "s"}
            </Badge>
          )}
        </div>
      </div>
      <ChartWarningSummary dataset={dataset} warningRefs={warningRefs} />
      <div className={chartDefinition.heightClassName}>
        {chartDefinition.render({ dataset, onDrillback })}
      </div>
      {chartDefinition.showSourceRows && (
        <SourceRowButtons dataset={dataset} onDrillback={onDrillback} />
      )}
    </section>
  );
}
 
function ChartWarningSummary({
  dataset,
  warningRefs,
}: {
  dataset: ParsedChartDataset;
  warningRefs: WarningRef[];
}) {
  Iif (warningRefs.length === 0) return null;
  return (
    <div
      className="mb-3 space-y-1 rounded-md border border-amber-500/25 bg-amber-500/10 px-2 py-1.5 text-[11px] leading-snug text-amber-700 dark:text-amber-300"
      aria-label={`Chart warnings for ${chartTitle(dataset)}`}
    >
      {warningRefs.map((warning) => (
        <div key={`${warning.code}-${warning.message}`} className="flex gap-1">
          <span>{warning.message}</span>
        </div>
      ))}
    </div>
  );
}
 
function SourceRowButtons({
  dataset,
  onDrillback,
}: {
  dataset: ParsedChartDataset;
  onDrillback: DrillbackHandler;
}) {
  const sourceRows = chartSourcePoints(dataset)
    .map((point) => point.source_row)
    .filter((sourceRow): sourceRow is SourceRow => Boolean(sourceRow));
  const uniqueRows = Array.from(
    new Map(sourceRows.map((row) => [row.id, row])).values(),
  );
  Iif (uniqueRows.length === 0) return null;
  return (
    <div className="mt-3 flex flex-wrap gap-2 border-t pt-3">
      {uniqueRows.slice(0, 6).map((sourceRow) => (
        <Button
          key={sourceRow.id}
          type="button"
          size="sm"
          variant="outline"
          aria-label={`Show calculation trace for ${sourceRow.label}`}
          title={sourceRow.label}
          onClick={() => onDrillback(sourceRow.id)}
        >
          {sourceRow.label}
        </Button>
      ))}
    </div>
  );
}