-
Notifications
You must be signed in to change notification settings - Fork 504
/
index.tsx
126 lines (118 loc) · 3.14 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { LoanType, OfferType, RatesType, SupportedCoinsType } from 'core/types'
import { model } from 'data'
import { Text } from 'blockchain-info-components'
import React from 'react'
import styled from 'styled-components'
const Bar = styled.div`
position: relative;
width: 100%;
height: 8px;
background-color: ${props => props.theme.grey100};
margin-top: 16px;
margin-bottom: 48px;
border-radius: 8px;
`
const Line = styled.div<{ position: number }>`
top: 0;
position: absolute;
z-index: 3;
left: ${props => props.position * 100}%;
&:before {
content: '';
width: 1px;
top: -8px;
position: relative;
display: block;
height: 24px;
border-right: 2px dashed ${props => props.theme.greyFade400};
}
`
const Percentage = styled(Text)`
font-size: 12px;
font-weight: 600;
position: absolute;
left: 50%;
bottom: -8px;
transform: translateX(-50%);
`
const Current = styled.div<{ color: string; width: number }>`
z-index: 2;
width: ${props => props.width * 100}%;
height: 8px;
background-color: ${props => props.theme[props.color]};
border-radius: 8px;
transition: width 0.3s;
position: relative;
`
const CurrentBackground = styled(Current)`
z-index: 1;
position: absolute;
top: 0;
left: 0;
`
type Props = {
loan: LoanType
offer: OfferType
rates: RatesType
showPercentages?: boolean
supportedCoins: SupportedCoinsType
}
const PADDING = 0.363636
const {
getCollateralizationColor,
getCollateralizationDisplayName
} = model.components.borrow
export const percentageFormatter = (n: number) => {
return (
Number(n * 100).toLocaleString(undefined, {
minimumFractionDigits: 0,
maximumFractionDigits: 2
}) + '%'
)
}
const CollateralizationBar: React.FC<Props> = props => {
const { offer } = props
const max = Math.ceil(
offer.terms.collateralRatio * PADDING + offer.terms.collateralRatio
)
const currentRatio = props.loan.collateralisationRatio
const currentCollateralStatus = getCollateralizationDisplayName(
currentRatio,
offer
)
const currentWidth = currentRatio >= max ? 1 : currentRatio / max
return (
<Bar>
<Current
width={currentWidth}
color={getCollateralizationColor(currentCollateralStatus)}
/>
<CurrentBackground
width={currentWidth === 0 ? 0 : currentWidth + 0.01}
color={'white'}
/>
<Line position={offer.callTerms.liquidationHardRatio / max}>
{props.showPercentages && (
<Percentage color='red600'>
{percentageFormatter(offer.callTerms.liquidationHardRatio)}
</Percentage>
)}
</Line>
<Line position={offer.callTerms.callTriggerRatio / max}>
{props.showPercentages && (
<Percentage color='orange600'>
{percentageFormatter(offer.callTerms.callTriggerRatio)}
</Percentage>
)}
</Line>
<Line position={offer.terms.collateralRatio / max}>
{props.showPercentages && (
<Percentage color='green600'>
{percentageFormatter(offer.terms.collateralRatio)}
</Percentage>
)}
</Line>
</Bar>
)
}
export default CollateralizationBar