Skip to content

Commit 0fce69c

Browse files
committed
feat: add connect and disconnect status
1 parent 653e61a commit 0fce69c

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface ConnectButtonInnerProps extends ButtonProps {
1717
onConnectClick?: (wallet?: Wallet) => void;
1818
onDisconnectClick?: () => void;
1919
onOpenProfileClick?: () => void;
20+
onSignInClick?: () => void;
2021
intl: IntlType;
2122
}
2223

@@ -31,6 +32,7 @@ export const ConnectButtonInner: React.FC<ConnectButtonInnerProps> = (props) =>
3132
onConnectClick,
3233
onDisconnectClick,
3334
onOpenProfileClick,
35+
onSignInClick,
3436
intl,
3537
__hashId__,
3638
className,
@@ -114,6 +116,10 @@ export const ConnectButtonInner: React.FC<ConnectButtonInnerProps> = (props) =>
114116
if (needSign) {
115117
return (
116118
<Dropdown.Button
119+
onClick={(e) => {
120+
onClick?.(e);
121+
onSignInClick?.();
122+
}}
117123
menu={{
118124
items: [
119125
{ key: 'profile', label: '我的资料', onClick: onOpenProfileClick },

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import classNames from 'classnames';
77

88
import { Address } from '../address';
99
import { CryptoPrice } from '../crypto-price';
10-
import { useProvider } from '../hooks';
10+
import { useConnection, useProvider } from '../hooks';
1111
import useIntl from '../hooks/useIntl';
1212
import { fillWithPrefix, writeCopyText } from '../utils';
1313
import { ChainSelect } from './chain-select';
@@ -57,6 +57,7 @@ export const ConnectButton: React.FC<ConnectButtonProps> = (props) => {
5757
const { wrapSSR, hashId } = useStyle(prefixCls);
5858
const [messageApi, contextHolder] = message.useMessage();
5959
const [showMenu, setShowMenu] = useState(false);
60+
const [signed, setSigned] = useState(false);
6061

6162
const { coverAddress = true } = typeof balance !== 'object' ? { coverAddress: true } : balance;
6263
const needSign = !!sign?.signIn && account?.status === ConnectStatus.Connected;
@@ -99,6 +100,7 @@ export const ConnectButton: React.FC<ConnectButtonProps> = (props) => {
99100
try {
100101
if (needSign) {
101102
await sign?.signIn?.(account?.address);
103+
setSigned(true);
102104
}
103105
} catch (error: any) {
104106
messageApi.error(error.message);
@@ -162,6 +164,10 @@ export const ConnectButton: React.FC<ConnectButtonProps> = (props) => {
162164
);
163165
}
164166

167+
const unsigned = needSign && !signed;
168+
169+
console.log('unsigned:', unsigned, account?.status);
170+
165171
const buttonInnerText = (
166172
<div className={`${prefixCls}-content`}>
167173
<div className={`${prefixCls}-content-inner`}>
@@ -196,6 +202,42 @@ export const ConnectButton: React.FC<ConnectButtonProps> = (props) => {
196202
}}
197203
onDisconnectClick={onDisconnectClick}
198204
onOpenProfileClick={() => setProfileOpen(true)}
205+
onSignInClick={() => {
206+
if (!sign?.signIn) {
207+
return;
208+
}
209+
210+
console.log('onSignInClick:', account, needSign, signed);
211+
if (signed) {
212+
return;
213+
}
214+
215+
if (account?.status === ConnectStatus.Signed) {
216+
setSigned(true);
217+
return;
218+
}
219+
220+
// If account is not connected, we need to sign in
221+
// If account is connected but not signed, we also need to sign in
222+
console.log('signIn:', account?.address, needSign);
223+
if (account?.status === ConnectStatus.Connected && signed) {
224+
return;
225+
}
226+
227+
// If account is not connected, we need to sign in
228+
// If account is connected but not signed, we also need to sign in
229+
console.log('signIn:', account?.address, needSign);
230+
if (account && needSign) {
231+
sign
232+
.signIn?.(account.address!)
233+
.then(() => {
234+
setSigned(true);
235+
})
236+
.catch((error) => {
237+
messageApi.error(error.message);
238+
});
239+
}
240+
}}
199241
__hashId__={hashId}
200242
>
201243
{buttonInnerText}

0 commit comments

Comments
 (0)