Skip to content
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

fix(auth): force to disconnect if failed to switch network #4511

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lang/zh-Hant.json
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@
"description": "src/views/Circle/Analytics/FollowerAnalytics/index.tsx"
},
"Bkx6Gk": {
"defaultMessage": "切換網絡中…"
"defaultMessage": "切換網路中…"
},
"BnMru1": {
"defaultMessage": "語言",
Expand Down
14 changes: 12 additions & 2 deletions src/components/Hook/useTargetNetwork.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Chain, useNetwork, useSwitchNetwork } from 'wagmi'
import { useEffect } from 'react'
import { Chain, useDisconnect, useNetwork, useSwitchNetwork } from 'wagmi'

export const useTargetNetwork = (target: Chain) => {
const { disconnect } = useDisconnect()
const { chain: currentChain } = useNetwork()
const { switchNetwork, isLoading } = useSwitchNetwork()
const { switchNetwork, isLoading, error } = useSwitchNetwork()

const isUnsupportedNetwork = currentChain?.id !== target.id
const targetChainId = target.id
Expand All @@ -12,6 +14,14 @@ export const useTargetNetwork = (target: Chain) => {
switchNetwork(targetChainId)
}

// disconnect if failed to switch network
useEffect(() => {
if (!error) return
if (error.name === 'UserRejectedRequestError') return

disconnect()
}, [error])

return {
isUnsupportedNetwork,
isSwitchingNetwork: isLoading,
Expand Down
Loading