"use client"; import React, { useMemo } from "react"; import { cn, parseNumber } from "@/lib/utils"; import type { ChartData, SignStyle, TopChartTableRow } from "../types"; import { CHART_HEIGHTS, TABLE_DIMENSIONS } from "@/components/lge-dashboard/chart/chart.config"; import { UNIT_TEXT } from "@/const/lg-common.const"; import {isCagrRow, isSampleRow} from "@/lib/lg-dashboard"; import BaseTableHeader from "../table/core/BaseTableHeader"; import { formatSignedValue, getSignTextClass, resolveSignStyle, toRoundedNumber } from "../table/core/table-core.helpers"; export type TopChartTableProps = { title?: string; categories: string[]; rows: TopChartTableRow[]; chart?: | React.ReactNode | ((ctx: { chartData: ChartData; }) => React.ReactNode); chartHeight?: number | string; minWidthPx?: number; minWidth?: number; tableClassName?: string; wrapperClassName?: string; signStyle?: SignStyle; fillCAGR?: boolean; unitText?: string; }; export default function TopChartTable({ title, rows, categories, chart, chartHeight = CHART_HEIGHTS.DEFAULT, minWidthPx = TABLE_DIMENSIONS.MIN_WIDTH.STANDARD, minWidth, tableClassName, wrapperClassName, signStyle, fillCAGR = false, unitText = UNIT_TEXT, }: Readonly) { const resolvedSignStyle = resolveSignStyle(signStyle, { positiveTextClassName: "lg-num-pos", negativeTextClassName: "lg-num-neg", zeroTextClassName: "", arrowWithNumber: true, }); const chartData: ChartData = useMemo( () => ({ categories, series: rows.map((row) => ({ name: row.label, data: row.values.map((value) => parseNumber(value) ?? null), })), }), [categories, rows] ); const chartNode = useMemo(() => { if (!chart) { return null; } return typeof chart === "function" ? chart({ chartData }) : chart; }, [chart, chartData]); return (
{categories.map((category) => ( ))} {rows.map((row, rowIndex) => { const signRow = !!signStyle?.rowSelector?.(row, rowIndex); const sampleRow = isSampleRow(row.label ?? ""); const cagrRow = isCagrRow(row.label ?? ""); const rowTotal = parseNumber(row.total); return ( {categories.map((_, categoryIndex) => { const rawValue = row.values?.[categoryIndex]; if(sampleRow){ return ; } const roundedValue = toRoundedNumber(rawValue, cagrRow? 1 : 0); if (!signRow) { if (roundedValue === null) { return ; } const shouldFillCell = fillCAGR && cagrRow && rowTotal !== null && roundedValue > rowTotal + 5; return ; } if (roundedValue === null) { return ; } return ( ); })} ); })}
{chartNode ?
{chartNode}
: null}
사례수 {category}
{row.label} {row.sample} {rawValue}-{roundedValue.toLocaleString()}- {formatSignedValue(roundedValue, resolvedSignStyle)}
); }