Skip to content

Commit

Permalink
feat: Some ui updates & Header chain switch update
Browse files Browse the repository at this point in the history
  • Loading branch information
aiden-cao committed Oct 23, 2024
1 parent b573854 commit 101199d
Show file tree
Hide file tree
Showing 14 changed files with 380 additions and 290 deletions.
5 changes: 5 additions & 0 deletions .release/.changeset/big-spoons-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bnb-chain/canonical-bridge-widget": patch
---

Some ui updates & Header chain switch update
1 change: 1 addition & 0 deletions apps/canonical-bridge-ui/core/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const en = {
'select-modal.destination.incompatible.tooltip':
'The token you’ve selected is incompatible with this network. Please select another token.',

'wallet.network.switch-network': 'Please switch the network',
'wallet.network.wrong-network': 'Wrong network',
'wallet.error.switch-network':
'An error occurred when attempting to switch the network. Please select another network or try again.',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect } from 'react';
import { useAccount } from 'wagmi';

import { useAppDispatch } from '@/modules/store/StoreProvider';
import { setSendValue } from '@/modules/transfer/action';
Expand All @@ -9,12 +10,13 @@ export function useDefaultSelectedInfo() {
const { isReady, defaultSelectedInfo } = useAggregator();
const { selectDefault } = useSelection();
const dispatch = useAppDispatch();
const { chainId } = useAccount();

useEffect(() => {
if (isReady) {
selectDefault(defaultSelectedInfo);
dispatch(setSendValue(defaultSelectedInfo.amount));
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isReady]);
}, [isReady, chainId]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import { getTokenBalances } from '@/modules/aggregator/shared/getTokenBalances';
import { sortTokens } from '@/modules/aggregator/shared/sortTokens';

export function useSelection() {
const { getFromChains, getToChains, getTokens, getToToken, adapters } = useAggregator();
const { chainId } = useAccount();
const { getFromChains, getToChains, getTokens, getToToken, adapters, defaultSelectedInfo } =
useAggregator();

const fromChain = useAppSelector((state) => state.transfer.fromChain);
const toChain = useAppSelector((state) => state.transfer.toChain);
Expand Down Expand Up @@ -72,6 +74,38 @@ export function useSelection() {
});
};

const selectFromChain = async (tmpFromChain: IBridgeChain) => {
// After selecting fromChain, if toChain becomes incompatible, reselect the first compatible network in toChain list.
const toChains = getToChains({
fromChainId: tmpFromChain.id,
});

const tmpToChain =
toChains.find((c) => isChainOrTokenCompatible(c) && c.id === toChain?.id) ??
toChains.find((c) => isChainOrTokenCompatible(c) && c.chainType !== 'link');

const tmpTokens = await getSortedTokens({
fromChainId: tmpFromChain.id,
tokens: getTokens({
fromChainId: tmpFromChain.id,
toChainId: tmpToChain?.id,
}),
});

const newToken =
tmpTokens.find(
(t) =>
isChainOrTokenCompatible(t) &&
t.displaySymbol.toUpperCase() === selectedToken?.displaySymbol.toUpperCase(),
) ?? tmpTokens.find((t) => isChainOrTokenCompatible(t));

updateSelectedInfo({
tmpToken: newToken,
tmpFromChain,
tmpToChain,
});
};

return {
async selectDefault({
fromChainId,
Expand All @@ -87,6 +121,15 @@ export function useSelection() {
bridgeTypes.map((item) => [item, { symbol: tokenSymbol }]),
) as any as IBridgeToken;

if (chainId && chainId !== defaultSelectedInfo.fromChainId) {
const fromChains = getFromChains({});
const chain = fromChains.find((chain) => chain.id === chainId);
if (chain) {
selectFromChain(chain);
return;
}
}

const fromChains = getFromChains({
toChainId,
token,
Expand Down Expand Up @@ -116,38 +159,7 @@ export function useSelection() {
token: newToken,
});
},

async selectFromChain(tmpFromChain: IBridgeChain) {
// After selecting fromChain, if toChain becomes incompatible, reselect the first compatible network in toChain list.
const toChains = getToChains({
fromChainId: tmpFromChain.id,
});
const tmpToChain =
toChains.find((c) => isChainOrTokenCompatible(c) && c.id === toChain?.id) ??
toChains.find((c) => isChainOrTokenCompatible(c) && c.chainType !== 'link');

const tmpTokens = await getSortedTokens({
fromChainId: tmpFromChain.id,
tokens: getTokens({
fromChainId: tmpFromChain.id,
toChainId: tmpToChain?.id,
}),
});

const newToken =
tmpTokens.find(
(t) =>
isChainOrTokenCompatible(t) &&
t.displaySymbol.toUpperCase() === selectedToken?.displaySymbol.toUpperCase(),
) ?? tmpTokens.find((t) => isChainOrTokenCompatible(t));

updateSelectedInfo({
tmpToken: newToken,
tmpFromChain,
tmpToChain,
});
},

selectFromChain,
async selectToChain(tmpToChain: IBridgeChain) {
const fromChainId = fromChain!.id;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Button, useColorMode, useIntl, useTheme } from '@bnb-chain/space';
import { Button, useColorMode, useIntl, useTheme, ButtonProps } from '@bnb-chain/space';

import { useAppSelector } from '@/modules/store/StoreProvider';
import { useEvmSwitchChain } from '@/modules/wallet/hooks/useEvmSwitchChain';
import { reportEvent } from '@/core/utils/gtm';

export const SwitchNetworkButton = () => {
export const SwitchNetworkButton = (props: ButtonProps) => {
const { formatMessage } = useIntl();

const fromChain = useAppSelector((state) => state.transfer.fromChain);
Expand Down Expand Up @@ -35,6 +35,7 @@ export const SwitchNetworkButton = () => {
});
}
}}
{...props}
>
{formatMessage({ id: 'transfer.button.switch-network' })}
</Button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import {
Flex,
Typography,
useBreakpointValue,
useColorMode,
useDisclosure,
useIntl,
useTheme,
} from '@bnb-chain/space';
import { Flex, Typography, useColorMode, useDisclosure, useIntl, useTheme } from '@bnb-chain/space';

import { SelectButton } from '@/modules/transfer/components/SelectButton';
import { useAppSelector } from '@/modules/store/StoreProvider';
Expand All @@ -16,26 +8,27 @@ import { SourceNetworkModal } from '@/modules/aggregator/components/SelectModal/
export function FromSection() {
const { colorMode } = useColorMode();
const { isOpen, onClose, onOpen } = useDisclosure();
const isBase = useBreakpointValue({ base: true, md: false }) ?? false;
const { formatMessage } = useIntl();

const fromChain = useAppSelector((state) => state.transfer.fromChain);
const theme = useTheme();

return (
<Flex flexDir="column" gap={'12px'} w={'100%'} flex={1} h={'64px'}>
{isBase ? (
<Flex alignItems="center" justifyContent={'space-between'}>
<Typography
variant="body"
lineHeight={'16px'}
size={'sm'}
color={theme.colors[colorMode].text.placeholder}
>
{formatMessage({ id: 'from.section.title' })}
</Typography>
</Flex>
) : null}
<Flex
alignItems="center"
justifyContent={'space-between'}
display={{ base: 'flex', md: 'none' }}
>
<Typography
variant="body"
lineHeight={'16px'}
size={'sm'}
color={theme.colors[colorMode].text.placeholder}
>
{formatMessage({ id: 'from.section.title' })}
</Typography>
</Flex>

<SelectButton
isActive={isOpen}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
Flex,
Typography,
useBreakpointValue,
useColorMode,
useIntl,
useTheme,
} from '@bnb-chain/space';
import { Flex, Typography, useColorMode, useIntl, useTheme } from '@bnb-chain/space';

import { TransferToIcon } from '@/core/components/icons/TransferToIcon';
import { FromSection } from '@/modules/transfer/components/FromSection';
Expand All @@ -15,44 +8,45 @@ export const NetWorkSection = () => {
const { formatMessage } = useIntl();
const { colorMode } = useColorMode();
const theme = useTheme();
const isBase = useBreakpointValue({ base: true, md: false }) ?? false;

return (
<Flex flexDir={'column'} gap={'12px'}>
{!isBase ? (
<>
<Flex flexDir={'row'}>
<Flex alignItems="center" justifyContent={'space-between'} flex={1}>
<Typography
variant="body"
size={'sm'}
lineHeight={'16px'}
color={theme.colors[colorMode].text.placeholder}
>
{formatMessage({ id: 'from.section.title' })}
</Typography>
</Flex>
<Flex alignItems="center" justifyContent={'space-between'} flex={1} ml="49px">
<Typography
variant="body"
lineHeight={'16px'}
size={'sm'}
color={theme.colors[colorMode].text.placeholder}
>
{formatMessage({ id: 'to.section.title' })}
</Typography>
</Flex>
</Flex>{' '}
</>
) : null}
<Flex flexDir={'row'} display={{ base: 'none', md: 'flex' }}>
<Flex alignItems="center" justifyContent={'space-between'} flex={1}>
<Typography
variant="body"
size={'sm'}
lineHeight={'16px'}
color={theme.colors[colorMode].text.placeholder}
>
{formatMessage({ id: 'from.section.title' })}
</Typography>
</Flex>
<Flex
alignItems="center"
justifyContent={'space-between'}
flex={1}
ml="49px"
display={{ base: 'none', md: 'flex' }}
>
<Typography
variant="body"
lineHeight={'16px'}
size={'sm'}
color={theme.colors[colorMode].text.placeholder}
>
{formatMessage({ id: 'to.section.title' })}
</Typography>
</Flex>
</Flex>{' '}
<Flex
flexDir={['column', 'column', 'row']}
justifyContent={'space-between'}
alignItems={'center'}
gap={['16px', '16px', '16px', '12px']}
>
<FromSection />
<TransferToIcon w={'24px'} h={'24px'} transform={!isBase ? '' : 'rotate(90deg)'} />
<TransferToIcon w={'24px'} h={'24px'} transform={{ base: 'rotate(90deg)', md: 'none' }} />
<ToSection />
</Flex>
</Flex>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
import { Flex, Skeleton, SkeletonCircle, useBreakpointValue } from '@bnb-chain/space';
import { Flex, Skeleton, SkeletonCircle } from '@bnb-chain/space';

export const ReceiveLoading = () => {
const isBase = useBreakpointValue({ base: true, lg: false }) ?? false;

return !isBase ? (
<Flex flexDir={'column'} gap={'8px'}>
<Flex gap={'8px'} mb={'2px'}>
<Skeleton height={'24px'} width={'160px'} />
<Skeleton height={'24px'} width={'80px'} />
</Flex>
<Skeleton height={'12px'} maxW={'333px'} width={'100%'} />
<Skeleton height={'12px'} maxW={'371px'} width={'100%'} />
</Flex>
) : (
<Flex flexDir={'row'} alignItems={'flex-start'} justifyContent={'space-between'} gap={'23px'}>
<Flex flexDir={'column'} flex={1} gap={'8px'}>
<Flex gap={'8px'} mb={'2px'} flexDir={'row'}>
<Skeleton height={'12px'} flexBasis={'66%'} />
<Skeleton height={'12px'} flexBasis={'33%'} />
</Flex>
return (
<>
<Flex flexDir={'column'} gap={'8px'} display={{ base: 'none', lg: 'flex' }}>
<Flex gap={'8px'} mb={'2px'}>
<Skeleton height={'24px'} flexBasis={'66%'} />
<Skeleton height={'24px'} flexBasis={'33%'} />
<Skeleton height={'24px'} width={'160px'} />
<Skeleton height={'24px'} width={'80px'} />
</Flex>
<Skeleton height={'12px'} maxW={'333px'} width={'100%'} />
<Skeleton height={'12px'} maxW={'371px'} width={'100%'} />
</Flex>
<Flex
display={{ base: 'flex', lg: 'none' }}
flexDir={'row'}
alignItems={'flex-start'}
justifyContent={'space-between'}
gap={'23px'}
>
<Flex flexDir={'column'} flex={1} gap={'8px'}>
<Flex gap={'8px'} mb={'2px'} flexDir={'row'}>
<Skeleton height={'12px'} flexBasis={'66%'} />
<Skeleton height={'12px'} flexBasis={'33%'} />
</Flex>
<Flex gap={'8px'} mb={'2px'}>
<Skeleton height={'24px'} flexBasis={'66%'} />
<Skeleton height={'24px'} flexBasis={'33%'} />
</Flex>

<Flex gap={'8px'} mb={'2px'}>
<Skeleton height={'16px'} width={'100%'} borderRadius={'4px'} />
</Flex>
<Flex gap={'8px'} mb={'2px'}>
<Skeleton height={'16px'} width={'100%'} borderRadius={'4px'} />
</Flex>
<Flex gap={'8px'} mb={'2px'}>
<Skeleton height={'16px'} width={'100%'} borderRadius={'4px'} />
<Flex gap={'8px'} mb={'2px'}>
<Skeleton height={'16px'} width={'100%'} borderRadius={'4px'} />
</Flex>
<Flex gap={'8px'} mb={'2px'}>
<Skeleton height={'16px'} width={'100%'} borderRadius={'4px'} />
</Flex>
<Flex gap={'8px'} mb={'2px'}>
<Skeleton height={'16px'} width={'100%'} borderRadius={'4px'} />
</Flex>
</Flex>
<SkeletonCircle w={'32px'} h={'32px'} />
</Flex>

<SkeletonCircle w={'32px'} h={'32px'} />
</Flex>
</>
);
};
Loading

0 comments on commit 101199d

Please sign in to comment.