From ec697240bb3f6ff4d58843d1dc6a873af92dd644 Mon Sep 17 00:00:00 2001 From: Nadai <112663528+Nadai2010@users.noreply.github.com> Date: Wed, 21 Aug 2024 16:59:26 +0100 Subject: [PATCH] Add workaround on wallet connectors to remove from localStorage (#122) --- packages/nextjs/services/web3/connectors.tsx | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/nextjs/services/web3/connectors.tsx b/packages/nextjs/services/web3/connectors.tsx index 82872b75..05584560 100644 --- a/packages/nextjs/services/web3/connectors.tsx +++ b/packages/nextjs/services/web3/connectors.tsx @@ -1,4 +1,4 @@ -import { argent, braavos } from "@starknet-react/core"; +import { argent, braavos, InjectedConnector } from "@starknet-react/core"; import { getTargetNetworks } from "~~/utils/scaffold-stark"; import { BurnerConnector } from "./stark-burner/BurnerConnector"; import scaffoldConfig from "~~/scaffold.config"; @@ -7,6 +7,17 @@ const targetNetworks = getTargetNetworks(); export const connectors = getConnectors(); +// workaround helper function to properly disconnect with removing local storage (prevent autoconnect infinite loop) +function withDisconnectWrapper(connector: InjectedConnector) { + const connectorDisconnect = connector.disconnect; + const _disconnect = (): Promise => { + localStorage.removeItem("lastUsedConnector"); + return connectorDisconnect(); + }; + connector.disconnect = _disconnect.bind(connector); + return connector; +} + function getConnectors() { const { targetNetworks } = scaffoldConfig; @@ -18,7 +29,7 @@ function getConnectors() { connectors.push(new BurnerConnector()); } - return connectors.sort(() => Math.random() - 0.5); + return connectors.sort(() => Math.random() - 0.5).map(withDisconnectWrapper); } export const appChains = targetNetworks;