@@ -3,7 +3,8 @@ import type { Address } from 'viem'
33import CampaignRewardsBanner from '@/lend/components/CampaignRewardsBanner'
44import { MarketInformationComp } from '@/lend/components/MarketInformationComp'
55import { MarketInformationTabs } from '@/lend/components/MarketInformationTabs'
6- import { ManageLoanTabs } from '@/lend/components/PageLoanManage/ManageLoanTabs'
6+ import { LoanCreateTabs } from '@/lend/components/PageLendMarket/LoanCreateTabs'
7+ import { ManageLoanTabs } from '@/lend/components/PageLendMarket/ManageLoanTabs'
78import { useOneWayMarket } from '@/lend/entities/chain'
89import { useBorrowPositionDetails } from '@/lend/hooks/useBorrowPositionDetails'
910import { useLendPageTitle } from '@/lend/hooks/useLendPageTitle'
@@ -13,8 +14,8 @@ import { helpers } from '@/lend/lib/apiLending'
1314import networks from '@/lend/networks'
1415import useStore from '@/lend/store/useStore'
1516import { type MarketUrlParams } from '@/lend/types/lend.types'
16- import { getVaultPathname , parseMarketParams } from '@/lend/utils/helpers'
17- import { getCollateralListPathname } from '@/lend/utils/utilsRouter'
17+ import { getCollateralListPathname , parseMarketParams } from '@/lend/utils/helpers'
18+ import { getVaultPathname } from '@/lend/utils/utilsRouter'
1819import { MarketDetails } from '@/llamalend/features/market-details'
1920import { BorrowPositionDetails , NoPosition } from '@/llamalend/features/market-position-details'
2021import { UserPositionHistory } from '@/llamalend/features/user-position-history'
@@ -35,41 +36,28 @@ import { SizesAndSpaces } from '@ui-kit/themes/design/1_sizes_spaces'
3536
3637const { Spacing } = SizesAndSpaces
3738
38- const Page = ( ) => {
39+ export const LendMarketPage = ( ) => {
3940 const params = useParams < MarketUrlParams > ( )
40- const { rMarket, rChainId } = parseMarketParams ( params )
41+ const { rMarket, rChainId : chainId } = parseMarketParams ( params )
42+
43+ const { data : market , isSuccess } = useOneWayMarket ( chainId , rMarket )
4144 const { llamaApi : api = null , connectState } = useCurve ( )
4245 const titleMapper = useTitleMapper ( )
43- const { data : market , isSuccess } = useOneWayMarket ( rChainId , rMarket )
44- const rOwmId = market ?. id ?? ''
45- const userActiveKey = helpers . getUserActiveKey ( api , market ! )
46+ const { provider, connect } = useWallet ( )
47+
4648 const isPageVisible = useLayoutStore ( ( state ) => state . isPageVisible )
4749 const fetchAllMarketDetails = useStore ( ( state ) => state . markets . fetchAll )
50+ const fetchUserMarketBalances = useStore ( ( state ) => state . user . fetchUserMarketBalances )
4851 const fetchAllUserMarketDetails = useStore ( ( state ) => state . user . fetchAll )
4952 const setMarketsStateKey = useStore ( ( state ) => state . markets . setStateByKey )
50- const { provider, connect } = useWallet ( )
5153
54+ const marketId = market ?. id ?? '' // todo: use market?.id directly everywhere since we pass the market too!
55+ const userActiveKey = helpers . getUserActiveKey ( api , market ! )
5256 const { signerAddress } = api ?? { }
57+ useLendPageTitle ( market ?. collateral_token ?. symbol ?? rMarket , t `Lend` )
5358
54- const { data : loanExists } = useLoanExists ( {
55- chainId : rChainId ,
56- marketId : market ?. id ,
57- userAddress : signerAddress ,
58- } )
59-
60- const [ isLoaded , setLoaded ] = useState ( false )
61-
62- const borrowPositionDetails = useBorrowPositionDetails ( {
63- chainId : rChainId ,
64- market : market ?? undefined ,
65- marketId : rOwmId ,
66- } )
67- const marketDetails = useMarketDetails ( {
68- chainId : rChainId ,
69- llamma : market ,
70- llammaId : rOwmId ,
71- } )
72- const network = networks [ rChainId ]
59+ const marketDetails = useMarketDetails ( { chainId, market, marketId } )
60+ const network = networks [ chainId ]
7361 const {
7462 data : userCollateralEvents ,
7563 isLoading : collateralEventsIsLoading ,
@@ -83,62 +71,73 @@ const Page = () => {
8371 borrowToken : market ?. borrowed_token ,
8472 network,
8573 } )
74+ const { data : loanExists , isLoading : isLoanExistsLoading } = useLoanExists ( {
75+ chainId,
76+ marketId : marketId ,
77+ userAddress : signerAddress ,
78+ } )
8679
87- const isInSoftLiquidation =
88- borrowPositionDetails . liquidationAlert . softLiquidation || borrowPositionDetails . liquidationAlert . hardLiquidation
80+ const [ isLoaded , setLoaded ] = useState ( false )
8981
90- useEffect ( ( ) => {
91- if ( api && market && isPageVisible ) {
92- if ( loanExists ) setMarketsStateKey ( 'marketDetailsView' , 'user' )
93- setLoaded ( true )
94- }
95- } , [ api , isPageVisible , loanExists , market , setMarketsStateKey ] )
82+ const borrowPositionDetails = useBorrowPositionDetails ( { chainId, market : market , marketId } )
9683
9784 useEffect ( ( ) => {
9885 // delay fetch rest after form details are fetched first
9986 const timer = setTimeout ( async ( ) => {
100- if ( ! api || ! market || ! isPageVisible || ! isLoaded ) return
101- void fetchAllMarketDetails ( api , market , true )
102- if ( api . signerAddress && loanExists ) {
103- void fetchAllUserMarketDetails ( api , market , true )
87+ if ( ! api || ! market || ! isPageVisible ) return
88+ await fetchAllMarketDetails ( api , market , true )
89+ if ( api . signerAddress ) {
90+ await fetchUserMarketBalances ( api , market , true )
91+ if ( loanExists ) {
92+ void fetchAllUserMarketDetails ( api , market , true )
93+ }
10494 }
10595 } , REFRESH_INTERVAL [ '3s' ] )
10696 return ( ) => clearTimeout ( timer )
107- } , [ api , fetchAllMarketDetails , fetchAllUserMarketDetails , isLoaded , isPageVisible , loanExists , market ] )
108-
109- useLendPageTitle ( market ?. collateral_token ?. symbol , 'Manage' )
110-
111- const pageProps = {
112- params,
113- rChainId,
114- rOwmId,
115- isLoaded,
97+ } , [
11698 api ,
99+ fetchAllMarketDetails ,
100+ fetchUserMarketBalances ,
101+ fetchAllUserMarketDetails ,
102+ isPageVisible ,
117103 market ,
118- userActiveKey,
119- titleMapper,
120- }
104+ loanExists ,
105+ ] )
106+
107+ useEffect ( ( ) => {
108+ if ( api && market && isPageVisible ) {
109+ if ( loanExists ) setMarketsStateKey ( 'marketDetailsView' , 'user' )
110+ setLoaded ( true )
111+ }
112+ } , [ api , isPageVisible , loanExists , market , setMarketsStateKey ] )
121113
122- const positionDetailsHrefs = {
123- borrow : '' ,
124- supply : getVaultPathname ( params , rOwmId ) ,
125- }
114+ const pageProps = { params, rChainId : chainId , rOwmId : marketId , isLoaded, api, market, userActiveKey, titleMapper }
126115
127116 return isSuccess && ! market ? (
128117 < ErrorPage title = "404" subtitle = { t `Market Not Found` } continueUrl = { getCollateralListPathname ( params ) } />
129118 ) : provider ? (
130119 < >
131120 < DetailPageStack >
132121 < AppPageFormsWrapper >
133- { rChainId && rOwmId && < ManageLoanTabs isInSoftLiquidation = { isInSoftLiquidation } { ...pageProps } /> }
122+ { chainId &&
123+ marketId &&
124+ ! isLoanExistsLoading &&
125+ ( loanExists ? (
126+ < ManageLoanTabs position = { borrowPositionDetails } { ...pageProps } />
127+ ) : (
128+ < LoanCreateTabs { ...pageProps } params = { params } />
129+ ) ) }
134130 </ AppPageFormsWrapper >
135131 < Stack flexDirection = "column" flexGrow = { 1 } sx = { { gap : Spacing . md } } >
136132 < CampaignRewardsBanner
137- chainId = { rChainId }
133+ chainId = { chainId }
138134 borrowAddress = { market ?. addresses ?. controller || '' }
139135 supplyAddress = { market ?. addresses ?. vault || '' }
140136 />
141- < MarketInformationTabs currentTab = { 'borrow' } hrefs = { positionDetailsHrefs } >
137+ < MarketInformationTabs
138+ currentTab = { 'borrow' }
139+ hrefs = { { borrow : '' , supply : getVaultPathname ( params , marketId ) } }
140+ >
142141 { loanExists ? (
143142 < BorrowPositionDetails { ...borrowPositionDetails } />
144143 ) : (
@@ -185,5 +184,3 @@ const Page = () => {
185184 </ Box >
186185 )
187186}
188-
189- export default Page
0 commit comments