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,6 +263,7 @@ export function useMintAccountInfo(
address: string | undefined address: string | undefined
): MintAccountInfo | undefined { ): MintAccountInfo | undefined {
const accountInfo = useAccountInfo(address); const accountInfo = useAccountInfo(address);
return React.useMemo(() => {
if (address === undefined) return; if (address === undefined) return;
try { try {
@ -276,6 +277,7 @@ export function useMintAccountInfo(
} catch (err) { } catch (err) {
reportError(err, { address }); reportError(err, { address });
} }
}, [address, accountInfo]);
} }
export function useTokenAccountInfo( export function useTokenAccountInfo(