vega-frontend-monorepo/apps/trading/hooks/use-vega-wallet-eager-connect.ts
Matthew Russell 018e5fe139
Task/33 add sentry to trading (#189)
* add sentry setup, add VEGA_ENV to env files

* rename custom error page

* capture exception for eager connect

* add link to setup docs in comment

* add browser tracing plugin

* define sentry auth token once

* enable sourcemaps on build

* use correct sentry config property
2022-04-04 12:45:40 -07:00

42 lines
1.1 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';
import { captureException } from '@sentry/nextjs';
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) {
captureException(
new Error(
`Can't eager connect, connector: ${cfgObj.connector} not found`
)
);
return;
}
connect(Connectors[cfgObj.connector]);
}, [connect]);
}