2021-02-04 19:15:23 +01:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { isEqual } from 'lodash';
|
2020-06-04 20:13:11 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
import TokenCell from '../token-cell';
|
|
|
|
import { useI18nContext } from '../../../hooks/useI18nContext';
|
|
|
|
import { useTokenTracker } from '../../../hooks/useTokenTracker';
|
2021-09-10 19:37:19 +02:00
|
|
|
import { getShouldHideZeroBalanceTokens } from '../../../selectors';
|
2021-03-11 19:07:41 +01:00
|
|
|
import { getTokens } from '../../../ducks/metamask/metamask';
|
2020-06-04 20:13:11 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function TokenList({ onTokenClick }) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const t = useI18nContext();
|
2021-03-09 20:35:55 +01:00
|
|
|
const shouldHideZeroBalanceTokens = useSelector(
|
|
|
|
getShouldHideZeroBalanceTokens,
|
|
|
|
);
|
2020-06-04 20:13:11 +02:00
|
|
|
// use `isEqual` comparison function because the token array is serialized
|
|
|
|
// from the background so it has a new reference with each background update,
|
|
|
|
// even if the tokens haven't changed
|
2021-02-04 19:15:23 +01:00
|
|
|
const tokens = useSelector(getTokens, isEqual);
|
2021-03-09 20:35:55 +01:00
|
|
|
const { loading, tokensWithBalances } = useTokenTracker(
|
2021-03-11 19:07:41 +01:00
|
|
|
tokens,
|
2021-03-09 20:35:55 +01:00
|
|
|
true,
|
2021-03-11 19:07:41 +01:00
|
|
|
shouldHideZeroBalanceTokens,
|
2021-03-09 20:35:55 +01:00
|
|
|
);
|
2020-06-04 20:13:11 +02:00
|
|
|
if (loading) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
display: 'flex',
|
|
|
|
height: '250px',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
padding: '30px',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t('loadingTokens')}
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-06-04 20:13:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{tokensWithBalances.map((tokenData, index) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return <TokenCell key={index} {...tokenData} onClick={onTokenClick} />;
|
2020-06-04 20:13:11 +02:00
|
|
|
})}
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-06-04 20:13:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TokenList.propTypes = {
|
|
|
|
onTokenClick: PropTypes.func.isRequired,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|