forked from solana-foundation/explorer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddress.tsx
More file actions
170 lines (152 loc) · 5.69 KB
/
Address.tsx
File metadata and controls
170 lines (152 loc) · 5.69 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
'use client';
import { useTokenInfo } from '@entities/token-info';
import { Connection, programs } from '@metaplex/js';
import { useCluster } from '@providers/cluster';
import { cn } from '@shared/utils';
import { PublicKey } from '@solana/web3.js';
import { displayAddress, TokenLabelInfo } from '@utils/tx';
import { useClusterPath } from '@utils/url';
import Link from 'next/link';
import React from 'react';
import { useState } from 'react';
import useAsyncEffect from 'use-async-effect';
import { EditIcon, NicknameEditor, useNickname } from '@/app/features/nicknames';
import { useVisibility } from '@/app/shared/lib/visibility';
import { Copyable } from './Copyable';
type Props = {
pubkey: PublicKey;
alignRight?: boolean;
link?: boolean;
raw?: boolean;
truncate?: boolean;
truncateUnknown?: boolean;
truncateChars?: number;
useMetadata?: boolean;
overrideText?: string;
tokenLabelInfo?: TokenLabelInfo;
fetchTokenLabelInfo?: boolean;
'aria-label'?: string;
};
export function Address({
pubkey,
alignRight,
link,
raw,
truncate,
truncateUnknown,
truncateChars,
useMetadata,
overrideText,
tokenLabelInfo,
fetchTokenLabelInfo,
'aria-label': ariaLabel,
}: Props) {
const address = pubkey.toBase58();
const { cluster, clusterInfo } = useCluster();
const addressPath = useClusterPath({ pathname: `/address/${address}` });
const [showNicknameEditor, setShowNicknameEditor] = useState(false);
const nickname = useNickname(address);
const { ref: containerRef, isVisible } = useVisibility(fetchTokenLabelInfo);
const display = displayAddress(address, cluster, tokenLabelInfo);
if (truncateUnknown && address === display) {
truncate = true;
}
let addressLabel = raw ? address : display;
const metaplexData = useTokenMetadata(useMetadata, address);
if (metaplexData && metaplexData.data) {
addressLabel = metaplexData.data.data.name;
}
const shouldFetchTokenInfo = fetchTokenLabelInfo && isVisible;
const tokenInfo = useTokenInfo(shouldFetchTokenInfo, address, cluster, clusterInfo?.genesisHash);
if (tokenInfo) {
addressLabel = displayAddress(address, cluster, tokenInfo);
}
if (truncateChars && addressLabel === address) {
addressLabel = addressLabel.slice(0, truncateChars) + '…';
}
if (overrideText) {
addressLabel = overrideText;
}
// Prepend nickname if exists
const displayText = nickname ? `"${nickname}" (${addressLabel})` : addressLabel;
const handleMouseEnter = (text: string) => {
const elements = document.querySelectorAll(`[data-address="${text}"]`);
elements.forEach(el => {
(el as HTMLElement).classList.add('address-highlight');
});
};
const handleMouseLeave = (text: string) => {
const elements = document.querySelectorAll(`[data-address="${text}"]`);
elements.forEach(el => {
(el as HTMLElement).classList.remove('address-highlight');
});
};
const content = (
<div className="d-flex align-items-center gap-2" aria-label={ariaLabel}>
<Copyable text={address}>
<span
data-address={address}
className="font-monospace"
onMouseEnter={() => handleMouseEnter(address)}
onMouseLeave={() => handleMouseLeave(address)}
title={nickname ? displayText : undefined}
>
{link ? (
<Link
className={truncate || nickname ? 'text-truncate address-truncate' : ''}
href={addressPath}
>
{displayText}
</Link>
) : (
<span className={truncate || nickname ? 'text-truncate address-truncate' : ''}>
{displayText}
</span>
)}
</span>
</Copyable>
<button
className="btn btn-sm btn-link p-0 text-muted"
onClick={() => setShowNicknameEditor(true)}
title="Edit nickname"
style={{ fontSize: '0.875rem', lineHeight: 1 }}
>
<EditIcon />
</button>
{showNicknameEditor && <NicknameEditor address={address} onClose={() => setShowNicknameEditor(false)} />}
</div>
);
return (
<span ref={containerRef}>
<div className={cn('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>
</span>
);
}
const useTokenMetadata = (useMetadata: boolean | undefined, pubkey: string) => {
const [data, setData] = useState<programs.metadata.MetadataData>();
const { url } = useCluster();
useAsyncEffect(
async isMounted => {
if (!useMetadata) return;
if (pubkey && !data) {
try {
const pda = await programs.metadata.Metadata.getPDA(pubkey);
const connection = new Connection(url);
const metadata = await programs.metadata.Metadata.load(connection, pda);
if (isMounted()) {
setData(metadata.data);
}
} catch {
if (isMounted()) {
setData(undefined);
}
}
}
},
[useMetadata, pubkey, url, data, setData],
);
return { data };
};