Add iframe component

This commit is contained in:
Isha 2024-11-06 15:47:56 +05:30 committed by IshaVenikar
parent 4a9f517d85
commit dfb11c0912
2 changed files with 63 additions and 3 deletions

View File

@ -24,6 +24,7 @@ import EnvironmentVariablesForm from 'pages/org-slug/projects/id/settings/Enviro
import { EnvironmentVariablesFormValues } from 'types/types';
import ConnectWallet from './ConnectWallet';
import { useWalletConnectClient } from 'context/WalletConnectContext';
import IframeComponent from './IFrame';
type ConfigureDeploymentFormValues = {
option: string;
@ -534,10 +535,8 @@ const Configure = () => {
</div>
) : (
<>
<Heading as="h4" className="md:text-lg font-medium mb-3">
Connect to your wallet
</Heading>
<ConnectWallet onAccountChange={onAccountChange} />
<IframeComponent />
{accounts.length > 0 && (
<div>
<Button

View File

@ -0,0 +1,61 @@
import React, { useEffect, useState, useRef } from 'react';
const IframeComponent: React.FC = () => {
const [localStorageData, setLocalStorageData] = useState<string | null>(null);
const iframeRef = useRef<HTMLIFrameElement | null>(null);
useEffect(() => {
// Listen for messages from the iframe
const handleMessage = (event: MessageEvent) => {
if (event.origin !== 'https://wallet.laconic.com') return; // Ensure it's from the iframe
const { action, key, value } = event.data;
if (action === 'localStorageDataResponse' && key === 'accountIndices/cosmos:laconic-testnet-2') {
setLocalStorageData(value); // Update state with the value from localStorage
}
};
window.addEventListener('message', handleMessage);
// Cleanup listener on unmount
return () => {
window.removeEventListener('message', handleMessage);
};
}, []);
// Send a message to the iframe to get data from localStorage
const requestLocalStorageData = (key: string) => {
if (iframeRef.current && iframeRef.current.contentWindow) {
iframeRef.current.contentWindow.postMessage(
{ action: 'getLocalStorageData', key }, // Send key to request data
'https://wallet.laconic.com'
);
}
};
// Call to request localStorage data when component mounts
useEffect(() => {
requestLocalStorageData('accountIndices/cosmos:laconic-testnet-2');
}, []);
return (
<div>
<iframe
// ref={iframeRef}
id="walletIframe"
src="https://wallet.laconic.com"
width="500" height="300"
sandbox="allow-scripts allow-same-origin"
></iframe>
{localStorageData ? (
<p>LocalStorage Data: {localStorageData}</p>
) : (
<p>Loading localStorage data...</p>
)}
</div>
);
};
export default IframeComponent;