import React, { useEffect, useRef, useState } from "react"; import YouTube from "react-youtube"; import styles from "./styles/CarouselSliders.module.css"; const CarouselSliders = (props) => { const [activeVideoID, setActiveVideoID] = useState(null); const players = useRef({}); const activeVid = props.matches.length ? props.matches[0].YouTubeID : ""; useEffect(() => { setActiveVideoID(activeVid); }, [props.matches]); const onReady = (event, videoId) => { players.current[videoId] = event.target; }; const onPlay = (event) => { const videoId = event.target.getVideoData().video_id; setActiveVideoID(videoId); // Pause other videos Object.values(players.current).forEach((player) => { const otherVideoId = player.getVideoData().video_id; if ( otherVideoId !== videoId && player.getPlayerState() === 1 /* Playing */ ) { player.pauseVideo(); } }); }; return ( <>
{!props.matches.length ? null : (
{props.matches.map((match, index) => { const start = (parseInt(match.Timestamp) / 1000) | 0; return (
onReady(event, match.YouTubeID)} onPlay={onPlay} />
); })}
)}
{props.matches.map((match, _) => { return ( { setActiveVideoID(match.YouTubeID); }} > ); })}
); }; export default CarouselSliders;