import { useState } from "react"; import { StyleSheet, View } from "react-native"; import type { ThirdwebClient } from "../../../../client/client.js"; import type { Theme } from "../../../core/design-system/index.js"; import { useChainSymbol } from "../../../core/hooks/others/useChainQuery.js"; import { useActiveAccount } from "../../../core/hooks/wallets/useActiveAccount.js"; import { useActiveWalletChain } from "../../../core/hooks/wallets/useActiveWalletChain.js"; import { useSendToken } from "../../../core/hooks/wallets/useSendToken.js"; import type { SupportedTokens, TokenInfo, } from "../../../core/utils/defaultTokens.js"; import { radius, spacing } from "../../design-system/index.js"; import { ThemedButton } from "../components/button.js"; import { type ContainerType, Header } from "../components/Header.js"; import { ThemedInput } from "../components/input.js"; import { Spacer } from "../components/spacer.js"; import { ThemedSpinner } from "../components/spinner.js"; import { ThemedText } from "../components/text.js"; import { ErrorView } from "./ErrorView.js"; import { SuccessView } from "./SuccessView.js"; import { TokenListScreen, TokenRow } from "./TokenListScreen.js"; type SendScreenProps = { theme: Theme; client: ThirdwebClient; onClose?: () => void; onBack?: () => void; containerType: ContainerType; supportedTokens?: SupportedTokens; }; export const SendScreen = (props: SendScreenProps) => { const { theme, client, onClose, onBack, containerType, supportedTokens } = props; const [receiverAddress, setReceiverAddress] = useState(""); const [amount, setAmount] = useState("0"); const [selectedToken, setSelectedToken] = useState(); const account = useActiveAccount(); const chain = useActiveWalletChain(); const { symbol } = useChainSymbol(chain); const sendMutation = useSendToken(client); const [screen, setScreen] = useState< "base" | "tokenList" | "success" | "error" >("base"); const handleTokenClicked = () => { setScreen("tokenList"); }; const handleSend = async () => { sendMutation.mutate( { amount, receiverAddress, tokenAddress: selectedToken?.address, }, { onError() { setScreen("error"); }, onSuccess() { setScreen("success"); }, }, ); }; if (screen === "success") { return ( <>
setScreen("base")} onClose={onClose} theme={theme} title="Funds Sent" /> ); } if (screen === "error") { return ( <>
setScreen("base")} onClose={onClose} theme={theme} title="Error Sending Funds" /> ); } if (screen === "tokenList") { return ( <>
setScreen("base")} onClose={onClose} theme={theme} title="Token to Send" /> { setSelectedToken(t); setScreen("base"); }} supportedTokens={supportedTokens} theme={theme} /> ); } return ( <>
Token Send to Amount {selectedToken?.symbol || symbol} } theme={theme} value={amount} /> {sendMutation.isPending ? ( ) : ( Send )} ); }; const styles = StyleSheet.create({ container: { flexDirection: "column", gap: spacing.md, paddingHorizontal: spacing.lg, paddingVertical: spacing.lg, }, inputContainer: { gap: spacing.sm, }, });