All files / src/pages/flowsheet-page/economics/results-panel/tables KeyFinancialMetricsTable.tsx

76.19% Statements 32/42
63.63% Branches 14/22
25% Functions 3/12
87.5% Lines 21/24

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                      50x                     5x   22x   253x 7x 5x 2x     22x 5x 2x         22x             7x 7x                   5x         22x 22x 7x   22x     22x 12x     15x      
import { Table2 } from "lucide-react";
import type { EconomicsResultLineRead } from "@/api/apiStore.gen";
import {
  formatAmount,
  formatUnit,
} from "../../shared/model/economicsFormatters";
import { KEY_FINANCIAL_METRIC_ROWS } from "../model/resultLineSelectors";
import { resultLineRowClassName, resultLineRowRef } from "./resultRowState";
import { ResultDataTable } from "./resultTable";
import { resultColumn } from "./resultTableColumns";
 
export function KeyFinancialMetricsTable({
  lines,
  resultCurrency,
  selectedSourceRowId,
  rowRefs,
}: {
  lines: EconomicsResultLineRead[];
  resultCurrency: string;
  selectedSourceRowId?: number;
  rowRefs: Map<number, HTMLElement>;
}) {
  const rows = [...KEY_FINANCIAL_METRIC_ROWS]
    .sort((left, right) => left.label.localeCompare(right.label))
    .map((item) => ({
      ...item,
      line: lines.find((line) => line.row_key === item.rowKey),
    }));
  const columns = [
    resultColumn<(typeof rows)[number]>({
      key: "metric",
      header: "Metric",
      render: ({ label }: (typeof rows)[number]) => label,
    }),
    resultColumn<(typeof rows)[number]>({
      key: "value",
      header: "Value",
      headClassName: "text-right",
      cellClassName: "text-right tabular-nums",
      render: ({ rowKey, line }: (typeof rows)[number]) =>
        line
          ? `${formatAmount(line.amount, {
              maximumFractionDigits:
                rowKey === "metric.simple_payback_years" ? 4 : 0,
            })} ${formatUnit(line.unit, resultCurrency)}`
          : "-",
    }),
  ];
 
  return (
    <section
      className="rounded-md border bg-background p-3"
      aria-label="Key financial metrics"
    >
      <div className="mb-2 flex items-center gap-2">
        <Table2 className="size-4 text-primary" />
        <h4 className="text-sm font-semibold">Key financial metrics</h4>
      </div>
      <ResultDataTable
        ariaLabel="Key financial metric rows"
        columns={columns}
        rows={rows}
        getRowKey={({ label, rowKey }) => `${label}-${rowKey}`}
        getRowRef={({ line }) =>
          line ? resultLineRowRef(rowRefs, line.id) : undefined
        }
        getRowAriaSelected={({ line }) =>
          line ? selectedSourceRowId === line.id : undefined
        }
        getRowClassName={({ line }) =>
          resultLineRowClassName({ line, selectedSourceRowId })
        }
      />
    </section>
  );
}