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

0% Statements 0/61
2.32% Branches 1/43
0% Functions 0/11
0% Lines 0/37

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                                                                                                                                                                                             
import { TableRow } from "@/ahuora-design-system/ui/table";
import type {
  EconomicsResultLineRead,
  EconomicsStudyFullRead,
} from "@/api/apiStore.gen";
import {
  formatAmount,
  formatUnit,
} from "../../shared/model/economicsFormatters";
import {
  ANNUAL_REVENUE_ROW_KEY,
  revenueRows,
} from "../model/resultLineSelectors";
import type { SourceNavigationHandler } from "../model/types";
import { numericField } from "../model/valueParsing";
import { resultLineRowClassName, resultLineRowRef } from "./resultRowState";
import { ACCORDION_TABLE_PANEL_CLASS } from "./resultStyles";
import { ResultDataTable, ResultTableCell } from "./resultTable";
import {
  amountWithUnitColumn,
  sourceLinkedLabelColumn,
} from "./resultTableColumns";
 
export function RevenueBreakdown({
  lines,
  resultCurrency,
  fullStudy,
  selectedSourceRowId,
  rowRefs,
  onNavigateToSource,
}: {
  lines: EconomicsResultLineRead[];
  resultCurrency: string;
  fullStudy?: EconomicsStudyFullRead;
  selectedSourceRowId?: number;
  rowRefs: Map<number, HTMLElement>;
  onNavigateToSource?: SourceNavigationHandler;
}) {
  const rows = revenueRows(lines);
  const annualRevenueLine = lines.find(
    (line) => line.row_key === ANNUAL_REVENUE_ROW_KEY,
  );
  const totalAmount = annualRevenueLine
    ? numericField(annualRevenueLine.amount)
    : null;
  const totalUnit = annualRevenueLine?.unit ?? `${resultCurrency}/year`;
  const columns = [
    sourceLinkedLabelColumn({
      key: "source",
      header: "Revenue source",
      fullStudy,
      returnLabel: "Revenue Breakdown",
      onNavigateToSource,
    }),
    amountWithUnitColumn<EconomicsResultLineRead>({
      getAmount: (line) => line.amount,
      getUnit: (line) => line.unit,
      resultCurrency,
    }),
  ];
 
  return (
    <section
      className={ACCORDION_TABLE_PANEL_CLASS}
      aria-label="Revenue breakdown"
    >
      <ResultDataTable
        ariaLabel="Revenue source result rows"
        columns={columns}
        rows={rows}
        getRowKey={(line) => line.id}
        emptyMessage="No revenue rows"
        getRowRef={(line) => resultLineRowRef(rowRefs, line.id)}
        getRowAriaSelected={(line) => selectedSourceRowId === line.id}
        getRowClassName={(line) =>
          resultLineRowClassName({ line, selectedSourceRowId })
        }
        footer={
          <TableRow className="border-t font-medium">
            <ResultTableCell>Total revenue</ResultTableCell>
            <ResultTableCell>
              {totalAmount == null
                ? "Unavailable"
                : `${formatAmount(totalAmount)} ${formatUnit(
                    totalUnit,
                    resultCurrency,
                  )}`}
            </ResultTableCell>
          </TableRow>
        }
      />
    </section>
  );
}