mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Hide zero balance tokens at useTokenTracker layer (#10630)
This commit is contained in:
parent
7aaf1c4eb3
commit
90bfaf6ac2
@ -10,10 +10,7 @@ import {
|
|||||||
getAssetImages,
|
getAssetImages,
|
||||||
getShouldHideZeroBalanceTokens,
|
getShouldHideZeroBalanceTokens,
|
||||||
} from '../../../selectors';
|
} from '../../../selectors';
|
||||||
import {
|
import { getTokens } from '../../../ducks/metamask/metamask';
|
||||||
getTokens,
|
|
||||||
getTokensWithBalance,
|
|
||||||
} from '../../../ducks/metamask/metamask';
|
|
||||||
|
|
||||||
export default function TokenList({ onTokenClick }) {
|
export default function TokenList({ onTokenClick }) {
|
||||||
const t = useI18nContext();
|
const t = useI18nContext();
|
||||||
@ -25,13 +22,11 @@ export default function TokenList({ onTokenClick }) {
|
|||||||
// from the background so it has a new reference with each background update,
|
// from the background so it has a new reference with each background update,
|
||||||
// even if the tokens haven't changed
|
// even if the tokens haven't changed
|
||||||
const tokens = useSelector(getTokens, isEqual);
|
const tokens = useSelector(getTokens, isEqual);
|
||||||
const tokensWithBalance = useSelector(getTokensWithBalance, isEqual);
|
|
||||||
|
|
||||||
const { loading, tokensWithBalances } = useTokenTracker(
|
const { loading, tokensWithBalances } = useTokenTracker(
|
||||||
shouldHideZeroBalanceTokens ? tokensWithBalance : tokens,
|
tokens,
|
||||||
true,
|
true,
|
||||||
|
shouldHideZeroBalanceTokens,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (loading) {
|
if (loading) {
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
|
@ -386,6 +386,3 @@ export const getUnconnectedAccountAlertShown = (state) =>
|
|||||||
state.metamask.unconnectedAccountAlertShownOrigins;
|
state.metamask.unconnectedAccountAlertShownOrigins;
|
||||||
|
|
||||||
export const getTokens = (state) => state.metamask.tokens;
|
export const getTokens = (state) => state.metamask.tokens;
|
||||||
|
|
||||||
export const getTokensWithBalance = (state) =>
|
|
||||||
state.metamask.tokens.filter((token) => Number(token.balance) > 0);
|
|
||||||
|
@ -4,21 +4,30 @@ import { useSelector } from 'react-redux';
|
|||||||
import { getCurrentNetwork, getSelectedAddress } from '../selectors';
|
import { getCurrentNetwork, getSelectedAddress } from '../selectors';
|
||||||
import { useEqualityCheck } from './useEqualityCheck';
|
import { useEqualityCheck } from './useEqualityCheck';
|
||||||
|
|
||||||
export function useTokenTracker(tokens, includeFailedTokens = false) {
|
export function useTokenTracker(
|
||||||
|
tokens,
|
||||||
|
includeFailedTokens = false,
|
||||||
|
hideZeroBalanceTokens = false,
|
||||||
|
) {
|
||||||
const network = useSelector(getCurrentNetwork);
|
const network = useSelector(getCurrentNetwork);
|
||||||
const userAddress = useSelector(getSelectedAddress);
|
const userAddress = useSelector(getSelectedAddress);
|
||||||
|
|
||||||
const [loading, setLoading] = useState(() => tokens?.length >= 0);
|
const [loading, setLoading] = useState(() => tokens?.length >= 0);
|
||||||
const [tokensWithBalances, setTokensWithBalances] = useState([]);
|
const [tokensWithBalances, setTokensWithBalances] = useState([]);
|
||||||
const [error, setError] = useState(null);
|
const [error, setError] = useState(null);
|
||||||
const tokenTracker = useRef(null);
|
const tokenTracker = useRef(null);
|
||||||
const memoizedTokens = useEqualityCheck(tokens);
|
const memoizedTokens = useEqualityCheck(tokens);
|
||||||
|
|
||||||
const updateBalances = useCallback((tokenWithBalances) => {
|
const updateBalances = useCallback(
|
||||||
setTokensWithBalances(tokenWithBalances);
|
(tokenWithBalances) => {
|
||||||
setLoading(false);
|
const matchingTokens = hideZeroBalanceTokens
|
||||||
setError(null);
|
? tokenWithBalances.filter((token) => Number(token.balance) > 0)
|
||||||
}, []);
|
: tokenWithBalances;
|
||||||
|
setTokensWithBalances(matchingTokens);
|
||||||
|
setLoading(false);
|
||||||
|
setError(null);
|
||||||
|
},
|
||||||
|
[hideZeroBalanceTokens],
|
||||||
|
);
|
||||||
|
|
||||||
const showError = useCallback((err) => {
|
const showError = useCallback((err) => {
|
||||||
setError(err);
|
setError(err);
|
||||||
|
Loading…
Reference in New Issue
Block a user