time-control.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import React from 'react';
  2. import classNames from 'classnames';
  3. import { connect } from 'react-redux';
  4. import { TimestampTag } from 'weaveworks-ui-components';
  5. import { trackAnalyticsEvent } from '../utils/tracking-utils';
  6. import { pauseTimeAtNow, resumeTime } from '../actions/request-actions';
  7. import { isPausedSelector, timeTravelSupportedSelector } from '../selectors/time-travel';
  8. import moment from 'moment'
  9. const className = isSelected => (
  10. classNames('time-control-action', { 'time-control-action-selected': isSelected })
  11. );
  12. class TimeControl extends React.Component {
  13. getTrackingMetadata = (data = {}) => {
  14. const { currentTopology } = this.props;
  15. return {
  16. layout: this.props.topologyViewMode,
  17. parentTopologyId: currentTopology && currentTopology.get('parentId'),
  18. topologyId: currentTopology && currentTopology.get('id'),
  19. ...data,
  20. };
  21. }
  22. handleNowClick = () => {
  23. trackAnalyticsEvent('scope.time.resume.click', this.getTrackingMetadata());
  24. this.props.resumeTime();
  25. }
  26. handlePauseClick = () => {
  27. trackAnalyticsEvent('scope.time.pause.click', this.getTrackingMetadata());
  28. this.props.pauseTimeAtNow();
  29. }
  30. render() {
  31. const { isPaused, pausedAt, topologiesLoaded } = this.props;
  32. // If Time Travel is supported, show an empty placeholder div instead
  33. // of this control, since time will be controlled through the timeline.
  34. // We return <div /> instead of null so that selector controls would
  35. // be aligned the same way between WC Explore and Scope standalone.
  36. if (this.props.timeTravelSupported) return <div />;
  37. return (
  38. <div className="time-control">
  39. <div className="time-control-controls">
  40. <div className="time-control-wrapper">
  41. {/* title="Show live state of the system"> */}
  42. <span
  43. className={className(!isPaused)}
  44. onClick={this.handleNowClick}
  45. disabled={!topologiesLoaded}>
  46. {!isPaused && <i className="fa fa-play" />}
  47. <span className="label" style={{fontSize:"14px"}}>实时</span>
  48. </span>
  49. {/* title="Pause updates (freezes the nodes in their current layout)"> */}
  50. <span
  51. className={className(isPaused)}
  52. onClick={this.handlePauseClick}
  53. disabled={!topologiesLoaded}
  54. >
  55. {isPaused && <i className="fa fa-pause" />}
  56. {/* <span className="label">{isPaused ? 'Paused' : 'Pause'}</span> */}
  57. <span className="label" style={{fontSize:"14px"}}>{isPaused ? '已暂停' : '暂停'}</span>
  58. </span>
  59. </div>
  60. </div>
  61. {isPaused
  62. && (
  63. <span className="time-control-info" style={{fontSize:"12px"}}>
  64. 状态暂停于
  65. {' '}
  66. {moment().format("YYYY-MM-DD HH:mm:ss")}
  67. {/* <TimestampTag inheritStyles relative timestamp={pausedAt} /> */}
  68. </span>
  69. )
  70. }
  71. </div>
  72. );
  73. }
  74. }
  75. function mapStateToProps(state) {
  76. return {
  77. currentTopology: state.get('currentTopology'),
  78. isPaused: isPausedSelector(state),
  79. pausedAt: state.get('pausedAt'),
  80. timeTravelSupported: timeTravelSupportedSelector(state),
  81. topologiesLoaded: state.get('topologiesLoaded'),
  82. topologyViewMode: state.get('topologyViewMode'),
  83. };
  84. }
  85. export default connect(
  86. mapStateToProps,
  87. {
  88. pauseTimeAtNow,
  89. resumeTime,
  90. }
  91. )(TimeControl);