"use client"; import { cn, parseNumber } from "@/lib/utils"; import { roundNumber } from "@/lib/lg-dashboard"; import type { GaugeColumn } from "../types"; import BaseTableRenderer from "./core/BaseTableRenderer"; import { getColumnAlign, getLeafColumns, toTableCellContent } from "./core/table-core.helpers"; type Props> = { columns: GaugeColumn[]; rows: T[]; className?: string; tableClassName?: string; wrapperClassName?: string; scale?: number; fillColor?: string; trackColor?: string; cellHeight?: number; valueFormatter?: (value: number) => string; }; const GaugeTable = >({ columns, rows, className, tableClassName, wrapperClassName, scale = 1, fillColor = "hsl(var(--lg-gray-200) / 0.85)", trackColor = "hsl(var(--lg-pink-300) / 0.2)", cellHeight = 26, valueFormatter, }: Props) => { const formatValue = valueFormatter ?? ((value: number) => String(roundNumber(value))); const clampRatio = (value: number) => Math.max(0, Math.min(1, value)); const leafColumns = getLeafColumns(columns); return ( {leafColumns.map((column, index) => ( ))} } > {rows.map((row, rowIndex) => ( {leafColumns.map((column) => { const align = getColumnAlign(column); if (column.rowHead) { const content = column.render ? column.render(row) : toTableCellContent(row[column.key]); return ( {content} ); } const rawValue = row[column.key]; const numericValue = parseNumber(rawValue); const max = "max" in column ? column.max ?? 100 : 100; const ratio = numericValue === null ? 0 : clampRatio((numericValue * scale) / max); const percentage = ratio * 100; const background = numericValue === null ? undefined : `linear-gradient(to right, ${fillColor} ${percentage}%, ${trackColor} 0%)`; if (("cellType" in column ? column.cellType : "gauge") === "number") { return ( {numericValue === null ? "-" : formatValue(numericValue)} ); } return ( {numericValue === null ? "-" : formatValue(numericValue)} ); })} ))} ); }; export default GaugeTable;