1- import React , { useContext , useEffect , useState } from 'react' ;
1+ import React , { useCallback , useContext , useEffect , useMemo , useState } from 'react' ;
22import { 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' ;
44import type { ButtonProps , MenuProps } from 'antd' ;
55import { Button , ConfigProvider , Dropdown , Space } from 'antd' ;
66import 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 >
0 commit comments