add useEagerConnect hook

This commit is contained in:
Matthew Russell 2022-02-22 21:39:12 -08:00
parent a44b8538fa
commit bb485d5783
3 changed files with 23 additions and 4 deletions

View File

@ -1,6 +1,9 @@
import { InjectedConnector, RestConnector } from '@vegaprotocol/react-helpers';
export const injected = new InjectedConnector();
export const rest = new RestConnector();
export const Connectors = {
injected: new InjectedConnector(),
rest: new RestConnector(),
injected,
rest,
};

View File

@ -1,6 +1,11 @@
import { Callout, Button } from '@vegaprotocol/ui-toolkit';
import { ReactHelpers, useVegaWallet } from '@vegaprotocol/react-helpers';
import { useEffect } from 'react';
import { rest } from '../lib/connectors';
import { LocalStorage } from '@vegaprotocol/storage';
export function Index() {
useEagerConnect();
const { publicKey, publicKeys, selectPublicKey } = useVegaWallet();
return (
<div className="m-24 ">
@ -39,3 +44,14 @@ export function Index() {
}
export default Index;
function useEagerConnect() {
const { connect } = useVegaWallet();
useEffect(() => {
// Might be safer to store connector name and eager connect using that
if (LocalStorage.getItem('vega_wallet_token')) {
connect(rest);
}
}, [connect]);
}

View File

@ -26,7 +26,7 @@ export const VegaWalletProvider = ({ children }: VegaWalletProviderProps) => {
setPublicKeys(publicKeysWithName);
setPublicKey(publicKeysWithName[0]);
} catch (err) {
console.log('connect failed');
console.error(err);
}
}, []);
@ -37,7 +37,7 @@ export const VegaWalletProvider = ({ children }: VegaWalletProviderProps) => {
setPublicKey(null);
connector.current = null;
} catch (err) {
console.log('disconnect failed', err);
console.error(err);
}
}, []);