diff --git a/src/options.tsx b/src/options.tsx index b7df781..800631f 100644 --- a/src/options.tsx +++ b/src/options.tsx @@ -235,11 +235,13 @@ function Options() { `Delete the profile "${nip19.npubEncode(selectedProfilePubKey)}"?` ) ) { + // delete from storage + await Storage.deleteProfile(selectedProfilePubKey); + // now update component const updateProfiles = profiles; delete updateProfiles[selectedProfilePubKey]; console.debug('updated profiles', updateProfiles); setProfiles(updateProfiles); - await saveProfiles(); } } diff --git a/src/storage.ts b/src/storage.ts index f8fd8c1..88ca448 100644 --- a/src/storage.ts +++ b/src/storage.ts @@ -15,7 +15,12 @@ export async function readActivePrivateKey(): Promise { return data[ConfigurationKeys.PRIVATE_KEY]; } export async function updateActivePrivateKey(privateKey: string) { - console.log('Storing new active pubKey:', getPublicKey(privateKey)); + if (privateKey == null || privateKey == '') { + console.log('Removing active profile (private key)'); + } else { + console.log('Storing new active pubKey:', getPublicKey(privateKey)); + } + return browser.storage.local.set({ [ConfigurationKeys.PRIVATE_KEY]: privateKey }); @@ -150,6 +155,12 @@ export async function addProfile( [ConfigurationKeys.PROFILES]: profiles }); + // if it's the first profile to be added, then set it as the active one + const activePrivateKey = await readActivePrivateKey(); + if (!activePrivateKey && Object.keys(profiles).length == 1) { + await updateActivePrivateKey(profile.privateKey); + } + return profiles; } export async function updateProfile( @@ -164,6 +175,35 @@ export async function updateProfile( return profiles; } +export async function deleteProfile( + profilePublicKey: string +): Promise { + console.debug(`Deleting profile: ${profilePublicKey}...`); + const profiles = await readProfiles(); + + // get the profile and private key for later checks + const profileToBeDeleted = profiles[profilePublicKey]; + const privateKeyToBeDeleted = profileToBeDeleted.privateKey; + + // delete from storage + delete profiles[profilePublicKey]; + await browser.storage.local.set({ + [ConfigurationKeys.PROFILES]: profiles + }); + + // now change the active, if it was removed + let activePrivateKey = await readActivePrivateKey(); + if (activePrivateKey == privateKeyToBeDeleted) { + if (Object.keys(profiles).length > 0) { + activePrivateKey = Object.entries(profiles)[0][1].privateKey; + } else { + activePrivateKey = ''; + } + await updateActivePrivateKey(activePrivateKey); + } + + return profiles; +} export async function getActiveProfile(): Promise { const privateKey = await readActivePrivateKey(); if (privateKey) {