import React from 'react'; import cx from 'classnames'; import isEmpty from 'lodash/isEmpty'; import type { ReportMetricRow } from '@bundle-stats/utils'; import { Button } from '../../ui/button'; import { Icon } from '../../ui/icon'; import { Table } from '../../ui/table'; import { Stack } from '../../layout/stack'; import { Metric } from '../metric'; import { Delta } from '../delta'; import { MetricsTableHeader, MetricsTableHeaderProps } from '../metrics-table-header'; import * as I18N from './metrics-table.i18n'; import css from './metrics-table.module.css'; const VISIBLE_COUNT = 500; const BASELINE_COLUMN_SPAN = 1; const CURRENT_COLUMN_SPAN = 3; interface RowProps extends React.ComponentProps { item: ReportMetricRow; renderHeader: (item: ReportMetricRow) => React.ReactNode; } const Row = ({ className = '', item, renderHeader, ...restProps }: RowProps) => ( {renderHeader(item)} {item.runs.map((run, index) => { const isBaseline = index === item.runs.length - 1; const valueClassName = cx(css.value, !isBaseline && css.current); // Empty cells if no value if (!run || typeof run.value === 'undefined') { return ( <> - {!isBaseline && } {!isBaseline && } ); } return ( <> {!isBaseline && ( <> {'delta' in run && ( )} {'delta' in run && ( )} )} ); })} ); interface MetricsTableProps extends Omit, 'title'> { runs: Array<{ label: string; internalBuildNumber: number; }>; items: Array; title?: React.ReactNode; showHeaderSum?: boolean; sort?: MetricsTableHeaderProps['sort']; updateSort?: MetricsTableHeaderProps['updateSort']; renderRowHeader?: (item: ReportMetricRow) => React.ReactNode; emptyMessage?: React.ReactNode; showAllItems: boolean; setShowAllItems: (value: boolean) => void; } export const MetricsTable = ({ className = '', runs, items, showHeaderSum = false, title, sort, updateSort, renderRowHeader = (item: any) => item.label, emptyMessage = I18N.EMPTY_MESSAGE, showAllItems, setShowAllItems, ...restProps }: MetricsTableProps) => { const columnCount = (runs.length - 1) * CURRENT_COLUMN_SPAN + BASELINE_COLUMN_SPAN + 1; const rootClassName = cx(css.root, className, runs.length > 1 && css.multipleRuns); const showEmpty = isEmpty(items); const showItems = !showEmpty; const hasHiddenItems = items?.length > VISIBLE_COUNT; const visibleItems = !hasHiddenItems || showAllItems ? items : items?.slice(0, VISIBLE_COUNT); return ( {showEmpty && (
{emptyMessage}
)} {showItems && ( <> {visibleItems.map((item) => ( ))} {hasHiddenItems && ( {showAllItems ? ( ) : ( )} )} )}
); };