-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Address.tsx
120 lines (111 loc) · 3 KB
/
Address.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import React from "react";
import { Link } from "react-router-dom";
import { PublicKey } from "@solana/web3.js";
import { clusterPath } from "utils/url";
import { displayAddress } from "utils/tx";
import { useCluster } from "providers/cluster";
import { Copyable } from "./Copyable";
import { useTokenRegistry } from "providers/mints/token-registry";
import { useState, useEffect } from "react";
import { Connection, programs } from "@metaplex/js";
type Props = {
pubkey: PublicKey;
alignRight?: boolean;
link?: boolean;
raw?: boolean;
truncate?: boolean;
truncateUnknown?: boolean;
truncateChars?: number;
useMetadata?: boolean;
overrideText?: string;
};
export function Address({
pubkey,
alignRight,
link,
raw,
truncate,
truncateUnknown,
truncateChars,
useMetadata,
overrideText,
}: Props) {
const address = pubkey.toBase58();
const { tokenRegistry } = useTokenRegistry();
const { cluster } = useCluster();
if (
truncateUnknown &&
address === displayAddress(address, cluster, tokenRegistry)
) {
truncate = true;
}
let addressLabel = raw
? address
: displayAddress(address, cluster, tokenRegistry);
var metaplexData = useTokenMetadata(useMetadata, address);
if (metaplexData && metaplexData.data)
addressLabel = metaplexData.data.data.name;
if (truncateChars && addressLabel === address) {
addressLabel = addressLabel.slice(0, truncateChars) + "…";
}
if (overrideText) {
addressLabel = overrideText;
}
const content = (
<Copyable text={address} replaceText={!alignRight}>
<span className="font-monospace">
{link ? (
<Link
className={truncate ? "text-truncate address-truncate" : ""}
to={clusterPath(`/address/${address}`)}
>
{addressLabel}
</Link>
) : (
<span className={truncate ? "text-truncate address-truncate" : ""}>
{addressLabel}
</span>
)}
</span>
</Copyable>
);
return (
<>
<div
className={`d-none d-lg-flex align-items-center ${
alignRight ? "justify-content-end" : ""
}`}
>
{content}
</div>
<div className="d-flex d-lg-none align-items-center">{content}</div>
</>
);
}
export const useTokenMetadata = (
useMetadata: boolean | undefined,
pubkey: string
) => {
const [data, setData] = useState<programs.metadata.MetadataData>();
var { url } = useCluster();
useEffect(() => {
if (!useMetadata) return;
if (pubkey && !data) {
programs.metadata.Metadata.getPDA(pubkey)
.then((pda) => {
const connection = new Connection(url);
programs.metadata.Metadata.load(connection, pda)
.then((metadata) => {
setData(metadata.data);
})
.catch(() => {
setData(undefined);
});
})
.catch(() => {
setData(undefined);
});
}
}, [useMetadata, pubkey, url, data, setData]);
return { data };
};