-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathAddress.tsx
More file actions
54 lines (49 loc) · 1.48 KB
/
Address.tsx
File metadata and controls
54 lines (49 loc) · 1.48 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
import toUnicode from 'punycode2/to-unicode';
import { useEffect, useMemo, useState } from 'react';
import { hex_to_ascii } from '../lib/string-utils';
import { getNameFromAddress } from '../lib/names';
export function Address({ addr }: { addr: string }) {
const addressShort = useMemo(
() => `${addr.substring(0, 5)}...${addr.substring(addr.length - 5)}`,
[addr]
);
const [nameOrAddress, setNameOrAddress] = useState(addressShort);
const [nameAscii, setNameAscii] = useState<string>();
const [showAscii, setShowAscii] = useState(false);
useEffect(() => {
const fn = async (addr: string) => {
const data = await getNameFromAddress(addr);
if (data.ok) {
const { name, namespace } = data.ok;
const nameStr = hex_to_ascii(name);
const namePunycodeStr = toUnicode(nameStr);
const namespaceStr = hex_to_ascii(namespace);
setNameAscii(nameStr === namePunycodeStr ? undefined : `${nameStr}.${namespaceStr}`);
setNameOrAddress(`${namePunycodeStr}.${namespaceStr}`);
}
};
fn(addr);
}, [addr]);
return (
<>
{nameAscii ? (
<span
onClick={() => {
setShowAscii(!showAscii);
}}
title={nameAscii}
>
{nameOrAddress} ⓘ
{showAscii && (
<>
<br />
{nameAscii}
</>
)}
</span>
) : (
<span title={nameOrAddress}>{nameOrAddress}</span>
)}
</>
);
}