import React from "react"; import { useAccounts, useAccountsDispatch, fetchAccountInfo, ActionType, Account, Status } from "../providers/accounts"; import { assertUnreachable, displayAddress } from "../utils"; import { useCluster } from "../providers/cluster"; import { PublicKey, LAMPORTS_PER_SOL } from "@solana/web3.js"; function AccountsCard() { const { accounts, idCounter } = useAccounts(); const dispatch = useAccountsDispatch(); const addressInput = React.useRef(null); const [error, setError] = React.useState(""); const { url } = useCluster(); const onNew = (address: string) => { if (address.length === 0) return; let pubkey; try { pubkey = new PublicKey(address); } catch (err) { setError(`${err}`); return; } dispatch({ type: ActionType.Input, pubkey }); fetchAccountInfo(dispatch, address, url); const inputEl = addressInput.current; if (inputEl) { inputEl.value = ""; } }; return (
{renderHeader()}
{accounts.map(account => renderAccountRow(account))}
Status Address Balance (SOL) Data (bytes) Owner
{idCounter + 1} New setError("")} onKeyDown={e => e.keyCode === 13 && onNew(e.currentTarget.value) } onSubmit={e => onNew(e.currentTarget.value)} ref={addressInput} className={`form-control text-address text-monospace ${ error ? "is-invalid" : "" }`} placeholder="input account address" /> {error ?
{error}
: null}
- - -
); } const renderHeader = () => { return (

Look Up Account(s)

); }; const renderAccountRow = (account: Account) => { let statusText; let statusClass; switch (account.status) { case Status.NotFound: statusClass = "danger"; statusText = "Not Found"; break; case Status.CheckFailed: statusClass = "danger"; statusText = "Error"; break; case Status.Checking: statusClass = "info"; statusText = "Fetching"; break; case Status.Success: if (account.details?.executable) { statusClass = "dark"; statusText = "Executable"; } else { statusClass = "success"; statusText = "Found"; } break; default: return assertUnreachable(account.status); } let data = "-"; let owner = "-"; if (account.details) { data = `${account.details.space}`; owner = displayAddress(account.details.owner); } let balance = "-"; if (account.lamports !== undefined) { balance = `◎${(1.0 * account.lamports) / LAMPORTS_PER_SOL}`; } return ( {account.id} {statusText} {account.pubkey.toBase58()} {balance} {data} {owner === "-" ? owner : {owner}} ); }; export default AccountsCard;