1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-25 03:20:23 +01:00
metamask-extension/ui/hooks/useRetryTransaction.js

83 lines
2.7 KiB
JavaScript
Raw Normal View History

import { useDispatch, useSelector } from 'react-redux';
import { useCallback, useState } from 'react';
import { showSidebar } from '../store/actions';
import { setCustomGasLimit, setCustomGasPrice } from '../ducks/gas/gas.duck';
import { getIsMainnet } from '../selectors';
import { isLegacyTransaction } from '../../shared/modules/transaction.utils';
import { useMetricEvent } from './useMetricEvent';
import { useIncrementedGasFees } from './useIncrementedGasFees';
/**
* @typedef {Object} RetryTransactionReturnValue
* @property {(event: Event) => void} retryTransaction - open edit gas popover
* to begin setting retry gas fees
* @property {boolean} showRetryEditGasPopover - Whether to show the popover
* @property {() => void} closeRetryEditGasPopover - close the popover.
*/
/**
* Provides a reusable hook that, given a transactionGroup, will return
* a method for beginning the retry process
* @param {Object} transactionGroup - the transaction group
* @return {RetryTransactionReturnValue}
*/
2020-11-03 00:41:28 +01:00
export function useRetryTransaction(transactionGroup) {
const { primaryTransaction } = transactionGroup;
const isMainnet = useSelector(getIsMainnet);
const hideBasic = !(isMainnet || process.env.IN_TEST);
const customGasSettings = useIncrementedGasFees(transactionGroup);
2020-11-03 00:41:28 +01:00
const trackMetricsEvent = useMetricEvent({
eventOpts: {
category: 'Navigation',
action: 'Activity Log',
name: 'Clicked "Speed Up"',
},
});
const dispatch = useDispatch();
const [showRetryEditGasPopover, setShowRetryEditGasPopover] = useState(false);
const closeRetryEditGasPopover = () => setShowRetryEditGasPopover(false);
2020-11-03 00:41:28 +01:00
const retryTransaction = useCallback(
async (event) => {
event.stopPropagation();
trackMetricsEvent();
if (process.env.SHOW_EIP_1559_UI) {
setShowRetryEditGasPopover(true);
} else {
if (isLegacyTransaction(primaryTransaction)) {
// To support the current process of cancelling or speeding up
// a transaction, we have to inform the custom gas state of the new
// gasPrice to start at.
dispatch(setCustomGasPrice(customGasSettings.gasPrice));
dispatch(setCustomGasLimit(primaryTransaction.txParams.gas));
}
dispatch(
showSidebar({
transitionName: 'sidebar-left',
type: 'customize-gas',
props: { transaction: primaryTransaction, hideBasic },
}),
);
}
2020-11-03 00:41:28 +01:00
},
[
dispatch,
trackMetricsEvent,
customGasSettings,
primaryTransaction,
hideBasic,
],
);
return {
retryTransaction,
showRetryEditGasPopover,
closeRetryEditGasPopover,
};
}