import React, { useCallback } from 'react';
import { Text, View, StyleSheet } from 'react-native';
import StyledButton from '../../StyledButton';
import AssetIcon from '../../AssetIcon';
import { fontStyles } from '../../../../styles/common';
import Identicon from '../../Identicon';
import NetworkMainAssetLogo from '../../NetworkMainAssetLogo';
import { useSelector } from 'react-redux';
import { getTokenList } from '../../../../reducers/tokens';
import { toChecksumAddress } from 'ethereumjs-util';
import { useAppThemeFromContext, mockTheme } from '../../../../util/theme';
const createStyles = (colors: any) =>
StyleSheet.create({
item: {
borderWidth: 1,
borderColor: colors.border.default,
padding: 8,
marginBottom: 8,
borderRadius: 8,
},
assetListElement: {
flex: 1,
flexDirection: 'row',
alignItems: 'flex-start',
},
text: {
...(fontStyles.normal as any),
color: colors.text.default,
},
textSymbol: {
...fontStyles.normal,
paddingBottom: 4,
fontSize: 16,
color: colors.text.default,
} as any,
assetInfo: {
flex: 1,
flexDirection: 'column',
alignSelf: 'center',
padding: 4,
},
assetIcon: {
flexDirection: 'column',
alignSelf: 'center',
marginRight: 12,
},
ethLogo: {
width: 50,
height: 50,
},
});
interface Props {
/**
* Array of assets objects returned from the search
*/
searchResults: any;
/**
* Callback triggered when a token is selected
*/
handleSelectAsset: any;
/**
* Message string to display when searchResults is empty
*/
emptyMessage: string;
}
const AssetList = ({
searchResults,
handleSelectAsset,
emptyMessage,
}: Props) => {
const tokenList = useSelector(getTokenList);
const { colors } = useAppThemeFromContext() || mockTheme;
const styles = createStyles(colors);
/**
* Render logo according to asset. Could be ETH, Identicon or contractMap logo
*
* @param {object} asset - Asset to generate the logo to render
*/
const renderLogo = useCallback(
(asset: any) => {
const { address, isETH } = asset;
if (isETH) {
return ;
}
const token =
tokenList?.[toChecksumAddress(address)] ||
tokenList?.[address.toLowerCase()];
const iconUrl = token?.iconUrl;
if (!iconUrl) {
return ;
}
return ;
},
[tokenList, styles],
);
return (
{searchResults.map((_: any, i: number) => {
const { symbol, name } = searchResults[i] || {};
return (
handleSelectAsset(searchResults[i])} // eslint-disable-line
key={i}
>
{renderLogo(searchResults[i])}
{symbol}
{!!name && {name}}
);
})}
{searchResults.length === 0 && (
{emptyMessage}
)}
);
};
export default AssetList;