You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.3 KiB
37 lines
1.3 KiB
4 years ago
|
import { useSelector } from 'react-redux';
|
||
|
import { useRouteMatch } from 'react-router-dom';
|
||
|
import { getTokens } from '../ducks/metamask/metamask';
|
||
4 years ago
|
import { getCurrentChainId } from '../selectors';
|
||
4 years ago
|
import { ASSET_ROUTE } from '../helpers/constants/routes';
|
||
4 years ago
|
import {
|
||
|
SWAPS_CHAINID_DEFAULT_TOKEN_MAP,
|
||
|
ETH_SWAPS_TOKEN_OBJECT,
|
||
4 years ago
|
} from '../../shared/constants/swaps';
|
||
4 years ago
|
|
||
|
/**
|
||
|
* Returns a token object for the asset that is currently being viewed.
|
||
4 years ago
|
* 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.
|
||
4 years ago
|
* @returns {import('./useTokenDisplayValue').Token}
|
||
|
*/
|
||
4 years ago
|
export function useCurrentAsset() {
|
||
4 years ago
|
// 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
|
||
4 years ago
|
const match = useRouteMatch({
|
||
|
path: `${ASSET_ROUTE}/:asset`,
|
||
|
exact: true,
|
||
|
strict: true,
|
||
4 years ago
|
});
|
||
|
const tokenAddress = match?.params?.asset;
|
||
|
const knownTokens = useSelector(getTokens);
|
||
4 years ago
|
const token =
|
||
4 years ago
|
tokenAddress && knownTokens.find(({ address }) => address === tokenAddress);
|
||
4 years ago
|
const chainId = useSelector(getCurrentChainId);
|
||
4 years ago
|
|
||
4 years ago
|
return (
|
||
|
token ??
|
||
|
(SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId] || ETH_SWAPS_TOKEN_OBJECT)
|
||
|
);
|
||
4 years ago
|
}
|