vega-frontend-monorepo/apps/trading/hooks/use-vega-wallet-eager-connect.ts
Matthew Russell d4652b3dd8
Task/Strict mode enabled for trading app (#150)
* enable strict mode and fix resulting type errors

* fix print affected command

* remove assign-deep and use lodash/merge, fix some type errors after enabling strict mode
2022-03-28 12:34:45 -07:00

39 lines
1.0 KiB
TypeScript

import { useVegaWallet, WALLET_CONFIG } from '@vegaprotocol/wallet';
import { useEffect } from 'react';
import { LocalStorage } from '@vegaprotocol/react-helpers';
import { Connectors } from '../lib/vega-connectors';
export function useEagerConnect() {
const { connect } = useVegaWallet();
useEffect(() => {
const cfg = LocalStorage.getItem(WALLET_CONFIG);
let cfgObj: { connector: 'rest'; token: string } | null;
try {
cfgObj = cfg ? JSON.parse(cfg) : null;
} catch {
cfgObj = null;
}
// No stored config, or config was malformed
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) {
console.warn(
`Can't eager connect, connector: ${cfgObj.connector} not found`
);
return;
}
connect(Connectors[cfgObj.connector]);
}, [connect]);
}