2022-03-14 13:18:11 +00:00
|
|
|
import { useVegaWallet, WALLET_CONFIG } from '@vegaprotocol/wallet';
|
2022-03-11 00:28:48 +00:00
|
|
|
import { useEffect } from 'react';
|
2022-03-14 13:18:11 +00:00
|
|
|
import { LocalStorage } from '@vegaprotocol/react-helpers';
|
2022-03-25 07:44:10 +00:00
|
|
|
import { Connectors } from '../lib/vega-connectors';
|
2022-04-04 19:45:40 +00:00
|
|
|
import { captureException } from '@sentry/nextjs';
|
2022-03-11 00:28:48 +00:00
|
|
|
|
|
|
|
export function useEagerConnect() {
|
|
|
|
const { connect } = useVegaWallet();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const cfg = LocalStorage.getItem(WALLET_CONFIG);
|
2022-03-28 19:34:45 +00:00
|
|
|
let cfgObj: { connector: 'rest'; token: string } | null;
|
2022-03-11 00:28:48 +00:00
|
|
|
|
2022-03-28 19:34:45 +00:00
|
|
|
try {
|
|
|
|
cfgObj = cfg ? JSON.parse(cfg) : null;
|
|
|
|
} catch {
|
|
|
|
cfgObj = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No stored config, or config was malformed
|
2022-03-11 00:28:48 +00:00
|
|
|
if (!cfgObj || !cfgObj.connector) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the connector string in local storage to find the right connector to auto
|
|
|
|
// connect to
|
|
|
|
const connector = Connectors[cfgObj.connector];
|
|
|
|
|
|
|
|
// Developer hasn't provided this connector
|
|
|
|
if (!connector) {
|
2022-04-04 19:45:40 +00:00
|
|
|
captureException(
|
|
|
|
new Error(
|
|
|
|
`Can't eager connect, connector: ${cfgObj.connector} not found`
|
|
|
|
)
|
2022-03-25 07:43:49 +00:00
|
|
|
);
|
|
|
|
return;
|
2022-03-11 00:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
connect(Connectors[cfgObj.connector]);
|
|
|
|
}, [connect]);
|
|
|
|
}
|