-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSelectAssetsInput.tsx
199 lines (182 loc) · 5.35 KB
/
SelectAssetsInput.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import React, { HTMLAttributes, useState } from "react";
import { useTheme } from "@emotion/react";
import styled from "@emotion/styled";
import { BN } from "@compolabs/spark-orderbook-ts-sdk";
import Button from "@components/Button";
import { IAssetBlock } from "@components/SelectAssets/AssetBlock";
import { SmartFlex } from "@components/SmartFlex";
import { BigNumberInput } from "@components/TokenInput/BigNumberInput";
import { TokenSelect } from "@screens/SwapScreen/TokenSelect";
import { DEFAULT_DECIMALS } from "@constants";
import { Token } from "@entity";
import Text, { TEXT_TYPES_MAP } from "../Text";
export interface AssetBlockData {
asset: Token;
walletBalance: string;
contractBalance: string;
orderBalance: string;
balance: string;
assetId: string;
price?: string;
}
interface IProps extends Omit<HTMLAttributes<HTMLDivElement>, "onSelect"> {
selected?: string;
onSelect: (option: AssetBlockData, index: number) => void;
onChangeValue: (value: BN) => void;
label?: string;
dataAssets: AssetBlockData[];
showBalance: IAssetBlock["options"]["showBalance"];
amount: BN;
decimals?: number;
}
const presentData = [
{
text: "25%",
value: 25,
},
{
text: "50%",
value: 50,
},
{
text: "75%",
value: 75,
},
{
text: "Max",
value: 100,
},
];
const SelectAssetsInput = ({
showBalance,
selected,
onSelect,
label,
dataAssets,
onChangeValue,
amount,
decimals = DEFAULT_DECIMALS,
}: IProps) => {
const theme = useTheme();
const selectedOption = dataAssets.find(({ assetId }) => selected === assetId);
const [selectPresent, setSelectPresent] = useState(0);
const handleSetAmount = (el: number) => {
setSelectPresent(el);
if (!showBalance || !selectedOption || !decimals) return;
const amount = new BN(
BN.parseUnits(new BN(selectedOption[showBalance] ?? 0).multipliedBy(el).div(new BN(100)), decimals).decimalPlaces(
0,
BN.ROUND_DOWN,
),
);
onChangeValue(amount);
};
if (!selectedOption || !showBalance) return;
const isInputError = new BN(BN.formatUnits(amount.toString(), decimals)).gt(selectedOption[showBalance] ?? 0);
return (
<SmartFlex gap="10px" column>
<SmartFlex gap="4px" column>
<SmartFlexInput error={isInputError}>
<InputContainer>
<TransparentInput
decimals={decimals}
placeholder="0.01"
value={amount}
onChange={(value: BN) => {
setSelectPresent(0);
onChangeValue(value);
}}
/>
</InputContainer>
<TokenSelect
assets={dataAssets}
label={label}
selectedOption={selectedOption}
showBalance={showBalance}
onSelect={onSelect}
/>
</SmartFlexInput>
{isInputError && (
<Text color={theme.colors.attention} type="BUTTON">
Not enough {selectedOption.asset.symbol}
</Text>
)}
</SmartFlex>
<SmartFlex alignItems="center" justifyContent="space-between">
<PriceText color={theme.colors.greenLight} type="BODY">
${BN.formatUnits(new BN(selectedOption?.price ?? 0).multipliedBy(amount), decimals).toFormat(2)}
</PriceText>
<SmartFlex gap="5px">
{presentData.map((el) => (
<ButtonPresent
key={el.text}
el={el.value}
selectPresent={selectPresent}
text
onClick={() => handleSetAmount(el.value)}
>
<TextPresent color={theme.colors.textSecondary}>{el.text}</TextPresent>
</ButtonPresent>
))}
</SmartFlex>
</SmartFlex>
</SmartFlex>
);
};
export default SelectAssetsInput;
const PriceText = styled(Text)`
overflow: clip;
width: 164px;
text-align: left;
`;
const InputContainer = styled.div`
display: flex;
align-items: center;
`;
const TextPresent = styled(Text)``;
const ButtonPresent = styled(Button)<{ el: number; selectPresent: number }>`
padding: 5px !important;
height: auto !important;
width: auto !important;
background: ${({ el, selectPresent }) => (el === selectPresent ? "#535353" : "#53535326")};
border-radius: 4px;
${TextPresent} {
color: ${({ theme, el, selectPresent }) => (el === selectPresent ? "white" : theme.colors.textSecondary)};
}
&:hover {
background: ${({ theme }) => theme.colors.textDisabled};
${TextPresent} {
color: ${({ theme }) => theme.colors.textPrimary};
}
}
`;
const SmartFlexInput = styled(SmartFlex)<{
error?: boolean;
}>`
padding: 8px 0px;
border-bottom: 1px solid ${({ error, theme }) => (error ? theme.colors.attention : theme.colors.textDisabled)};
input {
color: ${({ error, theme }) =>
(() => {
if (error) return theme.colors.attention;
return theme.colors.textPrimary;
})()};
}
&:hover {
border-bottom: 1px solid ${({ error, theme }) => (error ? theme.colors.attention : theme.colors.textPrimary)};
}
&:focus-within {
border-bottom: 1px solid ${({ error, theme }) => (error ? theme.colors.attention : theme.colors.textPrimary)};
}
`;
const TransparentInput = styled(BigNumberInput)`
color: white;
${TEXT_TYPES_MAP.H_TEXT};
background: transparent;
border: none;
width: 80%;
&:focus-visible {
border: none;
outline: none;
}
`;