From ae5db9b482f4f6a0fe0522551010fabdbae2206d Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sat, 2 Aug 2025 14:21:50 -0700 Subject: [PATCH] Fix rolodex contact management. --- src/lib/rolodex.ts | 52 +++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/src/lib/rolodex.ts b/src/lib/rolodex.ts index 06a434d..5f24a09 100644 --- a/src/lib/rolodex.ts +++ b/src/lib/rolodex.ts @@ -53,15 +53,26 @@ export class Rolodex { return true } const data: NamedData = { - [address]: utf8.toBuffer(name) + [address]: { + id: address, + name + } } if (existingName != null) { - const existingAddresses = await this.getAddresses(existingName) - data[existingName] = existingAddresses.filter(a => a !== address).sort() + const addresses = (await this.getAddresses(existingName)) + .filter(a => a !== address) + .sort() + data[existingName] = { + id: existingName, + addresses + } + } + const addresses = await this.getAddresses(name) + addresses.push(account.address) + data[name] = { + id: name, + addresses } - const existingAddresses = await this.getAddresses(name) - existingAddresses.push(account.address) - data[name] = existingAddresses const results = await Database.put(data, this.#DB_NAME) if (results.length !== Object.keys(data).length) { throw new Error('Unexpected results from adding address', { cause: results }) @@ -85,7 +96,10 @@ export class Rolodex { } const addresses = (await this.getAddresses(name)).filter(a => a !== address).sort() const data = { - [name]: addresses + [name]: { + id: name, + addresses + } } const isUpdated = await Database.put(data, this.#DB_NAME) if (!isUpdated) { @@ -102,9 +116,9 @@ export class Rolodex { * @returns {Promise} Promise for true if name and related addresses successfully removed, else false */ static async deleteName (name: string): Promise { - const data = await this.getAddresses(name) - data.push(name) - return await Database.delete(data, this.#DB_NAME) + const records = await this.getAddresses(name) + records.push(name) + return await Database.delete(records, this.#DB_NAME) } /** @@ -115,10 +129,10 @@ export class Rolodex { */ static async getAddresses (name: string): Promise { try { - const response = await Database.get(name, this.#DB_NAME) - const addresses = response[name] - return addresses - ? addresses.sort() + const records = await Database.get>(name, this.#DB_NAME) + const record = records[name] + return record?.addresses + ? record.addresses.sort() : [] } catch (err) { console.error(err) @@ -133,8 +147,8 @@ export class Rolodex { */ static async getAllNames (): Promise { try { - const response = await Database.getAll(this.#DB_NAME) - return Object.keys(response).filter(v => v.slice(0, 5) !== 'nano_') + const records = await Database.getAll(this.#DB_NAME) + return Object.keys(records).filter(v => v.slice(0, 5) !== 'nano_') } catch (err) { console.error(err) return [] @@ -149,9 +163,9 @@ export class Rolodex { */ static async getName (address: string): Promise { try { - const response = await Database.get(address, this.#DB_NAME) - const name = response[address] - return name + const records = await Database.get>(address, this.#DB_NAME) + const record = records[address] + return record?.name ?? null } catch (err) { console.error(err) return null -- 2.47.3