Skip to content

Commit

Permalink
feat: Remove close button in EmbeddedModal & Add address to onConnect…
Browse files Browse the repository at this point in the history
…ed callback (#244)
  • Loading branch information
wenty22 authored Dec 20, 2024
1 parent 6a74e3c commit 8b72b41
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-icons-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@node-real/walletkit': patch
---

Remove close button in EmbeddedModal & Add address to onConnected callback
6 changes: 3 additions & 3 deletions packages/walletkit/__dev__/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ import { bsc, mainnet } from 'viem/chains';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { useAccount, useDisconnect } from 'wagmi';
import { defaultTronConfig, tronLink, useTronWallet } from '@/tron/index';
import { Modal } from '@/core/base/components/Modal';
import { ModalBody } from '@/core/base/components/Modal/ModalBody';
import { useDisclosure } from '@/core/base/hooks/useDisclosure';
import { SwitchNetworkModal } from '@/core/modals/SwitchNetworkModal';

new VConsole();
Expand Down Expand Up @@ -111,6 +108,9 @@ function ConnectButton() {
tronConfig: {
initialChainId: '0xcd8690dc',
},
onConnected(params) {
console.log(params, '====xx');
},
})
}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useWalletKit } from '../providers/WalletKitProvider/context';
import { useConnectModal } from '../modals/ConnectModal/context';
import { useRouter } from '../providers/RouteProvider/context';

export function useAutoCloseConnectModal(isConnected: boolean) {
export function useAutoCloseConnectModal(isConnected: boolean, address: string | undefined | null) {
const { options, selectedWallet } = useWalletKit();

const router = useRouter();
Expand All @@ -14,6 +14,7 @@ export function useAutoCloseConnectModal(isConnected: boolean) {
if (router.route !== ViewRoutes.HOME && isConnected) {
options.onConnected?.({
wallet: selectedWallet,
address: address!,
});

if (options.closeModalAfterConnected) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ interface TemplateConnectingViewProps {
onTryAgain: () => void;
wallet: BaseWallet;
isConnected: boolean;
address: string | undefined | null;
}

export function TemplateConnectingView(props: TemplateConnectingViewProps) {
const { status, runConnect, onTryAgain, wallet, isConnected } = props;
const { status, runConnect, onTryAgain, wallet, isConnected, address } = props;

const { log } = useWalletKit();
const logos = useWalletLogos(wallet.logos);
Expand All @@ -54,7 +55,7 @@ export function TemplateConnectingView(props: TemplateConnectingViewProps) {

const isLoading = status === CONNECT_STATUS.CONNECTING;

useAutoCloseConnectModal(isConnected);
useAutoCloseConnectModal(isConnected, address);

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ export interface TemplateQRCodeViewProps {
onClickOpenWcModal: () => void;
isConnected: boolean;
isWalletConnect: boolean;
address: string | undefined | null;
}

export function TemplateQRCodeView(props: TemplateQRCodeViewProps) {
const { wallet, qrCodeUri, onClickOpenWcModal, isConnected, isWalletConnect } = props;
const { wallet, qrCodeUri, onClickOpenWcModal, isConnected, isWalletConnect, address } = props;

const { options } = useWalletKit();
const logos = useWalletLogos(wallet?.logos);

useAutoCloseConnectModal(isConnected);
useAutoCloseConnectModal(isConnected, address);

return (
<>
Expand Down
2 changes: 1 addition & 1 deletion packages/walletkit/src/core/modals/ConnectModal/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface ConnectModalOpenParams {
evmConfig?: {
initialChainId?: number;
};
onConnected?: (params: { wallet: BaseWallet }) => void;
onConnected?: (params: { wallet: BaseWallet; address: string }) => void;
}

export interface ConnectModalContextProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function EmbeddedConnectModal(props: BoxProps) {
return (
<Box className={cx('wk-embedded-connect-modal', clsModal, className)} {...restProps}>
<ModalContent className={clsContent} {...restProps}>
<Navbar showBack={history.length > 1} showClose={true} onBack={back} />
<Navbar showBack={history.length > 1} showClose={false} onBack={back} />
{view}
</ModalContent>
</Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface WalletKitConfig {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { useIsConnected } from '@/evm/hooks/useIsConnected';
import { useWalletConnector } from '@/evm/hooks/useWalletConnector';
import { useCallback } from 'react';
import { useConnectingStatus } from '@/evm/hooks/useConnectingStatus';
import { useAccount } from 'wagmi';

export function EvmConnectingView() {
const { selectedWallet } = useWalletKit();
const isConnected = useIsConnected();
const selectedConnector = useWalletConnector(selectedWallet.id);

const { connect, status, setStatus } = useConnectingStatus();
const { address } = useAccount();

const runConnect = useCallback(() => {
if (!selectedWallet.isInstalled()) return;
Expand All @@ -31,6 +33,7 @@ export function EvmConnectingView() {
onTryAgain={runConnect}
wallet={selectedWallet}
isConnected={isConnected}
address={address}
/>
);
}
3 changes: 3 additions & 0 deletions packages/walletkit/src/evm/components/EvmQRCodeView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useIsConnected } from '@/evm/hooks/useIsConnected';
import { useWalletConnectUri } from '@/evm/hooks/useWalletConnectUri';
import { useWalletConnectModal } from '@/evm/hooks/useWalletConnectModal';
import { EvmWallet, isWalletConnect } from '@/evm/wallets';
import { useAccount } from 'wagmi';

export function EvmQRCodeView() {
const { selectedWallet } = useWalletKit();
Expand All @@ -12,6 +13,7 @@ export function EvmQRCodeView() {
const wcModal = useWalletConnectModal();
const qrCodeUri = wcUri && ((selectedWallet as EvmWallet).getUri?.(wcUri) ?? wcUri);
const isConnected = useIsConnected();
const { address } = useAccount();

return (
<TemplateQRCodeView
Expand All @@ -20,6 +22,7 @@ export function EvmQRCodeView() {
onClickOpenWcModal={wcModal.onOpen}
isConnected={isConnected}
isWalletConnect={isWalletConnect(selectedWallet.id)}
address={address}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { EvmWallet } from '@/evm/wallets';
import { openLink } from '@/core/utils/common';
import { useWalletConnectUri } from '@/evm/hooks/useWalletConnectUri';
import { useConnectingStatus } from '@/evm/hooks/useConnectingStatus';
import { useAccount } from 'wagmi';

export function EvmUriConnectingView() {
const { selectedWallet } = useWalletKit();
const isConnected = useIsConnected();
const { address } = useAccount();

const { status, setStatus } = useConnectingStatus({
initialStatus: CONNECT_STATUS.CONNECTING,
Expand All @@ -34,6 +36,7 @@ export function EvmUriConnectingView() {
runConnect={() => undefined}
onTryAgain={onTryAgain}
wallet={selectedWallet}
address={address}
/>
);
}
1 change: 1 addition & 0 deletions packages/walletkit/src/evm/wallets/metaMask/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function metaMask(props: MetaMaskOptions = {}): EvmWallet {
getCreateConnectorFn() {
return metaMaskSDk({
useDeeplink: false,
headless: true,
openDeeplink(arg) {
openLink(arg);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { EventEmitter } from '@/core/utils/eventEmitter';
import { useSolanaConnect } from '@/solana/hooks/useSolanaConnect';
import { solanaCommonErrorHandler } from '@/solana/utils/solanaCommonErrorHandler';
import { SolanaWallet } from '@/solana/wallets';
import { WalletProviderProps } from '@solana/wallet-adapter-react';
import { useWallet, WalletProviderProps } from '@solana/wallet-adapter-react';
import { useCallback, useEffect, useState } from 'react';

type WalletError = Parameters<Required<WalletProviderProps>['onError']>[0];
Expand All @@ -18,6 +18,7 @@ export function SolanaConnectingView() {
);

const { isConnected, connect } = useSolanaConnect();
const { publicKey } = useWallet();

useEffect(() => {
const onError = (error: WalletError) => {
Expand Down Expand Up @@ -64,6 +65,7 @@ export function SolanaConnectingView() {
onTryAgain={runConnect}
wallet={selectedWallet}
isConnected={isConnected}
address={publicKey?.toBase58()}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { EventEmitter } from '@/core/utils/eventEmitter';
import { useTronConnect } from '@/tron/hooks/useTronConnect';
import { tronCommonErrorHandler } from '@/tron/utils/tronCommonErrorHandler';
import { TronWallet } from '@/tron/wallets';
import { WalletProviderProps } from '@tronweb3/tronwallet-adapter-react-hooks';
import { useWallet, WalletProviderProps } from '@tronweb3/tronwallet-adapter-react-hooks';
import { useCallback, useEffect, useState } from 'react';

type WalletError = Parameters<Required<WalletProviderProps>['onError']>[0];
Expand All @@ -18,6 +18,7 @@ export function TronConnectingView() {
);

const { connect, isConnected } = useTronConnect();
const { address } = useWallet();

useEffect(() => {
const onError = (error: WalletError) => {
Expand Down Expand Up @@ -62,6 +63,7 @@ export function TronConnectingView() {
onTryAgain={runConnect}
wallet={selectedWallet}
isConnected={isConnected}
address={address}
/>
);
}
2 changes: 1 addition & 1 deletion website/src/pages/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ export interface WalletKitConfig {
onChainAlreadyAdded?: (params: { wallet: BaseWallet; chainId: number }) => void;

// Once wallet is connected, this method will be invoked.
onConnected?: (params: { wallet: BaseWallet }) => void;
onConnected?: (params: { wallet: BaseWallet; address: string }) => void;

// handle wallet errors by yourself
onError?: (err: any, description: string) => void;
Expand Down

0 comments on commit 8b72b41

Please sign in to comment.