From 2c15f4a9166f8c65d564cbaf31347a869c59cb20 Mon Sep 17 00:00:00 2001
From: robertu <4065233+robertu7@users.noreply.github.com>
Date: Tue, 4 Jun 2024 21:18:59 +0800
Subject: [PATCH] fix(auth): force to disconnect if failed to switch network

---
 lang/zh-Hant.json                       |  2 +-
 src/components/Hook/useTargetNetwork.ts | 14 ++++++++++++--
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/lang/zh-Hant.json b/lang/zh-Hant.json
index e62ccf1dba..3bf9fe6252 100644
--- a/lang/zh-Hant.json
+++ b/lang/zh-Hant.json
@@ -766,7 +766,7 @@
     "description": "src/views/Circle/Analytics/FollowerAnalytics/index.tsx"
   },
   "Bkx6Gk": {
-    "defaultMessage": "切換網絡中…"
+    "defaultMessage": "切換網路中…"
   },
   "BnMru1": {
     "defaultMessage": "語言",
diff --git a/src/components/Hook/useTargetNetwork.ts b/src/components/Hook/useTargetNetwork.ts
index b920872070..028205db25 100644
--- a/src/components/Hook/useTargetNetwork.ts
+++ b/src/components/Hook/useTargetNetwork.ts
@@ -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
@@ -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,