Skip to content

Commit

Permalink
Merge pull request #138 from BootNodeDev/feat/113
Browse files Browse the repository at this point in the history
feat: add ExplorerLink component and update Hash component
  • Loading branch information
gabitoesmiapodo authored Jun 26, 2024
2 parents a758bf8 + af68b19 commit a1c147a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/pageComponents/home/Examples/demos/Hash.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { Address } from 'viem'
import { Address, Chain } from 'viem'

import BaseHash from '@/src/sharedComponents/Hash'
import { getExplorerLink } from '@/src/utils/getExplorerLink'

interface Props {
hash: Address
chain: Chain
}

const Hash = ({ hash }: Props) => {
const Hash = ({ chain, hash }: Props) => {
return (
<BaseHash
explorerURL={`https://etherscan.io/address/${hash}`}
explorerURL={getExplorerLink({ chain, hashOrAddress: hash })}
hash={hash}
onCopy={() => console.log('Copied!')}
showCopyButton
Expand Down
3 changes: 2 additions & 1 deletion src/pageComponents/home/Examples/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'
import styled from 'styled-components'

import { InnerContainer as Inner, ContainerPadding } from 'db-ui-toolkit'
import { mainnet } from 'viem/chains'
import { useAccount } from 'wagmi'

import { Props as ItemProps } from '@/src/pageComponents/home/Examples/Item'
Expand Down Expand Up @@ -75,7 +76,7 @@ const Examples: React.FC = ({ ...restProps }) => {
title: 'Token input',
},
{
demo: <Hash hash={address} />,
demo: <Hash chain={mainnet} hash={address} />,
href: '#',
icon: <ImgHash />,
text: 'Copy, open in explorer',
Expand Down
13 changes: 13 additions & 0 deletions src/sharedComponents/ExplorerLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { GetExplorerUrlParams, getExplorerLink } from '@/src/utils/getExplorerLink'

interface ExplorerLinkProps extends GetExplorerUrlParams {
text?: string
}

export const ExplorerLink = ({ text = 'View on explorer', ...props }: ExplorerLinkProps) => {
return (
<a href={getExplorerLink(props)} rel="noopener noreferrer" target="_blank">
{text}
</a>
)
}
29 changes: 29 additions & 0 deletions src/utils/getExplorerLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Address, Chain, Hash, isAddress, isHash } from 'viem'

export type GetExplorerUrlParams = {
chain: Chain
hashOrAddress: Hash | Address
explorerUrl?: string
}

/**
* Returns the explorer link for a given chain, explorer URL, and hash or address.
* You can provide an explorer URL to override the default explorer URL for the chain.
* @param {GetExplorerUrlParams} params - The parameters for generating the explorer link.
* @returns {string} - The explorer link.
* @throws {Error} - If the hash or address is invalid.
*/
export const getExplorerLink = ({ chain, explorerUrl, hashOrAddress }: GetExplorerUrlParams) => {
if (isAddress(hashOrAddress)) {
return explorerUrl
? `${explorerUrl}/address/${hashOrAddress}`
: `${chain.blockExplorers?.default.url}/address/${hashOrAddress}`
}
if (isHash(hashOrAddress)) {
return explorerUrl
? `${explorerUrl}/tx/${hashOrAddress}`
: `${chain.blockExplorers?.default}/tx/${hashOrAddress}`
}

throw new Error('Invalid hash or address')
}

0 comments on commit a1c147a

Please sign in to comment.