Skip to content

Commit

Permalink
feat: Add callback once wallet is connected
Browse files Browse the repository at this point in the history
  • Loading branch information
wenty22 committed Oct 16, 2024
1 parent 8f4ba9a commit 3c2e720
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-yaks-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@node-real/walletkit': patch
---

Add callback once wallet is connected
2 changes: 1 addition & 1 deletion packages/walletkit/__dev__/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const config: WalletKitConfig = {
options: {
closeModalOnEsc: false,
// gridLayoutThreshold: 1000,
onChainAlreadyAdded(wallet, chainId) {
onChainAlreadyAdded({ wallet, chainId }) {
console.log(wallet, chainId);
},
},
Expand Down
12 changes: 9 additions & 3 deletions packages/walletkit/src/core/hooks/useAutoCloseConnectModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import { useConnectModal } from '../modals/ConnectModal/context';
import { useRouter } from '../modals/ConnectModal/RouteProvider/context';

export function useAutoCloseConnectModal(isConnected: boolean) {
const { options } = useWalletKit();
const { options, selectedWallet } = useWalletKit();

const router = useRouter();
const { onClose } = useConnectModal();

useEffect(() => {
if (router.route !== ViewRoutes.HOME && isConnected && options.closeModalAfterConnected) {
onClose();
if (router.route !== ViewRoutes.HOME && isConnected) {
options.onConnected?.({
wallet: selectedWallet,
});

if (options.closeModalAfterConnected) {
onClose();
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isConnected, router.route, options.closeModalAfterConnected]);
Expand Down
2 changes: 2 additions & 0 deletions packages/walletkit/src/core/modals/ConnectModal/context.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React, { useContext } from 'react';
import { ViewRoutes } from './RouteProvider';
import { Action } from '@/core/providers/WalletKitProvider/context';
import { BaseWallet } from '@/core/configs/types';

export interface ConnectModalOpenParams {
action?: Action;
viewRoute?: ViewRoutes;
initialChainId?: number;
onConnected?: (params: { wallet: BaseWallet }) => void;
}

export interface ConnectModalContextProps {
Expand Down
7 changes: 5 additions & 2 deletions packages/walletkit/src/core/modals/ConnectModal/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function ConnectModalProvider(props: ConnectModalProviderProps) {
function WithRouter(props: ConnectModalProviderProps) {
const { children } = props;

const { setAction, evmConfig } = useWalletKit();
const { setAction, evmConfig, options } = useWalletKit();
const { isOpen, onClose, onOpen } = useDisclosure();
const router = useRouter();

Expand All @@ -43,11 +43,14 @@ function WithRouter(props: ConnectModalProviderProps) {
if (evmConfig && params.initialChainId) {
evmConfig.initialChainId = params.initialChainId;
}
if (params.onConnected) {
options.onConnected = params.onConnected;
}

onOpen();
},
};
}, [evmConfig, isOpen, onClose, onOpen, router, setAction]);
}, [evmConfig, isOpen, onClose, onOpen, options, router, setAction]);

return <ConnectModalContext.Provider value={value}>{children}</ConnectModalContext.Provider>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export interface WalletKitConfig {
openModalOnWrongNetwork?: boolean;

onClickWallet?: (wallet: BaseWallet, e?: React.MouseEvent) => undefined | boolean;
onChainAlreadyAdded?: (wallet: BaseWallet, chainId: number) => void;
onChainAlreadyAdded?: (params: { wallet: BaseWallet; chainId: number }) => void;
onConnected?: (params: { wallet: BaseWallet }) => void;
onError?: (err: any, description: string) => void;
};

Expand Down
5 changes: 4 additions & 1 deletion packages/walletkit/src/evm/hooks/useConnectingStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export function useConnectingStatus(props: UseConnectingStatusProps = {}) {
data.chainId === evmConfig.initialChainId &&
action === 'add-network'
) {
options.onChainAlreadyAdded?.(selectedWallet, evmConfig?.initialChainId);
options.onChainAlreadyAdded?.({
wallet: selectedWallet,
chainId: data.chainId,
});
}
}
};
Expand Down

0 comments on commit 3c2e720

Please sign in to comment.