2021-02-04 19:15:23 +01:00
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
import { useRouteMatch } from 'react-router-dom';
|
|
|
|
import { getTokens } from '../ducks/metamask/metamask';
|
|
|
|
import { ASSET_ROUTE } from '../helpers/constants/routes';
|
|
|
|
import { ETH_SWAPS_TOKEN_OBJECT } from '../helpers/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 ETH_SWAPS_TOKEN_OBJECT when the user is viewing either
|
|
|
|
* the primary, unfiltered, activity list or the ETH asset page.
|
|
|
|
* @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,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
const tokenAddress = match?.params?.asset;
|
|
|
|
const knownTokens = useSelector(getTokens);
|
2020-11-03 00:41:28 +01:00
|
|
|
const token =
|
2021-02-04 19:15:23 +01:00
|
|
|
tokenAddress && knownTokens.find(({ address }) => address === tokenAddress);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return token ?? ETH_SWAPS_TOKEN_OBJECT;
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|