import { useRef, useState, useEffect } from 'react'; import { FormattedMessage } from 'react-intl'; import { showAlertForError } from 'mastodon/actions/alerts'; import api from 'mastodon/api'; import { Button } from 'mastodon/components/button'; import { CopyPasteText } from 'mastodon/components/copy_paste_text'; import { useAppDispatch } from 'mastodon/store'; interface OEmbedResponse { html: string; } const EmbedModal: React.FC<{ id: string; onClose: () => void; }> = ({ id, onClose }) => { const iframeRef = useRef(null); const intervalRef = useRef>(); const [oembed, setOembed] = useState(null); const dispatch = useAppDispatch(); useEffect(() => { api() .get(`/api/web/embeds/${id}`) .then((res) => { const data = res.data as OEmbedResponse; setOembed(data); const iframeDocument = iframeRef.current?.contentWindow?.document; if (!iframeDocument) { return ''; } iframeDocument.open(); iframeDocument.write(data.html); iframeDocument.close(); iframeDocument.body.style.margin = '0px'; // This is our best chance to ensure the parent iframe has the correct height... intervalRef.current = setInterval( () => window.requestAnimationFrame(() => { if (iframeRef.current) { iframeRef.current.width = `${iframeDocument.body.scrollWidth}px`; iframeRef.current.height = `${iframeDocument.body.scrollHeight}px`; } }), 100, ); return ''; }) .catch((error: unknown) => { dispatch(showAlertForError(error)); }); }, [dispatch, id, setOembed]); useEffect( () => () => { if (intervalRef.current) { clearInterval(intervalRef.current); } }, [], ); return (