-
Notifications
You must be signed in to change notification settings - Fork 673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: optimistic disconnect #1871
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@rainbow-me/rainbowkit": patch | ||
--- | ||
|
||
Introduced optimistic disconnect behavior by monitoring `isDisconnecting` in `RainbowKitWagmiStateProvider`. If `isDisconnecting` is `true`, the `useConnectionStatus` hook will mark its state as `disconnected`. |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this is not ideal especially in the long term, but wagmi's Maybe we can look at having As long as we mark |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React, { | ||
ReactNode, | ||
createContext, | ||
useContext, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
|
||
interface RainbowKitWagmiStateContextValue { | ||
isDisconnecting: boolean; | ||
setIsDisconnecting: (isDisconnecting: boolean) => void; | ||
} | ||
|
||
const RainbowKitWagmiStateContext = | ||
createContext<RainbowKitWagmiStateContextValue>({ | ||
isDisconnecting: false, | ||
setIsDisconnecting: () => {}, | ||
}); | ||
|
||
interface RainbowKitWagmiStateProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export function RainbowKitWagmiStateProvider({ | ||
children, | ||
}: RainbowKitWagmiStateProviderProps) { | ||
// The 'status' state from the `useDisconnect` hook in wagmi can't be used for 'pending' logic, | ||
// as it doesn't share its state globally. It only shares its state within the same component (like useState). | ||
const [isDisconnecting, setIsDisconnecting] = useState(false); | ||
|
||
return ( | ||
<RainbowKitWagmiStateContext.Provider | ||
value={useMemo( | ||
() => ({ | ||
isDisconnecting, | ||
setIsDisconnecting, | ||
}), | ||
[isDisconnecting], | ||
)} | ||
> | ||
{children} | ||
</RainbowKitWagmiStateContext.Provider> | ||
); | ||
} | ||
|
||
export const useRainbowKitWagmiState = () => | ||
useContext(RainbowKitWagmiStateContext); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { useAccount } from 'wagmi'; | ||
import { useAuthenticationStatus } from '../components/RainbowKitProvider/AuthenticationContext'; | ||
import { useRainbowKitWagmiState } from '../components/RainbowKitProvider/RainbowKitWagmiStateProvider'; | ||
|
||
export type ConnectionStatus = | ||
| 'disconnected' | ||
|
@@ -10,8 +11,9 @@ export type ConnectionStatus = | |
export function useConnectionStatus(): ConnectionStatus { | ||
const authenticationStatus = useAuthenticationStatus(); | ||
const { isConnected } = useAccount(); | ||
const { isDisconnecting } = useRainbowKitWagmiState(); | ||
|
||
if (!isConnected) { | ||
if (!isConnected || isDisconnecting) { | ||
return 'disconnected'; | ||
} | ||
|
||
Comment on lines
13
to
19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the most important part out of all which makes this work |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You technically can still open the connect modal if your
connectionStatus
is returningdisconnected
.If we allow users to open the connect modal by the time WalletConnect takes time to disconnect, they'll face the issue with shim disconnection which is a known issue in wagmi rn. Ofc only if they start connecting different wallets at once, but we'll make sure to disable it.