Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/wagmi/src/wagmi-provider/config-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ export const AntDesignWeb3ConfigProvider: React.FC<AntDesignWeb3ConfigProviderPr
[siwe, currentChain, signMessageAsync],
);

const signOut = async () => {
console.log('call_signOut');
};

return (
<Web3ConfigProvider
locale={locale}
Expand All @@ -247,6 +251,7 @@ export const AntDesignWeb3ConfigProvider: React.FC<AntDesignWeb3ConfigProviderPr
sign={
siwe && {
signIn,
signOut,
}
}
balance={
Expand Down
101 changes: 74 additions & 27 deletions packages/web3/src/connect-button/connect-button-inner.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useContext, useEffect, useState } from 'react';
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';
import { MoreOutlined } from '@ant-design/icons';
import type { Wallet } from '@ant-design/web3-common';
import { ConnectStatus, type Account, type Wallet } from '@ant-design/web3-common';
import type { ButtonProps, MenuProps } from 'antd';
import { Button, ConfigProvider, Dropdown, Space } from 'antd';
import classNames from 'classnames';
Expand All @@ -13,7 +13,11 @@ export interface ConnectButtonInnerProps extends ButtonProps {
preContent: React.ReactNode;
showQuickConnect?: boolean;
availableWallets?: Wallet[];
needSign?: boolean;
onConnectClick?: (wallet?: Wallet) => void;
onDisconnectClick?: () => void;
onOpenProfileClick?: () => void;
onSignInClick?: () => void;
intl: IntlType;
}

Expand All @@ -26,25 +30,32 @@ export const ConnectButtonInner: React.FC<ConnectButtonInnerProps> = (props) =>
children,
onClick,
onConnectClick,
onDisconnectClick,
onOpenProfileClick,
onSignInClick,
intl,
__hashId__,
className,
needSign,
...restProps
} = props;
const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);
const prefixCls = getPrefixCls('web3-connect-button');
const [firstInstallWallet, setFirstInstallWallet] = useState<Wallet | undefined>(undefined);
const [items, setItems] = useState<MenuProps['items']>([]);

const getWalletIcon = (wallet: Wallet) => {
const icon = wallet.icon;
const getWalletIcon = useCallback(
(wallet: Wallet) => {
const icon = wallet.icon;

return (
<span className={classNames(__hashId__, `${prefixCls}-quick-connect-icon`)}>
{typeof icon === 'string' ? <img src={icon} alt={`${wallet.name} Icon`} /> : icon}
</span>
);
};
return (
<span className={classNames(__hashId__, `${prefixCls}-quick-connect-icon`)}>
{typeof icon === 'string' ? <img src={icon} alt={`${wallet.name} Icon`} /> : icon}
</span>
);
},
[__hashId__, prefixCls],
);

const generateQuickConnectItems = async (wallets: Wallet[] = []) => {
if (!showQuickConnect) {
Expand Down Expand Up @@ -100,23 +111,49 @@ export const ConnectButtonInner: React.FC<ConnectButtonInnerProps> = (props) =>
generateQuickConnectItems(availableWallets);
}, [availableWallets, showQuickConnect]);

const buttonContent =
showQuickConnect && firstInstallWallet ? (
<Dropdown.Button
{...restProps}
menu={{
items,
}}
className={classNames(className, `${prefixCls}-quick-connect`)}
onClick={(e) => {
onClick?.(e);
onConnectClick?.(firstInstallWallet);
}}
>
{children}
{getWalletIcon(firstInstallWallet)}
</Dropdown.Button>
) : (
const buttonContent = useMemo(() => {
// if ( account?.status === ConnectStatus.Connected) {
if (needSign) {
return (
<Dropdown.Button
onClick={(e) => {
onClick?.(e);
onSignInClick?.();
}}
menu={{
items: [
{ key: 'profile', label: '我的资料', onClick: onOpenProfileClick },
{ key: 'disconnect', label: '断开', onClick: onDisconnectClick },
],
}}
{...restProps}
className={className}
>
{children}
</Dropdown.Button>
);
}

if (showQuickConnect && firstInstallWallet) {
return (
<Dropdown.Button
{...restProps}
menu={{
items,
}}
className={classNames(className, `${prefixCls}-quick-connect`)}
onClick={(e) => {
onClick?.(e);
onConnectClick?.(firstInstallWallet);
}}
>
{children}
{getWalletIcon(firstInstallWallet)}
</Dropdown.Button>
);
}

return (
<Button
{...restProps}
className={className}
Expand All @@ -128,6 +165,16 @@ export const ConnectButtonInner: React.FC<ConnectButtonInnerProps> = (props) =>
{children}
</Button>
);
}, [
firstInstallWallet,
items,
needSign,
onClick,
onConnectClick,
onOpenProfileClick,
onDisconnectClick,
showQuickConnect,
]);

return preContent ? (
<Space.Compact>
Expand Down
55 changes: 52 additions & 3 deletions packages/web3/src/connect-button/connect-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import React, { useContext, useMemo, useState } from 'react';
import { CopyOutlined, LoginOutlined, UserOutlined } from '@ant-design/icons';
import { ConnectStatus, type Chain, type Wallet } from '@ant-design/web3-common';
import type { ButtonProps } from 'antd';
import { Avatar, ConfigProvider, Divider, Dropdown, message } from 'antd';
import { Avatar, Badge, ConfigProvider, Divider, Dropdown, message } from 'antd';
import classNames from 'classnames';

import { Address } from '../address';
import { CryptoPrice } from '../crypto-price';
import { useProvider } from '../hooks';
import { useConnection, useProvider } from '../hooks';
import useIntl from '../hooks/useIntl';
import { fillWithPrefix, writeCopyText } from '../utils';
import { ChainSelect } from './chain-select';
Expand Down Expand Up @@ -57,9 +57,11 @@ export const ConnectButton: React.FC<ConnectButtonProps> = (props) => {
const { wrapSSR, hashId } = useStyle(prefixCls);
const [messageApi, contextHolder] = message.useMessage();
const [showMenu, setShowMenu] = useState(false);
const [signed, setSigned] = useState(false);

const { coverAddress = true } = typeof balance !== 'object' ? { coverAddress: true } : balance;
const needSign = !!(sign?.signIn && account?.status === ConnectStatus.Connected && account);
const needSign = !!sign?.signIn && account?.status === ConnectStatus.Connected;

let buttonText: React.ReactNode = intl.getMessage(intl.messages.connect);
if (account) {
buttonText = (
Expand Down Expand Up @@ -98,6 +100,7 @@ export const ConnectButton: React.FC<ConnectButtonProps> = (props) => {
try {
if (needSign) {
await sign?.signIn?.(account?.address);
setSigned(true);
}
} catch (error: any) {
messageApi.error(error.message);
Expand Down Expand Up @@ -161,9 +164,14 @@ export const ConnectButton: React.FC<ConnectButtonProps> = (props) => {
);
}

const unsigned = needSign && !signed;

console.log('unsigned:', unsigned, account?.status);

const buttonInnerText = (
<div className={`${prefixCls}-content`}>
<div className={`${prefixCls}-content-inner`}>
{needSign && account.status !== ConnectStatus.Signed && <Badge status="error" />}
<div className={`${prefixCls}-text`}>{buttonText}</div>
{(account?.avatar || avatar) && (
<>
Expand All @@ -177,18 +185,59 @@ export const ConnectButton: React.FC<ConnectButtonProps> = (props) => {
</div>
);

console.log('signIn:', typeof sign?.signIn, account?.status);

const buttonContent = (
<ConnectButtonInner
intl={intl}
{...buttonProps}
preContent={chainSelectRender}
showQuickConnect={quickConnect && !account}
availableWallets={availableWallets}
needSign={needSign}
onConnectClick={(wallet?: Wallet) => {
if (!account) {
onConnectClick?.(wallet);
}
}}
onDisconnectClick={onDisconnectClick}
onOpenProfileClick={() => setProfileOpen(true)}
onSignInClick={() => {
if (!sign?.signIn) {
return;
}

console.log('onSignInClick:', account, needSign, signed);
if (signed) {
return;
}

if (account?.status === ConnectStatus.Signed) {
setSigned(true);
return;
}

// If account is not connected, we need to sign in
// If account is connected but not signed, we also need to sign in
console.log('signIn:', account?.address, needSign);
if (account?.status === ConnectStatus.Connected && signed) {
return;
}

// If account is not connected, we need to sign in
// If account is connected but not signed, we also need to sign in
console.log('signIn:', account?.address, needSign);
if (account && needSign) {
sign
.signIn?.(account.address!)
.then(() => {
setSigned(true);
})
.catch((error) => {
messageApi.error(error.message);
});
}
}}
__hashId__={hashId}
>
{buttonInnerText}
Expand Down
Loading