import React from 'react';
import cx from 'classnames';
import sum from 'lodash/sum';
import {
METRIC_TYPE_CONFIGS,
MetricTypes,
type ReportMetricRow,
getMetricRunInfo,
} from '@bundle-stats/utils';
import { Table } from '../../ui/table';
import { Delta } from '../delta';
import { JobName } from '../job-name';
import { Metric } from '../metric';
import { SortButton, SortButtonProps } from '../sort-button';
import * as I18N from './metrics-table-header.i18n';
import css from './metrics-table-header.module.css';
const METRIC_TYPE = METRIC_TYPE_CONFIGS[MetricTypes.FileSize];
const BASELINE_COLUMN_SPAN = 1;
const CURRENT_COLUMN_SPAN = 3;
interface JobColumnProps {
job: { label: string; internalBuildNumber: number };
isBaseline: boolean;
}
const JobColumn = ({ job, isBaseline }: JobColumnProps) => {
const colSpan = isBaseline ? BASELINE_COLUMN_SPAN : CURRENT_COLUMN_SPAN;
const { label, internalBuildNumber } = job;
if (!job) {
return
-;
}
return (
{label}
);
};
const getRowsRunTotal = (rows: Array, runIndex: number): number =>
sum(rows.map((row) => row?.runs?.[runIndex]?.value || 0));
interface ColumnSumProps {
rows: Array;
isBaseline: boolean;
runIndex: number;
sort?: SortButtonProps['sort'];
updateSort?: SortButtonProps['updateSort'];
}
const SumColumn = ({ rows, isBaseline, runIndex, updateSort, sort }: ColumnSumProps) => {
const currentRunTotal = getRowsRunTotal(rows, runIndex);
const baselineRunTotal = !isBaseline ? getRowsRunTotal(rows, runIndex + 1) : 0;
const total = getMetricRunInfo(METRIC_TYPE, currentRunTotal, baselineRunTotal);
const fieldPath = `runs[${runIndex}]`;
return (
<>
{!isBaseline && (
<>
{'delta' in total && (
)}
{'delta' in total && (
)}
>
)}
>
);
};
export interface MetricsTableHeaderProps {
/**
* Metric column title
*/
metricTitle?: React.ReactNode;
/**
* Show column sum flag
*/
showSum?: boolean;
/**
* Array of jobs
*/
jobs: Array<{ label: string; internalBuildNumber: number }>;
/**
* Report rows
*/
rows: Array;
/**
* Sort field data
*/
sort?: any;
/**
* Update sort field
*/
updateSort?: (val: any) => void;
}
export const MetricsTableHeader = (
props: MetricsTableHeaderProps & React.ComponentProps<'thead'>,
) => {
const { className = '', metricTitle = '', showSum = false, jobs, rows, sort, updateSort } = props;
const rootClassName = cx(css.root, showSum && css.showHeaderSum, className);
return (
{metricTitle || ' '}
{jobs.map((job, runIndex) => (
))}
{showSum && (
{jobs.map((_, runIndex) => (
))}
)}
);
};