1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-28 05:12:18 +01:00
metamask-extension/ui/pages/swaps/searchable-item-list/list-item-search/list-item-search.component.js

131 lines
4.0 KiB
JavaScript
Raw Normal View History

import React, { useState, useEffect, useRef } from 'react';
2021-06-03 18:08:37 +02:00
import { useSelector } from 'react-redux';
import PropTypes from 'prop-types';
import Fuse from 'fuse.js';
2021-06-03 18:08:37 +02:00
import log from 'loglevel';
import InputAdornment from '@material-ui/core/InputAdornment';
import TextField from '../../../../components/ui/text-field';
import { usePrevious } from '../../../../hooks/usePrevious';
2021-06-03 18:08:37 +02:00
import { isValidHexAddress } from '../../../../../shared/modules/hexstring-utils';
import { fetchToken } from '../../swaps.util';
import { getCurrentChainId } from '../../../../selectors/selectors';
import { getUseNewSwapsApi } from '../../../../ducks/swaps/swaps';
2020-10-06 20:28:38 +02:00
const renderAdornment = () => (
2020-11-03 00:41:28 +01:00
<InputAdornment position="start" style={{ marginRight: '12px' }}>
<img src="images/search.svg" width="17" height="17" alt="" />
2020-10-06 20:28:38 +02:00
</InputAdornment>
);
2020-10-06 20:28:38 +02:00
2020-11-03 00:41:28 +01:00
export default function ListItemSearch({
2020-10-06 20:28:38 +02:00
onSearch,
error,
listToSearch = [],
fuseSearchKeys,
searchPlaceholderText,
defaultToAll,
2021-06-03 18:08:37 +02:00
shouldSearchForImports,
2020-10-06 20:28:38 +02:00
}) {
const fuseRef = useRef();
const [searchQuery, setSearchQuery] = useState('');
2021-06-03 18:08:37 +02:00
const chainId = useSelector(getCurrentChainId);
const useNewSwapsApi = useSelector(getUseNewSwapsApi);
2020-10-06 20:28:38 +02:00
2021-06-03 18:08:37 +02:00
/**
* Search a custom token for import based on a contract address.
* @param {String} contractAddress
*/
const handleSearchTokenForImport = async (contractAddress) => {
setSearchQuery(contractAddress);
try {
const token = await fetchToken(contractAddress, chainId, useNewSwapsApi);
2021-06-03 18:08:37 +02:00
if (token) {
token.primaryLabel = token.symbol;
token.secondaryLabel = token.name;
token.notImported = true;
onSearch({
searchQuery: contractAddress,
results: [token],
});
return;
}
} catch (e) {
log.error('Token not found, show 0 results.', e);
}
onSearch({
searchQuery: contractAddress,
results: [], // No token for import found.
});
};
const handleSearch = async (newSearchQuery) => {
const trimmedNewSearchQuery = newSearchQuery.trim();
const validHexAddress = isValidHexAddress(trimmedNewSearchQuery);
const fuseSearchResult = fuseRef.current.search(newSearchQuery);
2021-06-03 18:08:37 +02:00
const results =
defaultToAll && newSearchQuery === '' ? listToSearch : fuseSearchResult;
if (shouldSearchForImports && results.length === 0 && validHexAddress) {
await handleSearchTokenForImport(trimmedNewSearchQuery);
return;
}
setSearchQuery(newSearchQuery);
2020-10-06 20:28:38 +02:00
onSearch({
searchQuery: newSearchQuery,
2021-06-03 18:08:37 +02:00
results,
});
};
2020-10-06 20:28:38 +02:00
useEffect(() => {
if (!fuseRef.current) {
fuseRef.current = new Fuse(listToSearch, {
shouldSort: true,
threshold: 0.45,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 1,
keys: fuseSearchKeys,
});
2020-10-06 20:28:38 +02:00
}
}, [fuseSearchKeys, listToSearch]);
2020-10-06 20:28:38 +02:00
2021-04-01 20:44:42 +02:00
const previousListToSearch = usePrevious(listToSearch ?? []);
2020-10-06 20:28:38 +02:00
useEffect(() => {
2020-11-03 00:41:28 +01:00
if (
fuseRef.current &&
searchQuery &&
previousListToSearch !== listToSearch
) {
fuseRef.current.setCollection(listToSearch);
const fuseSearchResult = fuseRef.current.search(searchQuery);
onSearch({ searchQuery, results: fuseSearchResult });
2020-10-06 20:28:38 +02:00
}
}, [listToSearch, searchQuery, onSearch, previousListToSearch]);
2020-10-06 20:28:38 +02:00
return (
<TextField
data-testid="search-list-items"
className="searchable-item-list__search"
placeholder={searchPlaceholderText}
type="text"
value={searchQuery}
onChange={(e) => handleSearch(e.target.value)}
error={error}
fullWidth
startAdornment={renderAdornment()}
autoComplete="off"
autoFocus
/>
);
2020-10-06 20:28:38 +02:00
}
ListItemSearch.propTypes = {
onSearch: PropTypes.func,
error: PropTypes.string,
listToSearch: PropTypes.array.isRequired,
fuseSearchKeys: PropTypes.arrayOf(PropTypes.object).isRequired,
searchPlaceholderText: PropTypes.string,
defaultToAll: PropTypes.bool,
2021-06-03 18:08:37 +02:00
shouldSearchForImports: PropTypes.bool,
};