1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-04 23:14:56 +01:00
metamask-extension/ui/hooks/useCurrentAsset.js

41 lines
1.4 KiB
JavaScript
Raw Normal View History

import { useSelector } from 'react-redux';
import { useRouteMatch } from 'react-router-dom';
import { getTokens } from '../ducks/metamask/metamask';
import { getCurrentChainId } from '../selectors';
import { ASSET_ROUTE } from '../helpers/constants/routes';
import { isEqualCaseInsensitive } from '../helpers/utils/util';
import {
SWAPS_CHAINID_DEFAULT_TOKEN_MAP,
ETH_SWAPS_TOKEN_OBJECT,
} from '../../shared/constants/swaps';
2020-10-06 20:28:38 +02:00
/**
* Returns a token object for the asset that is currently being viewed.
* Will return the default token object for the current chain when the
* user is viewing either the primary, unfiltered, activity list or the
* default token asset page.
2020-10-06 20:28:38 +02:00
* @returns {import('./useTokenDisplayValue').Token}
*/
2020-11-03 00:41:28 +01:00
export function useCurrentAsset() {
2020-10-06 20:28:38 +02:00
// To determine which primary currency to display for swaps transactions we need to be aware
// of which asset, if any, we are viewing at present
2020-11-03 00:41:28 +01:00
const match = useRouteMatch({
path: `${ASSET_ROUTE}/:asset`,
exact: true,
strict: true,
});
const tokenAddress = match?.params?.asset;
const knownTokens = useSelector(getTokens);
2020-11-03 00:41:28 +01:00
const token =
tokenAddress &&
knownTokens.find(({ address }) =>
isEqualCaseInsensitive(address, tokenAddress),
);
const chainId = useSelector(getCurrentChainId);
2020-10-06 20:28:38 +02:00
return (
token ??
(SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId] || ETH_SWAPS_TOKEN_OBJECT)
);
2020-10-06 20:28:38 +02:00
}