import { useCallback } from 'react' import { useTranslations } from 'next-intl' import { useAppDispatch } from '@/store/hooks' import { getTxBlockExplorerLink } from './helpers/getTxBlockExplorerLink' import { getExplorerAddressLink } from './helpers/getExplorerAddressLink' import { useBridgeTxStatus } from './helpers/useBridgeTxStatus' import { isNull } from 'lodash' import { removeTransaction } from '@/slices/_transactions/reducer' import { TransactionPayloadDetail } from '../Activity/Transaction/components/TransactionPayloadDetail' import { Chain, Token } from '@/utils/types' import { TimeRemaining } from './components/TimeRemaining' import { calculateEstimatedTimeStatus } from './helpers/calculateEstimatedTimeStatus' import { DropdownMenu } from './components/DropdownMenu' import { MenuItem } from './components/MenuItem' import { useBridgeTxUpdater } from './helpers/useBridgeTxUpdater' import { AnimatedProgressBar } from './components/AnimatedProgressBar' import { TransactionSupport } from './components/TransactionSupport' import { RightArrow } from '@/components/icons/RightArrow' import { Address } from 'viem' import { useIsTxReverted } from './helpers/useIsTxReverted' import { useTxRefundStatus } from './helpers/useTxRefundStatus' import { DISCORD_URL } from '@/constants/urls' interface _TransactionProps { connectedAddress: string destinationAddress: Address | null originValue: number originChain: Chain originToken: Token destinationChain: Chain destinationToken: Token originTxHash: string bridgeModuleName: string routerAddress: string estimatedTime: number // in seconds timestamp: number currentTime: number kappa?: string status: 'pending' | 'completed' | 'reverted' | 'refunded' disabled: boolean } /** TODO: Update naming after refactoring existing Activity / Transaction flow */ export const _Transaction = ({ connectedAddress, destinationAddress, originValue, originChain, originToken, destinationChain, destinationToken, originTxHash, bridgeModuleName, routerAddress, estimatedTime, timestamp, currentTime, kappa, status, disabled, }: _TransactionProps) => { const dispatch = useAppDispatch() const t = useTranslations('Time') const handleClearTransaction = useCallback(() => { dispatch(removeTransaction({ originTxHash })) }, [dispatch]) const [originTxExplorerLink, originExplorerName] = getTxBlockExplorerLink( originChain?.id, originTxHash ) const [destExplorerAddressLink, destExplorerName] = getExplorerAddressLink( destinationChain?.id, destinationAddress ?? connectedAddress ) // TODO: this could be cleaned up let { remainingTime, delayedTime, delayedTimeInMin, isEstimatedTimeReached, isCheckTxStatus, isCheckTxForRevert, isCheckTxForRefund, } = calculateEstimatedTimeStatus(currentTime, timestamp, estimatedTime) if (bridgeModuleName === 'Gas.zip') { isCheckTxForRefund = isEstimatedTimeReached } const [isTxCompleted, _kappa, deliveryChainId] = useBridgeTxStatus({ originChainId: originChain?.id, destinationChainId: destinationChain?.id, originTxHash, bridgeModuleName, kappa: kappa, checkStatus: isCheckTxStatus && status === 'pending', currentTime: currentTime, }) const isTxReverted = useIsTxReverted( originTxHash as Address, originChain, isCheckTxForRevert && status === 'pending' ) const isTxRefunded = useTxRefundStatus( kappa, routerAddress as Address, originChain, isCheckTxForRefund && status === 'pending' && ['SynapseRFQ', 'Gas.zip'].includes(bridgeModuleName) ) useBridgeTxUpdater({ connectedAddress, destinationChain, kappa: _kappa, originTxHash, isTxComplete: isTxCompleted, isTxReverted, isTxRefunded, deliveryChainId, }) // Show transaction support if the transaction is delayed by more than 5 minutes and not finalized or reverted const showTransactionSupport = status === 'reverted' || status === 'refunded' || (status === 'pending' && delayedTimeInMin && delayedTimeInMin <= -5) return (