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); const handleMouseMove = useCallback(() => { if (!moved) setMoved(true); if (timeout.current) clearTimeout(timeout.current); timeout.current = setTimeout(() => { if (moved) setMoved(false); timeout.current = null; }, 3000); }, [setMoved, moved]); const handleMouseLeave = useCallback(() => { setMoved(false); }, [setMoved]); const handleClick = useCallback( (e: React.MouseEvent) => { if (!clickareaRef.current || clickareaRef.current !== e.target) return; if (videoState.popout !== null) 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] ); const lastBackdropValue = useRef(null); useEffect(() => { const currentValue = moved || videoState.isPaused; if (currentValue !== lastBackdropValue.current) { lastBackdropValue.current = currentValue; if (!currentValue) videoState.closePopout(); props.onBackdropChange?.(currentValue); } }, [videoState, moved, props]); const showUI = moved || videoState.isPaused; return (
{props.children}
); }