"use client"; import type React from "react"; import { cn, parseNumber } from "@/lib/utils"; import { isSampleRow } from "@/lib/lg-dashboard"; import type { DataColumn, SignStyle } from "../types"; import BaseTableRenderer from "./core/BaseTableRenderer"; import { appendCellMarker, formatSignedValue, getColumnAlign, getSignCellBackground, getSignTextClass, resolveSignStyle, shouldApplySignToColumn, toRoundedNumber, toTableCellContent, type ResolvedSignStyle, } from "./core/table-core.helpers"; export type MergedTableCell = { key: keyof T; content: React.ReactNode; startRowIndex?: number; rowSpan?: number; className?: string; }; type Props> = { columns: DataColumn[]; rows: T[]; title?: string; wrapperClassName?: string; tableClassName?: string; subheadRowClassName?: string; minWidth?: number; signStyle?: SignStyle; signStyleFallback?: Partial; fillGreaterThan5?: boolean; starUnder30Sample?: boolean; unitText?: string; mergedCell?: MergedTableCell; getCellClassName?: (row: T, column: DataColumn, rowIndex: number) => string | undefined; }; export default function BaseDataTable>({ columns, rows, title, wrapperClassName, tableClassName, subheadRowClassName, minWidth, signStyle, signStyleFallback, fillGreaterThan5 = false, starUnder30Sample = false, unitText, mergedCell, getCellClassName, }: Readonly>) { const resolvedSignStyle = resolveSignStyle(signStyle, signStyleFallback); const mergedCellStart = mergedCell?.startRowIndex ?? 0; const mergedCellSpan = mergedCell?.rowSpan ?? Math.max(0, rows.length - mergedCellStart); return ( {rows.map((row, rowIndex) => { const sampleRow = isSampleRow((row.rowLabel as string) ?? ""); const signRow = !!signStyle?.rowSelector?.(row, rowIndex); const rowTotal = parseNumber(row.total); return ( {columns.map((column) => { const dynamicCellClassName = getCellClassName?.(row, column, rowIndex); if (mergedCell && column.key === mergedCell.key && rowIndex >= mergedCellStart) { if (rowIndex > mergedCellStart) { return null; } return ( {mergedCell.content} ); } const rawValue = row[column.key]; const align = getColumnAlign(column); const defaultContent = column.render ? column.render(row) : toTableCellContent(rawValue); const numericValue = column.numeric ? parseNumber(rawValue ?? defaultContent) ?? 0 : 0; const applySign = signRow && shouldApplySignToColumn(column, signStyle); if (column.rowHead) { return ( {defaultContent} ); } if (applySign) { const roundedValue = toRoundedNumber(rawValue); const displayText = roundedValue === null ? "-" : formatSignedValue(roundedValue, resolvedSignStyle); const cellStyle: React.CSSProperties = resolvedSignStyle.paint === "cell" ? { textAlign: align, background: getSignCellBackground(roundedValue ?? 0, resolvedSignStyle) } : { textAlign: align }; return ( {displayText} ); } const shouldFillCell = fillGreaterThan5 && rowTotal !== null && numericValue > rowTotal + 5; const shouldMarkSample = starUnder30Sample && sampleRow && numericValue < 30; const displayContent = shouldMarkSample ? appendCellMarker(defaultContent, "*") : defaultContent; return ( {displayContent} ); })} ); })} ); }