This repository was archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBlockExplorerLink.tsx
69 lines (61 loc) · 1.81 KB
/
BlockExplorerLink.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import React, { ReactElement } from 'react'
import { Network } from 'types'
import { ExternalLink } from 'components/analytics/ExternalLink'
import LogoWrapper, { LOGO_MAP } from 'components/common/LogoWrapper'
import { abbreviateString } from 'utils'
import { BlockExplorerLinkType, getExplorerUrl } from 'utils/getExplorerUrl'
export interface Props {
/**
* type of BlockExplorerLink
*/
type: BlockExplorerLinkType
/**
* address or transaction or other hash
*/
identifier: string
/**
* network number | chain id
*/
networkId?: number
/**
* label to replace textContent generated from identifier
*/
label?: string | ReactElement | void
/**
* Use the URL as a label
*/
useUrlAsLabel?: boolean
/**
* className to pass on to <a/>
*/
className?: string // to allow subclassing styles with styled-components
/**
* to show explorer logo
*/
showLogo?: boolean
}
/**
* Dumb BlockExplorerLink, a pure UI component
*
* Does not make any assumptions regarding the network.
* Expects all data as input. Does not use any hooks internally.
*/
export const BlockExplorerLink: React.FC<Props> = (props: Props) => {
const { type, identifier, label: labelProp, useUrlAsLabel = false, className, networkId, showLogo = false } = props
if (!networkId || !identifier) {
return null
}
const url = getExplorerUrl(networkId, type, identifier)
const label = labelProp || (useUrlAsLabel && url) || abbreviateString(identifier, 6, 4)
return (
<ExternalLink href={url} target="_blank" rel="noopener noreferrer" className={className}>
<span>{label}</span>
{showLogo && (
<LogoWrapper
title={`Open it on ${networkId === Network.GNOSIS_CHAIN ? 'Gnosisscan' : 'Etherscan'}`}
src={LOGO_MAP.etherscan}
/>
)}
</ExternalLink>
)
}