From fe8be2ee21bd1d823b4e4f166b777485b8bd16f9 Mon Sep 17 00:00:00 2001 From: Willian Mitsuda Date: Wed, 10 Nov 2021 05:00:27 -0300 Subject: [PATCH] Retrofit useMultipleMetadata into AddressTransactions page --- src/AddressTransactions.tsx | 20 +++++++++++++------- src/useSourcify.ts | 9 ++++++--- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/AddressTransactions.tsx b/src/AddressTransactions.tsx index 19d41bb..69ebad2 100644 --- a/src/AddressTransactions.tsx +++ b/src/AddressTransactions.tsx @@ -31,7 +31,7 @@ import { useFeeToggler } from "./search/useFeeToggler"; import { SelectionContext, useSelection } from "./useSelection"; import { useMultipleETHUSDOracle } from "./usePriceOracle"; import { useAppConfigContext } from "./useAppConfig"; -import { useSourcify } from "./useSourcify"; +import { useMultipleMetadata } from "./useSourcify"; import SourcifyLogo from "./sourcify.svg"; type BlockParams = { @@ -181,12 +181,18 @@ const AddressTransactions: React.FC = () => { const [feeDisplay, feeDisplayToggler] = useFeeToggler(); const selectionCtx = useSelection(); + const addresses = useMemo(() => [checksummedAddress], [checksummedAddress]); const { sourcifySource } = useAppConfigContext(); - const rawMetadata = useSourcify( - checksummedAddress, + const metadatas = useMultipleMetadata( + undefined, + addresses, provider?.network.chainId, sourcifySource ); + const addressMetadata = + checksummedAddress !== undefined + ? metadatas[checksummedAddress] + : undefined; return ( @@ -224,18 +230,18 @@ const AddressTransactions: React.FC = () => { Contract - {rawMetadata === undefined ? ( + {addressMetadata === undefined ? ( - ) : rawMetadata === null ? ( + ) : addressMetadata === null ? ( @@ -316,7 +322,7 @@ const AddressTransactions: React.FC = () => { diff --git a/src/useSourcify.ts b/src/useSourcify.ts index 2b2f9c4..1911583 100644 --- a/src/useSourcify.ts +++ b/src/useSourcify.ts @@ -87,6 +87,7 @@ const fetchSourcifyMetadata = async ( } }; +// TODO: replace every occurrence with the multiple version one export const useSourcify = ( address: ChecksummedAddress | undefined, chainId: number | undefined, @@ -121,7 +122,7 @@ export const useSourcify = ( }; export const useMultipleMetadata = ( - baseMetadatas: Record, + baseMetadatas: Record | undefined, addresses: (ChecksummedAddress | undefined)[], chainId: number | undefined, source: SourcifySource @@ -146,7 +147,9 @@ export const useMultipleMetadata = ( } const results = await Promise.all(promises); - const metadatas: Record = { ...baseMetadatas }; + const metadatas: Record = baseMetadatas + ? { ...baseMetadatas } + : {}; for (let i = 0; i < results.length; i++) { metadatas[dedupedAddresses[i]] = results[i]; } @@ -156,7 +159,7 @@ export const useMultipleMetadata = ( const deduped = new Set( addresses.filter( (a): a is ChecksummedAddress => - a !== undefined && baseMetadatas[a] === undefined + a !== undefined && baseMetadatas?.[a] === undefined ) ); fetchMetadata(Array.from(deduped));