mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-22 17:33:23 +01:00
Track when token balance is changed to update value on approval screen (#16964)
* Track when token balance is changed to update value on approval screen * Code refactor * Fix unit test * Fix sign approval balance * Refactor code
This commit is contained in:
parent
c61d44a3be
commit
362497ed64
@ -50,7 +50,6 @@ export default class SignatureRequestOriginal extends Component {
|
|||||||
};
|
};
|
||||||
|
|
||||||
state = {
|
state = {
|
||||||
fromAccount: this.props.fromAccount,
|
|
||||||
showSignatureRequestWarning: false,
|
showSignatureRequestWarning: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -274,11 +273,13 @@ export default class SignatureRequestOriginal extends Component {
|
|||||||
};
|
};
|
||||||
|
|
||||||
render = () => {
|
render = () => {
|
||||||
const { messagesCount, conversionRate, nativeCurrency } = this.props;
|
|
||||||
const {
|
const {
|
||||||
|
messagesCount,
|
||||||
|
conversionRate,
|
||||||
|
nativeCurrency,
|
||||||
fromAccount: { address, balance, name },
|
fromAccount: { address, balance, name },
|
||||||
showSignatureRequestWarning,
|
} = this.props;
|
||||||
} = this.state;
|
const { showSignatureRequestWarning } = this.state;
|
||||||
const { t } = this.context;
|
const { t } = this.context;
|
||||||
|
|
||||||
const rejectNText = t('rejectRequestsN', [messagesCount]);
|
const rejectNText = t('rejectRequestsN', [messagesCount]);
|
||||||
|
@ -1,22 +1,33 @@
|
|||||||
|
import { isEqual } from 'lodash';
|
||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
import { useSelector, useDispatch } from 'react-redux';
|
import { useSelector, useDispatch } from 'react-redux';
|
||||||
import { getCollectibles } from '../ducks/metamask/metamask';
|
import { getCollectibles, getTokens } from '../ducks/metamask/metamask';
|
||||||
import { getAssetDetails } from '../helpers/utils/token-util';
|
import { getAssetDetails } from '../helpers/utils/token-util';
|
||||||
import { hideLoadingIndication, showLoadingIndication } from '../store/actions';
|
import { hideLoadingIndication, showLoadingIndication } from '../store/actions';
|
||||||
|
import { isEqualCaseInsensitive } from '../../shared/modules/string-utils';
|
||||||
import { usePrevious } from './usePrevious';
|
import { usePrevious } from './usePrevious';
|
||||||
|
import { useTokenTracker } from './useTokenTracker';
|
||||||
|
|
||||||
export function useAssetDetails(tokenAddress, userAddress, transactionData) {
|
export function useAssetDetails(tokenAddress, userAddress, transactionData) {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
// state selectors
|
// state selectors
|
||||||
const collectibles = useSelector(getCollectibles);
|
const collectibles = useSelector(getCollectibles);
|
||||||
|
const tokens = useSelector(getTokens, isEqual);
|
||||||
|
const currentToken = tokens.find((token) =>
|
||||||
|
isEqualCaseInsensitive(token.address, tokenAddress),
|
||||||
|
);
|
||||||
|
|
||||||
// in-hook state
|
// in-hook state
|
||||||
const [currentAsset, setCurrentAsset] = useState(null);
|
const [currentAsset, setCurrentAsset] = useState(null);
|
||||||
|
const { tokensWithBalances } = useTokenTracker(
|
||||||
|
currentToken ? [currentToken] : [],
|
||||||
|
);
|
||||||
|
|
||||||
// previous state checkers
|
// previous state checkers
|
||||||
const prevTokenAddress = usePrevious(tokenAddress);
|
const prevTokenAddress = usePrevious(tokenAddress);
|
||||||
const prevUserAddress = usePrevious(userAddress);
|
const prevUserAddress = usePrevious(userAddress);
|
||||||
const prevTransactionData = usePrevious(transactionData);
|
const prevTransactionData = usePrevious(transactionData);
|
||||||
|
const prevTokenBalance = usePrevious(tokensWithBalances);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function getAndSetAssetDetails() {
|
async function getAndSetAssetDetails() {
|
||||||
@ -33,7 +44,8 @@ export function useAssetDetails(tokenAddress, userAddress, transactionData) {
|
|||||||
if (
|
if (
|
||||||
tokenAddress !== prevTokenAddress ||
|
tokenAddress !== prevTokenAddress ||
|
||||||
userAddress !== prevUserAddress ||
|
userAddress !== prevUserAddress ||
|
||||||
transactionData !== prevTransactionData
|
transactionData !== prevTransactionData ||
|
||||||
|
(prevTokenBalance && prevTokenBalance !== tokensWithBalances)
|
||||||
) {
|
) {
|
||||||
getAndSetAssetDetails();
|
getAndSetAssetDetails();
|
||||||
}
|
}
|
||||||
@ -46,6 +58,8 @@ export function useAssetDetails(tokenAddress, userAddress, transactionData) {
|
|||||||
userAddress,
|
userAddress,
|
||||||
transactionData,
|
transactionData,
|
||||||
collectibles,
|
collectibles,
|
||||||
|
tokensWithBalances,
|
||||||
|
prevTokenBalance,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (currentAsset) {
|
if (currentAsset) {
|
||||||
|
@ -19,6 +19,7 @@ const renderUseAssetDetails = ({
|
|||||||
chainId: '0x5',
|
chainId: '0x5',
|
||||||
},
|
},
|
||||||
tokenList: {},
|
tokenList: {},
|
||||||
|
tokens: [],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user