2022-12-20 13:54:28 +01:00
|
|
|
import { isEqual } from 'lodash';
|
2022-03-09 15:38:12 +01:00
|
|
|
import { useState, useEffect } from 'react';
|
|
|
|
import { useSelector, useDispatch } from 'react-redux';
|
2022-12-20 13:54:28 +01:00
|
|
|
import { getCollectibles, getTokens } from '../ducks/metamask/metamask';
|
2022-07-23 16:37:31 +02:00
|
|
|
import { getAssetDetails } from '../helpers/utils/token-util';
|
2022-03-09 15:38:12 +01:00
|
|
|
import { hideLoadingIndication, showLoadingIndication } from '../store/actions';
|
2022-12-20 13:54:28 +01:00
|
|
|
import { isEqualCaseInsensitive } from '../../shared/modules/string-utils';
|
2022-03-09 15:38:12 +01:00
|
|
|
import { usePrevious } from './usePrevious';
|
2022-12-20 13:54:28 +01:00
|
|
|
import { useTokenTracker } from './useTokenTracker';
|
2022-03-09 15:38:12 +01:00
|
|
|
|
|
|
|
export function useAssetDetails(tokenAddress, userAddress, transactionData) {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
// state selectors
|
|
|
|
const collectibles = useSelector(getCollectibles);
|
2022-12-20 13:54:28 +01:00
|
|
|
const tokens = useSelector(getTokens, isEqual);
|
|
|
|
const currentToken = tokens.find((token) =>
|
|
|
|
isEqualCaseInsensitive(token.address, tokenAddress),
|
|
|
|
);
|
2022-03-09 15:38:12 +01:00
|
|
|
|
|
|
|
// in-hook state
|
|
|
|
const [currentAsset, setCurrentAsset] = useState(null);
|
2022-12-20 13:54:28 +01:00
|
|
|
const { tokensWithBalances } = useTokenTracker(
|
|
|
|
currentToken ? [currentToken] : [],
|
|
|
|
);
|
2022-03-09 15:38:12 +01:00
|
|
|
|
|
|
|
// previous state checkers
|
|
|
|
const prevTokenAddress = usePrevious(tokenAddress);
|
|
|
|
const prevUserAddress = usePrevious(userAddress);
|
|
|
|
const prevTransactionData = usePrevious(transactionData);
|
2022-12-20 13:54:28 +01:00
|
|
|
const prevTokenBalance = usePrevious(tokensWithBalances);
|
2022-03-09 15:38:12 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
async function getAndSetAssetDetails() {
|
|
|
|
dispatch(showLoadingIndication());
|
|
|
|
const assetDetails = await getAssetDetails(
|
|
|
|
tokenAddress,
|
|
|
|
userAddress,
|
|
|
|
transactionData,
|
|
|
|
collectibles,
|
|
|
|
);
|
|
|
|
setCurrentAsset(assetDetails);
|
|
|
|
dispatch(hideLoadingIndication());
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
tokenAddress !== prevTokenAddress ||
|
|
|
|
userAddress !== prevUserAddress ||
|
2022-12-20 13:54:28 +01:00
|
|
|
transactionData !== prevTransactionData ||
|
|
|
|
(prevTokenBalance && prevTokenBalance !== tokensWithBalances)
|
2022-03-09 15:38:12 +01:00
|
|
|
) {
|
|
|
|
getAndSetAssetDetails();
|
|
|
|
}
|
|
|
|
}, [
|
|
|
|
dispatch,
|
|
|
|
prevTokenAddress,
|
|
|
|
prevTransactionData,
|
|
|
|
prevUserAddress,
|
|
|
|
tokenAddress,
|
|
|
|
userAddress,
|
|
|
|
transactionData,
|
|
|
|
collectibles,
|
2022-12-20 13:54:28 +01:00
|
|
|
tokensWithBalances,
|
|
|
|
prevTokenBalance,
|
2022-03-09 15:38:12 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
if (currentAsset) {
|
|
|
|
const {
|
|
|
|
standard,
|
|
|
|
symbol,
|
|
|
|
image,
|
|
|
|
name,
|
|
|
|
balance,
|
2022-07-23 16:37:31 +02:00
|
|
|
tokenId,
|
|
|
|
toAddress,
|
|
|
|
tokenAmount,
|
|
|
|
decimals,
|
2022-03-09 15:38:12 +01:00
|
|
|
} = currentAsset;
|
2022-06-11 19:55:35 +02:00
|
|
|
|
2022-07-23 16:37:31 +02:00
|
|
|
return {
|
|
|
|
toAddress,
|
|
|
|
tokenId,
|
|
|
|
decimals,
|
|
|
|
tokenAmount,
|
|
|
|
assetAddress: tokenAddress,
|
|
|
|
assetStandard: standard,
|
|
|
|
tokenSymbol: symbol ?? '',
|
|
|
|
tokenImage: image,
|
|
|
|
userBalance: balance,
|
|
|
|
assetName: name,
|
|
|
|
};
|
2022-03-09 15:38:12 +01:00
|
|
|
}
|
2022-06-11 19:55:35 +02:00
|
|
|
|
2022-07-23 16:37:31 +02:00
|
|
|
return {};
|
2022-03-09 15:38:12 +01:00
|
|
|
}
|