import * as React from 'react' import * as q from '../../../../backend/src/Model' import CustomIconButton from '../helper/CustomIconButton' import Pause from '@material-ui/icons/PauseCircleFilled' import Resume from '@material-ui/icons/PlayCircleFilled' import { AppState } from '../../reducers' import { bindActionCreators } from 'redux' import { connect } from 'react-redux' import { treeActions } from '../../actions' import { StyleRulesCallback, withStyles } from '@material-ui/core/styles' import { Tooltip } from '@material-ui/core' const styles: StyleRulesCallback = theme => ({ icon: { color: theme.palette.primary.contrastText, verticalAlign: 'middle' as 'middle', }, bufferStats: { minWidth: '8em', }, }) interface Props { classes: any actions: { tree: typeof treeActions, } paused: boolean tree?: q.Tree } class PauseButton extends React.Component { private timer?: any constructor(props: Props) { super(props) this.state = { changes: 0 } } private renderBufferStats() { if (!this.props.tree) { return } return ( {this.state.changes} changes
buffer at {Math.round(this.props.tree.unmergedChanges().fillState() * 10000) / 100}%
) } public componentDidMount() { this.timer = setInterval(() => { if (!this.props.paused || !this.props.tree) { return } const changes = this.props.tree.unmergedChanges() if (this.state.changes !== changes.length) { this.setState({ changes: changes.length }) } }, 300) } public componentWillUnmount() { this.timer && clearInterval(this.timer) } public render() { const message = this.props.paused ? 'Resumes updating the tree, after applying all recorded changes' : 'Stops all updates, records changes until the buffer is full.' return (
{this.props.paused ? : }
{this.props.paused ? this.renderBufferStats() : null}
) } } const mapStateToProps = (state: AppState) => { return { paused: state.tree.get('paused'), tree: state.tree.get('tree'), } } const mapDispatchToProps = (dispatch: any) => { return { actions: { tree: bindActionCreators(treeActions, dispatch), }, } } export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(PauseButton))