Skip to content

Commit

Permalink
Merge pull request #124 from lidofinance/fix/useAutoConnectCheck
Browse files Browse the repository at this point in the history
Fix: isAutoConnectionSuitable
  • Loading branch information
alx-khramov authored Mar 13, 2024
2 parents b61aba9 + 7ff093c commit 48affeb
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 10 deletions.
4 changes: 2 additions & 2 deletions apps/demo-react/components/ConnectDisconnect.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useWeb3 } from 'reef-knot/web3-react';
import {
useConnectorInfo,
useAutoConnectCheck,
useEagerConnect,
useForceDisconnect,
} from 'reef-knot/core-react';
Expand All @@ -11,7 +11,7 @@ const ConnectDisconnect = (props: { handleOpen: () => void }) => {
const { handleOpen } = props;
const { forceDisconnect } = useForceDisconnect();
const { account } = useWeb3();
const { isAutoConnectionSuitable } = useConnectorInfo();
const { isAutoConnectionSuitable } = useAutoConnectCheck();
const { eagerConnect } = useEagerConnect();

const handleDisconnect = () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/core-react/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @reef-knot/core-react

## 2.1.1

### Patch Changes

- Fix isAutoConnectionSuitable, add a separate useAutoConnectCheck hook

## 2.1.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/core-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reef-knot/core-react",
"version": "2.1.0",
"version": "2.1.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down
1 change: 1 addition & 0 deletions packages/core-react/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './useReefKnotContext';
export * from './useAutoConnect';
export * from './useAutoConnectCheck';
export * from './useEagerConnect';
export * from './useDisconnect';
export * from './useConnectorInfo';
15 changes: 15 additions & 0 deletions packages/core-react/src/hooks/useAutoConnectCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useReefKnotContext } from './useReefKnotContext';

export const useAutoConnectCheck = () => {
const { walletDataList } = useReefKnotContext();
const autoConnectOnlyAdapters = walletDataList.filter(
({ autoConnectOnly }) => autoConnectOnly,
);
const isAutoConnectionSuitable = autoConnectOnlyAdapters.some((adapter) =>
adapter.detector?.(),
);

return {
isAutoConnectionSuitable,
};
};
4 changes: 1 addition & 3 deletions packages/core-react/src/hooks/useConnectorInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ type ConnectorInfo = {
isLedger: boolean;
isLedgerLive: boolean;
isDappBrowser: boolean;
isAutoConnectionSuitable: boolean;
};

export const useConnectorInfo = (): ConnectorInfo => {
const { connector } = useAccount();

// These checks are working only for connected wallets! There is no connector if a wallet is not connected yet.
const isLedger = connector instanceof LedgerHIDConnector;
const isLedgerLive = connector instanceof LedgerLiveConnector;
const isGnosis = connector instanceof SafeConnector;
const isDappBrowser = !!globalThis.window?.ethereum && isMobileOrTablet;
const isAutoConnectionSuitable = isLedgerLive || isGnosis || isDappBrowser;

let connectorName = connector?.name;

Expand All @@ -37,6 +36,5 @@ export const useConnectorInfo = (): ConnectorInfo => {
isLedger,
isLedgerLive,
isDappBrowser,
isAutoConnectionSuitable,
};
};
4 changes: 2 additions & 2 deletions packages/core-react/src/hooks/useDisconnect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useCallback, useContext } from 'react';
import { useAccount, useDisconnect as useDisconnectWagmi } from 'wagmi';
import { useConnectorInfo } from './useConnectorInfo';
import { AcceptTermsModalContext } from '../context/acceptTermsModal';
import { useAutoConnectCheck } from './useAutoConnectCheck';

export const useForceDisconnect = () => {
const { disconnect } = useDisconnectWagmi();
Expand All @@ -23,7 +23,7 @@ export const useDisconnect = (): {
const { isConnected } = useAccount();
const { disconnect } = useDisconnectWagmi();

const { isAutoConnectionSuitable } = useConnectorInfo();
const { isAutoConnectionSuitable } = useAutoConnectCheck();
const available = isConnected && !isAutoConnectionSuitable;

return {
Expand Down
7 changes: 7 additions & 0 deletions packages/reef-knot/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# reef-knot

## 2.1.1

### Patch Changes

- Updated dependencies
- @reef-knot/core-react@2.1.1

## 2.1.0

### Minor Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/reef-knot/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reef-knot",
"version": "2.1.0",
"version": "2.1.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down Expand Up @@ -42,7 +42,7 @@
},
"dependencies": {
"@reef-knot/connect-wallet-modal": "2.1.0",
"@reef-knot/core-react": "2.1.0",
"@reef-knot/core-react": "2.1.1",
"@reef-knot/web3-react": "2.1.0",
"@reef-knot/ui-react": "1.0.8",
"@reef-knot/wallets-list": "1.12.0",
Expand Down

0 comments on commit 48affeb

Please sign in to comment.