/* eslint-disable jsx-a11y/media-has-caption */ "use client"; import {useInView} from "framer-motion"; import {useRef, FC, useEffect, useState, useCallback} from "react"; import {Button, cn, Spinner, Tooltip} from "@heroui/react"; import {PlayBoldIcon, PauseBoldIcon} from "@/components/icons"; import {RotateLeftLinearIcon} from "@/components/icons"; interface VideoInViewProps { src: string; playMode?: "auto" | "manual"; autoPlay?: boolean; poster?: string; width?: number; height?: number; controls?: boolean; className?: string; } export const VideoInView: FC = ({ src, width, height, poster, autoPlay = true, playMode = "manual", controls = false, className, }) => { const [isLoading, setIsLoading] = useState(true); const [isPlaying, setIsPlaying] = useState(false); const videoRef = useRef(null); const isVisible = useInView(videoRef); // play video when it is visible and playMode is auto useEffect(() => { if (playMode !== "auto") { return; } if (isVisible) { videoRef.current?.play(); } else { videoRef.current?.pause(); } }, [isVisible]); const handleCanPlay = useCallback(() => { setIsLoading(false); }, []); useEffect(() => { const videoEl = videoRef.current; if (videoEl) { if (videoEl.readyState > 3) { // HAVE_FUTURE_DATA: enough data to start playing handleCanPlay(); } else { videoEl.addEventListener("canplaythrough", handleCanPlay); } // Cleanup the event listener return () => { videoEl.removeEventListener("canplaythrough", handleCanPlay); }; } }, []); const onRestart = useCallback(() => { if (videoRef.current) { videoRef.current.currentTime = 0; videoRef.current.play(); setIsPlaying(true); } }, []); const onTogglePlay = useCallback(() => { if (videoRef.current) { if (!isPlaying) { videoRef.current.play(); } else { videoRef.current.pause(); } setIsPlaying((v) => !v); } }, [isPlaying]); return (
{isLoading && ( )}
); };