import React, { useEffect, useLayoutEffect, useState, useRef } from "react"; import { Link, useLocation, useParams, useNavigate } from 'react-router-dom'; import MediaPlayer from "./MediaPlayer"; import SupportModal from "./SupportModal"; import CallInfo from "./CallInfo"; import ListCalls from "./ListCalls"; import { useSelector, useDispatch } from 'react-redux' import { setLive, setFilter,setDateFilter } from "../features/callPlayer/callPlayerSlice"; import { getCalls, getOlderCalls, getNewerCalls, addCall } from "../features/calls/callsSlice"; import { useGetGroupsQuery, useGetTalkgroupsQuery } from '../features/api/apiSlice' import { useInView } from 'react-intersection-observer'; import { Container, Label, Rail, Sticky, Menu, Icon, Sidebar } from "semantic-ui-react"; import "./CallPlayer.css"; import queryString from '../query-string'; import io from 'socket.io-client'; import { useCallLink } from "./CallLinks"; const socket = io(process.env.REACT_APP_BACKEND_SERVER); // ---------------------------------------------------- function CallPlayer(props) { const { shortName } = useParams(); const loadCallId = props.loadCallId; const callsData = props.callsData; const { ref: loadOlderRef, inView: loadOlderInView } = useInView({ /* Optional options */ threshold: 0.5 }); const { ref: loadNewerRef, inView: loadNewerInView } = useInView({ /* Optional options */ threshold: 0.5 }); const { data: groupsData, isSuccess: isGroupsSuccess } = useGetGroupsQuery(shortName); const { data: talkgroupsData, isSuccess: isTalkgroupsSuccess } = useGetTalkgroupsQuery(shortName); const [autoPlay, setAutoPlay] = useState(true); const [currentCall, setCurrentCall] = useState(false); const [isPlaying, setIsPlaying] = useState(false); const {callLink,callDownload,callTweet} = useCallLink(props.call) const navigate = useNavigate(); const dispatch = useDispatch(); const positionRef = useRef(); // lets us get the Y Scroll offset for the Call List const pageYOffset = useRef(); // Store the current Scroll Offset in a way that guarantees the latest value is available when Call Data is updated const shouldPlayAddCallRef = useRef(); // we need to do this to make the current value of isPlaying available in the socket message callback shouldPlayAddCallRef.current = (!isPlaying && autoPlay)?true:false; let currentCallId = false; if (currentCall) { currentCallId = currentCall._id; } const handlePlayPause = (playing) => { setIsPlaying(playing); } const handleAutoPlay = (currentAutoPlay) => { setAutoPlay(!currentAutoPlay); } const playCall = (data) => { setCurrentCall(data.call); setIsPlaying(true); } const callEnded = () => { if (callsData) { const currentIndex = callsData.ids.findIndex(callId => callId === currentCallId); if (autoPlay && (currentIndex > 0)) { const nextCallId = callsData.ids[currentIndex - 1]; const nextCall = callsData.entities[nextCallId]; setCurrentCall(nextCall); setIsPlaying(true); } else { setIsPlaying(false); } } else { console.log("Somehow called, callEnded() but callsData was false"); } } useEffect(() => { if (loadNewerInView && callsData && (callsData.ids.length > 0)) { dispatch(getNewerCalls({})); } }, [loadNewerInView]); useEffect(() => { if (loadOlderInView && callsData && (callsData.ids.length > 0)) { dispatch(getOlderCalls({})); } }, [loadOlderInView]); useEffect(() => { if ( loadCallId && callsData && !currentCall) { const call = callsData.entities[loadCallId]; if (call) { setCurrentCall(call); } } }, [loadCallId, callsData]) useLayoutEffect( () => { const scrollAmount = parseInt(positionRef.current.clientHeight) - parseInt(pageYOffset.current); if (scrollAmount > 0) { console.log("useLayoutEffect for callsData - ref: " + pageYOffset.current + " current: " + positionRef.current.clientHeight + " Scroll Amount: " + scrollAmount) window.scrollBy(0, scrollAmount); } }, [callsData]) return (