Added ability to run add recommended relays from popup.

This commit is contained in:
Ryan Breen
2023-01-28 20:46:49 -05:00
parent 6a31c3b135
commit b6a4a5b5dc
5 changed files with 51 additions and 58 deletions

View File

@@ -3,15 +3,19 @@ import {
getProfileNames,
setProfileIndex,
getProfileIndex,
getRelays,
RECOMMENDED_RELAYS,
saveRelays,
} from './utils';
import Alpine from 'alpinejs';
window.Alpine = Alpine;
const log = msg => bglog(msg, 'popup');
const log = console.log;
Alpine.data('popup', () => ({
profileNames: ['Default'],
profileIndex: 0,
relayCount: 0,
async init() {
log('Initializing backend.');
@@ -20,6 +24,7 @@ Alpine.data('popup', () => ({
this.$watch('profileIndex', async () => {
await this.loadNames();
await this.setProfileIndex();
await this.countRelays();
});
// Even though loadProfileIndex will immediately trigger a profile refresh, we still
@@ -29,6 +34,7 @@ Alpine.data('popup', () => ({
// profile when first loading the popup.
await this.loadNames();
await this.loadProfileIndex();
await this.countRelays();
},
async setProfileIndex() {
@@ -51,6 +57,21 @@ Alpine.data('popup', () => ({
await browser.runtime.openOptionsPage();
window.close();
},
async countRelays() {
let relays = await getRelays(this.profileIndex);
this.relayCount = relays.length;
},
async addRelays() {
let relays = RECOMMENDED_RELAYS.map(r => ({
url: r.href,
read: true,
write: true,
}));
await saveRelays(this.profileIndex, relays);
await this.countRelays();
},
}));
Alpine.start();