import React, { useState, useEffect, useRef, AllHTMLAttributes, MutableRefObject, } from 'react'; import { useIntersection } from 'react-use'; import playroomConfig from '../../config'; interface IframeProps extends AllHTMLAttributes { src: string; intersectionRootRef: MutableRefObject; } export default function Iframe({ intersectionRootRef, style, src, ...restProps }: IframeProps) { const [loaded, setLoaded] = useState(false); const [renderedSrc, setRenderedSrc] = useState(null); const iframeRef = useRef(null); const intersection = useIntersection(iframeRef, { root: intersectionRootRef.current, rootMargin: '800px', threshold: 0, }); const intersectionRatio = intersection?.intersectionRatio ?? null; useEffect(() => { if (intersectionRatio === null) { return; } if (intersectionRatio > 0 && src !== renderedSrc) { setRenderedSrc(src); } }, [intersectionRatio, src, renderedSrc]); useEffect(() => { if (renderedSrc !== null) { const location = iframeRef.current?.contentWindow?.location; if (location) { location.replace(renderedSrc); } } }, [renderedSrc]); return (