|
| 1 | +import { StatusBarAlignment, StatusBarItem, Uri, window } from "vscode"; |
| 2 | + |
| 3 | +import { apiUrlToWebsite } from "./explorer/apiUrlToWebsite"; |
| 4 | +import { ApiName, explorerApiUrls } from "./explorer/networks"; |
| 5 | + |
| 6 | +export function renderStatusBarItems( |
| 7 | + args: |
| 8 | + | { |
| 9 | + contractAddress: string; |
| 10 | + contractName: string; |
| 11 | + apiName: ApiName; |
| 12 | + } |
| 13 | + | {} |
| 14 | +) { |
| 15 | + if ("contractAddress" in args) { |
| 16 | + const { apiName, contractAddress, contractName } = args; |
| 17 | + const website = apiUrlToWebsite(explorerApiUrls[apiName]); |
| 18 | + const link = `${website}/address/${contractAddress}`; |
| 19 | + |
| 20 | + const where = apiName.endsWith("etherscan") |
| 21 | + ? "on Etherscan" |
| 22 | + : "in the explorer"; |
| 23 | + |
| 24 | + const tooltip = `Open ${link}`; |
| 25 | + renderStatusBarItem({ |
| 26 | + key: "ethereum-viewer.etherscan-link", |
| 27 | + text: `$(eye) See ${contractName} ${where} (${contractAddress})`, |
| 28 | + tooltip, |
| 29 | + command: { |
| 30 | + title: "Open on Etherscan", |
| 31 | + command: "vscode.open", |
| 32 | + arguments: [Uri.parse(link)], |
| 33 | + }, |
| 34 | + priority: 0, |
| 35 | + }); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +const _renderedItems = new Map<string, StatusBarItem>(); |
| 40 | + |
| 41 | +interface RenderStatusBarItemArgs |
| 42 | + extends Pick<StatusBarItem, "text" | "tooltip" | "command">, |
| 43 | + Pick<window.StatusBarItemOptions, "alignment" | "priority"> { |
| 44 | + priority?: number; |
| 45 | + key: string; |
| 46 | +} |
| 47 | + |
| 48 | +function renderStatusBarItem(args: RenderStatusBarItemArgs) { |
| 49 | + let item = _renderedItems.get(args.key)!; |
| 50 | + |
| 51 | + item ||= window.createStatusBarItem( |
| 52 | + args.alignment || StatusBarAlignment.Left, |
| 53 | + args.priority |
| 54 | + ); |
| 55 | + |
| 56 | + item.tooltip = args.tooltip; |
| 57 | + item.text = args.text; |
| 58 | + item.command = args.command; |
| 59 | + |
| 60 | + item.show(); |
| 61 | + |
| 62 | + _renderedItems.set(args.key, item); |
| 63 | + |
| 64 | + return item; |
| 65 | +} |
0 commit comments