Skip to content

Commit

Permalink
chore: improves the react connected hook when using extension
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherferreira9 committed Jan 8, 2025
1 parent d647181 commit 1bf6435
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
25 changes: 25 additions & 0 deletions packages/sdk-react/src/EventsHandlers/useHandleTerminateEvent.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>);
},
[debug, setConnecting, setConnected, setError],
);
};
11 changes: 10 additions & 1 deletion packages/sdk-react/src/MetaMaskProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -130,6 +131,8 @@ const MetaMaskProviderClient = ({
const onConnect = useHandleConnectEvent(eventHandlerProps);

const onDisconnect = useHandleDisconnectEvent(eventHandlerProps);

const onTerminate = useHandleTerminateEvent(eventHandlerProps);

const onAccountsChanged = useHandleAccountsChangedEvent(eventHandlerProps);

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export async function terminate(instance: MetaMaskSDK) {
}

if (instance.options.extensionOnly) {
instance.emit(
MetaMaskSDKEvent.ProviderUpdate,
PROVIDER_UPDATE_TYPE.TERMINATE,
);
logger(

Check failure on line 55 in packages/sdk/src/services/MetaMaskSDK/ConnectionManager/terminate.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (18.x)

Expected blank line before this statement
`[MetaMaskSDK: terminate()] extensionOnly --- prevent switching providers`,
);
Expand Down

0 comments on commit 1bf6435

Please sign in to comment.