Skip to content

Commit 29e2672

Browse files
authored
Merge pull request #13861 from ethereum/refactor-use-clipboard
refactor use clipboard
2 parents c04f073 + c06e776 commit 29e2672

File tree

3 files changed

+20
-33
lines changed

3 files changed

+20
-33
lines changed

src/components/Homepage/useHome.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import { GITHUB_REPO_URL } from "@/lib/constants"
2727
import SimpleDomainRegistryContent from "!!raw-loader!@/data/SimpleDomainRegistry.sol"
2828
import SimpleTokenContent from "!!raw-loader!@/data/SimpleToken.sol"
2929
import SimpleWalletContent from "!!raw-loader!@/data/SimpleWallet.sol"
30-
import { useClipboard, type UseClipboardReturn } from "@/hooks/useClipboard"
3130
import { useRtlFlip } from "@/hooks/useRtlFlip"
3231

3332
export const useHome = () => {
@@ -46,15 +45,14 @@ export const useHome = () => {
4645
setModalOpen(true)
4746
}
4847

49-
const codeExamples: (CodeExample & { clipboard: UseClipboardReturn })[] = [
48+
const codeExamples: CodeExample[] = [
5049
{
5150
title: t("page-index:page-index-developers-code-example-title-0"),
5251
description: t(
5352
"page-index:page-index-developers-code-example-description-0"
5453
),
5554
codeLanguage: "language-solidity",
5655
code: SimpleWalletContent,
57-
clipboard: useClipboard(SimpleWalletContent),
5856
},
5957
{
6058
title: t("page-index:page-index-developers-code-example-title-1"),
@@ -63,7 +61,6 @@ export const useHome = () => {
6361
),
6462
codeLanguage: "language-solidity",
6563
code: SimpleTokenContent,
66-
clipboard: useClipboard(SimpleTokenContent),
6764
},
6865
{
6966
title: t("page-index:page-index-developers-code-example-title-2"),
@@ -72,7 +69,6 @@ export const useHome = () => {
7269
),
7370
codeLanguage: "language-javascript",
7471
code: CreateWalletContent,
75-
clipboard: useClipboard(CreateWalletContent),
7672
},
7773
{
7874
title: t("page-index:page-index-developers-code-example-title-3"),
@@ -81,7 +77,6 @@ export const useHome = () => {
8177
),
8278
codeLanguage: "language-solidity",
8379
code: SimpleDomainRegistryContent,
84-
clipboard: useClipboard(SimpleDomainRegistryContent),
8580
},
8681
]
8782

src/hooks/useClipboard.ts

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,22 @@ export type UseClipboardOptions = {
88
timeout?: number
99
}
1010

11-
export type UseClipboardReturn = {
12-
value: string
13-
setValue: React.Dispatch<React.SetStateAction<string>>
14-
onCopy: () => void
15-
hasCopied: boolean
16-
}
17-
18-
export const useClipboard = (
19-
value: string,
20-
{ timeout }: UseClipboardOptions = {}
21-
): UseClipboardReturn => {
22-
const [copyValue, setCopyValue] = useState(value)
11+
export const useClipboard = ({ timeout = 1500 }: UseClipboardOptions = {}) => {
2312
const [hasCopied, setHasCopied] = useState(false)
2413
const [_, copy] = useCopyToClipboard()
2514

26-
const onCopy = () => {
27-
copy(value)
28-
.then(() => {
29-
setHasCopied(true)
30-
setTimeout(() => {
31-
setHasCopied(false)
32-
}, timeout || 1500)
33-
})
34-
.catch((error) => {
35-
console.error("Failed to copy!", error)
36-
})
15+
const onCopy = async (value: string) => {
16+
try {
17+
await copy(value)
18+
19+
setHasCopied(true)
20+
setTimeout(() => {
21+
setHasCopied(false)
22+
}, timeout)
23+
} catch (error) {
24+
console.error("Failed to copy!", error)
25+
}
3726
}
3827

39-
return { onCopy, hasCopied, value: copyValue, setValue: setCopyValue }
28+
return { onCopy, hasCopied }
4029
}

src/pages/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import {
7373
AccordionTrigger,
7474
} from "../../tailwind/ui/accordion"
7575

76+
import { useClipboard } from "@/hooks/useClipboard"
7677
import { fetchCommunityEvents } from "@/lib/api/calendarEvents"
7778
import { fetchEthPrice } from "@/lib/api/fetchEthPrice"
7879
import { fetchGrowThePie } from "@/lib/api/fetchGrowThePie"
@@ -190,6 +191,8 @@ const HomePage = ({
190191
bentoItems,
191192
} = useHome()
192193

194+
const { onCopy, hasCopied } = useClipboard()
195+
193196
return (
194197
<MainArticle className="flex w-full flex-col items-center" dir={dir}>
195198
<PageMetadata
@@ -411,7 +414,7 @@ const HomePage = ({
411414
{/* Mobile */}
412415
<Accordion type="single" collapsible className="md:hidden">
413416
{codeExamples.map(
414-
({ title, description, code, codeLanguage, clipboard }) => (
417+
({ title, description, code, codeLanguage }) => (
415418
<AccordionItem
416419
key={title}
417420
value={title}
@@ -438,10 +441,10 @@ const HomePage = ({
438441
{code}
439442
</Codeblock>
440443
<Button
441-
onClick={clipboard.onCopy}
444+
onClick={() => onCopy(code)}
442445
className="absolute end-4 top-24"
443446
>
444-
{clipboard.hasCopied ? <MdCheck /> : <IoMdCopy />}
447+
{hasCopied ? <MdCheck /> : <IoMdCopy />}
445448
</Button>
446449
</Suspense>
447450
</AccordionContent>

0 commit comments

Comments
 (0)