From 58ec6792d9dbdfe948d27ec624fe27cfaff2984b Mon Sep 17 00:00:00 2001 From: Princi Vershwal Date: Wed, 24 Jan 2024 21:22:20 +0530 Subject: [PATCH] Trying to fix browser tests (#19575) --- .../settings/membership/portal/PortalFrame.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/apps/admin-x-settings/src/components/settings/membership/portal/PortalFrame.tsx b/apps/admin-x-settings/src/components/settings/membership/portal/PortalFrame.tsx index d2da516acb..c0ffd5aeb2 100644 --- a/apps/admin-x-settings/src/components/settings/membership/portal/PortalFrame.tsx +++ b/apps/admin-x-settings/src/components/settings/membership/portal/PortalFrame.tsx @@ -6,6 +6,7 @@ type PortalFrameProps = { onDestroyed?: () => void; selectedTab?: string; } + const PortalFrame: React.FC = ({href, onDestroyed, selectedTab}) => { if (!selectedTab) { selectedTab = 'signup'; @@ -13,6 +14,7 @@ const PortalFrame: React.FC = ({href, onDestroyed, selectedTab const iframeRef = useRef(null); const [hasLoaded, setHasLoaded] = useState(false); const [isInvisible, setIsInvisible] = useState(true); + const makeVisible = useCallback(() => { setTimeout(() => { if (iframeRef.current) { @@ -20,24 +22,29 @@ const PortalFrame: React.FC = ({href, onDestroyed, selectedTab } }, 300); }, [iframeRef]); + useEffect(() => { const messageListener = (event: MessageEvent) => { if (!href) { return; } const originURL = new URL(event.origin); + if (originURL.origin === new URL(href).origin) { if (event?.data?.type === 'portal-preview-ready') { makeVisible(); } } }; + window.addEventListener('message', messageListener, true); + return () => { window.removeEventListener('message', messageListener, true); onDestroyed?.(); }; }, [href, onDestroyed, makeVisible, hasLoaded]); + if (!href) { return null; } @@ -60,4 +67,4 @@ const PortalFrame: React.FC = ({href, onDestroyed, selectedTab ); }; -export default PortalFrame; \ No newline at end of file +export default PortalFrame;