-
Notifications
You must be signed in to change notification settings - Fork 822
/
explorer.ts
60 lines (50 loc) · 2.07 KB
/
explorer.ts
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
// Copyright (c) 2021 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
import * as React from 'react'
import { getLocale } from '../../../common/locale'
import { BraveWallet, BlockExplorerUrlTypes } from '../../constants/types'
import Amount from '../../utils/amount'
export function buildExplorerUrl (
network: BraveWallet.NetworkInfo, type: BlockExplorerUrlTypes,
value?: string, id?: string) {
const explorerURL = network.blockExplorerUrls[0]
if (type === 'contract') {
return id ? `${explorerURL}/${value}?a=${new Amount(id).format()}` : `${explorerURL}/${value}}`
}
const isFileCoinNet =
(network.chainId === BraveWallet.FILECOIN_TESTNET || network.chainId === BraveWallet.FILECOIN_MAINNET)
const isSolanaDevOrTestNet =
(network.chainId === BraveWallet.SOLANA_TESTNET || network.chainId === BraveWallet.SOLANA_DEVNET)
if (isFileCoinNet) {
return `${explorerURL}?cid=${value}`
} else if (isSolanaDevOrTestNet) {
const explorerIndex = explorerURL.lastIndexOf('?')
return `${explorerURL.substring(0, explorerIndex)}/${type}/${value}${explorerURL.substring(explorerIndex)}`
} else {
return `${explorerURL}/${type}/${value}`
}
}
export default function useExplorer (network: BraveWallet.NetworkInfo) {
return React.useCallback(
(type: BlockExplorerUrlTypes, value?: string, id?: string) => () => {
const explorerBaseURL = network.blockExplorerUrls[0]
if (!explorerBaseURL || !value) {
alert(getLocale('braveWalletTransactionExplorerMissing'))
return
}
const url = buildExplorerUrl(network, type, value, id)
if (!chrome.tabs) {
window.open(url, '_blank')
} else {
chrome.tabs.create({ url: url }, () => {
if (chrome.runtime.lastError) {
console.error('tabs.create failed: ' + chrome.runtime.lastError.message)
}
})
}
},
[network]
)
}