|
1 | 1 | "use client";
|
2 | 2 |
|
3 |
| -import { CheckSquare, Copy } from "@phosphor-icons/react"; |
4 |
| -import { Link } from "@pythnetwork/component-library/unstyled/Link"; |
5 |
| -import { useRef, useState } from "react"; |
6 |
| -import { useClipboard, useKeyboard, usePress } from "react-aria"; |
| 3 | +import { CopyButton } from "@pythnetwork/component-library/CopyButton"; |
7 | 4 |
|
8 | 5 | import TruncateToMiddle from "../TruncateToMiddle";
|
9 |
| -import styles from "./index.module.scss"; |
10 | 6 |
|
11 | 7 | const CopyAddress = ({ address, url }: { address: string; url?: string }) => {
|
12 |
| - const [copied, setCopied] = useState<boolean>(false); |
13 |
| - const timeout = useRef<NodeJS.Timeout | undefined>(undefined); |
14 |
| - |
15 |
| - const showCopied = () => { |
16 |
| - setCopied(true); |
17 |
| - if (timeout.current) clearTimeout(timeout.current); |
18 |
| - timeout.current = setTimeout(() => { |
19 |
| - setCopied(false); |
20 |
| - timeout.current = undefined; |
21 |
| - }, 2000); |
22 |
| - }; |
23 |
| - |
24 |
| - const { clipboardProps } = useClipboard({ |
25 |
| - getItems() { |
26 |
| - showCopied(); |
27 |
| - return [ |
28 |
| - { |
29 |
| - "text/plain": address, |
30 |
| - }, |
31 |
| - ]; |
32 |
| - }, |
33 |
| - }); |
34 |
| - |
35 |
| - const { pressProps } = usePress({ |
36 |
| - onPress: () => { |
37 |
| - void writeClipboardText(address); |
38 |
| - }, |
39 |
| - }); |
40 |
| - |
41 |
| - const { keyboardProps } = useKeyboard({ |
42 |
| - onKeyUp: (e) => { |
43 |
| - if (e.key === "Enter") { |
44 |
| - void writeClipboardText(address); |
45 |
| - } |
46 |
| - }, |
47 |
| - }); |
48 |
| - |
49 |
| - async function writeClipboardText(text: string) { |
50 |
| - try { |
51 |
| - await navigator.clipboard.writeText(text); |
52 |
| - showCopied(); |
53 |
| - } catch { |
54 |
| - // Silent fail - clipboard operations may not be supported in all environments |
55 |
| - } |
56 |
| - } |
57 |
| - |
58 |
| - return ( |
59 |
| - <div |
60 |
| - tabIndex={0} |
61 |
| - onKeyDown={(e) => { |
62 |
| - if (e.key === "Enter" || e.key === " ") { |
63 |
| - e.preventDefault(); |
64 |
| - void writeClipboardText(address); |
65 |
| - } |
66 |
| - }} |
67 |
| - onClick={(e) => { |
68 |
| - e.preventDefault(); |
69 |
| - void writeClipboardText(address); |
70 |
| - }} |
71 |
| - {...clipboardProps} |
72 |
| - {...pressProps} |
73 |
| - {...keyboardProps} |
74 |
| - className={styles.copyAddress} |
75 |
| - role="button" |
76 |
| - aria-label="Copy address to clipboard" |
77 |
| - > |
78 |
| - {url ? ( |
79 |
| - <Link href={url}> |
80 |
| - <TruncateToMiddle text={address} /> |
81 |
| - </Link> |
82 |
| - ) : ( |
| 8 | + return Boolean(url) ? ( |
| 9 | + <form> |
| 10 | + <CopyButton text={address} formAction={url as string} type="submit"> |
83 | 11 | <TruncateToMiddle text={address} />
|
84 |
| - )} |
85 |
| - <div className={styles.icon}> |
86 |
| - {copied ? ( |
87 |
| - <CheckSquare size={24} className={styles.copied} /> |
88 |
| - ) : ( |
89 |
| - <Copy size={24} /> |
90 |
| - )} |
91 |
| - </div> |
92 |
| - </div> |
| 12 | + </CopyButton> |
| 13 | + </form> |
| 14 | + ) : ( |
| 15 | + <CopyButton text={address}> |
| 16 | + <TruncateToMiddle text={address} /> |
| 17 | + </CopyButton> |
93 | 18 | );
|
94 | 19 | };
|
95 | 20 |
|
|
0 commit comments