summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/views/viewer/player/player.container.js
blob: a2f999d635a5e4e3a6ca2f36654311eb1de468aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import React, { Component } from 'react'
import { connect } from 'react-redux'

import actions from 'app/actions'
import { floatInRange } from 'app/utils'

import PlayerTranscript from './player.transcript'
import PlayerFullscreen from './player.fullscreen'

class PlayerContainer extends Component {
  componentDidMount() {
    // console.log(this.props.sections)
    const { sections } = this.props
    actions.viewer.setCurrentSection(sections[0], sections[1])
  }

  componentDidUpdate(prevProps) {
    if (this.props.audio.play_ts === prevProps.audio.play_ts) return
    this.setCurrentSection()
  }

  setCurrentSection() {
    const { audio, sections, currentSection, autoAdvance } = this.props
    const { play_ts } = audio
    if (floatInRange(currentSection.start_ts, play_ts, currentSection.end_ts)) {
      return
    }

    if (autoAdvance) {
      const insideSection = sections.some((section, i) => {
        if (floatInRange(section.start_ts, play_ts, section.end_ts)) {
          if (currentSection !== section) {
            const nextSection = sections[i+1]
            actions.viewer.setCurrentSection(section, nextSection)
          }
          return true
        }
        return false
      })
      if (!insideSection) {
        actions.viewer.setCurrentSection(sections[sections.length-1], null)
      }
    } else {
      actions.viewer.reachedEndOfSection()
    }
  }
  
  render() {
    // const { } = this.props
    const { currentSection } = this.props
    if (!currentSection) { return <div /> }
    // console.log(currentSection)
    return (
      <div className='viewer-container'>
        <PlayerTranscript section={currentSection} />
        <PlayerFullscreen />
      </div>
    )
  }
}

const mapStateToProps = state => ({
  audio: state.audio,
  sections: state.viewer.sections,
  currentSection: state.viewer.currentSection,
  autoAdvance: state.viewer.autoAdvance,
})

export default connect(mapStateToProps)(PlayerContainer)