import React, { Component } from 'react' import { connect } from 'react-redux' import actions from 'app/actions' import { timestampToSeconds, floatInRange } from 'app/utils' import { parseSubtitles } from 'app/utils/transcript.utils' export default class VideoSubtitles extends Component { state = { subtitles: [], current: null, } componentDidMount() { this.loadSubtitles() } componentDidUpdate(prevProps) { if (this.props.play_ts !== prevProps.play_ts) { this.updateCurrentSubtitle() } } loadSubtitles() { const subtitles = parseSubtitles(this.props.mediaItem, 0) if (subtitles) { this.setState({ subtitles, current: null }) } } updateCurrentSubtitle() { const { play_ts } = this.props const current = this.state.subtitles.filter(({ start_ts, end_ts }) => ( floatInRange(start_ts, play_ts, end_ts ) )).slice(-1) if (!current.length) { this.setState({ current: null }) } else { this.setState({ current: current[0] }) } } render() { const { cc } = this.props const { current } = this.state if (!cc || !current) return
return (
{current.lines.join(" ")}
) } }