1
1
import { ConnectButton } from '@rainbow-me/rainbowkit' ;
2
2
import { __ } from '@wordpress/i18n' ;
3
- import { useAccount , useEnsName , useNetwork , useSignMessage } from 'wagmi' ;
3
+ import {
4
+ useAccount ,
5
+ useConnect ,
6
+ useDisconnect ,
7
+ useEnsName ,
8
+ useNetwork ,
9
+ useSignMessage ,
10
+ } from 'wagmi' ;
4
11
import stylePropType from 'react-style-proptype' ;
5
12
import { SiweMessage } from 'siwe' ;
6
13
import PropTypes from 'prop-types' ;
@@ -56,6 +63,8 @@ export function WPRainbowConnect( {
56
63
const { data : ensName , isSuccess : isENSSuccess } = useEnsName ( {
57
64
address : accountData ?. address ,
58
65
} ) ;
66
+ const { activeConnector } = useConnect ( ) ;
67
+ const { disconnectAsync } = useDisconnect ( ) ;
59
68
60
69
const signIn = React . useCallback ( async ( ) => {
61
70
try {
@@ -67,7 +76,7 @@ export function WPRainbowConnect( {
67
76
setState ( ( x ) => ( { ...x , address, loading : false } ) ) ;
68
77
return ;
69
78
}
70
- if ( window . signingIn . length > 1 ) {
79
+ if ( window ? .signingIn . length > 1 ) {
71
80
return ;
72
81
}
73
82
setState ( ( x ) => ( { ...x , error : undefined , loading : true } ) ) ;
@@ -124,7 +133,12 @@ export function WPRainbowConnect( {
124
133
125
134
const [ triggeredLogin , setTriggeredLogin ] = React . useState ( false ) ;
126
135
React . useEffect ( ( ) => {
127
- if ( accountData && isENSSuccess && ! triggeredLogin ) {
136
+ if (
137
+ activeConnector &&
138
+ accountData &&
139
+ isENSSuccess &&
140
+ ! triggeredLogin
141
+ ) {
128
142
window . signingIn = ! window . signingIn
129
143
? [ true ]
130
144
: [ true , ...window . signingIn ] ;
@@ -165,7 +179,7 @@ export function WPRainbowConnect( {
165
179
) }
166
180
</ div >
167
181
) ;
168
- } else if ( account ) {
182
+ } else if ( activeConnector && account ) {
169
183
let loginButtonText = __ ( 'Continue Log In with Ethereum' ) ;
170
184
if ( state . address ) {
171
185
loginButtonText = `${ __ ( 'Logged In as ' ) } ${
@@ -204,7 +218,9 @@ export function WPRainbowConnect( {
204
218
// Make sure we don't have an active signing attempt.
205
219
setState ( { } ) ;
206
220
setTriggeredLogin ( false ) ;
207
- openConnectModal ( ) ;
221
+ // This is a little weird, but since RainbowKit autoconnects,
222
+ // we need to make sure we're disconnected before logging in.
223
+ disconnectAsync ( ) . then ( openConnectModal ) ;
208
224
} ;
209
225
button = (
210
226
< div
0 commit comments