Relays are partially working now in the options options script instead of background.

This commit is contained in:
Ryan Breen
2023-01-27 23:24:29 -05:00
parent f0c5cd29dc
commit 97f706e180
3 changed files with 44 additions and 22 deletions

View File

@@ -71,10 +71,10 @@
<tr> <tr>
<td class="p-2 w-1/3" x-text="relay.url"></td> <td class="p-2 w-1/3" x-text="relay.url"></td>
<td class="p-2 text-center"> <td class="p-2 text-center">
<input class="checkbox" type="checkbox" x-model="relay.read" @change="await saveRelaysForProfile(index)"> <input class="checkbox" type="checkbox" x-model="relay.read" @change="saveRelays">
</td> </td>
<td class="p-2 text-center"> <td class="p-2 text-center">
<input class="checkbox" type="checkbox" x-model="relay.write" @change="await saveRelaysForProfile(index)"> <input class="checkbox" type="checkbox" x-model="relay.write" @change="saveRelays">
</td> </td>
<td class="p-2 text-center"> <td class="p-2 text-center">
<button class="button" @click="await deleteRelay(index)">Delete</button> <button class="button" @click="await deleteRelay(index)">Delete</button>

View File

@@ -4,9 +4,12 @@ import {
deleteProfile, deleteProfile,
getProfileIndex, getProfileIndex,
getProfileNames, getProfileNames,
getRelays,
initialize, initialize,
newProfile, newProfile,
savePrivateKey,
saveProfileName, saveProfileName,
saveRelays,
} from './utils'; } from './utils';
const RECOMMENDED_RELAYS = [ const RECOMMENDED_RELAYS = [
@@ -49,6 +52,8 @@ Alpine.data('options', () => ({
}); });
} }
// We need to refresh the names BEFORE setting the profile index, or it won't work
// on init to set the correct profile.
await this.getProfileNames(); await this.getProfileNames();
await this.getProfileIndex(); await this.getProfileIndex();
await this.refreshProfile(); await this.refreshProfile();
@@ -59,6 +64,7 @@ Alpine.data('options', () => ({
await this.getProfileName(); await this.getProfileName();
await this.getNsec(); await this.getNsec();
await this.getNpub(); await this.getNpub();
await this.getRelays();
this.confirmClear = false; this.confirmClear = false;
this.confirmDelete = false; this.confirmDelete = false;
}, },
@@ -96,10 +102,7 @@ Alpine.data('options', () => ({
async saveProfile() { async saveProfile() {
if (!this.needsSave) return; if (!this.needsSave) return;
await browser.runtime.sendMessage({ await savePrivateKey(this.profileIndex, this.privKey);
kind: 'savePrivateKey',
payload: [this.profileIndex, this.privKey],
});
await saveProfileName(this.profileIndex, this.profileName); await saveProfileName(this.profileIndex, this.profileName);
await this.getProfileNames(); await this.getProfileNames();
await this.refreshProfile(); await this.refreshProfile();
@@ -122,19 +125,14 @@ Alpine.data('options', () => ({
// Relay functions // Relay functions
async getRelaysForProfile() { async getRelays() {
this.relays = await browser.runtime.sendMessage({ this.relays = await getRelays(this.profileIndex);
kind: 'getRelaysForProfile',
payload: this.profileIndex,
});
}, },
async saveRelaysForProfile() { async saveRelays() {
await browser.runtime.sendMessage({ console.log(this.relays);
kind: 'saveRelaysForProfile', await saveRelays(this.profileIndex, this.relays);
payload: [this.profileIndex, this.relays], await this.getRelays(this.profileIndex);
});
await this.getRelaysForProfile();
this.newRelay = ''; this.newRelay = '';
}, },
@@ -150,8 +148,11 @@ Alpine.data('options', () => ({
this.setUrlError('URL already exists'); this.setUrlError('URL already exists');
return; return;
} }
this.relays.push({ url: url.href, read: true, write: true }); this.relays = [
await this.saveRelaysForProfile(); ...this.relays,
{ url: url.href, read: true, write: true },
];
await this.saveRelays();
} catch (error) { } catch (error) {
this.setUrlError('Invalid websocket URL'); this.setUrlError('Invalid websocket URL');
} }
@@ -159,7 +160,7 @@ Alpine.data('options', () => ({
async deleteRelay(index) { async deleteRelay(index) {
this.relays.splice(index, 1); this.relays.splice(index, 1);
await this.saveRelaysForProfile(); await this.saveRelays();
}, },
setUrlError(message) { setUrlError(message) {
@@ -169,6 +170,8 @@ Alpine.data('options', () => ({
}, 3000); }, 3000);
}, },
// General
async clearData() { async clearData() {
await clearData(); await clearData();
await this.init(false); await this.init(false);

View File

@@ -61,9 +61,9 @@ export async function initialize() {
await getOrSetDefault('profiles', [await generateProfile()]); await getOrSetDefault('profiles', [await generateProfile()]);
} }
export async function generateProfile() { export async function generateProfile(name = 'Default') {
return { return {
name: 'Default', name,
privKey: await generatePrivateKey(), privKey: await generatePrivateKey(),
hosts: [], hosts: [],
relays: [], relays: [],
@@ -86,6 +86,13 @@ export async function saveProfileName(index, profileName) {
await storage.set({ profiles }); await storage.set({ profiles });
} }
export async function savePrivateKey(index, privateKey) {
await browser.runtime.sendMessage({
kind: 'savePrivateKey',
payload: [index, privateKey],
});
}
export async function newProfile() { export async function newProfile() {
let profiles = await getProfiles(); let profiles = await getProfiles();
const newProfile = await generateProfile('New Profile'); const newProfile = await generateProfile('New Profile');
@@ -93,3 +100,15 @@ export async function newProfile() {
await storage.set({ profiles }); await storage.set({ profiles });
return profiles.length - 1; return profiles.length - 1;
} }
export async function getRelays(profileIndex) {
let profile = await getProfile(profileIndex);
return profile.relays || [];
}
export async function saveRelays(profileIndex, relays) {
let profiles = await getProfile(profileIndex);
console.log('saving: ', relays);
profile.relays = [...relays];
await storage.set({ profiles });
}