Extract single Sourcify metadata helper function

This commit is contained in:
Willian Mitsuda 2021-11-25 15:41:40 -03:00
parent 843781c8ba
commit 379ffda83e
2 changed files with 16 additions and 17 deletions

View File

@ -1,4 +1,4 @@
import React, { useState, useEffect, useMemo, useContext } from "react";
import React, { useState, useEffect, useContext } from "react";
import {
useParams,
useNavigate,
@ -20,7 +20,7 @@ import AddressTransactionResults from "./address/AddressTransactionResults";
import Contracts from "./address/Contracts";
import { RuntimeContext } from "./useRuntime";
import { useAppConfigContext } from "./useAppConfig";
import { useMultipleMetadata } from "./useSourcify";
import { useSingleMetadata } from "./useSourcify";
import SourcifyLogo from "./sourcify.svg";
const AddressTransactions: React.FC = () => {
@ -83,21 +83,12 @@ const AddressTransactions: React.FC = () => {
resolveName();
}, [provider, addressOrName, navigate, direction, searchParams]);
const addresses = useMemo(
() => (checksummedAddress ? [checksummedAddress] : []),
[checksummedAddress]
);
const { sourcifySource } = useAppConfigContext();
const metadatas = useMultipleMetadata(
undefined,
addresses,
const addressMetadata = useSingleMetadata(
checksummedAddress,
provider?.network.chainId,
sourcifySource
);
const addressMetadata =
checksummedAddress !== undefined
? metadatas[checksummedAddress]
: undefined;
return (
<StandardFrame>
@ -151,7 +142,7 @@ const AddressTransactions: React.FC = () => {
<FontAwesomeIcon icon={faQuestionCircle} />
</span>
) : (
<span className="self-center text-green-500">
<span className="self-center">
<img
src={SourcifyLogo}
alt="Sourcify logo"
@ -169,9 +160,7 @@ const AddressTransactions: React.FC = () => {
<Route
path="*"
element={
<AddressTransactionResults
address={checksummedAddress}
/>
<AddressTransactionResults address={checksummedAddress} />
}
/>
<Route

View File

@ -121,6 +121,16 @@ export const useSourcify = (
return rawMetadata;
};
export const useSingleMetadata = (
address: ChecksummedAddress | undefined,
chainId: number | undefined,
source: SourcifySource
) => {
const addresses = useMemo(() => (address ? [address] : []), [address]);
const metadatas = useMultipleMetadata(undefined, addresses, chainId, source);
return address !== undefined ? metadatas[address] : undefined;
};
export const useMultipleMetadata = (
baseMetadatas: Record<string, Metadata | null> | undefined,
addresses: (ChecksummedAddress | undefined)[],