From fa95a041dd6139d2bd965e086e3206c277eaeebb Mon Sep 17 00:00:00 2001 From: JohnMark Sill Date: Thu, 2 Jun 2022 14:31:20 -0500 Subject: [PATCH] fix: height of video is now constant in history viewer --- .../components/HistoryViewer/HistoryVideo.tsx | 42 +++++++++---------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/web/src/components/HistoryViewer/HistoryVideo.tsx b/web/src/components/HistoryViewer/HistoryVideo.tsx index d5ffac538..5880508e8 100644 --- a/web/src/components/HistoryViewer/HistoryVideo.tsx +++ b/web/src/components/HistoryViewer/HistoryVideo.tsx @@ -11,7 +11,6 @@ interface OnTimeUpdateEvent { interface VideoProperties { posterUrl: string; videoUrl: string; - height: number; } interface HistoryVideoProps { @@ -32,21 +31,21 @@ export const HistoryVideo = ({ onPlay, }: HistoryVideoProps) => { const apiHost = useApiHost(); - const videoRef = useRef(null); - const [videoHeight, setVideoHeight] = useState(0); + const videoRef = useRef(null); + + const [posterLoaded, setPosterLoaded] = useState(false); + const [videoHeight, setVideoHeight] = useState(undefined); + const [videoProperties, setVideoProperties] = useState({ posterUrl: '', videoUrl: '', - height: 0, }); - const currentVideo = videoRef.current; - if (currentVideo && !videoHeight) { - const currentVideoHeight = currentVideo.offsetHeight; - if (currentVideoHeight > 0) { - setVideoHeight(currentVideoHeight); + useEffect(() => { + if (posterLoaded && videoRef.current) { + setVideoHeight(videoRef.current?.offsetHeight); } - } + }, [posterLoaded, videoRef.current]); useEffect(() => { const idExists = !isNullOrUndefined(id); @@ -55,16 +54,20 @@ export const HistoryVideo = ({ videoRef.current = null; } + const posterUrl = `${apiHost}/api/events/${id}/snapshot.jpg`; + const poster = new Image(); + poster.src = posterUrl; + poster.onload = () => { + setPosterLoaded(true); + }; setVideoProperties({ - posterUrl: `${apiHost}/api/events/${id}/snapshot.jpg`, + posterUrl, videoUrl: `${apiHost}/vod/event/${id}/index.m3u8`, - height: videoHeight, }); } else { setVideoProperties({ posterUrl: '', videoUrl: '', - height: 0, }); } }, [id, videoHeight, videoRef, apiHost]); @@ -117,12 +120,7 @@ export const HistoryVideo = ({ [videoIsPlaying, onTimeUpdate] ); - const videoPropertiesIsUndefined = isNullOrUndefined(videoProperties); - if (videoPropertiesIsUndefined) { - return
; - } - - const { posterUrl, videoUrl, height } = videoProperties; + const { posterUrl, videoUrl } = videoProperties; return ( ); };