1- import { useCallback , useMemo } from 'react'
1+ import { useMemo } from 'react'
22
3+ import ActionButton from 'components/common/Button/ActionButton'
34import DropDownButton from 'components/common/Button/DropDownButton'
4- import { AccountArrowDown , ArrowUpLine , CoinsSwap , Enter } from 'components/common/Icons'
5+ import { ArrowUpLine , CoinsSwap } from 'components/common/Icons'
6+ import Text from 'components/common/Text'
7+ import { Tooltip } from 'components/common/Tooltip'
8+ import ConditionalWrapper from 'hocs/ConditionalWrapper'
59import useCurrentAccount from 'hooks/accounts/useCurrentAccount'
610import useDepositModal from 'hooks/common/useDepositModal'
711import useLendAndReclaimModal from 'hooks/common/useLendAndReclaimModal'
812import useCurrentAccountDeposits from 'hooks/wallet/useCurrentAccountDeposits'
913import useCurrentWalletBalance from 'hooks/wallet/useCurrentWalletBalance'
14+ import useAutoLend from 'hooks/wallet/useAutoLend'
1015import useStore from 'store'
1116import { byDenom } from 'utils/array'
17+ import { isDepositOnlyAsset } from 'utils/assets'
1218import { BN } from 'utils/helpers'
1319
1420export const LEND_BUTTON_META = {
@@ -29,39 +35,35 @@ export default function LendButton(props: Props) {
2935 const accountDeposits = useCurrentAccountDeposits ( )
3036 const currentAccount = useCurrentAccount ( )
3137 const walletBalance = useCurrentWalletBalance ( props . data . asset . denom )
32- const isAutoLendEnabled = props . data . asset . isAutoLendEnabled
3338 const assetDepositAmount = accountDeposits . find ( byDenom ( props . data . asset . denom ) ) ?. amount
3439 const address = useStore ( ( s ) => s . address )
3540 const hasWalletBalance = walletBalance && BN ( walletBalance . amount ) . isGreaterThan ( 0 )
36-
37- // Check if user has this specific asset in deposits or lends
38- const hasThisAssetInAccount = useMemo ( ( ) => {
39- const inDeposits = currentAccount ?. deposits ?. find ( byDenom ( props . data . asset . denom ) ) ?. amount
40- const inLends = currentAccount ?. lends ?. find ( byDenom ( props . data . asset . denom ) ) ?. amount
41- return ( inDeposits && ! inDeposits . isZero ( ) ) || ( inLends && ! inLends . isZero ( ) )
42- } , [ currentAccount ?. deposits , currentAccount ?. lends , props . data . asset . denom ] )
43-
44- const handleWithdraw = useCallback ( ( ) => {
45- useStore . setState ( { fundAndWithdrawModal : 'withdraw' } )
46- } , [ ] )
41+ const isDefaultAccount = currentAccount ?. kind === 'default'
42+ const isLendEnabled = ! ! props . data . asset . isAutoLendEnabled
43+ const { isAutoLendEnabledForCurrentAccount } = useAutoLend ( )
44+ const isDepositOnly = isDepositOnlyAsset ( props . data . asset )
4745
4846 const ITEMS : DropDownItem [ ] = useMemo (
4947 ( ) => [
50- ...( hasWalletBalance
48+ {
49+ icon : < ArrowUpLine /> ,
50+ text : 'Deposit' ,
51+ onClick : ( ) => {
52+ openDeposit ( props . data )
53+ } ,
54+ } ,
55+ ...( isLendEnabled
5156 ? [
52- {
53- icon : < Enter /> ,
54- text : 'Deposit' ,
55- onClick : ( ) => openDeposit ( props . data ) ,
56- } ,
5757 {
5858 icon : < CoinsSwap /> ,
5959 text : 'Deposit & Lend' ,
60- onClick : ( ) => openDepositAndLend ( props . data ) ,
60+ onClick : ( ) => {
61+ openDepositAndLend ( props . data )
62+ } ,
6163 } ,
6264 ]
6365 : [ ] ) ,
64- ...( assetDepositAmount
66+ ...( isLendEnabled && assetDepositAmount
6567 ? [
6668 {
6769 icon : < ArrowUpLine /> ,
@@ -70,35 +72,100 @@ export default function LendButton(props: Props) {
7072 } ,
7173 ]
7274 : [ ] ) ,
73- ...( hasThisAssetInAccount
74- ? [
75- {
76- icon : < AccountArrowDown /> ,
77- text : 'Withdraw' ,
78- onClick : handleWithdraw ,
79- } ,
80- ]
81- : [ ] ) ,
82- ] ,
83- [
84- assetDepositAmount ,
85- hasThisAssetInAccount ,
86- hasWalletBalance ,
87- handleWithdraw ,
88- openDeposit ,
89- openDepositAndLend ,
90- openLend ,
91- props . data ,
9275 ] ,
76+ [ assetDepositAmount , isLendEnabled , openLend , openDeposit , openDepositAndLend , props . data ] ,
9377 )
9478
95- if ( ! isAutoLendEnabled && address ) return null
96- if ( ! address ) return null
97- if ( ITEMS . length === 0 ) return null
79+ // If user has wallet balance and it's a default account, show Manage dropdown
80+ if ( hasWalletBalance && address && isDefaultAccount ) {
81+ return (
82+ < div className = 'flex justify-end' >
83+ < DropDownButton items = { ITEMS } text = 'Manage' color = 'tertiary' />
84+ </ div >
85+ )
86+ }
87+
88+ // For LSTs, show Deposit button only
89+ if ( isDepositOnly ) {
90+ return (
91+ < div className = 'flex justify-end' >
92+ < ConditionalWrapper
93+ condition = { ! hasWalletBalance && ! ! address }
94+ wrapper = { ( children ) => (
95+ < Tooltip
96+ type = 'warning'
97+ content = {
98+ < Text size = 'sm' > { `You don't have any ${ props . data . asset . symbol } in your wallet.` } </ Text >
99+ }
100+ contentClassName = 'max-w-[200px]'
101+ className = 'ml-auto'
102+ >
103+ { children }
104+ </ Tooltip >
105+ ) }
106+ >
107+ < ActionButton
108+ leftIcon = { < ArrowUpLine /> }
109+ disabled = { ! hasWalletBalance }
110+ color = 'tertiary'
111+ onClick = { ( e ) => {
112+ openDeposit ( props . data )
113+ e . stopPropagation ( )
114+ } }
115+ text = 'Deposit'
116+ short
117+ />
118+ </ ConditionalWrapper >
119+ </ div >
120+ )
121+ }
122+
123+ // If asset doesn't support lending, don't show anything
124+ if ( ! isLendEnabled ) return null
98125
99126 return (
100127 < div className = 'flex justify-end' >
101- < DropDownButton items = { ITEMS } text = 'Manage' color = 'tertiary' />
128+ < ConditionalWrapper
129+ condition = { ! hasWalletBalance && ! ! address }
130+ wrapper = { ( children ) => (
131+ < Tooltip
132+ type = 'warning'
133+ content = {
134+ < Text size = 'sm' > { `You don't have any ${ props . data . asset . symbol } in your wallet.` } </ Text >
135+ }
136+ contentClassName = 'max-w-[200px]'
137+ className = 'ml-auto'
138+ >
139+ { children }
140+ </ Tooltip >
141+ ) }
142+ >
143+ { isAutoLendEnabledForCurrentAccount ? (
144+ < ActionButton
145+ leftIcon = { < ArrowUpLine /> }
146+ disabled = { ! hasWalletBalance }
147+ color = 'tertiary'
148+ onClick = { ( e ) => {
149+ openLend ( props . data )
150+ e . stopPropagation ( )
151+ } }
152+ text = 'Lend'
153+ short
154+ />
155+ ) : (
156+ < ActionButton
157+ leftIcon = { < ArrowUpLine /> }
158+ disabled = { ! hasWalletBalance }
159+ color = 'tertiary'
160+ onClick = { ( e ) => {
161+ openDeposit ( props . data )
162+ e . stopPropagation ( )
163+ } }
164+ text = 'Deposit'
165+ short
166+ />
167+ ) }
168+ </ ConditionalWrapper >
102169 </ div >
103170 )
104171}
0 commit comments