Fixed AddCall Scrolling

This commit is contained in:
Luke Berndt
2023-01-16 09:40:12 -05:00
parent dfae44d861
commit 14906865a1
2 changed files with 39 additions and 35 deletions

View File

@@ -1,4 +1,4 @@
import React, { useEffect, useState, useRef } from "react"; import React, { useEffect, useLayoutEffect, useState, useRef } from "react";
import { Link, useLocation, useParams, useNavigate } from 'react-router-dom'; import { Link, useLocation, useParams, useNavigate } from 'react-router-dom';
import MediaPlayer from "./MediaPlayer"; import MediaPlayer from "./MediaPlayer";
@@ -9,7 +9,7 @@ import CalendarModal from "./CalendarModalContainer";
import CallInfo from "./CallInfo"; import CallInfo from "./CallInfo";
import ListCalls from "./ListCalls"; import ListCalls from "./ListCalls";
import { useSelector, useDispatch } from 'react-redux' import { useSelector, useDispatch } from 'react-redux'
import { setLive, setShortName, setFilter } from "../features/callPlayer/callPlayerSlice"; import { setLive, setShortName, setFilter,setDateFilter } from "../features/callPlayer/callPlayerSlice";
import { getCalls, getOlderCalls, getNewerCalls, addCall } from "../features/calls/callsSlice"; import { getCalls, getOlderCalls, getNewerCalls, addCall } from "../features/calls/callsSlice";
import { useGetGroupsQuery, useGetTalkgroupsQuery } from '../features/api/apiSlice' import { useGetGroupsQuery, useGetTalkgroupsQuery } from '../features/api/apiSlice'
import { useInView } from 'react-intersection-observer'; import { useInView } from 'react-intersection-observer';
@@ -26,7 +26,7 @@ import {
import "./CallPlayer.css"; import "./CallPlayer.css";
import queryString from '../query-string'; import queryString from '../query-string';
import io from 'socket.io-client'; import io from 'socket.io-client';
import { setDateFilter } from "./call-actions";
const socket = io(process.env.REACT_APP_BACKEND_SERVER); const socket = io(process.env.REACT_APP_BACKEND_SERVER);
@@ -45,19 +45,14 @@ function CallPlayer(props) {
threshold: 0.5 threshold: 0.5
}); });
const { data: groupsData, isSuccess: isGroupsSuccess } = useGetGroupsQuery(shortName); const { data: groupsData, isSuccess: isGroupsSuccess } = useGetGroupsQuery(shortName);
//const { data:callsData, isSuccess:isCallsSuccess } = useGetCallsQuery({shortName});
const { loading: callsLoading, data: callsData } = useSelector((state) => state.calls); const { loading: callsLoading, data: callsData } = useSelector((state) => state.calls);
//console.log(callsData);
const { data: talkgroupsData, isSuccess: isTalkgroupsSuccess } = useGetTalkgroupsQuery(shortName); const { data: talkgroupsData, isSuccess: isTalkgroupsSuccess } = useGetTalkgroupsQuery(shortName);
//const allCalls = callsData?callsData.ids.map( id => callsData.entities[id] ):[]
const [autoPlay, setAutoPlay] = useState(true); const [autoPlay, setAutoPlay] = useState(true);
const [addCallScroll, setAddCallScroll] = useState(false); const [addCallScroll, setAddCallScroll] = useState(false);
const [currentCall, setCurrentCall] = useState(false); const [currentCall, setCurrentCall] = useState(false);
const [prevScrollHeight, setPrevScrollHeight] = useState(0); const [prevScrollHeight, setPrevScrollHeight] = useState(0);
const [playTime, setPlayTime] = useState(0);
const [selectLoadCall, setSelectLoadCall] = useState(false);
const [urlOptions, setUrlOptions] = useState(false); const [urlOptions, setUrlOptions] = useState(false);
const [isPlaying, setIsPlaying] = useState(false); const [isPlaying, setIsPlaying] = useState(false);
const [sidebarOpened, setSidebarOpened] = useState(false); const [sidebarOpened, setSidebarOpened] = useState(false);
@@ -69,7 +64,8 @@ function CallPlayer(props) {
const navigate = useNavigate(); const navigate = useNavigate();
const dispatch = useDispatch(); const dispatch = useDispatch();
const positionRef = useRef(); 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 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; shouldPlayAddCallRef.current = (!isPlaying && autoPlay)?true:false;
@@ -83,11 +79,10 @@ function CallPlayer(props) {
const pathname = useLocation().pathname; const pathname = useLocation().pathname;
let currentCallId = false; let currentCallId = false;
let currentCallMedia = false;
if (currentCall) { if (currentCall) {
currentCallId = currentCall._id; currentCallId = currentCall._id;
currentCallMedia = currentCall.url;
} }
@@ -101,7 +96,9 @@ function CallPlayer(props) {
if (sidebarOpened) setSidebarOpened(false); if (sidebarOpened) setSidebarOpened(false);
} }
const handleAutoPlay = (currentAutoPlay) => {
setAutoPlay(!currentAutoPlay);
}
const handleSidebarToggle = () => setSidebarOpened(!sidebarOpened); const handleSidebarToggle = () => setSidebarOpened(!sidebarOpened);
const handleFilterToggle = () => setFilterVisible(!filterVisible); const handleFilterToggle = () => setFilterVisible(!filterVisible);
const handleCalendarToggle = () => setCalendarVisible(!calendarVisible); const handleCalendarToggle = () => setCalendarVisible(!calendarVisible);
@@ -157,15 +154,17 @@ function CallPlayer(props) {
switch (message.type) { switch (message.type) {
case 'calls': case 'calls':
const ref = positionRef.current; const ref = positionRef.current;
pageYOffset.current = positionRef.current.clientHeight;
setPrevScrollHeight(positionRef.current.clientHeight); setPrevScrollHeight(positionRef.current.clientHeight);
console.log("Prior to setAddCallScroll, addCallScroll was: " + addCallScroll);
setAddCallScroll(true); setAddCallScroll(true);
if (shouldPlayAddCallRef.current) { if (shouldPlayAddCallRef.current) {
setCurrentCall(message); setCurrentCall(message);
} }
console.log("Prior to addCall, addCallScroll was: " + addCallScroll);
dispatch(addCall(message)); dispatch(addCall(message));
console.log("Got: " + message);
console.log("Got: " + message._id);
break break
default: default:
break break
@@ -225,14 +224,14 @@ function CallPlayer(props) {
} }
const handleLiveToggle = () => { const handleLiveToggle = (currentlyLive) => {
if (!live) { if (!live) {
dispatch(setDateFilter(false)); dispatch(setDateFilter(false));
dispatch(setLive(true)); dispatch(setLive(true));
setCurrentCall(false); setCurrentCall(false);
dispatch(getCalls({})); dispatch(getCalls({}));
this.startSocket(); startSocket();
} }
} }
@@ -240,10 +239,9 @@ function CallPlayer(props) {
setCalendarVisible(!calendarVisible); setCalendarVisible(!calendarVisible);
if (didUpdate) { if (didUpdate) {
dispatchEvent(setLive(false)); dispatch(setLive(false));
setCurrentCall(false); setCurrentCall(false);
stopSocket(); stopSocket();
//this.socket.close();
} }
} }
@@ -291,7 +289,6 @@ function CallPlayer(props) {
const _id = uri['call-id']; const _id = uri['call-id'];
const date = new Date(parseInt(uri['time'])); const date = new Date(parseInt(uri['time']));
setLoadCallId(_id); setLoadCallId(_id);
setSelectLoadCall(true);
setAutoPlay(false); setAutoPlay(false);
if (!urlOptions) setUrlOptions(true); if (!urlOptions) setUrlOptions(true);
//callActions.fetchCallInfo(_id); //callActions.fetchCallInfo(_id);
@@ -341,21 +338,27 @@ function CallPlayer(props) {
}, [loadOlderInView]); }, [loadOlderInView]);
useEffect(() => { useEffect(() => {
if (addCallScroll) { if ( loadCallId && callsData) {
const ref = positionRef.current;
window.scrollBy(0, positionRef.current.clientHeight - prevScrollHeight);
setAddCallScroll(false);
}
if (selectLoadCall && loadCallId && callsData) {
const call = callsData.entities[loadCallId]; const call = callsData.entities[loadCallId];
if (call) { if (call) {
setSelectLoadCall(false);
setCurrentCall(call); setCurrentCall(call);
} }
} }
}, [callsData]) }, [callsData])
useLayoutEffect( () => {
const scrollAmount = parseInt(positionRef.current.clientHeight) - parseInt(pageYOffset.current);
if (scrollAmount > 0) {
console.log("useLayoutEffect for callsData - state prevScrollHeight: " + prevScrollHeight + " ref: " + pageYOffset.current + " current: " + positionRef.current.clientHeight + " Scroll Amount: " + scrollAmount)
window.scrollBy(0, scrollAmount);
}
}, [callsData])
/*
useEffect(() => {
}, [callsData]);
*/
useEffect(() => { useEffect(() => {
setStateFromUri(); setStateFromUri();
return () => { return () => {
@@ -510,7 +513,7 @@ function CallPlayer(props) {
style={{ minHeight: '100vh' }} style={{ minHeight: '100vh' }}
> >
<div ref={loadNewerRef} /> <div ref={loadNewerRef} />
<ListCalls callsData={callsData} activeCallId={currentCallId} talkgroups={talkgroupsData} playCall={playCall} /> <ListCalls callsData={callsData} activeCallId={currentCallId} talkgroups={talkgroupsData?talkgroupsData.talkgroups:false} playCall={playCall} />
<div ref={loadOlderRef} style={{ height: 50 }} /> <div ref={loadOlderRef} style={{ height: 50 }} />
@@ -524,7 +527,7 @@ function CallPlayer(props) {
</Container> </Container>
<Menu fixed="bottom" compact inverted > <Menu fixed="bottom" compact inverted >
<Menu.Item active={autoPlay} onClick={() => setAutoPlay(!autoPlay)}><Icon name="level up" /><span className="desktop-only">Autoplay</span></Menu.Item> <Menu.Item active={autoPlay} onClick={() => handleAutoPlay(autoPlay)}><Icon name="level up" /><span className="desktop-only">Autoplay</span></Menu.Item>
<MediaPlayer call={currentCall} onEnded={callEnded} onPlayPause={handlePlayPause} /> <MediaPlayer call={currentCall} onEnded={callEnded} onPlayPause={handlePlayPause} />
<Menu.Menu position="right" className="desktop-only"> <Menu.Menu position="right" className="desktop-only">
<Menu.Item><SupportModal /></Menu.Item> <Menu.Item><SupportModal /></Menu.Item>

View File

@@ -66,6 +66,7 @@ const initialState = {
data: callsAdapter.getInitialState(), data: callsAdapter.getInitialState(),
oldestCallTime: now, oldestCallTime: now,
newestCallTime: now, newestCallTime: now,
adjustCallScroll: false
} }
function buildUrlParams(a) { function buildUrlParams(a) {
@@ -201,9 +202,9 @@ export const callsSlice = createSlice({
name: 'calls', name: 'calls',
initialState, initialState,
reducers: { reducers: {
addCall: (state, action) => { addCall: (state, action) => {
state.data = callsAdapter.addOne(state.data, action.payload) state.data = callsAdapter.addOne(state.data, action.payload)
state.adjustCallScroll = true;
}, },
}, },
extraReducers: { extraReducers: {
@@ -219,7 +220,7 @@ export const callsSlice = createSlice({
if (state.data && (state.data.ids.length > 0)) { if (state.data && (state.data.ids.length > 0)) {
const first = state.data.ids[0] const first = state.data.ids[0]
const firstTime = state.data.entities[first].time; const firstTime = state.data.entities[first].time;
const last = state.data.ids[state.data.ids[0].length-1]; const last = state.data.ids[state.data.ids.length-1];
const lastTime = state.data.entities[last].time; const lastTime = state.data.entities[last].time;
state.newestCallTime = new Date(firstTime) state.newestCallTime = new Date(firstTime)
state.oldestCallTime = new Date(lastTime) state.oldestCallTime = new Date(lastTime)
@@ -237,7 +238,7 @@ export const callsSlice = createSlice({
if (state.data && (state.data.ids.length > 0)) { if (state.data && (state.data.ids.length > 0)) {
const first = state.data.ids[0] const first = state.data.ids[0]
const firstTime = state.data.entities[first].time; const firstTime = state.data.entities[first].time;
const last = state.data.ids[state.data.ids[0].length-1]; const last = state.data.ids[state.data.ids.length-1];
const lastTime = state.data.entities[last].time; const lastTime = state.data.entities[last].time;
state.newestCallTime = new Date(firstTime) state.newestCallTime = new Date(firstTime)
state.oldestCallTime = new Date(lastTime) state.oldestCallTime = new Date(lastTime)
@@ -255,7 +256,7 @@ export const callsSlice = createSlice({
if (state.data && (state.data.ids.length > 0)) { if (state.data && (state.data.ids.length > 0)) {
const first = state.data.ids[0] const first = state.data.ids[0]
const firstTime = state.data.entities[first].time; const firstTime = state.data.entities[first].time;
const last = state.data.ids[state.data.ids[0].length-1]; const last = state.data.ids[state.data.ids.length-1];
const lastTime = state.data.entities[last].time; const lastTime = state.data.entities[last].time;
state.newestCallTime = new Date(firstTime) state.newestCallTime = new Date(firstTime)
state.oldestCallTime = new Date(lastTime) state.oldestCallTime = new Date(lastTime)