|
| 1 | +import React, { useState, useRef } from 'react'; |
| 2 | +import { useAddressBookStore } from '@/stores/useAddressBookStore'; |
| 3 | + |
| 4 | +function truncateAddress(addr) { |
| 5 | + if (addr.length <= 16) return addr; |
| 6 | + return `${addr.slice(0, 8)}...${addr.slice(-6)}`; |
| 7 | +} |
| 8 | + |
| 9 | +function AddressBook() { |
| 10 | + const { addresses, addAddress, removeAddress, exportAddresses, importAddresses } = |
| 11 | + useAddressBookStore(); |
| 12 | + const [name, setName] = useState(''); |
| 13 | + const [address, setAddress] = useState(''); |
| 14 | + const [importMessage, setImportMessage] = useState(null); |
| 15 | + const fileInputRef = useRef(null); |
| 16 | + |
| 17 | + const handleAdd = (e) => { |
| 18 | + e.preventDefault(); |
| 19 | + if (!name.trim() || !address.trim()) return; |
| 20 | + const success = addAddress(name, address); |
| 21 | + if (!success) { |
| 22 | + setImportMessage({ success: false, message: 'Address already exists' }); |
| 23 | + setTimeout(() => setImportMessage(null), 3000); |
| 24 | + return; |
| 25 | + } |
| 26 | + setName(''); |
| 27 | + setAddress(''); |
| 28 | + }; |
| 29 | + |
| 30 | + const handleExport = () => { |
| 31 | + const json = exportAddresses(); |
| 32 | + const blob = new Blob([json], { type: 'application/json' }); |
| 33 | + const url = URL.createObjectURL(blob); |
| 34 | + const a = document.createElement('a'); |
| 35 | + a.href = url; |
| 36 | + a.download = 'quantara-address-book.json'; |
| 37 | + a.click(); |
| 38 | + URL.revokeObjectURL(url); |
| 39 | + }; |
| 40 | + |
| 41 | + const handleImport = (e) => { |
| 42 | + const file = e.target.files?.[0]; |
| 43 | + if (!file) return; |
| 44 | + const reader = new FileReader(); |
| 45 | + reader.onload = (event) => { |
| 46 | + const result = importAddresses(event.target.result); |
| 47 | + setImportMessage(result); |
| 48 | + setTimeout(() => setImportMessage(null), 4000); |
| 49 | + }; |
| 50 | + reader.readAsText(file); |
| 51 | + e.target.value = ''; |
| 52 | + }; |
| 53 | + |
| 54 | + const handleCopy = (addr) => { |
| 55 | + navigator.clipboard.writeText(addr); |
| 56 | + }; |
| 57 | + |
| 58 | + return ( |
| 59 | + <div className="flex flex-col gap-6 w-full"> |
| 60 | + <form onSubmit={handleAdd} className="flex flex-col gap-3"> |
| 61 | + <input |
| 62 | + type="text" |
| 63 | + placeholder="Name (e.g. My Wallet)" |
| 64 | + value={name} |
| 65 | + onChange={(e) => setName(e.target.value)} |
| 66 | + className="w-full rounded-lg border border-[#300734] bg-transparent px-4 py-3 text-sm text-white placeholder-gray outline-none focus:border-[#a855f7] transition-colors" |
| 67 | + /> |
| 68 | + <input |
| 69 | + type="text" |
| 70 | + placeholder="Wallet address (0x...)" |
| 71 | + value={address} |
| 72 | + onChange={(e) => setAddress(e.target.value)} |
| 73 | + className="w-full rounded-lg border border-[#300734] bg-transparent px-4 py-3 text-sm text-white placeholder-gray outline-none focus:border-[#a855f7] transition-colors" |
| 74 | + /> |
| 75 | + <button |
| 76 | + type="submit" |
| 77 | + disabled={!name.trim() || !address.trim()} |
| 78 | + className="w-full rounded-lg border border-[#a855f7] bg-transparent py-3 text-sm font-semibold text-white transition-colors hover:bg-[#a855f7]/20 disabled:opacity-40 disabled:cursor-not-allowed cursor-pointer" |
| 79 | + > |
| 80 | + Add Address |
| 81 | + </button> |
| 82 | + </form> |
| 83 | + |
| 84 | + <div className="flex gap-2"> |
| 85 | + <button |
| 86 | + onClick={handleExport} |
| 87 | + disabled={addresses.length === 0} |
| 88 | + className="flex-1 rounded-lg border border-light-purple bg-transparent py-2.5 text-xs font-semibold text-white transition-colors hover:border-[#a855f7] disabled:opacity-40 disabled:cursor-not-allowed cursor-pointer" |
| 89 | + > |
| 90 | + Export JSON |
| 91 | + </button> |
| 92 | + <button |
| 93 | + onClick={() => fileInputRef.current?.click()} |
| 94 | + className="flex-1 rounded-lg border border-light-purple bg-transparent py-2.5 text-xs font-semibold text-white transition-colors hover:border-[#a855f7] cursor-pointer" |
| 95 | + > |
| 96 | + Import JSON |
| 97 | + </button> |
| 98 | + <input |
| 99 | + ref={fileInputRef} |
| 100 | + type="file" |
| 101 | + accept=".json" |
| 102 | + onChange={handleImport} |
| 103 | + className="hidden" |
| 104 | + /> |
| 105 | + </div> |
| 106 | + |
| 107 | + {importMessage && ( |
| 108 | + <div |
| 109 | + className={`rounded-lg px-4 py-2.5 text-xs ${ |
| 110 | + importMessage.success |
| 111 | + ? 'border border-green-500/40 text-green-400' |
| 112 | + : 'border border-red-500/40 text-red-400' |
| 113 | + }`} |
| 114 | + > |
| 115 | + {importMessage.message} |
| 116 | + </div> |
| 117 | + )} |
| 118 | + |
| 119 | + {addresses.length === 0 ? ( |
| 120 | + <p className="text-center text-sm text-gray py-8"> |
| 121 | + No saved addresses yet. Add one above or import from a JSON file. |
| 122 | + </p> |
| 123 | + ) : ( |
| 124 | + <div className="flex flex-col gap-2"> |
| 125 | + {addresses.map((entry) => ( |
| 126 | + <div |
| 127 | + key={entry.id} |
| 128 | + className="flex items-center justify-between rounded-lg border border-[#300734] bg-transparent px-4 py-3" |
| 129 | + > |
| 130 | + <div className="flex flex-col gap-0.5 min-w-0"> |
| 131 | + <span className="text-sm font-semibold text-white truncate"> |
| 132 | + {entry.name} |
| 133 | + </span> |
| 134 | + <button |
| 135 | + onClick={() => handleCopy(entry.address)} |
| 136 | + className="text-xs text-gray hover:text-[#a855f7] transition-colors cursor-pointer text-left truncate max-w-[260px]" |
| 137 | + title="Click to copy" |
| 138 | + > |
| 139 | + {truncateAddress(entry.address)} |
| 140 | + </button> |
| 141 | + </div> |
| 142 | + <button |
| 143 | + onClick={() => removeAddress(entry.id)} |
| 144 | + className="ml-3 shrink-0 text-xs text-red-400 hover:text-red-300 transition-colors cursor-pointer px-2 py-1" |
| 145 | + > |
| 146 | + Delete |
| 147 | + </button> |
| 148 | + </div> |
| 149 | + ))} |
| 150 | + </div> |
| 151 | + )} |
| 152 | + </div> |
| 153 | + ); |
| 154 | +} |
| 155 | + |
| 156 | +export default AddressBook; |
0 commit comments