import { CheckIcon, ExternalLinkIcon } from "@radix-ui/react-icons"; import { useCallback, useEffect, useRef, useState } from "react"; import type { Hex } from "viem"; import type { WaitForReceiptOptions } from "../../../../transaction/actions/wait-for-tx-receipt.js"; import type { PreparedTransaction } from "../../../../transaction/prepare-transaction.js"; import { formatExplorerTxUrl } from "../../../../utils/url.js"; import type { WindowAdapter } from "../../../core/adapters/WindowAdapter.js"; import { useCustomTheme } from "../../../core/design-system/CustomThemeProvider.js"; import { iconSize } from "../../../core/design-system/index.js"; import { useChainExplorers } from "../../../core/hooks/others/useChainQuery.js"; import { useSendTransaction } from "../../hooks/transaction/useSendTransaction.js"; import { AccentFailIcon } from "../ConnectWallet/icons/AccentFailIcon.js"; import { Container, ModalHeader } from "../components/basic.js"; import { Button } from "../components/buttons.js"; import { Spacer } from "../components/Spacer.js"; import { Spinner } from "../components/Spinner.js"; import { Text } from "../components/text.js"; export function ExecutingTxScreen(props: { tx: PreparedTransaction; closeModal: () => void; onTxSent: (data: WaitForReceiptOptions) => void; onBack: (() => void) | undefined; windowAdapter: WindowAdapter; }) { const sendTxCore = useSendTransaction({ payModal: false, }); const [txHash, setTxHash] = useState(); const [txError, setTxError] = useState(); const chainExplorers = useChainExplorers(props.tx.chain); const [status, setStatus] = useState<"loading" | "failed" | "sent">( "loading", ); const theme = useCustomTheme(); const sendTx = useCallback(async () => { setStatus("loading"); setTxError(undefined); try { const txData = await sendTxCore.mutateAsync(props.tx); setTxHash(txData.transactionHash); props.onTxSent(txData); setStatus("sent"); } catch (e) { // Do not reject the transaction here, because the user may want to try again // we only reject on modal close console.error(e); setTxError(e as Error); setStatus("failed"); } }, [sendTxCore, props.tx, props.onTxSent]); const done = useRef(false); useEffect(() => { if (done.current) { return; } done.current = true; sendTx(); }, [sendTx]); return ( {status === "loading" && } {status === "failed" && } {status === "sent" && ( )} {status === "loading" && "Sending transaction"} {status === "failed" && "Transaction failed"} {status === "sent" && "Transaction sent"} {status === "failed" && txError ? txError.message || "" : ""} {status === "failed" && ( )} {status === "sent" && ( <> {txHash && ( <> )} )} {/* CSS Animations */} ); }