import React, { PureComponent } from 'react'; import classnames from 'classnames'; import { LogLabelStatsModel } from 'app/core/logs_model'; function LogLabelStatsRow(logLabelStatsModel: LogLabelStatsModel) { const { active, count, proportion, value } = logLabelStatsModel; const percent = `${Math.round(proportion * 100)}%`; const barStyle = { width: percent }; const className = classnames('logs-stats-row', { 'logs-stats-row--active': active }); return (
{value}
{count}
{percent}
); } const STATS_ROW_LIMIT = 5; interface Props { stats: LogLabelStatsModel[]; label: string; value: string; rowCount: number; onClickClose: () => void; } export class LogLabelStats extends PureComponent { render() { const { label, rowCount, stats, value, onClickClose } = this.props; const topRows = stats.slice(0, STATS_ROW_LIMIT); let activeRow = topRows.find(row => row.value === value); let otherRows = stats.slice(STATS_ROW_LIMIT); const insertActiveRow = !activeRow; // Remove active row from other to show extra if (insertActiveRow) { activeRow = otherRows.find(row => row.value === value); otherRows = otherRows.filter(row => row.value !== value); } const otherCount = otherRows.reduce((sum, row) => sum + row.count, 0); const topCount = topRows.reduce((sum, row) => sum + row.count, 0); const total = topCount + otherCount; const otherProportion = otherCount / total; return (
{label}: {total} of {rowCount} rows have that label
{topRows.map(stat => )} {insertActiveRow && activeRow && } {otherCount > 0 && ( )}
); } }