Add iframe to add accounts
This commit is contained in:
parent
d5784a40d1
commit
a1da25e513
@ -43,6 +43,7 @@ import useAccountsData from "./hooks/useAccountsData";
|
||||
import { useWebViewHandler } from "./hooks/useWebViewHandler";
|
||||
import SignMessageEmbed from "./screens/SignMessageEmbed";
|
||||
import ExportPKEmbed from "./screens/ExportPKEmbed";
|
||||
import { AddAccountEmbed } from "./screens/AddAccountEmbed";
|
||||
|
||||
const Stack = createStackNavigator<StackParamsList>();
|
||||
|
||||
@ -392,6 +393,13 @@ const App = (): React.JSX.Element => {
|
||||
header: () => <></>,
|
||||
}}
|
||||
/>
|
||||
<Stack.Screen
|
||||
name="add-account-embed"
|
||||
component={AddAccountEmbed}
|
||||
options={{
|
||||
header: () => <></>,
|
||||
}}
|
||||
/>
|
||||
<Stack.Screen
|
||||
name="sign-request-embed"
|
||||
component={SignMessageEmbed}
|
||||
|
61
src/screens/AddAccountEmbed.tsx
Normal file
61
src/screens/AddAccountEmbed.tsx
Normal file
@ -0,0 +1,61 @@
|
||||
import React, { useEffect } from 'react';
|
||||
|
||||
import { useNetworks } from '../context/NetworksContext';
|
||||
import { COSMOS } from '../utils/constants';
|
||||
import { sendMessage } from '../utils/misc';
|
||||
import useAccountsData from '../hooks/useAccountsData';
|
||||
import useGetOrCreateAccounts from '../hooks/useGetOrCreateAccounts';
|
||||
import { addAccount, retrieveSingleAccount } from '../utils/accounts';
|
||||
import { useAccounts } from '../context/AccountsContext';
|
||||
import { Account, NetworksDataState } from '../types';
|
||||
|
||||
export const AddAccountEmbed = () => {
|
||||
const { networksData } = useNetworks();
|
||||
const { accounts, setAccounts, setCurrentIndex } =
|
||||
useAccounts();
|
||||
const { getAccountsData } = useAccountsData();
|
||||
|
||||
const addAccountHandler = async (network: NetworksDataState) => {
|
||||
const newAccount = await addAccount(network);
|
||||
if (newAccount) {
|
||||
setAccounts([...accounts, newAccount]);
|
||||
setCurrentIndex(newAccount.index);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const handleAddAccount = async (event: MessageEvent) => {
|
||||
if (event.data.type !== 'ADD_ACCOUNT') return;
|
||||
|
||||
if (event.origin !== process.env.REACT_APP_DEPLOY_APP_URL) {
|
||||
console.log('Unauthorized app.');
|
||||
return;
|
||||
}
|
||||
|
||||
const network = networksData.find(network => network.chainId === event.data.chainId);
|
||||
|
||||
await addAccountHandler(network!);
|
||||
|
||||
const accountsData = await getAccountsData(event.data.chainId);
|
||||
|
||||
console.log({accountsData})
|
||||
|
||||
sendMessage(event.source as Window, 'ADD_ACCOUNT_RESPONSE', { message: event.data.message, accountsData }, event.origin);
|
||||
};
|
||||
|
||||
window.addEventListener('message', handleAddAccount);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('message', handleAddAccount);
|
||||
};
|
||||
}, [networksData, getAccountsData]);
|
||||
|
||||
// Custom hook for adding listener to get accounts data
|
||||
useGetOrCreateAccounts();
|
||||
|
||||
console.log('wallet')
|
||||
return (
|
||||
<>
|
||||
</>
|
||||
)
|
||||
};
|
@ -42,6 +42,7 @@ export type StackParamsList = {
|
||||
"auto-sign-in": undefined;
|
||||
"sign-request-embed": undefined;
|
||||
"export-pk": undefined;
|
||||
"add-account-embed": undefined;
|
||||
};
|
||||
|
||||
export type Account = {
|
||||
|
Loading…
Reference in New Issue
Block a user