Skip to content

Commit 653e61a

Browse files
committed
feat: support sign status
1 parent 80a3684 commit 653e61a

File tree

3 files changed

+82
-29
lines changed

3 files changed

+82
-29
lines changed

packages/wagmi/src/wagmi-provider/config-provider.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ export const AntDesignWeb3ConfigProvider: React.FC<AntDesignWeb3ConfigProviderPr
238238
[siwe, currentChain, signMessageAsync],
239239
);
240240

241+
const signOut = async () => {
242+
console.log('call_signOut');
243+
};
244+
241245
return (
242246
<Web3ConfigProvider
243247
locale={locale}
@@ -247,6 +251,7 @@ export const AntDesignWeb3ConfigProvider: React.FC<AntDesignWeb3ConfigProviderPr
247251
sign={
248252
siwe && {
249253
signIn,
254+
signOut,
250255
}
251256
}
252257
balance={

packages/web3/src/connect-button/connect-button-inner.tsx

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React, { useContext, useEffect, useState } from 'react';
1+
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';
22
import { MoreOutlined } from '@ant-design/icons';
3-
import type { Wallet } from '@ant-design/web3-common';
3+
import { ConnectStatus, type Account, type Wallet } from '@ant-design/web3-common';
44
import type { ButtonProps, MenuProps } from 'antd';
55
import { Button, ConfigProvider, Dropdown, Space } from 'antd';
66
import classNames from 'classnames';
@@ -13,7 +13,10 @@ export interface ConnectButtonInnerProps extends ButtonProps {
1313
preContent: React.ReactNode;
1414
showQuickConnect?: boolean;
1515
availableWallets?: Wallet[];
16+
needSign?: boolean;
1617
onConnectClick?: (wallet?: Wallet) => void;
18+
onDisconnectClick?: () => void;
19+
onOpenProfileClick?: () => void;
1720
intl: IntlType;
1821
}
1922

@@ -26,25 +29,31 @@ export const ConnectButtonInner: React.FC<ConnectButtonInnerProps> = (props) =>
2629
children,
2730
onClick,
2831
onConnectClick,
32+
onDisconnectClick,
33+
onOpenProfileClick,
2934
intl,
3035
__hashId__,
3136
className,
37+
needSign,
3238
...restProps
3339
} = props;
3440
const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);
3541
const prefixCls = getPrefixCls('web3-connect-button');
3642
const [firstInstallWallet, setFirstInstallWallet] = useState<Wallet | undefined>(undefined);
3743
const [items, setItems] = useState<MenuProps['items']>([]);
3844

39-
const getWalletIcon = (wallet: Wallet) => {
40-
const icon = wallet.icon;
45+
const getWalletIcon = useCallback(
46+
(wallet: Wallet) => {
47+
const icon = wallet.icon;
4148

42-
return (
43-
<span className={classNames(__hashId__, `${prefixCls}-quick-connect-icon`)}>
44-
{typeof icon === 'string' ? <img src={icon} alt={`${wallet.name} Icon`} /> : icon}
45-
</span>
46-
);
47-
};
49+
return (
50+
<span className={classNames(__hashId__, `${prefixCls}-quick-connect-icon`)}>
51+
{typeof icon === 'string' ? <img src={icon} alt={`${wallet.name} Icon`} /> : icon}
52+
</span>
53+
);
54+
},
55+
[__hashId__, prefixCls],
56+
);
4857

4958
const generateQuickConnectItems = async (wallets: Wallet[] = []) => {
5059
if (!showQuickConnect) {
@@ -100,23 +109,45 @@ export const ConnectButtonInner: React.FC<ConnectButtonInnerProps> = (props) =>
100109
generateQuickConnectItems(availableWallets);
101110
}, [availableWallets, showQuickConnect]);
102111

103-
const buttonContent =
104-
showQuickConnect && firstInstallWallet ? (
105-
<Dropdown.Button
106-
{...restProps}
107-
menu={{
108-
items,
109-
}}
110-
className={classNames(className, `${prefixCls}-quick-connect`)}
111-
onClick={(e) => {
112-
onClick?.(e);
113-
onConnectClick?.(firstInstallWallet);
114-
}}
115-
>
116-
{children}
117-
{getWalletIcon(firstInstallWallet)}
118-
</Dropdown.Button>
119-
) : (
112+
const buttonContent = useMemo(() => {
113+
// if ( account?.status === ConnectStatus.Connected) {
114+
if (needSign) {
115+
return (
116+
<Dropdown.Button
117+
menu={{
118+
items: [
119+
{ key: 'profile', label: '我的资料', onClick: onOpenProfileClick },
120+
{ key: 'disconnect', label: '断开', onClick: onDisconnectClick },
121+
],
122+
}}
123+
{...restProps}
124+
className={className}
125+
>
126+
{children}
127+
</Dropdown.Button>
128+
);
129+
}
130+
131+
if (showQuickConnect && firstInstallWallet) {
132+
return (
133+
<Dropdown.Button
134+
{...restProps}
135+
menu={{
136+
items,
137+
}}
138+
className={classNames(className, `${prefixCls}-quick-connect`)}
139+
onClick={(e) => {
140+
onClick?.(e);
141+
onConnectClick?.(firstInstallWallet);
142+
}}
143+
>
144+
{children}
145+
{getWalletIcon(firstInstallWallet)}
146+
</Dropdown.Button>
147+
);
148+
}
149+
150+
return (
120151
<Button
121152
{...restProps}
122153
className={className}
@@ -128,6 +159,16 @@ export const ConnectButtonInner: React.FC<ConnectButtonInnerProps> = (props) =>
128159
{children}
129160
</Button>
130161
);
162+
}, [
163+
firstInstallWallet,
164+
items,
165+
needSign,
166+
onClick,
167+
onConnectClick,
168+
onOpenProfileClick,
169+
onDisconnectClick,
170+
showQuickConnect,
171+
]);
131172

132173
return preContent ? (
133174
<Space.Compact>

packages/web3/src/connect-button/connect-button.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useContext, useMemo, useState } from 'react';
22
import { CopyOutlined, LoginOutlined, UserOutlined } from '@ant-design/icons';
33
import { ConnectStatus, type Chain, type Wallet } from '@ant-design/web3-common';
44
import type { ButtonProps } from 'antd';
5-
import { Avatar, ConfigProvider, Divider, Dropdown, message } from 'antd';
5+
import { Avatar, Badge, ConfigProvider, Divider, Dropdown, message } from 'antd';
66
import classNames from 'classnames';
77

88
import { Address } from '../address';
@@ -59,7 +59,8 @@ export const ConnectButton: React.FC<ConnectButtonProps> = (props) => {
5959
const [showMenu, setShowMenu] = useState(false);
6060

6161
const { coverAddress = true } = typeof balance !== 'object' ? { coverAddress: true } : balance;
62-
const needSign = !!(sign?.signIn && account?.status === ConnectStatus.Connected && account);
62+
const needSign = !!sign?.signIn && account?.status === ConnectStatus.Connected;
63+
6364
let buttonText: React.ReactNode = intl.getMessage(intl.messages.connect);
6465
if (account) {
6566
buttonText = (
@@ -164,6 +165,7 @@ export const ConnectButton: React.FC<ConnectButtonProps> = (props) => {
164165
const buttonInnerText = (
165166
<div className={`${prefixCls}-content`}>
166167
<div className={`${prefixCls}-content-inner`}>
168+
{needSign && account.status !== ConnectStatus.Signed && <Badge status="error" />}
167169
<div className={`${prefixCls}-text`}>{buttonText}</div>
168170
{(account?.avatar || avatar) && (
169171
<>
@@ -177,18 +179,23 @@ export const ConnectButton: React.FC<ConnectButtonProps> = (props) => {
177179
</div>
178180
);
179181

182+
console.log('signIn:', typeof sign?.signIn, account?.status);
183+
180184
const buttonContent = (
181185
<ConnectButtonInner
182186
intl={intl}
183187
{...buttonProps}
184188
preContent={chainSelectRender}
185189
showQuickConnect={quickConnect && !account}
186190
availableWallets={availableWallets}
191+
needSign={needSign}
187192
onConnectClick={(wallet?: Wallet) => {
188193
if (!account) {
189194
onConnectClick?.(wallet);
190195
}
191196
}}
197+
onDisconnectClick={onDisconnectClick}
198+
onOpenProfileClick={() => setProfileOpen(true)}
192199
__hashId__={hashId}
193200
>
194201
{buttonInnerText}

0 commit comments

Comments
 (0)