import React, { useCallback, useEffect, useRef, useState } from "react"; import { useVideoPlayerState } from "../VideoContext"; interface BackdropControlProps { children?: React.ReactNode; onBackdropChange?: (showing: boolean) => void; } export function BackdropControl(props: BackdropControlProps) { const { videoState } = useVideoPlayerState(); const [moved, setMoved] = useState(false); const timeout = useRef | null>(null); const clickareaRef = useRef(null); // TODO fix infinite loop const handleMouseMove = useCallback(() => { setMoved(true); if (timeout.current) clearTimeout(timeout.current); timeout.current = setTimeout(() => { setMoved(false); timeout.current = null; }, 3000); }, [timeout, setMoved]); const handleMouseLeave = useCallback(() => { setMoved(false); }, [setMoved]); const handleClick = useCallback( (e: React.MouseEvent) => { if (!clickareaRef.current || clickareaRef.current !== e.target) return; if (videoState.isPlaying) videoState.pause(); else videoState.play(); }, [videoState, clickareaRef] ); const handleDoubleClick = useCallback( (e: React.MouseEvent) => { if (!clickareaRef.current || clickareaRef.current !== e.target) return; if (!videoState.isFullscreen) videoState.enterFullscreen(); else videoState.exitFullscreen(); }, [videoState, clickareaRef] ); useEffect(() => { props.onBackdropChange?.(moved || videoState.isPaused); }, [videoState, moved, props]); const showUI = moved || videoState.isPaused; return (
{props.children}
); }