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

53.24% Statements 41/77
27.41% Branches 17/62
50% Functions 6/12
50% Lines 24/48

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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221                                                      2x     6x   6x       4x     12x   4x 8x   12x                                                           4x         2x                         2x   2x       4x   4x 2x 2x                     22x                     44x                       20x   6x 12x                                                                                                                                                 44x     132x   132x      
import { type ComponentProps, Fragment, type ReactNode } from "react";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/ahuora-design-system/ui/table";
import type {
  EconomicsResultLineRead,
  EconomicsStudyFullRead,
} from "@/api/apiStore.gen";
import { cn } from "@/lib/utils";
import {
  formatAmount,
  formatUnit,
} from "../../shared/model/economicsFormatters";
import type { SourceNavigationHandler } from "../model/types";
import { SourceNavigationButton } from "../navigation/sourceNavigation";
import { resultLineRowRef } from "./resultRowState";
import {
  RESULT_NESTED_ROW_CLASS,
  RESULT_SELECTED_ROW_CLASS,
  RESULT_TABLE_TEXT_CLASS,
} from "./resultStyles";
 
export function ResultTable({
  className,
  ...props
}: ComponentProps<typeof Table>) {
  return (
    <Table className={cn(RESULT_TABLE_TEXT_CLASS, className)} {...props} />
  );
}
 
export function ResultTableHead({
  className,
  ...props
}: ComponentProps<typeof TableHead>) {
  return (
    <TableHead
      className={cn("h-9", RESULT_TABLE_TEXT_CLASS, className)}
      {...props}
    />
  );
}
 
export type ResultTableColumn<T> = {
  key: string;
  header: ReactNode;
  headClassName?: string;
  cellClassName?: string;
  render: (row: T) => ReactNode;
};
 
type ResultDataTableProps<T> = {
  ariaLabel: string;
  columns: ResultTableColumn<T>[];
  rows: T[];
  getRowKey: (row: T) => string | number;
  emptyMessage?: ReactNode;
  tableClassName?: string;
  colgroup?: ReactNode;
  getRowId?: (row: T) => string | undefined;
  getRowRef?: (
    row: T,
  ) => ((node: HTMLTableRowElement | null) => void) | undefined;
  getRowClassName?: (row: T) => string | undefined;
  getRowAriaSelected?: (row: T) => boolean | undefined;
  renderDetailRow?: (row: T) => ReactNode;
  footer?: ReactNode;
};
 
export function ResultDataTable<T>({
  ariaLabel,
  columns,
  rows,
  getRowKey,
  emptyMessage = "No rows",
  tableClassName,
  colgroup,
  getRowId,
  getRowRef,
  getRowClassName,
  getRowAriaSelected,
  renderDetailRow,
  footer,
}: ResultDataTableProps<T>) {
  return (
    <ResultTable className={tableClassName} aria-label={ariaLabel}>
      {colgroup}
      <TableHeader>
        <TableRow>
          {columns.map((column) => (
            <ResultTableHead key={column.key} className={column.headClassName}>
              {column.header}
            </ResultTableHead>
          ))}
        </TableRow>
      </TableHeader>
      <TableBody>
        {rows.length === 0 ? (
          <TableRow>
            <ResultTableCell
              colSpan={columns.length}
              className="text-muted-foreground"
            >
              {emptyMessage}
            </ResultTableCell>
          </TableRow>
        ) : (
          rows.map((row) => {
            const rowRef = getRowRef?.(row);
            return (
              <Fragment key={getRowKey(row)}>
                <TableRow
                  id={getRowId?.(row)}
                  ref={rowRef}
                  tabIndex={rowRef ? -1 : undefined}
                  aria-selected={getRowAriaSelected?.(row)}
                  className={getRowClassName?.(row)}
                >
                  {columns.map((column) => (
                    <ResultTableCell
                      key={column.key}
                      className={column.cellClassName}
                    >
                      {column.render(row)}
                    </ResultTableCell>
                  ))}
                </TableRow>
                {renderDetailRow?.(row)}
              </Fragment>
            );
          })
        )}
        {footer}
      </TableBody>
    </ResultTable>
  );
}
 
export function AmountWithUnit({
  amount,
  unit,
  resultCurrency,
}: {
  amount: string | number | null | undefined;
  unit: string | null | undefined;
  resultCurrency: string;
}) {
  return (
    <>
      {formatAmount(amount)} {formatUnit(unit, resultCurrency)}
    </>
  );
}
 
export function SourceLinkedLabelCell({
  line,
  fullStudy,
  returnLabel,
  onNavigateToSource,
}: {
  line: EconomicsResultLineRead;
  fullStudy?: EconomicsStudyFullRead;
  returnLabel: string;
  onNavigateToSource?: SourceNavigationHandler;
}) {
  return (
    <div className="flex min-w-0 items-center gap-1">
      <span className="min-w-0 truncate">{line.label}</span>
      <SourceNavigationButton
        line={line}
        fullStudy={fullStudy}
        returnLabel={returnLabel}
        onNavigate={onNavigateToSource}
      />
    </div>
  );
}
 
export function ResultNestedContributorRow({
  line,
  selectedSourceRowId,
  rowRefs,
  className,
  children,
}: {
  line: EconomicsResultLineRead;
  selectedSourceRowId?: number;
  rowRefs: Map<number, HTMLElement>;
  className?: string;
  children: ReactNode;
}) {
  return (
    <div
      ref={resultLineRowRef(rowRefs, line.id)}
      tabIndex={-1}
      aria-selected={selectedSourceRowId === line.id}
      className={cn(
        RESULT_NESTED_ROW_CLASS,
        className,
        selectedSourceRowId === line.id ? RESULT_SELECTED_ROW_CLASS : undefined,
      )}
    >
      {children}
    </div>
  );
}
 
export function ResultTableCell({
  className,
  ...props
}: ComponentProps<typeof TableCell>) {
  return (
    <TableCell className={cn(RESULT_TABLE_TEXT_CLASS, className)} {...props} />
  );
}