1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00
metamask-extension/ui/hooks/useTransactionEventFragment.js
2022-02-03 05:58:28 +05:30

44 lines
1.1 KiB
JavaScript

import { useCallback, useEffect } from 'react';
import { useSelector } from 'react-redux';
import { useGasFeeContext } from '../contexts/gasFee';
import {
createTransactionEventFragment,
updateEventFragment,
} from '../store/actions';
import { selectMatchingFragment } from '../selectors';
import { TRANSACTION_EVENTS } from '../../shared/constants/transaction';
export const useTransactionEventFragment = () => {
const { transaction } = useGasFeeContext();
const fragment = useSelector((state) =>
selectMatchingFragment(state, {
fragmentOptions: {},
existingId: `transaction-added-${transaction?.id}`,
}),
);
useEffect(() => {
if (!fragment && transaction) {
createTransactionEventFragment(
transaction.id,
TRANSACTION_EVENTS.APPROVED,
);
}
}, [fragment, transaction]);
const updateTransactionEventFragment = useCallback(
(params) => {
if (!transaction) {
return;
}
updateEventFragment(`transaction-added-${transaction.id}`, params);
},
[transaction],
);
return {
updateTransactionEventFragment,
};
};