All files / atom/videoPlayer/src/components YouTubePlayer.js

90% Statements 9/10
100% Branches 9/9
66.66% Functions 2/3
90% Lines 9/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 70                      1x         10x   10x 10x                 10x         10x               10x 10x                             1x                        
import {forwardRef, useEffect} from 'react'
 
import PropTypes from 'prop-types'
 
import {toQueryString} from '@s-ui/js/lib/string'
 
import useImperativeApi from '../hooks/useImperativeApi.js'
import useYouTubeProperties from '../hooks/youtube/useYouTubeProperties.js'
import {BASE_CLASS, YOUTUBE_DEFAULT_TITLE} from '../settings/index.js'
import {YOUTUBE} from '../settings/players.js'
 
const YouTubePlayer = forwardRef(
  (
    {autoPlay, controls, muted, onLoadVideo, src, timeLimit, timeOffset, title = YOUTUBE_DEFAULT_TITLE},
    forwardedRef
  ) => {
    const {getEmbeddableUrl} = useYouTubeProperties()
 
    useEffect(() => {
      onLoadVideo({
        src,
        type: YOUTUBE.VIDEO_TYPE,
        duration: null,
        videoWidth: null,
        videoHeight: null
      })
    }, [onLoadVideo, src])
 
    useImperativeApi({
      ref: forwardedRef,
      getCurrentTime: () => Promise.resolve(null)
    })
 
    const params = toQueryString({
      autoplay: autoPlay ? '1' : '0',
      controls: controls ? '1' : '0',
      start: timeOffset || '0',
      end: timeLimit,
      mute: muted ? '1' : '0'
    })
 
    const videoSrc = `${getEmbeddableUrl(src)}?${params}`
    return (
      <div className={`${BASE_CLASS}-youtubePlayer`}>
        <iframe
          className={`${BASE_CLASS}-youtubePlayerFrame`}
          src={videoSrc}
          title={title}
          frameBorder="0"
          allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
          allowFullScreen
        />
      </div>
    )
  }
)
 
YouTubePlayer.propTypes = {
  autoPlay: PropTypes.bool,
  controls: PropTypes.bool,
  muted: PropTypes.bool,
  onLoadVideo: PropTypes.func,
  timeLimit: PropTypes.number,
  timeOffset: PropTypes.number,
  src: PropTypes.string,
  title: PropTypes.string
}
 
export default YouTubePlayer