Skip to content

Commit 1fb7656

Browse files
committed
refactor: loan info accordion with prev and current helper
1 parent 49c3571 commit 1fb7656

File tree

1 file changed

+72
-36
lines changed

1 file changed

+72
-36
lines changed

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

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,48 @@ type LoanInfoAccordionProps = {
3030
range?: number
3131
health: Query<Decimal>
3232
prevHealth?: Query<Decimal>
33-
bands: Query<[number, number]>
34-
prices: Query<readonly Decimal[]>
33+
bands?: Query<[number, number]>
34+
prices?: Query<readonly Decimal[]>
3535
rates: Query<{ borrowApr?: Decimal } | null>
3636
prevRates?: Query<{ borrowApr?: Decimal } | null>
3737
loanToValue: Query<Decimal | null>
3838
prevLoanToValue?: Query<Decimal | null>
3939
gas: Query<LoanInfoGasData | null>
40-
debt?: Query<Decimal | null> & { tokenSymbol: string }
41-
prevDebt?: Query<Decimal | null>
40+
debt?: Query<Decimal | null> & { tokenSymbol?: string }
41+
prevDebt?: Query<Decimal | null> & { tokenSymbol?: string }
42+
collateral?: Query<Decimal | null> & { tokenSymbol?: string }
43+
prevCollateral?: Query<Decimal | null> & { tokenSymbol?: string }
4244
leverage?: LoanLeverageActionInfoProps & { enabled: boolean }
4345
}
4446

47+
/**
48+
* Builds props for ActionInfo component that handles prev/current value transitions.
49+
* - Displays "previous -> current" when both values are available
50+
* - Displays "current" when only current value is available
51+
* - Displays "previous" when only previous value is available
52+
* - Displays empty value when neither are available
53+
*/
54+
const buildPrevCurrentValues = <T,>(
55+
current: Query<T | null> | undefined,
56+
previous: Query<T | null> | undefined,
57+
format: (value: NonNullable<T>) => string,
58+
emptyValue = '-',
59+
) => {
60+
const hasCurrent = current?.data != null
61+
const hasPrevious = previous?.data != null
62+
63+
return {
64+
...(hasCurrent && hasPrevious && { prevValue: format(previous!.data as NonNullable<T>) }),
65+
value: hasCurrent
66+
? format(current!.data as NonNullable<T>)
67+
: hasPrevious
68+
? format(previous!.data as NonNullable<T>)
69+
: emptyValue,
70+
error: current?.error ?? previous?.error,
71+
loading: current?.isLoading || previous?.isLoading,
72+
}
73+
}
74+
4575
export const LoanInfoAccordion = ({
4676
isOpen,
4777
toggle,
@@ -55,23 +85,22 @@ export const LoanInfoAccordion = ({
5585
loanToValue,
5686
prevLoanToValue,
5787
gas,
58-
debt,
5988
prevDebt,
89+
debt,
90+
prevCollateral,
91+
collateral,
6092
leverage,
6193
}: LoanInfoAccordionProps) => (
6294
// error tooltip isn't displayed correctly because accordion takes the mouse focus. Use title for now.
63-
<Box title={health.error?.message}>
95+
<Box title={(health.error ?? prevHealth?.error)?.message}>
6496
<Accordion
6597
ghost
6698
title={t`Health`}
6799
info={
68100
<ActionInfo
69101
label=""
70-
prevValue={prevHealth?.data == null ? undefined : formatNumber(prevHealth.data, { abbreviate: false })}
71-
value={health.data == null ? '∞' : formatNumber(health.data, { abbreviate: false })}
72-
valueColor={getHealthValueColor(Number(health.data ?? 100), useTheme())}
73-
error={health.error ?? prevHealth?.error}
74-
loading={health.isLoading || prevHealth?.isLoading}
102+
{...buildPrevCurrentValues(health, prevHealth, (v) => formatNumber(v, { abbreviate: false }), '∞')}
103+
valueColor={getHealthValueColor(Number(health.data ?? prevHealth?.data ?? 100), useTheme())}
75104
testId="borrow-health"
76105
/>
77106
}
@@ -80,31 +109,41 @@ export const LoanInfoAccordion = ({
80109
>
81110
<Stack>
82111
{leverage?.enabled && <LoanLeverageActionInfo {...leverage} />}
83-
{debt && (
112+
{(debt || prevDebt) && (
84113
<ActionInfo
85114
label={t`Debt`}
86-
{...(prevDebt?.data && { prevValue: formatNumber(prevDebt.data, { abbreviate: false }) })}
87-
value={debt.data == null ? '-' : formatNumber(debt.data, { abbreviate: false })}
88-
valueRight={debt.tokenSymbol}
89-
error={debt?.error ?? prevDebt?.error}
90-
loading={debt?.isLoading || prevDebt?.isLoading}
115+
{...buildPrevCurrentValues(debt, prevDebt, (v) => formatNumber(v, { abbreviate: false }))}
116+
valueRight={debt?.tokenSymbol ?? prevDebt?.tokenSymbol}
91117
testId="borrow-debt"
92118
/>
93119
)}
94-
<ActionInfo
95-
label={t`Band range`}
96-
value={bands.data ? `${bands.data[0]} to ${bands.data[1]}` : '-'}
97-
error={bands.error}
98-
loading={bands.isLoading}
99-
testId="borrow-band-range"
100-
/>
101-
<ActionInfo
102-
label={t`Price range`}
103-
value={prices.data?.map((p) => formatNumber(p, { abbreviate: false })).join(' - ') ?? '-'}
104-
error={prices.error}
105-
loading={prices.isLoading}
106-
testId="borrow-price-range"
107-
/>
120+
{(collateral || prevCollateral) && (
121+
<ActionInfo
122+
label={t`Collateral`}
123+
{...buildPrevCurrentValues(collateral, prevCollateral, (v) => formatNumber(v, { abbreviate: false }))}
124+
valueRight={collateral?.tokenSymbol ?? prevCollateral?.tokenSymbol}
125+
testId="borrow-collateral"
126+
/>
127+
)}
128+
129+
{bands && (
130+
<ActionInfo
131+
label={t`Band range`}
132+
value={bands.data ? `${bands.data[0]} to ${bands.data[1]}` : '-'}
133+
error={bands.error}
134+
loading={bands.isLoading}
135+
testId="borrow-band-range"
136+
/>
137+
)}
138+
{prices && (
139+
<ActionInfo
140+
label={t`Price range`}
141+
value={prices.data?.map((p) => formatNumber(p, { abbreviate: false })).join(' - ') ?? '-'}
142+
error={prices.error}
143+
loading={prices.isLoading}
144+
testId="borrow-price-range"
145+
/>
146+
)}
108147
{range != null && (
109148
<ActionInfo label={t`N`} value={formatNumber(range, { decimals: 0, abbreviate: false })} testId="borrow-n" />
110149
)}
@@ -116,14 +155,11 @@ export const LoanInfoAccordion = ({
116155
loading={rates.isLoading || prevRates?.isLoading}
117156
testId="borrow-apr"
118157
/>
119-
{loanToValue && (
158+
{(loanToValue || prevLoanToValue) && (
120159
<ActionInfo
121160
label={t`Loan to value ratio`}
122-
{...(prevLoanToValue?.data && { prevValue: formatPercent(prevLoanToValue.data) })}
123-
value={loanToValue.data ? formatPercent(loanToValue.data) : '-'}
161+
{...buildPrevCurrentValues(loanToValue, prevLoanToValue, formatPercent)}
124162
testId="borrow-ltv"
125-
error={prevLoanToValue?.error ?? loanToValue.error}
126-
loading={loanToValue.isLoading || prevLoanToValue?.isLoading}
127163
/>
128164
)}
129165
<ActionInfo

0 commit comments

Comments
 (0)