@reef-knot/core-react 2.0.0 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/AutoConnect.d.ts +1 -1
- package/dist/components/AutoConnect.d.ts.map +1 -1
- package/dist/components/AutoConnect.js +2 -4
- package/dist/helpers/getUnsupportedChainError.d.ts.map +1 -1
- package/dist/helpers/getUnsupportedChainError.js +6 -1
- package/dist/helpers/index.d.ts +4 -0
- package/dist/helpers/index.d.ts.map +1 -0
- package/dist/helpers/userAgents.d.ts +8 -0
- package/dist/helpers/userAgents.d.ts.map +1 -0
- package/dist/helpers/userAgents.js +11 -0
- package/dist/hooks/index.d.ts +4 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/useAutoConnect.d.ts +1 -3
- package/dist/hooks/useAutoConnect.d.ts.map +1 -1
- package/dist/hooks/useAutoConnect.js +23 -100
- package/dist/hooks/useAutoConnectCheck.d.ts +4 -0
- package/dist/hooks/useAutoConnectCheck.d.ts.map +1 -0
- package/dist/hooks/useAutoConnectCheck.js +19 -0
- package/dist/hooks/useConnectorInfo.d.ts +10 -0
- package/dist/hooks/useConnectorInfo.d.ts.map +1 -0
- package/dist/hooks/useConnectorInfo.js +30 -0
- package/dist/hooks/useDisconnect.d.ts +7 -0
- package/dist/hooks/useDisconnect.d.ts.map +1 -0
- package/dist/hooks/useDisconnect.js +39 -0
- package/dist/hooks/useEagerConnect.d.ts +15 -0
- package/dist/hooks/useEagerConnect.d.ts.map +1 -0
- package/dist/hooks/useEagerConnect.js +101 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/package.json +3 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { Chain } from 'wagmi';
|
|
3
3
|
import type { WalletAdapterData } from '@reef-knot/types';
|
|
4
|
-
export declare const AutoConnect: ({ children, autoConnect,
|
|
4
|
+
export declare const AutoConnect: ({ children, autoConnect, }: {
|
|
5
5
|
children: React.ReactNode;
|
|
6
6
|
autoConnect: boolean;
|
|
7
7
|
walletDataList: WalletAdapterData[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AutoConnect.d.ts","sourceRoot":"","sources":["../../src/components/AutoConnect.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAC9B,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAG1D,eAAO,MAAM,WAAW;
|
|
1
|
+
{"version":3,"file":"AutoConnect.d.ts","sourceRoot":"","sources":["../../src/components/AutoConnect.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAC9B,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAG1D,eAAO,MAAM,WAAW;cAIZ,MAAM,SAAS;iBACZ,OAAO;oBACJ,iBAAiB,EAAE;YAC3B,KAAK,EAAE;uBAKhB,CAAC"}
|
|
@@ -3,11 +3,9 @@ import { useAutoConnect } from '../hooks/useAutoConnect.js';
|
|
|
3
3
|
|
|
4
4
|
const AutoConnect = ({
|
|
5
5
|
children,
|
|
6
|
-
autoConnect
|
|
7
|
-
walletDataList,
|
|
8
|
-
chains
|
|
6
|
+
autoConnect
|
|
9
7
|
}) => {
|
|
10
|
-
useAutoConnect(autoConnect
|
|
8
|
+
useAutoConnect(autoConnect);
|
|
11
9
|
return React.createElement(React.Fragment, null, children);
|
|
12
10
|
};
|
|
13
11
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getUnsupportedChainError.d.ts","sourceRoot":"","sources":["../../src/helpers/getUnsupportedChainError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAErC,eAAO,MAAM,wBAAwB,oBAAqB,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"getUnsupportedChainError.d.ts","sourceRoot":"","sources":["../../src/helpers/getUnsupportedChainError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAErC,eAAO,MAAM,wBAAwB,oBAAqB,KAAK,EAAE,UAkBhE,CAAC"}
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
const getUnsupportedChainError = supportedChains => {
|
|
2
2
|
// Get names of supported chains to suggest them in case of "unsupported network" error
|
|
3
3
|
const supportedChainsNames = (() => {
|
|
4
|
-
const chains = supportedChains
|
|
4
|
+
const chains = supportedChains
|
|
5
|
+
// On Lido widgets the Polygon Mumbai network was added as a temporary workaround for the wagmi and walletconnect bug,
|
|
6
|
+
// when some wallets are failing to connect if there are only one supported network, so we need at least 2 of them.
|
|
7
|
+
// But we don't want to mention it in the error as a suggested supported network, because it is not really true, so temporary filtering it out.
|
|
8
|
+
// TODO: the issue is fixed in wagmi v1+, remove the filter after updating wagmi to v1+
|
|
9
|
+
.filter(chain => chain.id !== 80001).map(({
|
|
5
10
|
name
|
|
6
11
|
}) => name).filter(chainName => chainName !== 'unknown');
|
|
7
12
|
const lastChain = chains.pop();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/helpers/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,sBAAsB,CAAC;AACrC,cAAc,cAAc,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/// <reference types="ua-parser-js" />
|
|
2
|
+
export declare const device: import("ua-parser-js").IDevice;
|
|
3
|
+
export declare const browser: import("ua-parser-js").IBrowser;
|
|
4
|
+
export declare const os: import("ua-parser-js").IOS;
|
|
5
|
+
export declare const isMobile: boolean;
|
|
6
|
+
export declare const isTablet: boolean;
|
|
7
|
+
export declare const isMobileOrTablet: boolean;
|
|
8
|
+
//# sourceMappingURL=userAgents.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"userAgents.d.ts","sourceRoot":"","sources":["../../src/helpers/userAgents.ts"],"names":[],"mappings":";AAIA,eAAO,MAAM,MAAM,gCAAqB,CAAC;AACzC,eAAO,MAAM,OAAO,iCAAsB,CAAC;AAC3C,eAAO,MAAM,EAAE,4BAAiB,CAAC;AAEjC,eAAO,MAAM,QAAQ,SAA2B,CAAC;AACjD,eAAO,MAAM,QAAQ,SAA2B,CAAC;AACjD,eAAO,MAAM,gBAAgB,SAAuB,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { UAParser } from 'ua-parser-js';
|
|
2
|
+
|
|
3
|
+
const parser = new UAParser();
|
|
4
|
+
const device = parser.getDevice();
|
|
5
|
+
const browser = parser.getBrowser();
|
|
6
|
+
const os = parser.getOS();
|
|
7
|
+
const isMobile = device.type === 'mobile';
|
|
8
|
+
const isTablet = device.type === 'tablet';
|
|
9
|
+
const isMobileOrTablet = isMobile || isTablet;
|
|
10
|
+
|
|
11
|
+
export { browser, device, isMobile, isMobileOrTablet, isTablet, os };
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC"}
|
|
@@ -1,4 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
import { WalletAdapterData } from '@reef-knot/types';
|
|
3
|
-
export declare const useAutoConnect: (autoConnectEnabled: boolean, walletDataList: WalletAdapterData[], chains: Chain[]) => void;
|
|
1
|
+
export declare const useAutoConnect: (autoConnectEnabled: boolean) => void;
|
|
4
2
|
//# sourceMappingURL=useAutoConnect.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAutoConnect.d.ts","sourceRoot":"","sources":["../../src/hooks/useAutoConnect.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useAutoConnect.d.ts","sourceRoot":"","sources":["../../src/hooks/useAutoConnect.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,cAAc,uBAAwB,OAAO,SA4BzD,CAAC"}
|
|
@@ -1,115 +1,38 @@
|
|
|
1
1
|
import { __awaiter } from '../_virtual/_tslib.js';
|
|
2
2
|
import { useClient, useAccount } from 'wagmi';
|
|
3
|
-
import {
|
|
4
|
-
import { useRef, useContext, useEffect } from 'react';
|
|
5
|
-
import { AcceptTermsModalContext } from '../context/acceptTermsModal.js';
|
|
6
|
-
import { getUnsupportedChainError } from '../helpers/getUnsupportedChainError.js';
|
|
3
|
+
import { useRef, useCallback, useEffect } from 'react';
|
|
7
4
|
import { checkTermsAccepted } from '../helpers/checkTermsAccepted.js';
|
|
5
|
+
import { useEagerConnect } from './useEagerConnect.js';
|
|
8
6
|
|
|
9
|
-
const
|
|
10
|
-
var _a, _b, _c, _d;
|
|
11
|
-
let connectResult = null;
|
|
12
|
-
let connectError;
|
|
13
|
-
try {
|
|
14
|
-
connectResult = yield connect({
|
|
15
|
-
connector
|
|
16
|
-
});
|
|
17
|
-
} catch (e) {
|
|
18
|
-
connectResult = null; // ensure that connectResult is empty in case of an error
|
|
19
|
-
connectError = e;
|
|
20
|
-
}
|
|
21
|
-
if (connectResult === null || connectResult === void 0 ? void 0 : connectResult.chain.unsupported) {
|
|
22
|
-
// No errors during connection, but the chain is unsupported.
|
|
23
|
-
// This case is considered as error for now, and we explicitly call disconnect() here.
|
|
24
|
-
// This logic comes from previously used web3-react connection logic, which wasn't reworked yet after web3-react removal.
|
|
25
|
-
// web3-react logic was: if a chain is unsupported – break the connection, throw an error
|
|
26
|
-
// wagmi logic is: if a chain is unsupported – connect anyway, without errors, set `chain.unsupported` flag to true.
|
|
27
|
-
// So, here we are trying to mimic the legacy logic, because we are not ready to rework it yet.
|
|
28
|
-
connectResult = null;
|
|
29
|
-
connectError = getUnsupportedChainError(supportedChains);
|
|
30
|
-
yield disconnect();
|
|
31
|
-
// A user may change a chain in a wallet app, prepare for that event.
|
|
32
|
-
// There is a strong recommendation in the MetaMask documentation
|
|
33
|
-
// to reload the page upon chain changes, unless there is a good reason not to.
|
|
34
|
-
// This looks like a good general approach.
|
|
35
|
-
const provider = yield connector.getProvider();
|
|
36
|
-
provider.once('chainChanged', () => {
|
|
37
|
-
var _a;
|
|
38
|
-
return (_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.location.reload();
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
if (connectError) {
|
|
42
|
-
(_a = acceptTermsModal.setError) === null || _a === void 0 ? void 0 : _a.call(acceptTermsModal, connectError);
|
|
43
|
-
(_b = acceptTermsModal.setVisible) === null || _b === void 0 ? void 0 : _b.call(acceptTermsModal, true);
|
|
44
|
-
} else {
|
|
45
|
-
(_c = acceptTermsModal.setVisible) === null || _c === void 0 ? void 0 : _c.call(acceptTermsModal, false);
|
|
46
|
-
(_d = acceptTermsModal.setError) === null || _d === void 0 ? void 0 : _d.call(acceptTermsModal, undefined);
|
|
47
|
-
}
|
|
48
|
-
return {
|
|
49
|
-
connectResult,
|
|
50
|
-
connectError
|
|
51
|
-
};
|
|
52
|
-
});
|
|
53
|
-
const connectEagerly = (adapters, acceptTermsModal, supportedChains) => __awaiter(void 0, void 0, void 0, function* () {
|
|
54
|
-
var _e, _f, _g;
|
|
55
|
-
const isTermsAccepted = checkTermsAccepted();
|
|
56
|
-
let connectResult = null;
|
|
57
|
-
let connectError;
|
|
58
|
-
const continueConnection = connector => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
|
-
({
|
|
60
|
-
connectResult,
|
|
61
|
-
connectError
|
|
62
|
-
} = yield connectAndHandleErrors(connector, supportedChains, acceptTermsModal));
|
|
63
|
-
});
|
|
64
|
-
for (const adapter of adapters) {
|
|
65
|
-
if ((_e = adapter.detector) === null || _e === void 0 ? void 0 : _e.call(adapter)) {
|
|
66
|
-
// wallet is detected
|
|
67
|
-
if (!isTermsAccepted) {
|
|
68
|
-
// Terms of service were not accepted previously.
|
|
69
|
-
// So, for legal reasons, we must ask a user to accept the terms before connecting.
|
|
70
|
-
const onContinue = () => void continueConnection(adapter.connector);
|
|
71
|
-
(_f = acceptTermsModal.setOnContinue) === null || _f === void 0 ? void 0 : _f.call(acceptTermsModal, () => onContinue);
|
|
72
|
-
(_g = acceptTermsModal.setVisible) === null || _g === void 0 ? void 0 : _g.call(acceptTermsModal, true);
|
|
73
|
-
} else {
|
|
74
|
-
yield continueConnection(adapter.connector);
|
|
75
|
-
}
|
|
76
|
-
break; // no need to iterate over all other adapters if at least one was detected
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return {
|
|
80
|
-
connectResult,
|
|
81
|
-
connectError
|
|
82
|
-
};
|
|
83
|
-
});
|
|
84
|
-
const useAutoConnect = (autoConnectEnabled, walletDataList, chains) => {
|
|
7
|
+
const useAutoConnect = autoConnectEnabled => {
|
|
85
8
|
const isAutoConnectCalled = useRef(false);
|
|
86
9
|
const client = useClient();
|
|
87
10
|
const {
|
|
88
11
|
isConnected
|
|
89
12
|
} = useAccount();
|
|
90
13
|
const {
|
|
91
|
-
|
|
92
|
-
} =
|
|
93
|
-
const
|
|
94
|
-
useEffect(() => {
|
|
14
|
+
eagerConnect
|
|
15
|
+
} = useEagerConnect();
|
|
16
|
+
const autoConnect = useCallback(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
95
17
|
// Don't auto-connect if already connected or if the auto-connect feature is disabled or if already tried to auto-connect.
|
|
96
18
|
if (isConnected || !autoConnectEnabled || isAutoConnectCalled.current) return;
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
19
|
+
// The current logic is to try auto-connect only once, even if an error happened and connection was not successful.
|
|
20
|
+
isAutoConnectCalled.current = true;
|
|
21
|
+
// Try to eagerly connect wallets that are meant to be used only with auto-connection.
|
|
22
|
+
// For example, wallets with dApp browsers, or using iframes to open dApps.
|
|
23
|
+
const {
|
|
24
|
+
connectResult,
|
|
25
|
+
connectError
|
|
26
|
+
} = yield eagerConnect();
|
|
27
|
+
// If still not connected and there were no errors and the terms of service are accepted,
|
|
28
|
+
// call the default wagmi autoConnect method, which attempts to connect to the last used connector.
|
|
29
|
+
if (!connectResult && !connectError && checkTermsAccepted()) {
|
|
30
|
+
yield client.autoConnect();
|
|
31
|
+
}
|
|
32
|
+
}), [autoConnectEnabled, client, eagerConnect, isConnected]);
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
void autoConnect();
|
|
35
|
+
}, [autoConnect]);
|
|
113
36
|
};
|
|
114
37
|
|
|
115
38
|
export { useAutoConnect };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAutoConnectCheck.d.ts","sourceRoot":"","sources":["../../src/hooks/useAutoConnectCheck.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,mBAAmB;;CAY/B,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useReefKnotContext } from './useReefKnotContext.js';
|
|
2
|
+
|
|
3
|
+
const useAutoConnectCheck = () => {
|
|
4
|
+
const {
|
|
5
|
+
walletDataList
|
|
6
|
+
} = useReefKnotContext();
|
|
7
|
+
const autoConnectOnlyAdapters = walletDataList.filter(({
|
|
8
|
+
autoConnectOnly
|
|
9
|
+
}) => autoConnectOnly);
|
|
10
|
+
const isAutoConnectionSuitable = autoConnectOnlyAdapters.some(adapter => {
|
|
11
|
+
var _a;
|
|
12
|
+
return (_a = adapter.detector) === null || _a === void 0 ? void 0 : _a.call(adapter);
|
|
13
|
+
});
|
|
14
|
+
return {
|
|
15
|
+
isAutoConnectionSuitable
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export { useAutoConnectCheck };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
type ConnectorInfo = {
|
|
2
|
+
connectorName?: string;
|
|
3
|
+
isGnosis: boolean;
|
|
4
|
+
isLedger: boolean;
|
|
5
|
+
isLedgerLive: boolean;
|
|
6
|
+
isDappBrowser: boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare const useConnectorInfo: () => ConnectorInfo;
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=useConnectorInfo.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useConnectorInfo.d.ts","sourceRoot":"","sources":["../../src/hooks/useConnectorInfo.ts"],"names":[],"mappings":"AAQA,KAAK,aAAa,GAAG;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,EAAE,OAAO,CAAC;IAClB,QAAQ,EAAE,OAAO,CAAC;IAClB,YAAY,EAAE,OAAO,CAAC;IACtB,aAAa,EAAE,OAAO,CAAC;CACxB,CAAC;AAEF,eAAO,MAAM,gBAAgB,QAAO,aAuBnC,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { useAccount } from 'wagmi';
|
|
2
|
+
import { SafeConnector } from 'wagmi/connectors/safe';
|
|
3
|
+
import { LedgerHIDConnector, LedgerLiveConnector } from '@reef-knot/ledger-connector';
|
|
4
|
+
import { isMobileOrTablet } from '../helpers/userAgents.js';
|
|
5
|
+
|
|
6
|
+
const useConnectorInfo = () => {
|
|
7
|
+
var _a;
|
|
8
|
+
const {
|
|
9
|
+
connector
|
|
10
|
+
} = useAccount();
|
|
11
|
+
// These checks are working only for connected wallets! There is no connector if a wallet is not connected yet.
|
|
12
|
+
const isLedger = connector instanceof LedgerHIDConnector;
|
|
13
|
+
const isLedgerLive = connector instanceof LedgerLiveConnector;
|
|
14
|
+
const isGnosis = connector instanceof SafeConnector;
|
|
15
|
+
const isDappBrowser = !!((_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.ethereum) && isMobileOrTablet;
|
|
16
|
+
let connectorName = connector === null || connector === void 0 ? void 0 : connector.name;
|
|
17
|
+
// Do not set connector's name if the app is opened in a mobile wallet dapp browser,
|
|
18
|
+
// because we use a generic injected connector for this case and proper detection is hard.
|
|
19
|
+
// Also, it will be easy for a user to understand which wallet app is being used for connection.
|
|
20
|
+
if (isDappBrowser) connectorName = undefined;
|
|
21
|
+
return {
|
|
22
|
+
connectorName,
|
|
23
|
+
isGnosis,
|
|
24
|
+
isLedger,
|
|
25
|
+
isLedgerLive,
|
|
26
|
+
isDappBrowser
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export { useConnectorInfo };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDisconnect.d.ts","sourceRoot":"","sources":["../../src/hooks/useDisconnect.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,kBAAkB;;CAY9B,CAAC;AAEF,eAAO,MAAM,aAAa;wBACL,IAAI;CAWxB,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { useContext, useCallback } from 'react';
|
|
2
|
+
import { useDisconnect as useDisconnect$1, useAccount } from 'wagmi';
|
|
3
|
+
import { AcceptTermsModalContext } from '../context/acceptTermsModal.js';
|
|
4
|
+
import { useAutoConnectCheck } from './useAutoConnectCheck.js';
|
|
5
|
+
|
|
6
|
+
const useForceDisconnect = () => {
|
|
7
|
+
const {
|
|
8
|
+
disconnect
|
|
9
|
+
} = useDisconnect$1();
|
|
10
|
+
const {
|
|
11
|
+
acceptTermsModal: {
|
|
12
|
+
setVisible
|
|
13
|
+
}
|
|
14
|
+
} = useContext(AcceptTermsModalContext);
|
|
15
|
+
const forceDisconnect = useCallback(() => {
|
|
16
|
+
disconnect();
|
|
17
|
+
setVisible(false);
|
|
18
|
+
}, [disconnect, setVisible]);
|
|
19
|
+
return {
|
|
20
|
+
forceDisconnect
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
const useDisconnect = () => {
|
|
24
|
+
const {
|
|
25
|
+
isConnected
|
|
26
|
+
} = useAccount();
|
|
27
|
+
const {
|
|
28
|
+
disconnect
|
|
29
|
+
} = useDisconnect$1();
|
|
30
|
+
const {
|
|
31
|
+
isAutoConnectionSuitable
|
|
32
|
+
} = useAutoConnectCheck();
|
|
33
|
+
const available = isConnected && !isAutoConnectionSuitable;
|
|
34
|
+
return {
|
|
35
|
+
disconnect: available ? disconnect : undefined
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export { useDisconnect, useForceDisconnect };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Chain } from 'wagmi';
|
|
2
|
+
import type { ConnectResult } from '@wagmi/core';
|
|
3
|
+
import { WalletAdapterData } from '@reef-knot/types';
|
|
4
|
+
import { AcceptTermsModal } from '../context/acceptTermsModal';
|
|
5
|
+
export declare const connectEagerly: (adapters: WalletAdapterData[], acceptTermsModal: AcceptTermsModal, supportedChains: Chain[]) => Promise<{
|
|
6
|
+
connectResult: ConnectResult | null;
|
|
7
|
+
connectError?: Error | undefined;
|
|
8
|
+
}>;
|
|
9
|
+
export declare const useEagerConnect: () => {
|
|
10
|
+
eagerConnect: () => Promise<{
|
|
11
|
+
connectResult: ConnectResult | null;
|
|
12
|
+
connectError?: Error | undefined;
|
|
13
|
+
}>;
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=useEagerConnect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useEagerConnect.d.ts","sourceRoot":"","sources":["../../src/hooks/useEagerConnect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAE9B,OAAO,KAAK,EAAE,aAAa,EAAa,MAAM,aAAa,CAAC;AAE5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EACL,gBAAgB,EAEjB,MAAM,6BAA6B,CAAC;AAqDrC,eAAO,MAAM,cAAc,aACf,iBAAiB,EAAE,oBACX,gBAAgB,mBACjB,KAAK,EAAE;mBAET,aAAa,GAAG,IAAI;;EAgCpC,CAAC;AAEF,eAAO,MAAM,eAAe;;uBAlCX,aAAa,GAAG,IAAI;;;CA8CpC,CAAC"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { __awaiter } from '../_virtual/_tslib.js';
|
|
2
|
+
import { connect, disconnect } from 'wagmi/actions';
|
|
3
|
+
import { useContext, useCallback } from 'react';
|
|
4
|
+
import { AcceptTermsModalContext } from '../context/acceptTermsModal.js';
|
|
5
|
+
import { getUnsupportedChainError } from '../helpers/getUnsupportedChainError.js';
|
|
6
|
+
import { checkTermsAccepted } from '../helpers/checkTermsAccepted.js';
|
|
7
|
+
import { useReefKnotContext } from './useReefKnotContext.js';
|
|
8
|
+
|
|
9
|
+
const connectAndHandleErrors = (connector, supportedChains, acceptTermsModal) => __awaiter(void 0, void 0, void 0, function* () {
|
|
10
|
+
var _a, _b, _c, _d;
|
|
11
|
+
let connectResult = null;
|
|
12
|
+
let connectError;
|
|
13
|
+
try {
|
|
14
|
+
connectResult = yield connect({
|
|
15
|
+
connector
|
|
16
|
+
});
|
|
17
|
+
} catch (e) {
|
|
18
|
+
connectResult = null; // ensure that connectResult is empty in case of an error
|
|
19
|
+
connectError = e;
|
|
20
|
+
}
|
|
21
|
+
if (connectResult === null || connectResult === void 0 ? void 0 : connectResult.chain.unsupported) {
|
|
22
|
+
// No errors during connection, but the chain is unsupported.
|
|
23
|
+
// This case is considered as error for now, and we explicitly call disconnect() here.
|
|
24
|
+
// This logic comes from previously used web3-react connection logic, which wasn't reworked yet after web3-react removal.
|
|
25
|
+
// web3-react logic was: if a chain is unsupported – break the connection, throw an error
|
|
26
|
+
// wagmi logic is: if a chain is unsupported – connect anyway, without errors, set `chain.unsupported` flag to true.
|
|
27
|
+
// So, here we are trying to mimic the legacy logic, because we are not ready to rework it yet.
|
|
28
|
+
connectResult = null;
|
|
29
|
+
connectError = getUnsupportedChainError(supportedChains);
|
|
30
|
+
yield disconnect();
|
|
31
|
+
// A user may change a chain in a wallet app, prepare for that event.
|
|
32
|
+
// There is a strong recommendation in the MetaMask documentation
|
|
33
|
+
// to reload the page upon chain changes, unless there is a good reason not to.
|
|
34
|
+
// This looks like a good general approach.
|
|
35
|
+
const provider = yield connector.getProvider();
|
|
36
|
+
provider.once('chainChanged', () => {
|
|
37
|
+
var _a;
|
|
38
|
+
return (_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.location.reload();
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
if (connectError) {
|
|
42
|
+
(_a = acceptTermsModal.setError) === null || _a === void 0 ? void 0 : _a.call(acceptTermsModal, connectError);
|
|
43
|
+
(_b = acceptTermsModal.setVisible) === null || _b === void 0 ? void 0 : _b.call(acceptTermsModal, true);
|
|
44
|
+
} else {
|
|
45
|
+
(_c = acceptTermsModal.setVisible) === null || _c === void 0 ? void 0 : _c.call(acceptTermsModal, false);
|
|
46
|
+
(_d = acceptTermsModal.setError) === null || _d === void 0 ? void 0 : _d.call(acceptTermsModal, undefined);
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
connectResult,
|
|
50
|
+
connectError
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
const connectEagerly = (adapters, acceptTermsModal, supportedChains) => __awaiter(void 0, void 0, void 0, function* () {
|
|
54
|
+
var _e, _f, _g;
|
|
55
|
+
const isTermsAccepted = checkTermsAccepted();
|
|
56
|
+
let connectResult = null;
|
|
57
|
+
let connectError;
|
|
58
|
+
const continueConnection = connector => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
|
+
({
|
|
60
|
+
connectResult,
|
|
61
|
+
connectError
|
|
62
|
+
} = yield connectAndHandleErrors(connector, supportedChains, acceptTermsModal));
|
|
63
|
+
});
|
|
64
|
+
for (const adapter of adapters) {
|
|
65
|
+
if ((_e = adapter.detector) === null || _e === void 0 ? void 0 : _e.call(adapter)) {
|
|
66
|
+
// wallet is detected
|
|
67
|
+
if (!isTermsAccepted) {
|
|
68
|
+
// Terms of service were not accepted previously.
|
|
69
|
+
// So, for legal reasons, we must ask a user to accept the terms before connecting.
|
|
70
|
+
const onContinue = () => void continueConnection(adapter.connector);
|
|
71
|
+
(_f = acceptTermsModal.setOnContinue) === null || _f === void 0 ? void 0 : _f.call(acceptTermsModal, () => onContinue);
|
|
72
|
+
(_g = acceptTermsModal.setVisible) === null || _g === void 0 ? void 0 : _g.call(acceptTermsModal, true);
|
|
73
|
+
} else {
|
|
74
|
+
yield continueConnection(adapter.connector);
|
|
75
|
+
}
|
|
76
|
+
break; // no need to iterate over all other adapters if at least one was detected
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
connectResult,
|
|
81
|
+
connectError
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
const useEagerConnect = () => {
|
|
85
|
+
const {
|
|
86
|
+
acceptTermsModal
|
|
87
|
+
} = useContext(AcceptTermsModalContext);
|
|
88
|
+
const {
|
|
89
|
+
walletDataList,
|
|
90
|
+
chains
|
|
91
|
+
} = useReefKnotContext();
|
|
92
|
+
const autoConnectOnlyAdapters = walletDataList.filter(adapter => adapter.autoConnectOnly);
|
|
93
|
+
const eagerConnect = useCallback(() => {
|
|
94
|
+
return connectEagerly(autoConnectOnlyAdapters, acceptTermsModal, chains);
|
|
95
|
+
}, [acceptTermsModal, autoConnectOnlyAdapters, chains]);
|
|
96
|
+
return {
|
|
97
|
+
eagerConnect
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export { connectEagerly, useEagerConnect };
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
export { getConnectors, getWalletDataList } from './walletData/index.js';
|
|
2
2
|
export { useReefKnotContext } from './hooks/useReefKnotContext.js';
|
|
3
3
|
export { useAutoConnect } from './hooks/useAutoConnect.js';
|
|
4
|
+
export { useAutoConnectCheck } from './hooks/useAutoConnectCheck.js';
|
|
5
|
+
export { connectEagerly, useEagerConnect } from './hooks/useEagerConnect.js';
|
|
6
|
+
export { useDisconnect, useForceDisconnect } from './hooks/useDisconnect.js';
|
|
7
|
+
export { useConnectorInfo } from './hooks/useConnectorInfo.js';
|
|
4
8
|
export { ReefKnot, ReefKnotContext } from './context/reefKnot.js';
|
|
5
9
|
export { AcceptTermsModalContext, AcceptTermsModalContextProvider } from './context/acceptTermsModal.js';
|
|
6
10
|
export { LS_KEY_TERMS_ACCEPTANCE } from './constants/localStorage.js';
|
|
7
11
|
export { holesky } from './chains/holesky.js';
|
|
12
|
+
export { getUnsupportedChainError } from './helpers/getUnsupportedChainError.js';
|
|
13
|
+
export { checkTermsAccepted } from './helpers/checkTermsAccepted.js';
|
|
14
|
+
export { browser, device, isMobile, isMobileOrTablet, isTablet, os } from './helpers/userAgents.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reef-knot/core-react",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"exports": {
|
|
@@ -40,6 +40,7 @@
|
|
|
40
40
|
"ua-parser-js": "1.0.33"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
+
"@reef-knot/ledger-connector": "^3.0.0",
|
|
43
44
|
"@reef-knot/wallets-list": "^1.12.0",
|
|
44
45
|
"@reef-knot/types": "^1.5.0",
|
|
45
46
|
"@reef-knot/ui-react": "^1.0.8",
|
|
@@ -49,6 +50,7 @@
|
|
|
49
50
|
"wagmi": "^0.12.19"
|
|
50
51
|
},
|
|
51
52
|
"peerDependencies": {
|
|
53
|
+
"@reef-knot/ledger-connector": "^3.0.0",
|
|
52
54
|
"@reef-knot/wallets-list": "^1.4.1",
|
|
53
55
|
"@reef-knot/types": "^1.2.1",
|
|
54
56
|
"@reef-knot/ui-react": "^1.0.4",
|