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

View File

@ -121,6 +121,16 @@ export const useSourcify = (
return rawMetadata; 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 = ( export const useMultipleMetadata = (
baseMetadatas: Record<string, Metadata | null> | undefined, baseMetadatas: Record<string, Metadata | null> | undefined,
addresses: (ChecksummedAddress | undefined)[], addresses: (ChecksummedAddress | undefined)[],