import { useEffect, useRef } from 'react' import Hls from 'hls.js' import { injectGlobal } from '@emotion/css' import { isIOS, isSafari } from '@/web/utils/common' import { motion } from 'framer-motion' import { useSnapshot } from 'valtio' import uiStates from '../states/uiStates' const VideoCover = ({ source, onPlay }: { source?: string; onPlay?: () => void }) => { const videoRef = useRef(null) const hls = useRef() useEffect(() => { if (source && Hls.isSupported() && videoRef.current) { if (hls.current) hls.current.destroy() hls.current = new Hls() hls.current.loadSource(source) hls.current.attachMedia(videoRef.current) } return () => hls.current && hls.current.destroy() }, [source]) // Pause video cover when playing another video const { playingVideoID } = useSnapshot(uiStates) useEffect(() => { if (playingVideoID) { videoRef?.current?.pause() } else { videoRef?.current?.play() } }, [playingVideoID]) return ( {isSafari ? ( ) : (
)}
) } export default VideoCover