Skip to content

Commit 7b9be3a

Browse files
committed
refactor: tokenSymbol part of query's data
1 parent 1cddcf3 commit 7b9be3a

File tree

3 files changed

+18
-33
lines changed

3 files changed

+18
-33
lines changed

apps/main/src/llamalend/features/manage-loan/hooks/useAddCollateralForm.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { useAddCollateralPrices } from '@/llamalend/queries/add-collateral/add-c
1515
import { useMarketRates } from '@/llamalend/queries/market-rates'
1616
import { getUserHealthOptions } from '@/llamalend/queries/user-health.query'
1717
import { useUserState } from '@/llamalend/queries/user-state.query'
18-
import { mapQuery, withTokenSymbol } from '@/llamalend/queries/utils'
18+
import { mapQuery } from '@/llamalend/queries/utils'
1919
import type { CollateralParams } from '@/llamalend/queries/validation/manage-loan.types'
2020
import {
2121
collateralFormValidationSuite,
@@ -126,17 +126,13 @@ export const useAddCollateralForm = <ChainId extends LlamaChainId>({
126126

127127
const expectedCollateral = useMemo(
128128
() =>
129-
withTokenSymbol(
130-
{
131-
...mapQuery(userState, (state) => state?.collateral),
132-
data: decimal(
133-
values.userCollateral != null && userState.data?.collateral != null
134-
? new BigNumber(values.userCollateral).plus(new BigNumber(userState.data?.collateral)).toString()
135-
: null,
136-
),
137-
},
138-
collateralToken?.symbol,
139-
),
129+
mapQuery(userState, (state) => {
130+
const value =
131+
values.userCollateral != null &&
132+
state?.collateral != null &&
133+
decimal(new BigNumber(values.userCollateral).plus(state.collateral).toString())
134+
return value ? { value, tokenSymbol: collateralToken?.symbol } : null
135+
}),
140136
[collateralToken?.symbol, userState, values.userCollateral],
141137
)
142138

apps/main/src/llamalend/queries/utils.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@ import type { Query } from '@ui-kit/types/util'
44
* Maps a Query type to extract partial data from it.
55
* Preserves error and loading states while transforming the data.
66
*/
7-
export function mapQuery<TSource, TResult>(
8-
query: Query<TSource>,
9-
selector: (data: TSource) => TResult,
10-
): Query<TResult> {
11-
return {
12-
data: query.data === undefined ? undefined : selector(query.data),
13-
isLoading: query.isLoading,
14-
error: query.error,
15-
}
16-
}
7+
export const mapQuery = <TSource, TResult>(query: Query<TSource>, selector: (data: TSource) => TResult) => ({
8+
...query,
9+
data: query.data && selector(query.data),
10+
})
1711

1812
export const formatQueryValue = <T>(query: Query<T | null> | undefined, format: (value: NonNullable<T>) => string) =>
1913
query?.data != null ? format(query.data as NonNullable<T>) : undefined
@@ -22,8 +16,3 @@ export const getQueryState = (current: Query<unknown> | undefined, previous: Que
2216
error: current?.error ?? previous?.error,
2317
loading: current?.isLoading || previous?.isLoading,
2418
})
25-
26-
export const withTokenSymbol = <T>(query: Query<T | null>, tokenSymbol?: string) => ({
27-
...query,
28-
tokenSymbol,
29-
})

apps/main/src/llamalend/widgets/manage-loan/LoanInfoAccordion.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ type LoanInfoAccordionProps = {
4343
loanToValue: Query<Decimal | null>
4444
prevLoanToValue?: Query<Decimal | null>
4545
gas: Query<LoanInfoGasData | null>
46-
debt?: Query<Decimal | null> & { tokenSymbol?: string }
47-
collateral?: Query<Decimal | null> & { tokenSymbol?: string }
46+
debt?: Query<{ value: Decimal; tokenSymbol?: string } | null>
47+
collateral?: Query<{ value: Decimal; tokenSymbol?: string } | null>
4848
// userState values are used as prev values if collateral or debt are available
4949
userState?: Query<UserState> & { borrowTokenSymbol?: string; collateralTokenSymbol?: string }
5050
leverage?: LoanLeverageActionInfoProps & { enabled: boolean }
@@ -95,20 +95,20 @@ export const LoanInfoAccordion = ({
9595
{(debt || prevDebt) && (
9696
<ActionInfo
9797
label={t`Debt`}
98-
value={formatQueryValue(debt, (v) => formatNumber(v, { abbreviate: false }))}
98+
value={formatQueryValue(debt, (v) => formatNumber(v.value, { abbreviate: false }))}
9999
prevValue={prevDebt && formatNumber(prevDebt, { abbreviate: false })}
100100
{...getQueryState(debt, userState)}
101-
valueRight={debt?.tokenSymbol ?? userState?.borrowTokenSymbol}
101+
valueRight={debt?.data?.tokenSymbol ?? userState?.borrowTokenSymbol}
102102
testId="borrow-debt"
103103
/>
104104
)}
105105
{(collateral || prevCollateral) && (
106106
<ActionInfo
107107
label={t`Collateral`}
108-
value={formatQueryValue(collateral, (v) => formatNumber(v, { abbreviate: false }))}
108+
value={formatQueryValue(collateral, (v) => formatNumber(v.value, { abbreviate: false }))}
109109
prevValue={prevCollateral && formatNumber(prevCollateral, { abbreviate: false })}
110110
{...getQueryState(collateral, userState)}
111-
valueRight={collateral?.tokenSymbol ?? userState?.collateralTokenSymbol}
111+
valueRight={collateral?.data?.tokenSymbol ?? userState?.collateralTokenSymbol}
112112
testId="borrow-collateral"
113113
/>
114114
)}

0 commit comments

Comments
 (0)