Skip to content

Commit 30f0b00

Browse files
committed
fix: update CopyAddress to use existing CopyButton component
1 parent 4a1f940 commit 30f0b00

File tree

2 files changed

+10
-107
lines changed

2 files changed

+10
-107
lines changed

apps/developer-hub/src/components/CopyAddress/index.module.scss

Lines changed: 0 additions & 22 deletions
This file was deleted.

apps/developer-hub/src/components/CopyAddress/index.tsx

Lines changed: 10 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,20 @@
11
"use client";
22

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";
74

85
import TruncateToMiddle from "../TruncateToMiddle";
9-
import styles from "./index.module.scss";
106

117
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">
8311
<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>
9318
);
9419
};
9520

0 commit comments

Comments
 (0)