-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from BootNodeDev/feat/113
feat: add ExplorerLink component and update Hash component
- Loading branch information
Showing
4 changed files
with
49 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |