Skip to content

Commit

Permalink
feat: improve token selection
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Feb 15, 2022
1 parent a318735 commit 5a87346
Show file tree
Hide file tree
Showing 30 changed files with 862 additions and 372 deletions.
8 changes: 4 additions & 4 deletions libs/react-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"css-minimizer-webpack-plugin": "^3.2.0",
"dotenv": "^16.0.0",
"dotenv-expand": "^8.0.1",
"eslint": "^8.8.0",
"eslint": "^8.9.0",
"eslint-config-react-app": "^7.0.0",
"eslint-webpack-plugin": "^3.1.1",
"file-loader": "^6.2.0",
Expand All @@ -61,20 +61,20 @@
"postcss-flexbugs-fixes": "^5.0.2",
"postcss-loader": "^6.2.1",
"postcss-normalize": "^10.0.1",
"postcss-preset-env": "^7.3.1",
"postcss-preset-env": "^7.3.3",
"prompts": "^2.4.2",
"react-app-polyfill": "^3.0.0",
"react-dev-utils": "^12.0.0",
"react-refresh": "^0.11.0",
"resolve": "^1.20.0",
"resolve-url-loader": "^5.0.0",
"sass-loader": "^12.3.0",
"sass-loader": "^12.5.0",
"semver": "^7.3.5",
"source-map-explorer": "^2.5.2",
"source-map-loader": "^3.0.0",
"stream-browserify": "^3.0.0",
"style-loader": "^3.3.1",
"tailwindcss": "^3.0.21",
"tailwindcss": "^3.0.22",
"terser-webpack-plugin": "^5.3.1",
"webpack": "^5.68.0",
"webpack-dev-server": "^4.7.4",
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
"@testing-library/jest-dom": "^5.16.2",
"@testing-library/react": "^12.0.0",
"@testing-library/user-event": "^13.2.1",
"@types/node": "^17.0.17",
"@types/node": "^17.0.18",
"@types/react": "^17.0.39",
"@types/react-dom": "^17.0.11",
"@types/uuid": "^8.3.4",
"@typescript-eslint/eslint-plugin": "^5.11.0",
"@typescript-eslint/parser": "^5.11.0",
"eslint": "^8.8.0",
"@typescript-eslint/eslint-plugin": "^5.12.0",
"@typescript-eslint/parser": "^5.12.0",
"eslint": "^8.9.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^8.3.0",
"eslint-config-react-app": "^7.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/widget/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"react-dom": "^18.0.0-rc.0",
"react-hook-form": "^7.27.0",
"react-i18next": "^11.15.4",
"react-query": "^3.34.14",
"react-query": "^3.34.15",
"react-resize-detector": "^7.0.0",
"react-router-dom": "^6.2.1",
"react-virtual": "^2.10.4"
Expand Down
2 changes: 1 addition & 1 deletion packages/widget/src/components/NavigationHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const NavigationHeader: React.FC = () => {
case routes.settings:
return t(`header.settings`);
case routes.selectToken:
return t(`header.iWouldLikeToSwap`);
return t(`header.wouldLikeToSwap`);
default:
return t(`header.swap`);
}
Expand Down
60 changes: 60 additions & 0 deletions packages/widget/src/components/SelectTokenDrawer/ChainSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Avatar, FormControl, ListItemIcon, MenuItem } from '@mui/material';
import { useFormContext } from 'react-hook-form';
import { SwapFormKey } from '../../providers/SwapFormProvider';
import { useWidgetConfig } from '../../providers/WidgetProvider';
import { Select } from '../Select';
import { FormTypeProps } from './types';

export const ChainSelect = ({ formType }: FormTypeProps) => {
const { register } = useFormContext();
const { supportedChains, fromChain, toChain } = useWidgetConfig();

const menuItems = supportedChains.map((chain) => (
<MenuItem key={chain.key} value={chain.key}>
<ListItemIcon>
<Avatar
src={chain.logoURI}
alt={chain.key}
sx={{ width: 24, height: 24 }}
>
{chain.name[0]}
</Avatar>
</ListItemIcon>
{chain.name}
</MenuItem>
));

return (
<>
<FormControl
fullWidth
sx={{ display: formType === 'from' ? 'inline-flex' : 'none' }}
>
<Select
MenuProps={{ elevation: 2 }}
defaultValue={fromChain}
inputProps={{
...register(SwapFormKey.FromChain),
}}
>
{menuItems}
</Select>
</FormControl>
<FormControl
fullWidth
sx={{ display: formType === 'to' ? 'inline-flex' : 'none' }}
>
<Select
MenuProps={{ elevation: 2 }}
defaultValue={toChain}
inputProps={{
...register(SwapFormKey.ToChain),
}}
hidden={formType === 'to'}
>
{menuItems}
</Select>
</FormControl>
</>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Search as SearchIcon } from '@mui/icons-material';
import { FormControl, InputAdornment } from '@mui/material';
import { useFormContext } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { SwapFormKey } from '../../providers/SwapFormProvider';
import { Input } from '../Input';
import { FormTypeProps } from './types';

export const SearchTokenInput = ({ formType }: FormTypeProps) => {
const { t } = useTranslation();
const { register } = useFormContext();

return (
<>
<FormControl
fullWidth
sx={{ display: formType === 'from' ? 'inline-flex' : 'none' }}
>
<Input
size="small"
placeholder={t(`swap.tokenSearch`)}
endAdornment={
<InputAdornment position="end">
<SearchIcon />
</InputAdornment>
}
inputProps={{
...register(SwapFormKey.FromSearchTokensFilter),
}}
autoComplete="off"
/>
</FormControl>
<FormControl
fullWidth
sx={{ display: formType === 'to' ? 'inline-flex' : 'none' }}
>
<Input
size="small"
placeholder={t(`swap.tokenSearch`)}
endAdornment={
<InputAdornment position="end">
<SearchIcon />
</InputAdornment>
}
inputProps={{
...register(SwapFormKey.ToSearchTokensFilter),
}}
autoComplete="off"
/>
</FormControl>
</>
);
};
166 changes: 70 additions & 96 deletions packages/widget/src/components/SelectTokenDrawer/SelectTokenDrawer.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { Search as SearchIcon } from '@mui/icons-material';
import {
Avatar,
Box,
Divider,
Drawer,
FormControl,
InputAdornment,
ListItemIcon,
MenuItem,
Typography,
} from '@mui/material';
Expand All @@ -21,32 +17,40 @@ import { useFormContext } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { useResizeDetector } from 'react-resize-detector';
import { useMatch, useNavigate } from 'react-router-dom';
import { useWidgetConfig } from '../../providers/WidgetProvider';
import {
SwapFormDirection,
SwapFormKey,
} from '../../providers/SwapFormProvider';
import { routes } from '../../utils/routes';
import { Input } from '../Input';
import { Select } from '../Select';
import { TokenList } from '../TokenList';
import { ChainSelect } from './ChainSelect';
import { SearchTokenInput } from './SearchTokenInput';
import { TokenFilterSelect } from './SelectTokenDrawer.style';
import { SelectTokenDrawerBase, SelectTokenDrawerProps } from './types';
import {
SelectTokenDrawerBase,
SelectTokenDrawerProps,
TokenFilterType,
} from './types';

export const SelectTokenDrawer = forwardRef<
SelectTokenDrawerBase,
SelectTokenDrawerProps
>(({ containerRef, formType }, ref) => {
>(({ containerRef }, ref) => {
const { t } = useTranslation();
const navigate = useNavigate();
const homeMatch = useMatch(routes.home);
const [open, setOpen] = useState(false);
const [formType, setFormType] = useState<SwapFormDirection>('from');
const { register } = useFormContext();
const { supportedChains, fromChain, toChain } = useWidgetConfig();

const defaultChainValue = formType === 'from' ? fromChain : toChain;
const selectChainFormName = `${formType}Chain`;

const openDrawer = useCallback(() => {
navigate(routes.selectToken, { replace: true });
setOpen(true);
}, [navigate]);
const openDrawer = useCallback(
(type) => {
navigate(routes.selectToken, { replace: true });
setFormType(type);
setOpen(true);
},
[navigate],
);

const closeDrawer = useCallback(() => {
setOpen(false);
Expand All @@ -73,9 +77,6 @@ export const SelectTokenDrawer = forwardRef<
const { height: drawerHeaderHeight, ref: drawerHeaderRef } =
useResizeDetector<HTMLDivElement | null>();

const availableTokenListHeight =
drawerHeight && drawerHeaderHeight ? drawerHeight - drawerHeaderHeight : 0;

return (
<Drawer
container={containerRef.current}
Expand Down Expand Up @@ -104,86 +105,59 @@ export const SelectTokenDrawer = forwardRef<
SlideProps={{ container: containerRef.current }}
>
<Box role="presentation">
<Box ref={drawerHeaderRef}>
<Box p={3}>
<FormControl variant="standard" fullWidth>
<Input
size="small"
placeholder={t(`swap.tokenSearch`)}
endAdornment={
<InputAdornment position="end">
<SearchIcon />
</InputAdornment>
}
/>
</FormControl>
</Box>
<Divider light />
<Box mt={3} mx={3}>
<Typography
variant="subtitle1"
noWrap
sx={{ fontWeight: 500 }}
mb={1}
>
{t(`swap.selectChain`)}
</Typography>
<FormControl fullWidth>
<Select
defaultValue={defaultChainValue ?? 'eth'}
inputProps={{
'aria-label': '',
...register(selectChainFormName),
}}
MenuProps={{ elevation: 2 }}
<TokenList
height={drawerHeight ?? 0}
headerHeight={drawerHeaderHeight ?? 0}
onClick={closeDrawer}
formType={formType}
>
<Box ref={drawerHeaderRef}>
<Box p={3}>
<SearchTokenInput formType={formType} />
</Box>
<Divider light />
<Box mt={3} mx={3}>
<Typography
variant="subtitle1"
noWrap
sx={{ fontWeight: 500 }}
mb={1}
>
{supportedChains.map((chain) => (
<MenuItem key={chain.key} value={chain.key}>
<ListItemIcon>
<Avatar
src={chain.logoURI}
alt={chain.key}
sx={{ width: 24, height: 24 }}
>
{chain.name[0]}
</Avatar>
</ListItemIcon>
{chain.name}
</MenuItem>
))}
</Select>
</FormControl>
<Box
mt={3}
sx={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
}}
>
<Typography variant="subtitle1" noWrap sx={{ fontWeight: 500 }}>
{t(`swap.selectToken`)}
{t(`swap.selectChain`)}
</Typography>
<FormControl>
<TokenFilterSelect
defaultValue={0}
inputProps={{
'aria-label': '',
}}
MenuProps={{ elevation: 2, MenuListProps: { dense: true } }}
>
<MenuItem value={0}>{t(`swap.myTokens`)}</MenuItem>
<MenuItem value={1}>{t(`swap.allTokens`)}</MenuItem>
</TokenFilterSelect>
</FormControl>
<ChainSelect formType={formType} />
<Box
mt={3}
pb={1}
sx={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
}}
>
<Typography variant="subtitle1" noWrap sx={{ fontWeight: 500 }}>
{t(`swap.selectToken`)}
</Typography>
<FormControl>
<TokenFilterSelect
defaultValue={TokenFilterType.My}
MenuProps={{ elevation: 2, MenuListProps: { dense: true } }}
inputProps={{
...register(SwapFormKey.MyTokensFilter),
}}
>
<MenuItem value={TokenFilterType.My}>
{t(`swap.myTokens`)}
</MenuItem>
<MenuItem value={TokenFilterType.All}>
{t(`swap.allTokens`)}
</MenuItem>
</TokenFilterSelect>
</FormControl>
</Box>
</Box>
</Box>
</Box>
<TokenList
height={availableTokenListHeight}
onClick={closeDrawer}
chainFormName={selectChainFormName}
/>
</TokenList>
</Box>
</Drawer>
);
Expand Down
Loading

0 comments on commit 5a87346

Please sign in to comment.