import React from 'react'; import moment from 'moment'; import classNames from 'classnames'; import { connect } from 'react-redux'; import { trackAnalyticsEvent } from '../utils/tracking-utils'; import { pauseTimeAtNow, resumeTime, startTimeTravel } from '../actions/app-actions'; const className = isSelected => ( classNames('time-control-action', { 'time-control-action-selected': isSelected }) ); class TimeControl extends React.Component { constructor(props, context) { super(props, context); this.handleNowClick = this.handleNowClick.bind(this); this.handlePauseClick = this.handlePauseClick.bind(this); this.handleTravelClick = this.handleTravelClick.bind(this); this.getTrackingMetadata = this.getTrackingMetadata.bind(this); } componentDidMount() { // Force periodic updates every one second for the paused info. this.timer = setInterval(() => { this.forceUpdate(); }, 1000); } componentWillUnmount() { clearInterval(this.timer); } getTrackingMetadata(data = {}) { const { currentTopology } = this.props; return { layout: this.props.topologyViewMode, topologyId: currentTopology && currentTopology.get('id'), parentTopologyId: currentTopology && currentTopology.get('parentId'), ...data }; } handleNowClick() { trackAnalyticsEvent('scope.time.resume.click', this.getTrackingMetadata()); this.props.resumeTime(); } handlePauseClick() { trackAnalyticsEvent('scope.time.pause.click', this.getTrackingMetadata()); this.props.pauseTimeAtNow(); } handleTravelClick() { if (!this.props.showingTimeTravel) { trackAnalyticsEvent('scope.time.travel.click', this.getTrackingMetadata({ open: true })); this.props.startTimeTravel(); } else { trackAnalyticsEvent('scope.time.travel.click', this.getTrackingMetadata({ open: false })); this.props.resumeTime(); } } render() { const { showingTimeTravel, pausedAt, timeTravelTransitioning, topologiesLoaded, hasHistoricReports } = this.props; const isPausedNow = pausedAt && !showingTimeTravel; const isTimeTravelling = showingTimeTravel; const isRunningNow = !pausedAt; if (!topologiesLoaded) return null; return (
{timeTravelTransitioning && }
{isRunningNow && } Live {isPausedNow && } {isPausedNow ? 'Paused' : 'Pause'} {hasHistoricReports && {isTimeTravelling && } Time Travel }
{(isPausedNow || isTimeTravelling) && Showing state from {moment(pausedAt).fromNow()} } {isRunningNow && timeTravelTransitioning && Resuming the live state }
); } } function mapStateToProps(state) { return { hasHistoricReports: state.getIn(['capabilities', 'historic_reports']), topologyViewMode: state.get('topologyViewMode'), topologiesLoaded: state.get('topologiesLoaded'), currentTopology: state.get('currentTopology'), showingTimeTravel: state.get('showingTimeTravel'), timeTravelTransitioning: state.get('timeTravelTransitioning'), pausedAt: state.get('pausedAt'), }; } export default connect( mapStateToProps, { resumeTime, pauseTimeAtNow, startTimeTravel, } )(TimeControl);