From bb485d5783e8d65d74c6659bdaf5d9a90292a427 Mon Sep 17 00:00:00 2001 From: Matthew Russell Date: Tue, 22 Feb 2022 21:39:12 -0800 Subject: [PATCH] add useEagerConnect hook --- apps/trading/lib/connectors.ts | 7 +++++-- apps/trading/pages/index.page.tsx | 16 ++++++++++++++++ .../src/lib/vega-wallet/provider.tsx | 4 ++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/apps/trading/lib/connectors.ts b/apps/trading/lib/connectors.ts index f42de85fb..2fdb3da2c 100644 --- a/apps/trading/lib/connectors.ts +++ b/apps/trading/lib/connectors.ts @@ -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, }; diff --git a/apps/trading/pages/index.page.tsx b/apps/trading/pages/index.page.tsx index f939a27f0..24e93cf8d 100644 --- a/apps/trading/pages/index.page.tsx +++ b/apps/trading/pages/index.page.tsx @@ -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 (
@@ -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]); +} diff --git a/libs/react-helpers/src/lib/vega-wallet/provider.tsx b/libs/react-helpers/src/lib/vega-wallet/provider.tsx index 4fe842230..b15ff58be 100644 --- a/libs/react-helpers/src/lib/vega-wallet/provider.tsx +++ b/libs/react-helpers/src/lib/vega-wallet/provider.tsx @@ -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); } }, []);