From a992bb5f94c5f8dfb60e6a84bbe1fd12ccaac47c Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Wed, 12 Aug 2020 22:41:04 +0800 Subject: [PATCH] Use common provider for explorer cached data (#11582) --- .../components/account/OwnedTokensCard.tsx | 5 +- .../components/account/TokenHistoryCard.tsx | 22 +- .../account/TransactionHistoryCard.tsx | 20 +- explorer/src/pages/AccountDetailsPage.tsx | 20 +- explorer/src/pages/TransactionDetailsPage.tsx | 34 +-- explorer/src/providers/accounts/history.tsx | 139 ++++------- explorer/src/providers/accounts/index.tsx | 137 +++-------- explorer/src/providers/accounts/tokens.tsx | 92 ++------ explorer/src/providers/cache.tsx | 108 +++++++++ .../src/providers/transactions/details.tsx | 103 +++------ explorer/src/providers/transactions/index.tsx | 218 +++--------------- 11 files changed, 312 insertions(+), 586 deletions(-) create mode 100644 explorer/src/providers/cache.tsx diff --git a/explorer/src/components/account/OwnedTokensCard.tsx b/explorer/src/components/account/OwnedTokensCard.tsx index 2845de2c6b..428f8a11f9 100644 --- a/explorer/src/components/account/OwnedTokensCard.tsx +++ b/explorer/src/components/account/OwnedTokensCard.tsx @@ -1,6 +1,6 @@ import React from "react"; import { PublicKey } from "@solana/web3.js"; -import { FetchStatus } from "providers/accounts"; +import { FetchStatus } from "providers/cache"; import { useFetchAccountOwnedTokens, useAccountOwnedTokens, @@ -24,7 +24,8 @@ export function OwnedTokensCard({ pubkey }: { pubkey: PublicKey }) { return null; } - const { status, tokens } = ownedTokens; + const { status } = ownedTokens; + const tokens = ownedTokens.data?.tokens; const fetching = status === FetchStatus.Fetching; if (fetching && (tokens === undefined || tokens.length === 0)) { return ; diff --git a/explorer/src/components/account/TokenHistoryCard.tsx b/explorer/src/components/account/TokenHistoryCard.tsx index dcd1fdff1d..434e70a9b9 100644 --- a/explorer/src/components/account/TokenHistoryCard.tsx +++ b/explorer/src/components/account/TokenHistoryCard.tsx @@ -4,7 +4,7 @@ import { ConfirmedSignatureInfo, ParsedInstruction, } from "@solana/web3.js"; -import { FetchStatus } from "providers/accounts"; +import { FetchStatus } from "providers/cache"; import { useAccountHistories, useFetchAccountHistory, @@ -34,7 +34,7 @@ export function TokenHistoryCard({ pubkey }: { pubkey: PublicKey }) { return null; } - const { tokens } = ownedTokens; + const tokens = ownedTokens.data?.tokens; if (tokens === undefined || tokens.length === 0) return null; return ; @@ -62,17 +62,17 @@ function TokenHistoryTable({ tokens }: { tokens: TokenInfoWithPubkey[] }) { const fetchedFullHistory = tokens.every((token) => { const history = accountHistories[token.pubkey.toBase58()]; - return history && history.foundOldest === true; + return history?.data?.foundOldest === true; }); const fetching = tokens.some((token) => { const history = accountHistories[token.pubkey.toBase58()]; - return history && history.status === FetchStatus.Fetching; + return history?.status === FetchStatus.Fetching; }); const failed = tokens.some((token) => { const history = accountHistories[token.pubkey.toBase58()]; - return history && history.status === FetchStatus.FetchFailed; + return history?.status === FetchStatus.FetchFailed; }); const mintAndTxs = tokens @@ -81,12 +81,13 @@ function TokenHistoryTable({ tokens }: { tokens: TokenInfoWithPubkey[] }) { history: accountHistories[token.pubkey.toBase58()], })) .filter(({ history }) => { - return ( - history !== undefined && history.fetched && history.fetched.length > 0 - ); + return history?.data?.fetched && history.data.fetched.length > 0; }) .flatMap(({ mint, history }) => - (history.fetched as ConfirmedSignatureInfo[]).map((tx) => ({ mint, tx })) + (history?.data?.fetched as ConfirmedSignatureInfo[]).map((tx) => ({ + mint, + tx, + })) ); if (mintAndTxs.length === 0) { @@ -196,7 +197,8 @@ function TokenTransactionRow({ if (!details) fetchDetails(tx.signature); }, []); // eslint-disable-line react-hooks/exhaustive-deps - const instructions = details?.transaction?.transaction.message.instructions; + const instructions = + details?.data?.transaction?.transaction.message.instructions; if (instructions) { const tokenInstructions = instructions.filter( (ix) => "parsed" in ix && ix.program === "spl-token" diff --git a/explorer/src/components/account/TransactionHistoryCard.tsx b/explorer/src/components/account/TransactionHistoryCard.tsx index 81c2ba8a87..0a584d45e6 100644 --- a/explorer/src/components/account/TransactionHistoryCard.tsx +++ b/explorer/src/components/account/TransactionHistoryCard.tsx @@ -1,10 +1,7 @@ import React from "react"; import { PublicKey } from "@solana/web3.js"; -import { - FetchStatus, - useAccountInfo, - useAccountHistory, -} from "providers/accounts"; +import { FetchStatus } from "providers/cache"; +import { useAccountInfo, useAccountHistory } from "providers/accounts"; import { useFetchAccountHistory } from "providers/accounts/history"; import { Signature } from "components/common/Signature"; import { ErrorCard } from "components/common/ErrorCard"; @@ -22,9 +19,11 @@ export function TransactionHistoryCard({ pubkey }: { pubkey: PublicKey }) { if (!history) refresh(); }, [address]); // eslint-disable-line react-hooks/exhaustive-deps - if (!info || !history || info.lamports === undefined) { + if (!history || info?.data === undefined) { return null; - } else if (history.fetched === undefined) { + } + + if (history?.data === undefined) { if (history.status === FetchStatus.Fetching) { return ; } @@ -34,7 +33,8 @@ export function TransactionHistoryCard({ pubkey }: { pubkey: PublicKey }) { ); } - if (history.fetched.length === 0) { + const transactions = history.data.fetched; + if (transactions.length === 0) { if (history.status === FetchStatus.Fetching) { return ; } @@ -48,8 +48,6 @@ export function TransactionHistoryCard({ pubkey }: { pubkey: PublicKey }) { } const detailsList: React.ReactNode[] = []; - const transactions = history.fetched; - for (var i = 0; i < transactions.length; i++) { const slot = transactions[i].slot; const slotTransactions = [transactions[i]]; @@ -126,7 +124,7 @@ export function TransactionHistoryCard({ pubkey }: { pubkey: PublicKey }) {
- {history.foundOldest ? ( + {history.data.foundOldest ? (
Fetched full history
) : (