From 1bf6435a60475a9200365a21af7d1468f5dbc2fe Mon Sep 17 00:00:00 2001 From: Christopher Ferreira Date: Wed, 8 Jan 2025 12:37:49 +0000 Subject: [PATCH] chore: improves the react connected hook when using extension --- .../EventsHandlers/useHandleTerminateEvent.ts | 25 +++++++++++++++++++ packages/sdk-react/src/MetaMaskProvider.tsx | 11 +++++++- .../ConnectionManager/terminate.ts | 4 +++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 packages/sdk-react/src/EventsHandlers/useHandleTerminateEvent.ts diff --git a/packages/sdk-react/src/EventsHandlers/useHandleTerminateEvent.ts b/packages/sdk-react/src/EventsHandlers/useHandleTerminateEvent.ts new file mode 100644 index 000000000..cd308f54b --- /dev/null +++ b/packages/sdk-react/src/EventsHandlers/useHandleTerminateEvent.ts @@ -0,0 +1,25 @@ +import { EthereumRpcError } from 'eth-rpc-errors'; +import { useCallback } from 'react'; +import { EventHandlerProps } from '../MetaMaskProvider'; +import { logger } from '../utils/logger'; + +export const useHandleTerminateEvent = ({ + debug, + setConnecting, + setConnected, + setError, +}: EventHandlerProps) => { + return useCallback( + (reason: unknown) => { + logger( + `[MetaMaskProvider: useHandleTerminateEvent()] on 'terminate' event.`, + reason, + ); + + setConnecting(false); + setConnected(false); + setError(reason as EthereumRpcError); + }, + [debug, setConnecting, setConnected, setError], + ); +}; diff --git a/packages/sdk-react/src/MetaMaskProvider.tsx b/packages/sdk-react/src/MetaMaskProvider.tsx index 1f28a9373..edecfadf5 100644 --- a/packages/sdk-react/src/MetaMaskProvider.tsx +++ b/packages/sdk-react/src/MetaMaskProvider.tsx @@ -23,6 +23,7 @@ import { useHandleInitializedEvent } from './EventsHandlers/useHandleInitialized import { useHandleOnConnectingEvent } from './EventsHandlers/useHandleOnConnectingEvent'; import { useHandleProviderEvent } from './EventsHandlers/useHandleProviderEvent'; import { useHandleSDKStatusEvent } from './EventsHandlers/useHandleSDKStatusEvent'; +import { useHandleTerminateEvent } from './EventsHandlers/useHandleTerminateEvent'; import { logger } from './utils/logger'; export interface EventHandlerProps { @@ -130,6 +131,8 @@ const MetaMaskProviderClient = ({ const onConnect = useHandleConnectEvent(eventHandlerProps); const onDisconnect = useHandleDisconnectEvent(eventHandlerProps); + + const onTerminate = useHandleTerminateEvent(eventHandlerProps); const onAccountsChanged = useHandleAccountsChangedEvent(eventHandlerProps); @@ -262,12 +265,18 @@ const MetaMaskProviderClient = ({ console.warn(`[MetaMaskProviderClient] activeProvider is undefined.`); return; } - setConnected(activeProvider.isConnected()); + + const isConnected = sdk.isExtensionActive() + ? !!account && account.length > 0 + : activeProvider.isConnected(); + + setConnected(isConnected); setAccount(activeProvider.getSelectedAddress() || undefined); setProvider(activeProvider); setChainId(activeProvider.getChainId() || undefined); activeProvider.on('_initialized', onInitialized); + activeProvider.on('terminate', onTerminate); activeProvider.on('connecting', onConnecting); activeProvider.on('connect', onConnect); activeProvider.on('disconnect', onDisconnect); diff --git a/packages/sdk/src/services/MetaMaskSDK/ConnectionManager/terminate.ts b/packages/sdk/src/services/MetaMaskSDK/ConnectionManager/terminate.ts index 2f6d44cf0..72ae77821 100644 --- a/packages/sdk/src/services/MetaMaskSDK/ConnectionManager/terminate.ts +++ b/packages/sdk/src/services/MetaMaskSDK/ConnectionManager/terminate.ts @@ -48,6 +48,10 @@ export async function terminate(instance: MetaMaskSDK) { } if (instance.options.extensionOnly) { + instance.emit( + MetaMaskSDKEvent.ProviderUpdate, + PROVIDER_UPDATE_TYPE.TERMINATE, + ); logger( `[MetaMaskSDK: terminate()] extensionOnly --- prevent switching providers`, );