"use client"; import React, { useMemo } from "react"; import { cn, parseNumber } from "@/lib/utils"; import BaseTableHeader from "../table/core/BaseTableHeader"; import { formatSignedValue, getSignTextClass, resolveSignStyle, toCssSize, toRoundedNumber } from "../table/core/table-core.helpers"; import { TopChartTablePreAppendCell, TopChartTablePreAppendCellConfig, TopChartTableRow, TopChartTableSectionRow } from "./types"; import {ChartData} from "../chart/types"; import {CHART_HEIGHTS, TABLE_DIMENSIONS} from "../chart/chart.config"; import type {DataColumn, SignStyle} from "@/components/table/types"; import { KFPA_DECIMAL_POINT } from "@/config/kfpa.config"; export type TopChartTableProps = { title?: string; columns: DataColumn>[]; rows: TopChartTableRow[]; sectionRows?: TopChartTableSectionRow[]; chart?: | React.ReactNode | ((ctx: { chartData: ChartData; }) => React.ReactNode); chartHeight?: number | string; minWidthPx?: number; minWidth?: number; maxWidth?: number; subLabelHeader?: string; rowHeaderWidth?: number | string; categoryColumnWidth?: number | string; categoryColumnWidths?: Array; tableClassName?: string; wrapperClassName?: string; signStyle?: SignStyle; }; const isPreAppendCellConfig = ( cell: TopChartTablePreAppendCell ): cell is TopChartTablePreAppendCellConfig => typeof cell === "object" && cell !== null && !Array.isArray(cell) && "content" in cell; const renderPreAppendCell = (cell: TopChartTablePreAppendCell, index: number) => { if (!isPreAppendCellConfig(cell)) { return {cell}; } return ( {cell.content} ); }; const getLeadingColumnWidths = (rows: TopChartTableRow[], preAppendColumnCount: number) => { const widths = Array(preAppendColumnCount).fill(undefined); rows.forEach((row) => { let columnIndex = 0; row.preAppendCells?.forEach((cell) => { const colSpan = isPreAppendCellConfig(cell) ? cell.colSpan ?? 1 : 1; const width = isPreAppendCellConfig(cell) ? toCssSize(cell.width) : undefined; if (width && colSpan === 1 && !widths[columnIndex]) { widths[columnIndex] = width; } columnIndex += colSpan; }); }); return widths; }; const getLabelColumnWidth = (rows: TopChartTableRow[]) => rows.map((row) => toCssSize(row.labelWidth)).find(Boolean); const getColumnKey = (column: DataColumn>) => String(column.id ?? ("key" in column ? column.key : column.header)); const getColumnLabel = (column: DataColumn>) => typeof column.header === "string" ? column.header : getColumnKey(column); export default function TopChartTable({ title, rows, sectionRows = [], columns, chart, chartHeight = CHART_HEIGHTS.DEFAULT, minWidthPx = TABLE_DIMENSIONS.MIN_WIDTH.STANDARD, minWidth, maxWidth, subLabelHeader = "", rowHeaderWidth, categoryColumnWidth, categoryColumnWidths, tableClassName, wrapperClassName, signStyle, }: Readonly) { const resolvedSignStyle = resolveSignStyle(signStyle, { positiveTextClassName: "kfpa-num-pos", negativeTextClassName: "kfpa-num-neg", zeroTextClassName: "text-zero", arrowWithNumber: true, valueFormatter: (value) => value.toFixed(KFPA_DECIMAL_POINT), }); const maxPreAppendCellCount = useMemo( () => Math.max(0, ...rows.map((row) => row.preAppendCells?.length ?? 0)), [rows] ); const leadingColumnCount = maxPreAppendCellCount + 1; const leadingColumnWidths = useMemo( () => [...getLeadingColumnWidths(rows, maxPreAppendCellCount), getLabelColumnWidth(rows)], [maxPreAppendCellCount, rows] ); const columnLabels = useMemo(() => columns.map(getColumnLabel), [columns]); const chartData: ChartData = useMemo( () => ({ categories: columnLabels, series: rows.map((row) => ({ name: row.label, data: row.values.map((value) => parseNumber(value) ?? null), })), }), [columnLabels, rows] ); const chartNode = useMemo(() => { if (!chart) { return null; } return typeof chart === "function" ? chart({ chartData }) : chart; }, [chart, chartData]); const rowHeaderStyle = useMemo(() => { const width = toCssSize(rowHeaderWidth); return width ? { width, minWidth: width } : undefined; }, [rowHeaderWidth]); const hasCategoryHeaderRow = useMemo( () => subLabelHeader.trim().length > 0 || columns.some((column) => getColumnLabel(column).trim().length > 0), [columns, subLabelHeader] ); return (
{leadingColumnWidths.map((width, index) => ( ))} {columns.map((column, index) => { const width = toCssSize(categoryColumnWidths?.[index] ?? column.width ?? categoryColumnWidth); return ( ); })} {hasCategoryHeaderRow && ( {columns.map((column) => { return ( ) })} )} {rows.map((row, rowIndex) => { const signRow = !!signStyle?.rowSelector?.(row, rowIndex); const preAppendCells = row.preAppendCells ?? []; const sectionRowsAfterCurrentRow = sectionRows.filter( (sectionRow) => sectionRow.afterRowIndex === rowIndex ); return ( {preAppendCells.map(renderPreAppendCell)} {columns.map((column, categoryIndex) => { const rawValue = row.values?.[categoryIndex]; const decimalPlaces = row.decimalPlaces ?? KFPA_DECIMAL_POINT; const roundedValue = toRoundedNumber(rawValue, decimalPlaces); if (roundedValue === null) { return ; } if(!signRow) { return ( ); } return ( ); })} {sectionRowsAfterCurrentRow.map((sectionRow, sectionRowIndex) => ( ))} ); })}
{chartNode ?
{chartNode}
: null}
{subLabelHeader} {column.header}
{row.label} - {roundedValue.toFixed(decimalPlaces)} {formatSignedValue(roundedValue, resolvedSignStyle)}
{sectionRow.content}
); }