Refresh mint distribution when account info refreshes (#13064)

This commit is contained in:
Justin Starry
2020-10-21 18:47:02 +08:00
committed by GitHub
parent 86a982150c
commit 02bf9ca834
2 changed files with 21 additions and 14 deletions

View File

@ -19,14 +19,17 @@ export function TokenLargestAccountsCard({ pubkey }: { pubkey: PublicKey }) {
const mintInfo = useMintAccountInfo(mintAddress); const mintInfo = useMintAccountInfo(mintAddress);
const largestAccounts = useTokenLargestTokens(mintAddress); const largestAccounts = useTokenLargestTokens(mintAddress);
const fetchLargestAccounts = useFetchTokenLargestAccounts(); const fetchLargestAccounts = useFetchTokenLargestAccounts();
const refreshLargest = () => fetchLargestAccounts(pubkey); const refreshLargest = React.useCallback(() => fetchLargestAccounts(pubkey), [
pubkey,
fetchLargestAccounts,
]);
const { cluster } = useCluster(); const { cluster } = useCluster();
const unit = TokenRegistry.get(mintAddress, cluster)?.symbol; const unit = TokenRegistry.get(mintAddress, cluster)?.symbol;
const unitLabel = unit ? `(${unit})` : ""; const unitLabel = unit ? `(${unit})` : "";
React.useEffect(() => { React.useEffect(() => {
if (!largestAccounts) refreshLargest(); if (mintInfo) refreshLargest();
}, [mintAddress]); // eslint-disable-line react-hooks/exhaustive-deps }, [mintInfo, refreshLargest]);
// Largest accounts hasn't started fetching // Largest accounts hasn't started fetching
if (largestAccounts === undefined) return null; if (largestAccounts === undefined) return null;
@ -45,6 +48,8 @@ export function TokenLargestAccountsCard({ pubkey }: { pubkey: PublicKey }) {
text="Failed to fetch largest accounts" text="Failed to fetch largest accounts"
/> />
); );
} else if (largestAccounts.status === FetchStatus.Fetching) {
return <LoadingCard message="Refreshing largest accounts" />;
} }
const accounts = largestAccounts.data.largest; const accounts = largestAccounts.data.largest;

View File

@ -263,19 +263,21 @@ export function useMintAccountInfo(
address: string | undefined address: string | undefined
): MintAccountInfo | undefined { ): MintAccountInfo | undefined {
const accountInfo = useAccountInfo(address); const accountInfo = useAccountInfo(address);
if (address === undefined) return; return React.useMemo(() => {
if (address === undefined) return;
try { try {
const data = accountInfo?.data?.details?.data; const data = accountInfo?.data?.details?.data;
if (!data) return; if (!data) return;
if (data.program !== "spl-token" || data.parsed.type !== "mint") { if (data.program !== "spl-token" || data.parsed.type !== "mint") {
throw new Error("Expected mint"); throw new Error("Expected mint");
}
return coerce(data.parsed.info, MintAccountInfo);
} catch (err) {
reportError(err, { address });
} }
}, [address, accountInfo]);
return coerce(data.parsed.info, MintAccountInfo);
} catch (err) {
reportError(err, { address });
}
} }
export function useTokenAccountInfo( export function useTokenAccountInfo(