You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.0 KiB
81 lines
2.0 KiB
4 years ago
|
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';
|
||
7 years ago
|
|
||
|
const contractList = Object.entries(contractMap)
|
||
4 years ago
|
.map(([address, tokenData]) => ({ ...tokenData, address }))
|
||
4 years ago
|
.filter((tokenData) => Boolean(tokenData.erc20));
|
||
7 years ago
|
|
||
|
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 },
|
||
|
],
|
||
4 years ago
|
});
|
||
7 years ago
|
|
||
|
export default class TokenSearch extends Component {
|
||
|
static contextTypes = {
|
||
|
t: PropTypes.func,
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
|
static defaultProps = {
|
||
|
error: null,
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
|
static propTypes = {
|
||
|
onSearch: PropTypes.func,
|
||
|
error: PropTypes.string,
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
5 years ago
|
state = {
|
||
|
searchQuery: '',
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
4 years ago
|
handleSearch(searchQuery) {
|
||
4 years ago
|
this.setState({ searchQuery });
|
||
|
const fuseSearchResult = fuse.search(searchQuery);
|
||
5 years ago
|
const addressSearchResult = contractList.filter((token) => {
|
||
4 years ago
|
return token.address.toLowerCase() === searchQuery.toLowerCase();
|
||
|
});
|
||
|
const results = [...addressSearchResult, ...fuseSearchResult];
|
||
|
this.props.onSearch({ searchQuery, results });
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
renderAdornment() {
|
||
7 years ago
|
return (
|
||
4 years ago
|
<InputAdornment position="start" style={{ marginRight: '12px' }}>
|
||
4 years ago
|
<img src="images/search.svg" width="17" height="17" alt="" />
|
||
7 years ago
|
</InputAdornment>
|
||
4 years ago
|
);
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
render() {
|
||
4 years ago
|
const { error } = this.props;
|
||
|
const { searchQuery } = this.state;
|
||
7 years ago
|
|
||
|
return (
|
||
|
<TextField
|
||
|
id="search-tokens"
|
||
|
placeholder={this.context.t('searchTokens')}
|
||
|
type="text"
|
||
|
value={searchQuery}
|
||
5 years ago
|
onChange={(e) => this.handleSearch(e.target.value)}
|
||
7 years ago
|
error={error}
|
||
|
fullWidth
|
||
4 years ago
|
autoFocus
|
||
4 years ago
|
autoComplete="off"
|
||
7 years ago
|
startAdornment={this.renderAdornment()}
|
||
|
/>
|
||
4 years ago
|
);
|
||
7 years ago
|
}
|
||
|
}
|