about summary refs log tree commit diff
path: root/src/view/com/util/post-embeds/VideoPlayerContext.tsx
blob: 473343ca4be7326afb0423a2a71fbf8c087ef137 (plain) (blame)
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
import React, {useContext} from 'react'
import type {VideoPlayer} from 'expo-video'
import {useVideoPlayer as useExpoVideoPlayer} from 'expo-video'

const VideoPlayerContext = React.createContext<VideoPlayer | null>(null)

export function VideoPlayerProvider({
  source,
  children,
}: {
  source: string
  children: React.ReactNode
}) {
  // eslint-disable-next-line @typescript-eslint/no-shadow
  const player = useExpoVideoPlayer(source, player => {
    player.loop = true
    player.play()
  })

  return (
    <VideoPlayerContext.Provider value={player}>
      {children}
    </VideoPlayerContext.Provider>
  )
}

export function useVideoPlayer() {
  const context = useContext(VideoPlayerContext)
  if (!context) {
    throw new Error('useVideoPlayer must be used within a VideoPlayerProvider')
  }
  return context
}