-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMainAssets.tsx
251 lines (227 loc) · 7.64 KB
/
MainAssets.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import React from "react";
import { Link } from "react-router-dom";
import { useTheme } from "@emotion/react";
import styled from "@emotion/styled";
import { observer } from "mobx-react";
import { BN } from "@compolabs/spark-orderbook-ts-sdk";
import Button from "@components/Button";
import { Column } from "@components/Flex";
import AssetBlock from "@components/SelectAssets/AssetBlock";
import SizedBox from "@components/SizedBox";
import { SmartFlex } from "@components/SmartFlex";
import Text from "@components/Text";
import { media } from "@themes/breakpoints";
import closeThin from "@assets/icons/closeThin.svg";
import DepositAssets from "@assets/icons/depositAssets.svg?react";
import { useWallet } from "@hooks/useWallet";
import { useStores } from "@stores";
import { MIXPANEL_EVENTS } from "@stores/MixPanelStore";
import { MODAL_TYPE } from "@stores/ModalStore";
import { BRIDGE_LINK } from "@constants";
interface MainAssetsProps {
setStep: (value: number) => void;
}
const MainAssets: React.FC<MainAssetsProps> = observer(({ setStep }) => {
const { balanceStore, quickAssetsStore, mixPanelStore, modalStore } = useStores();
const { isConnected } = useWallet();
// const [isLoading, setIsLoading] = useState(false);
const theme = useTheme();
const balancesInfoList = balanceStore.formattedBalanceInfoList;
const hasPositiveBalance = balancesInfoList.some((item) => !new BN(item.contractBalance).isZero());
const accumulateBalance = balancesInfoList.reduce(
(acc, account) => {
const balanceValue = new BN(account.balance).multipliedBy(account.price);
const contractBalanceValue = new BN(account.contractBalance).multipliedBy(account.price);
const walletBalance = new BN(account.walletBalance).multipliedBy(account.price);
return {
balance: acc.balance.plus(balanceValue),
contractBalance: acc.contractBalance.plus(contractBalanceValue),
walletBalance: acc.walletBalance.plus(walletBalance),
};
},
{ balance: BN.ZERO, contractBalance: BN.ZERO, walletBalance: BN.ZERO },
);
// const handleWithdraw = async () => {
// setIsLoading(true);
// await balanceStore.withdrawBalanceAll();
// setIsLoading(false);
// };
const closeAssets = () => {
quickAssetsStore.setCurrentStep(0);
quickAssetsStore.setQuickAssets(false);
};
const renderOverallContent = ({ isConnected, balance }: { isConnected: boolean; balance: BN }) => {
return (
<OverallBlock justifyContent="space-between">
<Text color={theme.colors.textPrimary} type="BUTTON">
Overall
</Text>
<Text color={theme.colors.greenLight}>${new BN(isConnected ? balance : BN.ZERO).toSignificant(2)}</Text>
</OverallBlock>
);
};
return (
<AssetsContainer justifyContent="space-between" column>
<SmartFlex alignItems="center" justifyContent="space-between" column>
<HeaderBlock alignItems="center" gap="10px" justifyContent="space-between">
<TextTitle
type="TEXT_BIG"
primary
onClick={() => mixPanelStore.trackEvent(MIXPANEL_EVENTS.CLICK_ASSETS, { page_name: location.pathname })}
>
Assets in V12
</TextTitle>
<CloseButton alt="Close Assets" src={closeThin} onClick={closeAssets} />
</HeaderBlock>
<WalletBlock gap="8px" column>
{isConnected ? (
accumulateBalance.contractBalance.isGreaterThan(BN.ZERO) && (
<>
{balancesInfoList.map((el) => {
const balance = new BN(el.contractBalance).isGreaterThan(BN.ZERO);
if (!balance) return;
return (
<AssetItem key={el.assetId}>
<AssetBlock options={{ showBalance: "contractBalance" }} token={el} />
</AssetItem>
);
})}
{renderOverallContent({ isConnected, balance: accumulateBalance.contractBalance })}
</>
)
) : (
<>
<BoxShadow>
{balancesInfoList.slice(0, 3).map((el) => (
<AssetItem key={el.assetId}>
<AssetBlock options={{ showBalance: "contractBalance" }} token={el} />
</AssetItem>
))}
</BoxShadow>
{renderOverallContent({ isConnected, balance: accumulateBalance.contractBalance })}
</>
)}
</WalletBlock>
</SmartFlex>
{!hasPositiveBalance && isConnected && (
<DepositedAssets alignItems="center" gap="20px" justifyContent="center" column>
<DepositAssets />
<TextTitleDeposit type="TEXT_BIG">
It looks like you don’t have assets in V12. Tap the{" "}
<LinkStyled
to="#"
onClick={() => {
quickAssetsStore.setQuickAssets(false);
window.open(BRIDGE_LINK, "_blank");
}}
>
bridge
</LinkStyled>{" "}
to grab some tokens.
</TextTitleDeposit>
</DepositedAssets>
)}
<BottomColumn justifyContent="space-between">
{!isConnected && (
<>
<SizedBoxStyled width={150}>
<Text type="BUTTON">Connect wallet to see your assets and trade</Text>
</SizedBoxStyled>
<Button green onClick={() => modalStore.open(MODAL_TYPE.CONNECT)}>
Connect wallet
</Button>
</>
)}
{accumulateBalance.contractBalance.isPositive() && (
<SmartFlexBlock>
<ButtonConfirm fitContent onClick={() => setStep(1)}>
Withdraw
</ButtonConfirm>
{/* <ButtonConfirm disabled={isLoading} fitContent onClick={handleWithdraw}>
{isLoading ? <Spinner height={14} /> : "Withdraw All"}
</ButtonConfirm> */}
</SmartFlexBlock>
)}
</BottomColumn>
</AssetsContainer>
);
});
export default MainAssets;
const ButtonConfirm = styled(Button)`
width: 100%;
`;
const SmartFlexBlock = styled(SmartFlex)`
width: 100%;
gap: 10px;
`;
const SizedBoxStyled = styled(SizedBox)`
margin: auto;
text-align: center;
`;
const HeaderBlock = styled(SmartFlex)`
width: 100%;
margin-bottom: 10px;
`;
const OverallBlock = styled(SmartFlex)`
margin: 16px 15px;
`;
const AssetItem = styled(SmartFlex)`
padding: 12px;
background: #00000026;
border-radius: 8px;
`;
const WalletBlock = styled(SmartFlex)`
width: 100%;
margin-top: 20px;
`;
const BottomColumn = styled(Column)`
gap: 15px;
width: 100%;
`;
const TextTitle = styled(Text)`
width: 100%;
text-align: left;
`;
const TextTitleDeposit = styled(TextTitle)`
text-align: center;
`;
const AssetsContainer = styled(SmartFlex)`
height: 100%;
`;
const BoxShadow = styled(SmartFlex)`
position: relative;
flex-direction: column;
gap: 8px;
&:after {
content: "";
height: 100%;
width: 100%;
position: absolute;
top: 0px;
background: linear-gradient(to bottom, transparent 0px, rgba(34, 34, 34, 0) 10%, rgba(34, 34, 34, 1) 100%);
${media.mobile} {
background: linear-gradient(to bottom, transparent 0px, rgba(0, 0, 0, 0) 10%, rgba(20, 20, 20, 1) 100%);
}
}
`;
const DepositedAssets = styled(SmartFlex)`
height: 100%;
width: 100%;
`;
const CloseButton = styled.img`
width: 30px;
height: 30px;
background: ${({ theme }) => theme.colors.bgIcon};
padding: 8px;
border-radius: 100px;
&:hover {
cursor: pointer;
}
`;
const LinkStyled = styled(Link)`
color: ${({ theme }) => theme.colors.greenLight};
text-decoration: underline;
&:hover {
cursor: pointer;
}
`;