-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
539 additions
and
360 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,56 @@ | ||
import type { Theme } from '@mui/material'; | ||
import { Box } from '@mui/material'; | ||
import { darken, lighten, styled } from '@mui/material/styles'; | ||
import { alpha, darken, lighten, styled } from '@mui/material/styles'; | ||
import type { MouseEventHandler } from 'react'; | ||
|
||
type CardVariant = 'default' | 'selected' | 'error'; | ||
|
||
const getBackgroundColor = (theme: Theme, variant?: CardVariant) => | ||
variant === 'selected' | ||
? theme.palette.mode === 'light' | ||
? alpha(theme.palette.primary.main, 0.04) | ||
: alpha(theme.palette.primary.main, 0.42) | ||
: theme.palette.background.paper; | ||
|
||
export const Card = styled(Box, { | ||
shouldForwardProp: (prop) => | ||
!['dense', 'variant', 'indented'].includes(prop as string), | ||
})<{ | ||
variant?: 'default' | 'error'; | ||
variant?: CardVariant; | ||
dense?: boolean; | ||
indented?: boolean; | ||
onClick?: MouseEventHandler<HTMLDivElement>; | ||
}>(({ theme, variant, dense, indented, onClick }) => ({ | ||
backgroundColor: theme.palette.background.paper, | ||
border: `1px solid`, | ||
borderColor: | ||
variant === 'error' | ||
? theme.palette.error.main | ||
: theme.palette.mode === 'light' | ||
? theme.palette.grey[300] | ||
: theme.palette.grey[800], | ||
borderRadius: dense | ||
? theme.shape.borderRadiusSecondary | ||
: theme.shape.borderRadius, | ||
overflow: 'hidden', | ||
position: 'relative', | ||
padding: indented ? theme.spacing(2) : 0, | ||
boxSizing: 'border-box', | ||
'&:hover': { | ||
cursor: onClick ? 'pointer' : 'default', | ||
backgroundColor: onClick | ||
? theme.palette.mode === 'light' | ||
? darken(theme.palette.background.paper, 0.02) | ||
: lighten(theme.palette.background.paper, 0.02) | ||
: theme.palette.background.paper, | ||
}, | ||
transition: theme.transitions.create(['background-color'], { | ||
duration: theme.transitions.duration.enteringScreen, | ||
easing: theme.transitions.easing.easeOut, | ||
}), | ||
})); | ||
}>(({ theme, variant, dense, indented, onClick }) => { | ||
const backgroundColor = getBackgroundColor(theme, variant); | ||
return { | ||
backgroundColor, | ||
border: `1px solid`, | ||
borderColor: | ||
variant === 'error' | ||
? theme.palette.error.main | ||
: variant === 'selected' | ||
? theme.palette.primary.main | ||
: theme.palette.mode === 'light' | ||
? theme.palette.grey[300] | ||
: theme.palette.grey[800], | ||
borderRadius: dense | ||
? theme.shape.borderRadiusSecondary | ||
: theme.shape.borderRadius, | ||
overflow: 'hidden', | ||
position: 'relative', | ||
padding: indented ? theme.spacing(2) : 0, | ||
boxSizing: 'border-box', | ||
'&:hover': { | ||
cursor: onClick ? 'pointer' : 'default', | ||
backgroundColor: onClick | ||
? theme.palette.mode === 'light' | ||
? darken(backgroundColor, 0.02) | ||
: lighten(backgroundColor, 0.02) | ||
: theme.palette.background.paper, | ||
}, | ||
transition: theme.transitions.create(['background-color'], { | ||
duration: theme.transitions.duration.enteringScreen, | ||
easing: theme.transitions.easing.easeOut, | ||
}), | ||
}; | ||
}); |
18 changes: 18 additions & 0 deletions
18
packages/widget/src/components/ChainSelect/ChainSelect.style.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Box } from '@mui/material'; | ||
import { styled } from '@mui/material/styles'; | ||
import { Card } from '../../components/Card'; | ||
|
||
export const ChainCard = styled(Card)(({ theme }) => ({ | ||
display: 'grid', | ||
placeItems: 'center', | ||
width: 56, | ||
height: 56, | ||
})); | ||
|
||
export const ChainContainer = styled(Box)(({ theme }) => ({ | ||
display: 'grid', | ||
gridTemplateColumns: 'repeat(auto-fit, 56px)', | ||
gridAutoRows: '56px', | ||
justifyContent: 'space-between', | ||
gap: theme.spacing(1.5), | ||
})); |
72 changes: 72 additions & 0 deletions
72
packages/widget/src/components/ChainSelect/ChainSelect.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* eslint-disable react/no-array-index-key */ | ||
import type { EVMChain } from '@lifi/sdk'; | ||
import { Avatar, Box, Skeleton, Typography } from '@mui/material'; | ||
import { useWatch } from 'react-hook-form'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import type { SwapFormTypeProps } from '../../providers'; | ||
import { SwapFormKeyHelper } from '../../providers'; | ||
import { maxChainToOrder } from '../../stores/chains'; | ||
import { navigationRoutes } from '../../utils'; | ||
import { ChainCard, ChainContainer } from './ChainSelect.style'; | ||
import { useChainSelect } from './useChainSelect'; | ||
|
||
export const ChainSelect = ({ formType }: SwapFormTypeProps) => { | ||
const navigate = useNavigate(); | ||
const { chains, getChains, setCurrentChain, isLoading } = | ||
useChainSelect(formType); | ||
const [chainId] = useWatch({ | ||
name: [SwapFormKeyHelper.getChainKey(formType)], | ||
}); | ||
|
||
const showAllChains = () => { | ||
navigate(navigationRoutes[`${formType}Chain`], { | ||
state: { formType }, | ||
}); | ||
}; | ||
|
||
const chainsToHide = (chains?.length ?? 0) - maxChainToOrder; | ||
|
||
return ( | ||
<ChainContainer> | ||
{isLoading | ||
? Array.from({ length: maxChainToOrder + 1 }).map((_, index) => ( | ||
<Skeleton | ||
key={index} | ||
variant="rectangular" | ||
width={56} | ||
height={56} | ||
sx={{ borderRadius: 1 }} | ||
/> | ||
)) | ||
: getChains().map((chain: EVMChain) => ( | ||
<ChainCard | ||
key={chain.id} | ||
onClick={() => setCurrentChain(chain.id)} | ||
variant={chainId === chain.id ? 'selected' : 'default'} | ||
> | ||
<Avatar | ||
src={chain.logoURI} | ||
alt={chain.key} | ||
sx={{ width: 40, height: 40 }} | ||
> | ||
{chain.name[0]} | ||
</Avatar> | ||
</ChainCard> | ||
))} | ||
{chainsToHide > 0 ? ( | ||
<ChainCard onClick={showAllChains}> | ||
<Box | ||
sx={{ | ||
width: 40, | ||
height: 40, | ||
display: 'grid', | ||
placeItems: 'center', | ||
}} | ||
> | ||
<Typography fontWeight={500}>+{chainsToHide}</Typography> | ||
</Box> | ||
</ChainCard> | ||
) : null} | ||
</ChainContainer> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './ChainSelect'; | ||
export * from './useChainSelect'; |
36 changes: 36 additions & 0 deletions
36
packages/widget/src/components/ChainSelect/useChainSelect.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { EVMChain } from '@lifi/sdk'; | ||
import { useFormContext } from 'react-hook-form'; | ||
import { useChains } from '../../hooks'; | ||
import type { SwapFormDirection } from '../../providers'; | ||
import { SwapFormKey, SwapFormKeyHelper } from '../../providers'; | ||
import { useChainOrder } from '../../stores/chains'; | ||
|
||
export const useChainSelect = (formType: SwapFormDirection) => { | ||
const { setValue } = useFormContext(); | ||
const { chains, isLoading } = useChains(); | ||
const [chainOrder, setChainOrder] = useChainOrder(); | ||
const chainKey = SwapFormKeyHelper.getChainKey(formType); | ||
|
||
const getChains = () => { | ||
if (!chains) { | ||
return []; | ||
} | ||
const selectedChains = chainOrder | ||
.map((chainId) => chains.find((chain) => chain.id === chainId)) | ||
.filter((chain) => chain) as EVMChain[]; | ||
|
||
return selectedChains; | ||
}; | ||
|
||
const setCurrentChain = (chainId: number) => { | ||
setValue(chainKey, chainId, { shouldDirty: true }); | ||
setValue(SwapFormKeyHelper.getTokenKey(formType), '', { | ||
shouldDirty: false, | ||
}); | ||
setValue(SwapFormKeyHelper.getAmountKey(formType), ''); | ||
setValue(SwapFormKey.TokenSearchFilter, ''); | ||
setChainOrder(chainId); | ||
}; | ||
|
||
return { chains, getChains, setCurrentChain, isLoading }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.