From cd92488362de633043e5d43f9411817a97353396 Mon Sep 17 00:00:00 2001 From: Adw8 Date: Wed, 6 Nov 2024 16:15:12 +0530 Subject: [PATCH] Fetch account from wallet using iframe message --- .../frontend/src/pages/org-slug/index.tsx | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/packages/frontend/src/pages/org-slug/index.tsx b/packages/frontend/src/pages/org-slug/index.tsx index a7125b79..a496998b 100644 --- a/packages/frontend/src/pages/org-slug/index.tsx +++ b/packages/frontend/src/pages/org-slug/index.tsx @@ -23,6 +23,48 @@ const Projects = () => { fetchProjects(); }, [orgSlug]); + useEffect(() => { + const getDataFromWallet = () => { + const iframe = document.getElementById('walletIframe') as HTMLIFrameElement; + + if (!iframe.contentWindow) { + console.error('Iframe not found or not loaded'); + return; + } + + // Request data from wallet + iframe.contentWindow.postMessage({ + type: 'REQUEST_WALLET_ACCOUNTS' + }, 'http://localhost:3001'); + }; + + // Listen for response from wallet + const handleMessage = (event: MessageEvent) => { + // Always verify origin for security + if (event.origin !== 'http://localhost:3001') return; + + if (event.data.type === 'WALLET_ACCOUNTS_DATA') { + console.log('Received data:', event.data.data); + // Handle the received data here + } else if (event.data.type === 'ERROR') { + console.error('Error from wallet:', event.data.message); + } + }; + + window.addEventListener('message', handleMessage); + + // Request data once iframe is loaded + const iframe = document.getElementById('walletIframe'); + if (iframe) { + iframe.onload = getDataFromWallet; + } + + // Cleanup + return () => { + window.removeEventListener('message', handleMessage); + }; + }, []); + return (
{/* Header */} @@ -49,6 +91,13 @@ const Projects = () => { return ; })} +