import React, { useState } from 'react';
import {
ColumnType,
DataGrid,
DataGridColumnProps,
Flag,
Icon,
Modal,
styled,
Button,
styledScrollbars,
FlagImagesMap,
} from '@jarvisnetwork/ui';
import { useWeb3React } from '@web3-react/core';
import { FixBigNumber, NetworkId } from '@jarvisnetwork/core-sdk';
import { NETWORKS } from '../../config/network';
import { useCurrencyBalances } from '../../hooks/useCurrencyBalances';
import { useNetworkCurrencies } from '../NetworkCurrenciesProvider';
import { usePricesFeed } from '../PriceFeedProvider';
const ModalContainer = styled.div`
background: ${(props) => props.theme.background.secondary};
color: ${(props) => props.theme.text.primary};
border-radius: 27px;
height: 485px;
width: 600px;
box-sizing: border-box;
display: flex;
flex-direction: column;
overflow: hidden;
@media screen and (max-width: ${(props) =>
props.theme.rwd.breakpoints[props.theme.rwd.mobileIndex]}px) {
min-width: 80vw;
max-width: calc(100% - 60px);
max-height: 600px;
width: auto;
height: auto;
margin: auto;
overflow: auto;
}
`;
const ModalWrapper = styled.div`
@media screen and (max-width: ${(props) =>
props.theme.rwd.breakpoints[props.theme.rwd.desktopIndex]}px) {
.account-modal {
> * {
height: auto;
padding-bottom: 30px;
}
}
}
`;
const ContentWrapper = styled.div`
width: 100%;
max-height: 100%;
padding: 15px;
box-sizing: border-box;
`;
const HeaderWrapper = styled.div`
display: block;
background: ${(props) => props.theme.background.primary};
font-size: ${(props) => props.theme.font.sizes.s};
align-items: center;
padding: 31px 24px;
`;
const HeaderTitle = styled.strong`
text-align: center;
color: ${(props) => props.theme.text.primary};
margin: auto;
`;
const IconButton = styled.button`
border: none;
background: none;
padding: 10px;
cursor: pointer;
outline: none !important;
color: ${(props) => props.theme.text.primary};
i svg {
width: 12px;
height: 12px;
position: relative;
top: 3px;
fill: ${(props) => props.theme.text.primary};
}
`;
const HeaderIconButton = styled(IconButton)`
padding: 0;
position: flex;
color: ${(props) => props.theme.text.primary};
`;
const HeaderTitleWrapper = styled.div`
display: flex;
margin-bottom: 22px;
`;
const HeaderActionContainer = styled.div`
display: grid;
grid-template-columns: auto auto auto;
justify-items: center;
font-size: ${(props) => props.theme.font.sizes.xs};
`;
const ActionHeaderButton = styled(IconButton)`
position: flex;
padding: 0;
i {
margin-right: 8px;
}
`;
const AssetWrapper = styled.div`
position: relative;
display: flex;
text-align: center;
align-items: center;
.logo {
width: 24px;
height: 24px;
}
p {
width: 65px;
margin-right: 2px;
margin-left: 2px;
}
@media screen and (max-width: ${(props) =>
props.theme.rwd.breakpoints[props.theme.rwd.desktopIndex - 1]}px) {
margin-right: 10px;
}
`;
const CustomValue = styled.div`
p {
margin: 0;
}
width: 100%;
.in-dollars {
color: ${(props) => props.theme.text.secondary};
@media screen and (max-width: ${(props) =>
props.theme.rwd.breakpoints[props.theme.rwd.desktopIndex - 1]}px) {
display: none;
}
}
`;
const AddMetaMaskButton = styled(Button)`
display: flex;
img {
width: 16px;
margin-left: 5px;
margin-right: 0;
}
`;
const Grid = styled(DataGrid)`
background: transparent;
height: 100%;
.rt-tr-group {
border: none !important;
}
.rt-td:first-child {
justify-content: flex-start;
padding: 8px 0px !important;
}
.rt-th {
padding: 12px 0px !important;
}
.rt-tbody {
max-height: 260px;
${(props) => styledScrollbars(props.theme)}
}
`;
interface AccountSummaryProps {
toggleAccountSummaryModal: () => void;
isAccountSummaryModalOpen: boolean;
}
export const AccountSummary = ({
toggleAccountSummaryModal,
isAccountSummaryModalOpen,
}: AccountSummaryProps) => {
const { account, connector, chainId, provider: library } = useWeb3React();
const CurrencyArrayList = useNetworkCurrencies();
const { value: tokenBalance } = useCurrencyBalances(CurrencyArrayList);
const usdcPrices = usePricesFeed();
const [isCopied, setIsCopied] = useState(false);
const handleAddToMetamask = (asset: any) => {
const currency = CurrencyArrayList?.find((value) => value.symbol === asset);
if (library && library.provider.request && currency && currency.isToken) {
library.provider.request({
method: 'wallet_watchAsset',
params: {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // need this for incorrect ethers provider type
type: 'ERC20',
options: {
address: currency.address,
symbol: currency.symbol,
decimals: currency.decimals,
image: `${window.location.href}${
FlagImagesMap[
currency.symbol as keyof typeof FlagImagesMap
] as string
}`,
},
},
});
}
};
const data = tokenBalance
? Object.values(tokenBalance)
.map(({ currency, balance }) => {
if (!usdcPrices || !usdcPrices[currency.pair])
return {
asset: currency.symbol,
amount: balance.toExact(2),
value: `N/A`,
};
return {
asset: currency.symbol,
amount: balance.toExact(2),
value: usdcPrices[currency.pair]
? `$${balance.multiply(usdcPrices[currency.pair]).toExact(2)}`
: `N/A`,
};
})
.sort((a, b) =>
Number(
FixBigNumber.toWei(b.amount!)
.subtract(FixBigNumber.toWei(a.amount!))
.toExact()
)
)
: [];
const balanceTableCols: DataGridColumnProps[] = [
{
header: 'Asset',
key: 'asset',
type: ColumnType.CustomCell,
cell: (cellData) => (
{cellData.original.asset}
{CurrencyArrayList?.find(
(asset) => asset.symbol === cellData.original.asset
)?.isToken && (
handleAddToMetamask(cellData.original.asset)}
>
Add to{' '}
)}
),
},
{
header: 'Amount',
key: 'amount',
type: ColumnType.Text,
cell: (cellData) => (
{cellData.original.amount}
),
},
{
header: 'Value',
key: 'value',
type: ColumnType.Text,
cell: (cellData) => (
${cellData.original.value}
),
},
];
const handleCopy = () => {
if (!account) return;
const copyAddressToClipBoard = () => {
if ('clipboard' in navigator) {
return navigator.clipboard.writeText(account);
}
return document.execCommand('copy', true, account);
};
copyAddressToClipBoard();
setIsCopied(true);
setTimeout(() => {
setIsCopied(false);
}, 1500);
};
const handleViewOnExplorer = () => {
if (!chainId) return;
window.open(
`${NETWORKS[chainId as NetworkId]?.blockExplorerUrls}/address/${account}`,
'_blank'
);
};
const handleDisconnected = () => {
if (!connector) return;
// eslint-disable-next-line no-unused-expressions
connector.deactivate ? connector.deactivate() : connector.resetState();
toggleAccountSummaryModal();
};
return (
{account}
{isCopied ? 'Copied!' : 'Copy Address'}
View on Explorer
Disconnect
);
};