diff --git a/web/src/components/HistoryViewer/HistoryVideo.tsx b/web/src/components/HistoryViewer/HistoryVideo.tsx index f5dcb6fbd..23aadebd9 100644 --- a/web/src/components/HistoryViewer/HistoryVideo.tsx +++ b/web/src/components/HistoryViewer/HistoryVideo.tsx @@ -1,5 +1,5 @@ import { h } from 'preact'; -import { useCallback, useEffect, useMemo, useRef, useState } from 'preact/hooks'; +import { useCallback, useEffect, useRef, useState } from 'preact/hooks'; import { useApiHost } from '../../api'; interface OnTimeUpdateEvent { @@ -66,23 +66,40 @@ export const HistoryVideo = ({ } }, [id, videoHeight]); - const playVideo = (video: HTMLMediaElement) => { - const videoHasNotLoaded = video.readyState <= 1; - if (videoHasNotLoaded) { - video.load(); - } - - video.play().catch((e) => { - console.error('Fail', e); - }); - }; - useEffect(() => { + const playVideo = (video: HTMLMediaElement) => { + console.debug('playVideo: attempt playback'); + video + .play() + .then(() => { + console.debug('playVideo: video started'); + }) + .catch((e) => { + console.error('Fail', { e }); + }); + }; + + const attemptPlayVideo = (video: HTMLMediaElement) => { + const videoHasNotLoaded = video.readyState <= 1; + console.debug('playVideo', { videoHasNotLoaded }); + if (videoHasNotLoaded) { + console.debug('playVideo: attempt to load video'); + video.oncanplay = () => { + console.debug('onLoad: video loaded'); + playVideo(video); + }; + video.load(); + } else { + playVideo(video); + } + }; + const video = videoRef.current; const videoExists = !isNullOrUndefined(video); if (videoExists) { + console.log('check should start', { videoIsPlaying }); if (videoIsPlaying) { - playVideo(video); + attemptPlayVideo(video); } else { video.pause(); } @@ -110,28 +127,24 @@ export const HistoryVideo = ({ [videoIsPlaying] ); - const Video = useCallback(() => { - const videoPropertiesIsUndefined = isNullOrUndefined(videoProperties); - if (videoPropertiesIsUndefined) { - return
; - } - const { posterUrl, videoUrl, height } = videoProperties; - return ( - - ); - }, [videoProperties, videoHeight, videoRef]); - - return ; + const videoPropertiesIsUndefined = isNullOrUndefined(videoProperties); + if (videoPropertiesIsUndefined) { + return ; + } + const { posterUrl, videoUrl, height } = videoProperties; + return ( + + ); }; diff --git a/web/src/components/HistoryViewer/HistoryViewer.tsx b/web/src/components/HistoryViewer/HistoryViewer.tsx index e55930c99..f1e5ef173 100644 --- a/web/src/components/HistoryViewer/HistoryViewer.tsx +++ b/web/src/components/HistoryViewer/HistoryViewer.tsx @@ -2,12 +2,12 @@ import { Fragment, h } from 'preact'; import { useEffect, useState } from 'preact/hooks'; import { useEvents } from '../../api'; import { useSearchString } from '../../hooks/useSearchString'; -import { HistoryHeader } from './HistoryHeader'; -import Timeline from '../Timeline/Timeline'; -import { HistoryVideo } from './HistoryVideo'; -import { TimelineEvent } from '../Timeline/TimelineEvent'; -import { TimelineChangeEvent } from '../Timeline/TimelineChangeEvent'; import { getNowYesterdayInLong } from '../../utils/dateUtil'; +import Timeline from '../Timeline/Timeline'; +import { TimelineChangeEvent } from '../Timeline/TimelineChangeEvent'; +import { TimelineEvent } from '../Timeline/TimelineEvent'; +import { HistoryHeader } from './HistoryHeader'; +import { HistoryVideo } from './HistoryVideo'; export default function HistoryViewer({ camera }) { const { searchString } = useSearchString(500, `camera=${camera}&after=${getNowYesterdayInLong()}`); @@ -38,10 +38,6 @@ export default function HistoryViewer({ camera }) { } }; - const handlePlay = () => { - setIsPlaying((previous) => !previous); - }; - const onPlayHandler = () => { setIsPlaying(true); }; @@ -50,9 +46,10 @@ export default function HistoryViewer({ camera }) { setIsPlaying(false); }; - const handlePrevious = () => {}; - - const handleNext = () => {}; + const onPlayPauseHandler = (isPlaying: boolean) => { + console.debug('onPlayPauseHandler: setting isPlaying', { isPlaying }); + setIsPlaying(isPlaying); + }; return (