11import { Box , Typography , useTheme } from '@mui/material' ;
22import type { NextPage } from 'next' ;
33import { useRouter } from 'next/router' ;
4+ import { useState } from 'react' ;
45import { GoBackHeader } from '../components/common/GoBackHeader' ;
56import { RateDisplay } from '../components/common/RateDisplay' ;
67import { ReserveDetailsBar } from '../components/common/ReserveDetailsBar' ;
78import { Row } from '../components/common/Row' ;
89import { Section , SectionSize } from '../components/common/Section' ;
910import { StackedText } from '../components/common/StackedText' ;
11+ import { ToggleButton } from '../components/common/ToggleButton' ;
12+ import { TooltipText } from '../components/common/TooltipText' ;
1013import { NotPoolBar } from '../components/pool/NotPoolBar' ;
1114import { WithdrawAnvil } from '../components/withdraw/WithdrawAnvil' ;
15+ import { useSettings , ViewType } from '../contexts' ;
1216import {
1317 useBackstop ,
1418 usePool ,
@@ -23,6 +27,7 @@ import { estimateEmissionsApr } from '../utils/math';
2327
2428const Withdraw : NextPage = ( ) => {
2529 const theme = useTheme ( ) ;
30+ const { viewType } = useSettings ( ) ;
2631
2732 const router = useRouter ( ) ;
2833 const { poolId, assetId } = router . query ;
@@ -38,7 +43,10 @@ const Withdraw: NextPage = () => {
3843 const reserve = pool ?. reserves . get ( safeAssetId ) ;
3944 const tokenSymbol = tokenMetadata ?. symbol ?? toCompactAddress ( safeAssetId ) ;
4045
41- const currentDeposit = reserve && poolUser ? poolUser . getCollateralFloat ( reserve ) : undefined ;
46+ const [ showCollateral , setShowCollateral ] = useState ( true ) ;
47+
48+ const currentCollateral = reserve && poolUser ? poolUser . getCollateralFloat ( reserve ) : undefined ;
49+ const currentSupply = reserve && poolUser ? poolUser . getSupplyFloat ( reserve ) : undefined ;
4250 const emissionsPerAsset =
4351 reserve && reserve . supplyEmissions !== undefined
4452 ? reserve . supplyEmissions . emissionsPerYearPerToken (
@@ -52,19 +60,79 @@ const Withdraw: NextPage = () => {
5260 ? estimateEmissionsApr ( emissionsPerAsset , backstop . backstopToken , oraclePrice )
5361 : undefined ;
5462
63+ const hasSupplyBalance = currentSupply !== undefined && currentSupply > 0 ;
64+
5565 if ( poolError ?. message === NOT_BLEND_POOL_ERROR_MESSAGE ) {
5666 return < NotPoolBar poolId = { safePoolId } /> ;
5767 }
5868
69+ const handleCollateralClick = ( ) => {
70+ if ( ! showCollateral ) {
71+ setShowCollateral ( true ) ;
72+ }
73+ } ;
74+
75+ const handleNonCollateralClick = ( ) => {
76+ if ( showCollateral ) {
77+ setShowCollateral ( false ) ;
78+ }
79+ } ;
80+
5981 return (
6082 < >
6183 < Row >
6284 < GoBackHeader poolId = { safePoolId } />
6385 </ Row >
6486 < ReserveDetailsBar action = "withdraw" poolId = { safePoolId } activeReserveId = { safeAssetId } />
6587
66- < Row >
67- < Section width = { SectionSize . FULL } sx = { { padding : '12px' } } >
88+ < Row sx = { { flexDirection : viewType === ViewType . REGULAR ? 'row' : 'column' } } >
89+ { hasSupplyBalance && (
90+ < Section
91+ width = { viewType === ViewType . REGULAR ? SectionSize . TILE : SectionSize . FULL }
92+ sx = { {
93+ display : 'flex' ,
94+ flexDirection : 'row' ,
95+ justifyContent : 'space-between' ,
96+ alignItems : 'center' ,
97+ padding : '12px' ,
98+ } }
99+ >
100+ < TooltipText
101+ tooltip = {
102+ 'Supplied funds can be collateralized. A position with collateral enabled is separate from a position with collateral disabled and must be withdrawn separately.'
103+ }
104+ width = "auto"
105+ textVariant = "body2"
106+ textColor = { 'text.secondary' }
107+ >
108+ { 'Collateral' }
109+ </ TooltipText >
110+ < Box sx = { { display : 'flex' , flexDirection : 'row' } } >
111+ < ToggleButton
112+ active = { showCollateral }
113+ palette = { theme . palette . lend }
114+ sx = { { width : '50%' , padding : '6px' } }
115+ onClick = { handleCollateralClick }
116+ >
117+ Enabled
118+ </ ToggleButton >
119+ < ToggleButton
120+ active = { ! showCollateral }
121+ palette = { theme . palette . lend }
122+ sx = { { width : '50%' , padding : '6px' } }
123+ onClick = { handleNonCollateralClick }
124+ >
125+ Disabled
126+ </ ToggleButton >
127+ </ Box >
128+ </ Section >
129+ ) }
130+ < Section
131+ width = {
132+ hasSupplyBalance && viewType === ViewType . REGULAR ? SectionSize . TILE : SectionSize . FULL
133+ }
134+ sx = { { padding : '12px' } }
135+ >
68136 < Box
69137 sx = { {
70138 width : '100%' ,
@@ -79,7 +147,10 @@ const Withdraw: NextPage = () => {
79147 Available
80148 </ Typography >
81149 < Typography variant = "h4" sx = { { color : theme . palette . lend . main } } >
82- { toBalance ( currentDeposit ) }
150+ { toBalance (
151+ showCollateral ? currentCollateral : currentSupply ,
152+ reserve ?. config . decimals
153+ ) }
83154 </ Typography >
84155 </ Box >
85156 < Box >
@@ -129,7 +200,7 @@ const Withdraw: NextPage = () => {
129200 </ Section >
130201 </ Row >
131202 < Row >
132- < WithdrawAnvil poolId = { safePoolId } assetId = { safeAssetId } />
203+ < WithdrawAnvil poolId = { safePoolId } assetId = { safeAssetId } isCollateral = { showCollateral } />
133204 </ Row >
134205 </ >
135206 ) ;
0 commit comments