import React, { Component } from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; import debounce from 'lodash.debounce'; import { Slider } from 'stremio-common'; import styles from './styles'; class TimeSlider extends Component { constructor(props) { super(props); this.state = { time: null }; } shouldComponentUpdate(nextProps, nextState) { return nextState.time !== this.state.time || nextProps.time !== this.props.time || nextProps.duration !== this.props.duration || nextProps.className !== this.props.className; } componentWillUnmount() { this.resetTimeDebounced.cancel(); } resetTimeDebounced = debounce(() => { this.setState({ time: null }); }, 1500) onSlide = (time) => { this.resetTimeDebounced.cancel(); this.setState({ time }); } onComplete = (time) => { this.setState({ time }); this.props.setTime(time); this.resetTimeDebounced(); } onCancel = () => { this.resetTimeDebounced.cancel(); this.setState({ time: null }); } formatTime = (time) => { const hours = ('0' + Math.floor((time / (1000 * 60 * 60)) % 24)).slice(-2); const minutes = ('0' + Math.floor((time / (1000 * 60)) % 60)).slice(-2); const seconds = ('0' + Math.floor((time / 1000) % 60)).slice(-2); return `${hours}:${minutes}:${seconds}`; } renderTimeLabel() { if (this.props.time === null) { return null; } const time = this.state.time !== null ? this.state.time : this.props.time; return (
{this.formatTime(time)}
); } renderDurationLabel() { if (this.props.duration === null) { return null; } return (
{this.formatTime(this.props.duration)}
); } renderSlider() { if (this.props.time === null || this.props.duration === null) { return null; } const time = this.state.time !== null ? this.state.time : this.props.time; return ( ); } render() { if (this.props.time === null && this.props.duration === null) { return null; } return (
{this.renderTimeLabel()} {this.renderSlider()} {this.renderDurationLabel()}
); } } TimeSlider.propTypes = { className: PropTypes.string, time: PropTypes.number, duration: PropTypes.number, setTime: PropTypes.func.isRequired }; export default TimeSlider;