Skip to content

Commit

Permalink
tweaks on add and delete profile
Browse files Browse the repository at this point in the history
  • Loading branch information
diegogurpegui committed Dec 27, 2023
1 parent da125eb commit 6d8466d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
42 changes: 41 additions & 1 deletion src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ export async function readActivePrivateKey(): Promise<string> {
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
});
Expand Down Expand Up @@ -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(
Expand All @@ -164,6 +175,35 @@ export async function updateProfile(

return profiles;
}
export async function deleteProfile(
profilePublicKey: string
): Promise<ProfilesConfig> {
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<ProfileConfig> {
const privateKey = await readActivePrivateKey();
if (privateKey) {
Expand Down

0 comments on commit 6d8466d

Please sign in to comment.