"use client"; import styled from "@emotion/styled"; import { CheckIcon, CrossCircledIcon } from "@radix-ui/react-icons"; import { useQuery } from "@tanstack/react-query"; import { useSyncExternalStore } from "react"; import { ethereum } from "../../../../../chains/chain-definitions/ethereum.js"; import { getCachedChain } from "../../../../../chains/utils.js"; import type { ThirdwebClient } from "../../../../../client/client.js"; import { getPastTransactions, getTransactionStore, type StoredTransaction, } from "../../../../../transaction/transaction-store.js"; import { shortenHex } from "../../../../../utils/address.js"; import { formatExplorerTxUrl } from "../../../../../utils/url.js"; import { useCustomTheme } from "../../../../core/design-system/CustomThemeProvider.js"; import { iconSize, spacing } from "../../../../core/design-system/index.js"; import { useWaitForReceipt } from "../../../../core/hooks/contract/useWaitForReceipt.js"; import { useChainExplorers, useChainIconUrl, } from "../../../../core/hooks/others/useChainQuery.js"; import { useActiveWalletChain } from "../../../../core/hooks/wallets/useActiveWalletChain.js"; import { Container } from "../../components/basic.js"; import { Button } from "../../components/buttons.js"; import { ChainIcon } from "../../components/ChainIcon.js"; import { Spacer } from "../../components/Spacer.js"; import { Spinner } from "../../components/Spinner.js"; import { Text } from "../../components/text.js"; import type { ConnectLocale } from "../locale/types.js"; export function WalletTransactionHistory(props: { onBack?: () => void; locale: ConnectLocale; client: ThirdwebClient; address: string; }) { const activeChain = useActiveWalletChain(); const chainExplorers = useChainExplorers(activeChain); const transactionStore = getTransactionStore(props.address); const reverseChronologicalTransactions = useSyncExternalStore( transactionStore.subscribe, transactionStore.getValue, ); const historicalTxQuery = useQuery({ enabled: !!activeChain, queryFn: () => getPastTransactions({ chain: activeChain || ethereum, client: props.client, walletAddress: props.address, }), queryKey: ["transactions", props.address, activeChain], }); const transactions = [ ...[...reverseChronologicalTransactions].reverse(), ...(historicalTxQuery.data || []), ]; return ( {historicalTxQuery.isLoading && ( Loading recent transactions... )} {!historicalTxQuery.isLoading && transactions.length === 0 ? ( No Transactions ) : ( {transactions.map((tx) => { return ( ); })} )} ); } function TransactionButton(props: { tx: StoredTransaction; client: ThirdwebClient; explorerUrl?: string; }) { const { data: fetchedReceipt, isLoading, error, } = useWaitForReceipt({ chain: getCachedChain(props.tx.chainId), client: props.client, queryOptions: { enabled: props.tx.receipt === undefined, }, transactionHash: props.tx.transactionHash, }); const chainIconQuery = useChainIconUrl(getCachedChain(props.tx.chainId)); const receipt = props.tx.receipt ?? fetchedReceipt; const decoded = props.tx.decoded; const content = (
{/* Row 1 */} {decoded ? decoded.name : `Transaction Sent`} {/* Row 2 */} {receipt?.to ? shortenHex(receipt?.to, 4) : shortenHex(props.tx.transactionHash, 4)}
{/* Status */} {isLoading && } {!isLoading && receipt && receipt.status === "success" && ( )} {(error || (!isLoading && receipt && receipt.status !== "success")) && ( )}
); if (props.explorerUrl) { return ( {content} ); } return content; } const TxButton = /* @__PURE__ */ styled(Button)(() => { const theme = useCustomTheme(); return { "&:hover": { background: theme.colors.secondaryButtonBg, }, background: theme.colors.tertiaryBg, height: "62px", }; });