1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/ui/hooks/useAssetDetails.js
Nidhi Kumari 33cc8d587a
NFT: Replaced all the instances of collectibles with NFTs (#17741)
* replaced all the instances of collectibles with nfts

* updated actions

* updated e2e seeder

* updated confirm Approve test

* updated test dapp change

* updated test dapp change

* nit fix

* nit fix

* updated casing and snapshots

* updated casinG

* added migrations

* updated ,igration

* updated 078.test

* updated tests for 078 migration

* updated migration

* updated 078 index.js
2023-02-17 00:53:29 +05:30

94 lines
2.5 KiB
JavaScript

import { isEqual } from 'lodash';
import { useState, useEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { getNfts, getTokens } from '../ducks/metamask/metamask';
import { getAssetDetails } from '../helpers/utils/token-util';
import { hideLoadingIndication, showLoadingIndication } from '../store/actions';
import { isEqualCaseInsensitive } from '../../shared/modules/string-utils';
import { usePrevious } from './usePrevious';
import { useTokenTracker } from './useTokenTracker';
export function useAssetDetails(tokenAddress, userAddress, transactionData) {
const dispatch = useDispatch();
// state selectors
const nfts = useSelector(getNfts);
const tokens = useSelector(getTokens, isEqual);
const currentToken = tokens.find((token) =>
isEqualCaseInsensitive(token.address, tokenAddress),
);
// in-hook state
const [currentAsset, setCurrentAsset] = useState(null);
const { tokensWithBalances } = useTokenTracker(
currentToken ? [currentToken] : [],
);
// previous state checkers
const prevTokenAddress = usePrevious(tokenAddress);
const prevUserAddress = usePrevious(userAddress);
const prevTransactionData = usePrevious(transactionData);
const prevTokenBalance = usePrevious(tokensWithBalances);
useEffect(() => {
async function getAndSetAssetDetails() {
dispatch(showLoadingIndication());
const assetDetails = await getAssetDetails(
tokenAddress,
userAddress,
transactionData,
nfts,
);
setCurrentAsset(assetDetails);
dispatch(hideLoadingIndication());
}
if (
tokenAddress !== prevTokenAddress ||
userAddress !== prevUserAddress ||
transactionData !== prevTransactionData ||
(prevTokenBalance && prevTokenBalance !== tokensWithBalances)
) {
getAndSetAssetDetails();
}
}, [
dispatch,
prevTokenAddress,
prevTransactionData,
prevUserAddress,
tokenAddress,
userAddress,
transactionData,
nfts,
tokensWithBalances,
prevTokenBalance,
]);
if (currentAsset) {
const {
standard,
symbol,
image,
name,
balance,
tokenId,
toAddress,
tokenAmount,
decimals,
} = currentAsset;
return {
toAddress,
tokenId,
decimals,
tokenAmount,
assetAddress: tokenAddress,
assetStandard: standard,
tokenSymbol: symbol ?? '',
tokenImage: image,
userBalance: balance,
assetName: name,
};
}
return {};
}