"use client"; import { FC, useEffect, useContext, useRef, useState } from "react"; import { VideoContext, VideoControls } from "./"; import { useDimensions } from "../../../../hooks"; import ReactPlayer from "react-player/lazy"; const VideoPlayer: FC = ({ isInline = true }: any) => { const { data, fullViewer, inlineViewer, isFullscreen, isMuted, isPlaying, onAutoPlayStarted, onPlayerReady, setFullViewer, setIsMuted, setIsPlaying, setInit, setInlineViewer, wrapper, } = useContext(VideoContext); const { width, height } = useDimensions(wrapper); const [progress, setProgress] = useState(null); const [duration, setDuration] = useState(null); const [playerDimensions, setPlayerDimensions] = useState({ vidWidth: 640, vidHeight: 390, aspect: 1, container: null, }); const vidWrapper = wrapper.current; const playerRef = useRef(null); let vidSrc = data?.src; const vidType = data?.type; if (vidType === "vimeo") vidSrc = `https://vimeo.com/${vidSrc}`; if (vidType === "youtube") vidSrc = `https://www.youtube.com/watch?v=${data?.vidSrc}`; useEffect(() => { handleResize(); }, [vidWrapper, width, height, inlineViewer]); const handleResize = () => { if (vidWrapper) { const { aspect } = playerDimensions; const vidHolder = vidWrapper.querySelector("#backgroundPlayer"); let containerWidth = width; let containerHeight = width * 10; if (height * aspect > width) { containerWidth = height * 10; containerHeight = height; } if (vidHolder) { // + 2 gives a pixel grace either side. vidHolder.style.width = `${Math.ceil(containerWidth + 2)}px`; vidHolder.style.height = `${Math.ceil(containerHeight + 2)}px`; } } }; const handleReady = () => { onPlayerReady && onPlayerReady(); if (vidWrapper && isInline) { const container: any = vidWrapper.getElementsByTagName("iframe")[0]; const vidWidth = container?.width; const vidHeight = container?.height; setPlayerDimensions({ vidWidth, vidHeight, aspect: vidWidth / vidHeight, container, }); if (container) { setInlineViewer(container); if (data?.autoPlay) { onAutoPlayStarted && onAutoPlayStarted(); setInit(false); setIsPlaying(true); } } } else { const fullPlayer = document.getElementById("fullPlayer"); const container = fullPlayer?.getElementsByTagName("iframe")[0]; setFullViewer(container); } }; const updateProgress = (progress: any) => { if (!isInline) { setProgress(progress); } }; const getDuration = (duration: any) => { if (!isInline) { setDuration(duration); } }; const togglePlay = (e: any) => { e.stopPropagation(); isInline ? (inlineViewer.playing = !isPlaying) : (fullViewer.playing = !isPlaying); setIsPlaying(!isPlaying); }; const toggleMute = (e: any) => { e.stopPropagation(); setIsMuted(!isMuted); }; const skipRender = !vidSrc || (isFullscreen && isInline); return skipRender ? null : ( <> ); }; export default VideoPlayer;