1+ import { useMemo } from 'react'
12import { useLoanToValueFromUserState } from '@/llamalend/features/manage-loan/hooks/useLoanToValueFromUserState'
3+ import { useHealthQueries } from '@/llamalend/hooks/useHealthQueries'
24import type { LlamaMarketTemplate , NetworkDict } from '@/llamalend/llamalend.types'
35import type { AddCollateralOptions } from '@/llamalend/mutations/add-collateral.mutation'
46import { useMarketRates } from '@/llamalend/queries/market-rates'
7+ import { getUserHealthOptions } from '@/llamalend/queries/user-health.query'
8+ import { mapQuery } from '@/llamalend/queries/utils'
9+ import type { Query } from '@/llamalend/widgets/manage-loan/loan.types'
510import { LoanFormAlerts } from '@/llamalend/widgets/manage-loan/LoanFormAlerts'
611import { LoanFormTokenInput } from '@/llamalend/widgets/manage-loan/LoanFormTokenInput'
712import { LoanFormWrapper } from '@/llamalend/widgets/manage-loan/LoanFormWrapper'
@@ -11,9 +16,15 @@ import Button from '@mui/material/Button'
1116import Stack from '@mui/material/Stack'
1217import { useSwitch } from '@ui-kit/hooks/useSwitch'
1318import { t } from '@ui-kit/lib/i18n'
19+ import { decimal } from '@ui-kit/utils'
1420import { InputDivider } from '../../../widgets/InputDivider'
1521import { useAddCollateralForm } from '../hooks/useAddCollateralForm'
1622
23+ const withTokenSymbol = < T , > ( query : Query < T | null > , tokenSymbol ?: string ) => ( {
24+ ...query ,
25+ tokenSymbol,
26+ } )
27+
1728export const AddCollateralForm = < ChainId extends IChainId > ( {
1829 market,
1930 networks,
@@ -37,15 +48,14 @@ export const AddCollateralForm = <ChainId extends IChainId>({
3748 action,
3849 params,
3950 values,
40- bands,
4151 health,
42- prices,
4352 gas,
4453 isApproved,
4554 formErrors,
4655 collateralToken,
4756 borrowToken,
4857 txHash,
58+ userState,
4959 } = useAddCollateralForm ( {
5060 market,
5161 network,
@@ -54,30 +64,74 @@ export const AddCollateralForm = <ChainId extends IChainId>({
5464 onAdded,
5565 } )
5666
57- const marketRates = useMarketRates ( params , isOpen )
67+ const prevCollateral = useMemo (
68+ ( ) =>
69+ withTokenSymbol (
70+ mapQuery ( userState , ( state ) => state ?. collateral ) ,
71+ collateralToken ?. symbol ,
72+ ) ,
73+ [ collateralToken ?. symbol , userState ] ,
74+ )
75+ const prevDebt = useMemo (
76+ ( ) =>
77+ withTokenSymbol (
78+ mapQuery ( userState , ( state ) => state ?. debt ) ,
79+ borrowToken ?. symbol ,
80+ ) ,
81+ [ borrowToken ?. symbol , userState ] ,
82+ )
83+ const prevLoanToValue = useLoanToValueFromUserState ( {
84+ chainId,
85+ marketId : params . marketId ,
86+ userAddress : params . userAddress ,
87+ collateralToken,
88+ borrowToken,
89+ enabled : isOpen ,
90+ } )
91+ const prevHealth = useHealthQueries ( ( isFull ) => getUserHealthOptions ( { ...params , isFull } , undefined ) )
5892
93+ const collateral = useMemo (
94+ ( ) =>
95+ withTokenSymbol (
96+ {
97+ ...mapQuery ( userState , ( state ) => state ?. collateral ) ,
98+ data : decimal (
99+ values . userCollateral
100+ ? + values . userCollateral + ( userState . data ?. collateral ? + userState . data ?. collateral : 0 )
101+ : null ,
102+ ) ,
103+ } ,
104+ collateralToken ?. symbol ,
105+ ) ,
106+ [ collateralToken ?. symbol , userState , values . userCollateral ] ,
107+ )
108+ const marketRates = useMarketRates ( params , isOpen )
109+ const loanToValue = useLoanToValueFromUserState ( {
110+ chainId : params . chainId ! ,
111+ marketId : params . marketId ,
112+ userAddress : params . userAddress ,
113+ collateralToken,
114+ borrowToken,
115+ enabled : ! ! enabled && ! ! values . userCollateral ,
116+ collateralDelta : values . userCollateral ,
117+ } )
59118 return (
60119 < LoanFormWrapper
61120 { ...form }
62121 onSubmit = { onSubmit }
63122 infoAccordion = {
64- < LoanInfoAccordion // todo: prevHealth, prevRates, debt, prevDebt
123+ < LoanInfoAccordion // todo: prevRates
65124 isOpen = { isOpen }
66125 toggle = { toggle }
126+ prevHealth = { prevHealth }
67127 health = { health }
68- bands = { bands }
69- prices = { prices }
70128 rates = { marketRates }
71- loanToValue = { useLoanToValueFromUserState ( {
72- chainId : params . chainId ! ,
73- marketId : params . marketId ,
74- userAddress : params . userAddress ,
75- collateralToken,
76- borrowToken,
77- enabled : isOpen ,
78- collateralDelta : values . userCollateral ,
79- } ) }
129+ prevLoanToValue = { prevLoanToValue }
130+ loanToValue = { loanToValue }
131+ prevDebt = { prevDebt }
80132 gas = { gas }
133+ prevCollateral = { prevCollateral }
134+ collateral = { collateral }
81135 />
82136 }
83137 >
0 commit comments