From 91568080bfe0e19ef997515fb3eb2b93f1cb640e Mon Sep 17 00:00:00 2001 From: Cryptosaka <79313706+Cryptosaka@users.noreply.github.com> Date: Wed, 11 Sep 2024 23:22:56 +0200 Subject: [PATCH 1/3] Added Address book modal to settings --- .../src/components/UI/modals/AddressBook.tsx | 104 ++++++++++++++++++ .../components/UI/sidebar/NetworkSettings.tsx | 20 ++++ packages/renderer/src/tools/utils.ts | 54 +++++++++ 3 files changed, 178 insertions(+) create mode 100644 packages/renderer/src/components/UI/modals/AddressBook.tsx diff --git a/packages/renderer/src/components/UI/modals/AddressBook.tsx b/packages/renderer/src/components/UI/modals/AddressBook.tsx new file mode 100644 index 00000000..a940c989 --- /dev/null +++ b/packages/renderer/src/components/UI/modals/AddressBook.tsx @@ -0,0 +1,104 @@ +import {useEffect, useState} from 'react'; +import Button from '../Button'; +import {getAddresses, removeAddress, saveNewAddress, trimMiddle} from '/@/tools'; +import {toast} from 'react-toastify'; +import Input from '../input/Input'; + +export default function AddressBook() { + const [showAdd, setShowAdd] = useState(false); + const [address, setAddress] = useState(''); + const [alias, setAlias] = useState(''); + const [addressBook, setAddressBook] = useState([]); + + useEffect(() => { + readFromStorage(); + }, []); + + const readFromStorage = () => { + const addresses = getAddresses(); + setAddressBook(addresses); + }; + + const saveAddress = () => { + if (!address) { + return toast.error('Please enter address and alias'); + } + saveNewAddress(address, alias); + setShowAdd(false); + readFromStorage(); + }; + + const deleteAddress = (address: string) => { + removeAddress(address); + readFromStorage(); + }; + + return ( +
+
+
+

Address book

+
+
+
+
+ {addressBook.length > 0 ? ( + addressBook.map(({address, alias}) => ( +
+
+
+

{alias}

+

{trimMiddle(address, 30)}

+
+
+
+
+
+ )) + ) : ( +
+
No addresses
+
+ )} +
+ {showAdd ? ( +
+

Address

+ { + setAddress(e.target.value); + }} + /> +

Alias

+ { + setAlias(e.target.value); + }} + /> +
+ ) : ( +
+ ); +} diff --git a/packages/renderer/src/components/UI/sidebar/NetworkSettings.tsx b/packages/renderer/src/components/UI/sidebar/NetworkSettings.tsx index 085a6978..1cfe4c11 100644 --- a/packages/renderer/src/components/UI/sidebar/NetworkSettings.tsx +++ b/packages/renderer/src/components/UI/sidebar/NetworkSettings.tsx @@ -6,6 +6,7 @@ import Button from '../Button'; import {useNetworkSettingsContext} from '/@/contexts/NetworkContext'; import {useNavigate} from 'react-router-dom'; import BackupWallet from '../modals/BackupWallet'; +import AddressBook from '../modals/AddressBook'; import {INetworkData} from '/@/types'; import {getPassphrase} from '/@/tools'; import {useRecoilState} from 'recoil'; @@ -31,8 +32,10 @@ export default function NetworkSettings({ * Show private key backup modal */ const toggleBackupModal = () => setShowBackupModal(!showBackupModal); + const toggleAddressBookModal = () => setShowAddressBookModal(!showAddressBookModal); const [showBackupModal, setShowBackupModal] = useState(false); + const [showAddressBookModal, setShowAddressBookModal] = useState(false); const [showModal, setShowModal] = useState(false); const [storedPassphrase, setStoredPassphrase] = useState(''); const {settings, saveSettings, availableNetworks} = useNetworkSettingsContext(); @@ -99,6 +102,16 @@ export default function NetworkSettings({
+
+
+ +
+
{!hideBackup && storedPassphrase && (
@@ -158,6 +171,13 @@ export default function NetworkSettings({ + + + ); diff --git a/packages/renderer/src/tools/utils.ts b/packages/renderer/src/tools/utils.ts index eb87a0ae..6be0b41e 100644 --- a/packages/renderer/src/tools/utils.ts +++ b/packages/renderer/src/tools/utils.ts @@ -349,3 +349,57 @@ export async function signTransaction(privateKey: string, params: unknown) { } return signResult; } + +export const saveNewAddress = (address: string, alias: string) => { + try { + const addresses = localStorage.getItem('addresses'); + if (addresses) { + const addressesArray = JSON.parse(addresses); + const existingAddress = addressesArray.find((item: any) => item.address === address); + if (existingAddress) { + existingAddress.alias = alias; + } else { + addressesArray.push({address, alias}); + } + localStorage.setItem('addresses', JSON.stringify(addressesArray)); + } else { + localStorage.setItem('addresses', JSON.stringify([{address, alias}])); + } + } catch (error) { + console.error('Error saving new address:', error); + } +}; + +export const getAddresses = () => { + try { + const addresses = localStorage.getItem('addresses'); + if (addresses) { + return JSON.parse(addresses); + } + return []; + } catch (error) { + console.error('Error getting addresses:', error); + return []; + } +}; + +export const removeAddress = (address: string) => { + try { + const addresses = localStorage.getItem('addresses'); + if (addresses) { + const addressesArray = JSON.parse(addresses); + const newAddresses = addressesArray.filter((item: any) => item.address !== address); + localStorage.setItem('addresses', JSON.stringify(newAddresses)); + } + } catch (error) { + console.error('Error removing address:', error); + } +}; + +export const clearAllAddresses = () => { + try { + localStorage.removeItem('addresses'); + } catch (error) { + console.error('Error clearing addresses:', error); + } +}; From def8092b125676c074985a48ec2acd02779de5c4 Mon Sep 17 00:00:00 2001 From: Cryptosaka <79313706+Cryptosaka@users.noreply.github.com> Date: Thu, 12 Sep 2024 18:40:59 -0800 Subject: [PATCH 2/3] Added address book to the transaction form --- .../src/components/UI/modals/AddressBook.tsx | 86 ++++++++++++------- .../components/UI/sidebar/NetworkSettings.tsx | 2 +- .../forms/transactionForm/TransactionForm.tsx | 27 +++++- 3 files changed, 80 insertions(+), 35 deletions(-) diff --git a/packages/renderer/src/components/UI/modals/AddressBook.tsx b/packages/renderer/src/components/UI/modals/AddressBook.tsx index a940c989..11bdb845 100644 --- a/packages/renderer/src/components/UI/modals/AddressBook.tsx +++ b/packages/renderer/src/components/UI/modals/AddressBook.tsx @@ -4,7 +4,15 @@ import {getAddresses, removeAddress, saveNewAddress, trimMiddle} from '/@/tools' import {toast} from 'react-toastify'; import Input from '../input/Input'; -export default function AddressBook() { +export default function AddressBook({ + editable, + selectable, + onSelect, +}: { + editable?: boolean; + selectable?: boolean; + onSelect?: (address: string) => void; +}) { const [showAdd, setShowAdd] = useState(false); const [address, setAddress] = useState(''); const [alias, setAlias] = useState(''); @@ -53,12 +61,22 @@ export default function AddressBook() {

{alias}

{trimMiddle(address, 30)}

-
+ {selectable && (
+ )} + {editable && ( +
+
+ )}
)) @@ -68,37 +86,39 @@ export default function AddressBook() { )} - {showAdd ? ( -
-

Address

- { - setAddress(e.target.value); - }} - /> -

Alias

- { - setAlias(e.target.value); - }} - /> + {editable ? ( + showAdd ? ( +
+

Address

+ { + setAddress(e.target.value); + }} + /> +

Alias

+ { + setAlias(e.target.value); + }} + /> +
+ ) : (
- ) : ( -