LogLabel.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import React, { PureComponent } from 'react';
  2. import { calculateLogsLabelStats, LogLabelStatsModel, LogRowModel } from 'app/core/logs_model';
  3. import { LogLabelStats } from './LogLabelStats';
  4. interface Props {
  5. getRows?: () => LogRowModel[];
  6. label: string;
  7. plain?: boolean;
  8. value: string;
  9. onClickLabel?: (label: string, value: string) => void;
  10. }
  11. interface State {
  12. showStats: boolean;
  13. stats: LogLabelStatsModel[];
  14. }
  15. export class LogLabel extends PureComponent<Props, State> {
  16. state = {
  17. stats: null,
  18. showStats: false,
  19. };
  20. onClickClose = () => {
  21. this.setState({ showStats: false });
  22. };
  23. onClickLabel = () => {
  24. const { onClickLabel, label, value } = this.props;
  25. if (onClickLabel) {
  26. onClickLabel(label, value);
  27. }
  28. };
  29. onClickStats = () => {
  30. this.setState(state => {
  31. if (state.showStats) {
  32. return { showStats: false, stats: null };
  33. }
  34. const allRows = this.props.getRows();
  35. const stats = calculateLogsLabelStats(allRows, this.props.label);
  36. return { showStats: true, stats };
  37. });
  38. };
  39. render() {
  40. const { getRows, label, plain, value } = this.props;
  41. const { showStats, stats } = this.state;
  42. const tooltip = `${label}: ${value}`;
  43. return (
  44. <span className="logs-label">
  45. <span className="logs-label__value" title={tooltip}>
  46. {value}
  47. </span>
  48. {!plain && (
  49. <span title="Filter for label" onClick={this.onClickLabel} className="logs-label__icon fa fa-search-plus" />
  50. )}
  51. {!plain && getRows && <span onClick={this.onClickStats} className="logs-label__icon fa fa-signal" />}
  52. {showStats && (
  53. <span className="logs-label__stats">
  54. <LogLabelStats
  55. stats={stats}
  56. rowCount={getRows().length}
  57. label={label}
  58. value={value}
  59. onClickClose={this.onClickClose}
  60. />
  61. </span>
  62. )}
  63. </span>
  64. );
  65. }
  66. }