From 4c2a38eefc314cd349286dbc715ed22b7077d13b Mon Sep 17 00:00:00 2001 From: Jumaru Date: Tue, 7 Nov 2023 22:45:14 +0200 Subject: [PATCH] Add copy functions --- src/WalletGenerator.js | 71 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 10 deletions(-) diff --git a/src/WalletGenerator.js b/src/WalletGenerator.js index b295396..5dc56c7 100644 --- a/src/WalletGenerator.js +++ b/src/WalletGenerator.js @@ -1,10 +1,11 @@ import React, { useState } from 'react'; import { ethers } from 'ethers'; - +import { ClipboardIcon, CheckIcon } from '@heroicons/react/24/solid'; const WalletGenerator = () => { const [walletInfo, setWalletInfo] = useState(null); + const [copied, setCopied] = useState({ address: false, mnemonic: false, privateKey: false }); const createWallet = () => { const wallet = ethers.Wallet.createRandom(); @@ -36,6 +37,14 @@ const WalletGenerator = () => { document.body.removeChild(link); }; + const copyToClipboard = (text, field) => { + navigator.clipboard.writeText(text); + setCopied({ ...copied, [field]: true }); + setTimeout(() => { + setCopied({ ...copied, [field]: false }); + }, 2000); // Reset the copied state after 2 seconds + }; + return ( <>
@@ -50,22 +59,64 @@ const WalletGenerator = () => { Create Random Wallet
-
- {walletInfo && ( -
-
-

Address: {walletInfo.address}

-

Mnemonic: {walletInfo.mnemonic}

-

Private Key: {walletInfo.privateKey}

+
+ {walletInfo ? ( +
+ {/* Address */} +
+

Address:

+
+

{walletInfo.address}

+
+ +
+ {copied.address &&

Address copied!

} + + {/* Mnemonic */} +
+

Mnemonic:

+
+

{walletInfo.mnemonic}

+
+ +
+ {copied.mnemonic &&

Mnemonic copied!

} + + {/* Private Key */} +
+

Private Key:

+
+

{walletInfo.privateKey}

+
+
+ {copied.privateKey &&

Private key copied!

} + + {/* Download Button */}
- + ) : ( +

No wallet information available.

)}