import toast from "react-hot-toast"; import { useSnapshot } from "valtio"; import { ArrowSquareOut, Copy, Wallet, X } from "phosphor-react"; import React, { useEffect, useState } from "react"; import Dinero from "dinero.js"; import Button from "./Button"; import { addFaucetAccount, deployHook, importAccount } from "../state/actions"; import state from "../state"; import Box from "./Box"; import Container from "./Container"; import Heading from "./Heading"; import Stack from "./Stack"; import Text from "./Text"; import Flex from "./Flex"; import { Dialog, DialogContent, DialogTitle, DialogDescription, DialogClose, DialogTrigger, } from "./Dialog"; import { css } from "../stitches.config"; import { Input } from "./Input"; const labelStyle = css({ color: "$mauve10", textTransform: "uppercase", fontSize: "10px", mb: "$0.5", }); const AccountDialog = ({ activeAccountAddress, setActiveAccountAddress, }: { activeAccountAddress: string | null; setActiveAccountAddress: React.Dispatch>; }) => { const snap = useSnapshot(state); const [showSecret, setShowSecret] = useState(false); const activeAccount = snap.accounts.find( (account) => account.address === activeAccountAddress ); return ( { setShowSecret(false); !open && setActiveAccountAddress(null); }} > {activeAccount?.name} Account Address {activeAccount?.address} Secret {showSecret ? activeAccount?.secret : "•".repeat(activeAccount?.secret.length || 16)}{" "} Balances & Objects {Dinero({ amount: Number(activeAccount?.xrp || "0"), precision: 6, }) .toUnit() .toLocaleString(undefined, { style: "currency", currency: "XRP", currencyDisplay: "name", })} Installed Hooks {activeAccount && activeAccount?.hooks?.length > 0 ? activeAccount?.hooks .map((i) => { return `${i?.substring(0, 6)}...${i?.substring( i.length - 4 )}`; }) .join(", ") : "–"} ); }; const Accounts = () => { const snap = useSnapshot(state); const [activeAccountAddress, setActiveAccountAddress] = useState< string | null >(null); useEffect(() => { const fetchAccInfo = async () => { if (snap.clientStatus === "online") { const requests = snap.accounts.map((acc) => snap.client?.send({ id: acc.address, command: "account_info", account: acc.address, }) ); const responses = await Promise.all(requests); responses.forEach((res: any) => { const address = res?.account_data?.Account as string; const balance = res?.account_data?.Balance as string; const sequence = res?.account_data?.Sequence as number; const accountToUpdate = state.accounts.find( (acc) => acc.address === address ); if (accountToUpdate) { accountToUpdate.xrp = balance; accountToUpdate.sequence = sequence; } }); const objectRequests = snap.accounts.map((acc) => { return snap.client?.send({ id: `${acc.address}-hooks`, command: "account_objects", account: acc.address, }); }); const objectResponses = await Promise.all(objectRequests); objectResponses.forEach((res: any) => { const address = res?.account as string; const accountToUpdate = state.accounts.find( (acc) => acc.address === address ); if (accountToUpdate) { accountToUpdate.hooks = res.account_objects .filter((ac: any) => ac?.LedgerEntryType === "Hook") .map((oo: any) => oo.HookHash); } }); } }; let fetchAccountInfoInterval: NodeJS.Timer; if (snap.clientStatus === "online") { fetchAccInfo(); fetchAccountInfoInterval = setInterval(() => fetchAccInfo(), 2000); } return () => { if (snap.accounts.length > 0) { if (fetchAccountInfoInterval) { clearInterval(fetchAccountInfoInterval); } } }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [snap.accounts, snap.clientStatus]); return ( Accounts {snap.accounts.map((account) => ( setActiveAccountAddress(account.address)} css={{ gap: "$3", p: "$2 $3", justifyContent: "center", cursor: "pointer", "@hover": { "&:hover": { background: "$mauve3", }, }, }} > {account.name} {account.address} ( {Dinero({ amount: Number(account?.xrp || "0"), precision: 6, }) .toUnit() .toLocaleString(undefined, { style: "currency", currency: "XRP", currencyDisplay: "name", })} ) ))} ); }; const ImportAccountDialog = () => { const [value, setValue] = useState(""); return ( Import account setValue(e.target.value)} /> ); }; export default Accounts;