vega-frontend-monorepo/libs/wallet/src/use-eager-connect.ts
m.ray 0473412487
Feat/471 cancel order (#610)
* chore: [#471] update @vegaprotocol/vegawallet-service-api-client to 0.4.12

* fix: [#471] set up storybook in order-list lib and add tailwind

* fix: [#471] organize order list components

* chore: [471] pull theme switcher changes

* feat: [#471] add cancel order button

* feat: [#471] initial impl of use order cancel hook

* fix: [#471] fix format of the price in order list

* fix: #471 fix static assets issue when merging

* fix:  #471 refactor order dialog to vega tx dialog

* fix: #471 move use cancel order hook in wallet lib

* fix: [#471] cancel order dialog and hook refactor

* fix: [#471] remove commented code from storybook preview and fix test

* fix: [#471] update order-list.tsx

* fix: [#471] fix update subscription - show order is cancelled

* fix: [#471] fix eslint error

* chore: [#471] refactoring and add tests for dialogs and cancel hook

* fix: #471 add ref to order list table

* fix: #471 add field for cancel fix test

* fix: #471 rename vega-order-transaction-dialog, error handiling, open dialog on finalized order

* fix: #471 sendTx body mandatory

* fix: #471 use BusEventType.Order to check the typename

* fix: #471 revert using BusEventType.Order to check the typename

* Update libs/wallet/src/order-hooks/use-order-cancel.tsx

Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com>

* fix: #471  fix order-list refactoring and fixes

* fix: #471 generate orders added as a mock in order-list

* fix: #471 reset transaction after order updated

* fix: #471 remove unused import useEffect

* fix: #471 generate mock orders

* fix: #471 revert generate mock orders

* fix: #471 order list price set to display all decimals

* fix: #471 generate orders updates

* fix: #471 remove unused import

* fix: #471 remove __typename from mock orders genOrder

* Update libs/wallet/src/order-hooks/order-event-query.ts

Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com>

* fix: #471 update order event sub and pull master changes

Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com>
2022-06-29 10:03:20 +01:00

56 lines
1.5 KiB
TypeScript

import { useVegaWallet, WALLET_CONFIG } from './';
import { useEffect, useState } from 'react';
import { LocalStorage } from '@vegaprotocol/react-helpers';
import type { VegaConnector } from './connectors/vega-connector';
export function useEagerConnect(Connectors: {
[connector: string]: VegaConnector;
}) {
const [connecting, setConnecting] = useState(true);
const { connect } = useVegaWallet();
useEffect(() => {
const attemptConnect = async () => {
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) {
setConnecting(false);
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) {
setConnecting(false);
console.error(
`Can't eager connect, connector: ${cfgObj.connector} not found`
);
return;
}
try {
await connect(Connectors[cfgObj.connector]);
} catch {
console.warn(`Failed to connect with connector: ${cfgObj.connector}`);
} finally {
setConnecting(false);
}
};
attemptConnect();
}, [connect, Connectors]);
return connecting;
}