Skip to content

Commit

Permalink
update xc-transfer page, balances (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
TopETH committed Apr 25, 2024
1 parent d2468fa commit 8bc44b9
Show file tree
Hide file tree
Showing 14 changed files with 323 additions and 154 deletions.
22 changes: 22 additions & 0 deletions src/components/Elements/Balance/index.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.container {
display: flex;
gap: 1rem;
}

.balanceItem {
display: flex;
gap: 0.5rem;
align-items: center;
}

.balanceWrapper {
display: flex;
align-items: center;
justify-content: center;
line-height: 1.5;
height: fit-content;
padding: 0.5rem 1.25rem;
border-radius: 2rem;
min-width: 8rem;
font-weight: 500;
}
50 changes: 40 additions & 10 deletions src/components/Elements/Balance/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Typography, useTheme } from '@mui/material';
import { Box, Typography, useTheme } from '@mui/material';

import { formatBalance } from '@/utils/functions';

import { useAccounts } from '@/contexts/account';

import styles from './index.module.scss';

interface BalanceProps {
coretimeBalance: number;
relayBalance: number;
Expand All @@ -10,16 +14,42 @@ interface BalanceProps {

const Balance = ({ relayBalance, coretimeBalance, symbol }: BalanceProps) => {
const theme = useTheme();
const {
state: { activeAccount },
} = useAccounts();

const items = [
{
label: 'Relay Chain',
value: relayBalance,
},
{
label: 'Coretime Chain',
value: coretimeBalance,
},
];

return (
<div style={{ display: 'flex', flexDirection: 'column' }}>
<Typography sx={{ color: theme.palette.text.primary, my: '0.5em' }}>
{`Relay chain: ${formatBalance(relayBalance.toString(), false)} ${symbol}`}
</Typography>
<Typography sx={{ color: theme.palette.text.primary, my: '0.5em' }}>
{`Coretime chain: ${formatBalance(coretimeBalance.toString(), false)} ${symbol}`}
</Typography>
</div>
return activeAccount ? (
<Box className={styles.container}>
{items.map(({ label, value }, index) => (
<Box key={index} className={styles.balanceItem}>
<Typography sx={{ color: theme.palette.text.primary }}>
{label}
</Typography>
<Box
sx={{
bgcolor: theme.palette.common.white,
color: theme.palette.common.black,
}}
className={styles.balanceWrapper}
>
{`${formatBalance(value.toString(), false)} ${symbol}`}
</Box>
</Box>
))}
</Box>
) : (
<></>
);
};

Expand Down
21 changes: 19 additions & 2 deletions src/components/Elements/Selectors/AssetSelector/index.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,24 @@
justify-content: center;
}

.option,
.activeOption {
min-width: 15rem;
margin: 1rem 5rem;
border-radius: 0.5rem !important;
text-transform: capitalize;
}

.activeOption {
border: 1px solid #e84d68;
background: linear-gradient(180deg, #e84d68 0%, #ad2b49 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}

.option {
min-width: 250px;
margin: 1em 5em;
border: 1px solid #cccccc !important;
background-color: white;
color: black;
}
9 changes: 2 additions & 7 deletions src/components/Elements/Selectors/AssetSelector/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ToggleButton, ToggleButtonGroup, useTheme } from '@mui/material';
import { ToggleButton, ToggleButtonGroup } from '@mui/material';
import FormControl from '@mui/material/FormControl';

import { AssetType } from '@/models';
Expand All @@ -16,7 +16,6 @@ export default function AssetSelector({
setAsset,
symbol,
}: AssetSelectorProps) {
const theme = useTheme();
const items = [
{
value: AssetType.TOKEN,
Expand All @@ -38,11 +37,7 @@ export default function AssetSelector({
>
{items.map(({ label, value }, index) => (
<ToggleButton
className={styles.option}
sx={{
color: theme.palette.text.primary,
border: `1px solid ${theme.palette.grey['200']}`,
}}
className={value === asset ? styles.activeOption : styles.option}
value={value}
key={index}
>
Expand Down
16 changes: 16 additions & 0 deletions src/components/Elements/Selectors/ChainSelector/index.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.chainItem {
display: flex;
gap: 0.5rem;
align-items: center;
}

.icon {
width: 1.5rem;
height: 1.5rem;
border-radius: 100%;
}

.label {
font-size: 1rem;
line-height: 1.5;
}
14 changes: 5 additions & 9 deletions src/components/Elements/Selectors/ChainSelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import Image from 'next/image';
import { useCoretimeApi, useRelayApi } from '@/contexts/apis';
import { ChainType, NetworkType } from '@/models';

import styles from './index.module.scss';

interface ChainSelectorProps {
chain: ChainType;
setChain: (_: ChainType) => void;
Expand Down Expand Up @@ -80,18 +82,12 @@ export const ChainSelector = ({ chain, setChain }: ChainSelectorProps) => {
>
{menuItems.map(({ icon, label, value, loading }, index) => (
<MenuItem value={value} key={index}>
<Box sx={{ display: 'flex', gap: '0.5rem', alignItems: 'center' }}>
<Image
src={icon}
alt='icon'
style={{ width: '2rem', height: '2rem', borderRadius: '100%' }}
/>
<Box className={styles.chainItem}>
<Image src={icon} alt='icon' className={styles.icon} />
{loading ? (
<CircularProgress size={'1.5rem'} />
) : (
<Typography sx={{ lineHeight: 1.5, fontSize: '1.25rem' }}>
{label}
</Typography>
<Typography className={styles.label}>{label}</Typography>
)}
</Box>
</MenuItem>
Expand Down
1 change: 1 addition & 0 deletions src/components/Elements/Selectors/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './AssetSelector';
export * from './ChainSelector';
export * from './RecipientSelector';
export * from './RegionSelector';
123 changes: 123 additions & 0 deletions src/contexts/balance/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import React, { createContext, useContext, useEffect, useState } from 'react';

import { useAccounts } from '../account';
import { useCoretimeApi, useRelayApi } from '../apis';
import { ApiState } from '../apis/types';
import { useToast } from '../toast';

interface Props {
children: React.ReactNode;
}

interface BalanceData {
balance: {
coretime: number;
relay: number;
};
}

const defaultBalanceData: BalanceData = {
balance: {
coretime: 0,
relay: 0,
},
};

const BalanceContext = createContext<BalanceData>(defaultBalanceData);

const BalanceProvider = ({ children }: Props) => {
const {
state: { activeAccount },
} = useAccounts();
const { toastWarning } = useToast();
const {
state: {
api: coretimeApi,
apiState: coretimeApiState,
symbol: coretimeSymbol,
name: coretimeChain,
},
} = useCoretimeApi();
const {
state: {
api: relayApi,
apiState: relayApiState,
symbol: relaySymbol,
name: relayChain,
},
} = useRelayApi();

const [coretimeBalance, setCoretimeBalance] = useState(0);
const [relayBalance, setRelayBalance] = useState(0);

useEffect(() => {
const subscribeBalances = async () => {
if (!activeAccount) {
setCoretimeBalance(0);
setRelayBalance(0);
return;
}
if (
coretimeApiState !== ApiState.READY ||
relayApiState !== ApiState.READY
)
return;

if (!coretimeApi || !relayApi) return;

const { address } = activeAccount;

const unsubscribeCoretime = await coretimeApi.queryMulti(
[[coretimeApi.query.system.account, address]],
([
{
data: { free },
},
]: [any]) => {
setCoretimeBalance(free as number);
free === 0 &&
toastWarning(
`The selected account does not have any ${coretimeSymbol} tokens on ${coretimeChain}.`
);
}
);

const unsubscribeRelay = await relayApi.queryMulti(
[[relayApi.query.system.account, address]],
([
{
data: { free },
},
]: [any]) => {
setRelayBalance(free as number);
free === 0 &&
toastWarning(
`The selected account does not have any ${relaySymbol} tokens on ${relayChain}.`
);
}
);
return () => {
unsubscribeCoretime();
unsubscribeRelay();
};
};
subscribeBalances();
}, [activeAccount, coretimeApi, coretimeApiState, relayApi, relayApiState]);

return (
<BalanceContext.Provider
value={{
balance: {
coretime: coretimeBalance,
relay: relayBalance,
},
}}
>
{children}
</BalanceContext.Provider>
);
};

const useBalances = () => useContext(BalanceContext);

export { BalanceProvider, useBalances };
79 changes: 0 additions & 79 deletions src/hooks/balance.tsx

This file was deleted.

Loading

0 comments on commit 8bc44b9

Please sign in to comment.