import React, { PureComponent } from 'react'; import { calculateLogsLabelStats, LogLabelStatsModel, LogRowModel } from 'app/core/logs_model'; import { LogLabelStats } from './LogLabelStats'; interface Props { getRows?: () => LogRowModel[]; label: string; plain?: boolean; value: string; onClickLabel?: (label: string, value: string) => void; } interface State { showStats: boolean; stats: LogLabelStatsModel[]; } export class LogLabel extends PureComponent { state = { stats: null, showStats: false, }; onClickClose = () => { this.setState({ showStats: false }); }; onClickLabel = () => { const { onClickLabel, label, value } = this.props; if (onClickLabel) { onClickLabel(label, value); } }; onClickStats = () => { this.setState(state => { if (state.showStats) { return { showStats: false, stats: null }; } const allRows = this.props.getRows(); const stats = calculateLogsLabelStats(allRows, this.props.label); return { showStats: true, stats }; }); }; render() { const { getRows, label, plain, value } = this.props; const { showStats, stats } = this.state; const tooltip = `${label}: ${value}`; return ( {value} {!plain && ( )} {!plain && getRows && } {showStats && ( )} ); } }