2021-02-04 19:15:23 +01:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import contractMap from '@metamask/contract-metadata';
|
|
|
|
import Fuse from 'fuse.js';
|
|
|
|
import InputAdornment from '@material-ui/core/InputAdornment';
|
|
|
|
import TextField from '../../../components/ui/text-field';
|
2018-05-20 08:04:19 +02:00
|
|
|
|
|
|
|
const contractList = Object.entries(contractMap)
|
2020-08-19 18:27:05 +02:00
|
|
|
.map(([address, tokenData]) => ({ ...tokenData, address }))
|
2021-02-04 19:15:23 +01:00
|
|
|
.filter((tokenData) => Boolean(tokenData.erc20));
|
2018-05-20 08:04:19 +02:00
|
|
|
|
|
|
|
const fuse = new Fuse(contractList, {
|
|
|
|
shouldSort: true,
|
|
|
|
threshold: 0.45,
|
|
|
|
location: 0,
|
|
|
|
distance: 100,
|
|
|
|
maxPatternLength: 32,
|
|
|
|
minMatchCharLength: 1,
|
|
|
|
keys: [
|
|
|
|
{ name: 'name', weight: 0.5 },
|
|
|
|
{ name: 'symbol', weight: 0.5 },
|
|
|
|
],
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-05-20 08:04:19 +02:00
|
|
|
|
|
|
|
export default class TokenSearch extends Component {
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-20 08:04:19 +02:00
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
error: null,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-20 08:04:19 +02:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
onSearch: PropTypes.func,
|
|
|
|
error: PropTypes.string,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-20 08:04:19 +02:00
|
|
|
|
2019-12-04 03:21:55 +01:00
|
|
|
state = {
|
|
|
|
searchQuery: '',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-20 08:04:19 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
handleSearch(searchQuery) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setState({ searchQuery });
|
|
|
|
const fuseSearchResult = fuse.search(searchQuery);
|
2020-02-15 21:34:12 +01:00
|
|
|
const addressSearchResult = contractList.filter((token) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return token.address.toLowerCase() === searchQuery.toLowerCase();
|
|
|
|
});
|
|
|
|
const results = [...addressSearchResult, ...fuseSearchResult];
|
|
|
|
this.props.onSearch({ searchQuery, results });
|
2018-05-20 08:04:19 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderAdornment() {
|
2018-05-20 08:04:19 +02:00
|
|
|
return (
|
2020-11-03 00:41:28 +01:00
|
|
|
<InputAdornment position="start" style={{ marginRight: '12px' }}>
|
2020-11-11 16:38:15 +01:00
|
|
|
<img src="images/search.svg" width="17" height="17" alt="" />
|
2018-05-20 08:04:19 +02:00
|
|
|
</InputAdornment>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-05-20 08:04:19 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { error } = this.props;
|
|
|
|
const { searchQuery } = this.state;
|
2018-05-20 08:04:19 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<TextField
|
|
|
|
id="search-tokens"
|
|
|
|
placeholder={this.context.t('searchTokens')}
|
|
|
|
type="text"
|
|
|
|
value={searchQuery}
|
2020-02-15 21:34:12 +01:00
|
|
|
onChange={(e) => this.handleSearch(e.target.value)}
|
2018-05-20 08:04:19 +02:00
|
|
|
error={error}
|
|
|
|
fullWidth
|
2020-10-12 23:10:51 +02:00
|
|
|
autoFocus
|
2018-05-20 08:04:19 +02:00
|
|
|
startAdornment={this.renderAdornment()}
|
|
|
|
/>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-05-20 08:04:19 +02:00
|
|
|
}
|
|
|
|
}
|