UI data loading finally working! hallelluja

This commit is contained in:
Ryan Breen
2023-01-15 23:01:02 -05:00
parent b84be8c351
commit 3315e2ee3c
3 changed files with 116 additions and 87 deletions

View File

@@ -1,4 +1,4 @@
import { generatePrivateKey , getPublicKey } from "nostr-tools"; import { generatePrivateKey, getPublicKey } from "nostr-tools";
const storage = browser.storage.local; const storage = browser.storage.local;
@@ -9,25 +9,36 @@ browser.runtime.onMessage.addListener(async (message, _sender, sendResponse) =>
case 'init': case 'init':
await initialize(); await initialize();
break; break;
case 'getProfiles':
let names = await getProfileNames();
sendResponse(names);
break;
case 'getProfileIndex':
sendResponse(await getProfileIndex());
break;
case 'setProfileIndex': case 'setProfileIndex':
await setProfileIndex(message.payload); await setProfileIndex(message.payload);
break; break;
case 'getActiveProfile': case 'getProfileIndex':
let ap = await currentProfile(); let profileIndex = await getProfileIndex();
sendResponse(ap); sendResponse(profileIndex);
break; break;
case 'newKey': case 'getPrivKey':
sendResponse(generatePrivateKey()); let privKey = await getPrivKey();
sendResponse(privKey);
break;
case 'getPubKey':
let pubKey = await getPubKey();
sendResponse(pubKey);
break;
case 'getHosts':
let hosts = await getHosts();
sendResponse(hosts);
break;
case 'getName':
let name = await getName();
sendResponse(name);
break;
case 'getProfileNames':
let profileNames = await getProfileNames();
sendResponse(profileNames);
break; break;
case 'newProfile': case 'newProfile':
sendResponse(await newProfile()); let newIndex = await newProfile();
sendResponse(newIndex);
break; break;
default: default:
break; break;
@@ -39,8 +50,8 @@ async function get(item) {
} }
async function getOrSetDefault(key, def) { async function getOrSetDefault(key, def) {
let val = storage.get(key)[key]; let val = (await storage.get(key))[key];
if (!val) { if (val == null || val == undefined) {
await storage.set({[key]: def}); await storage.set({[key]: def});
return def; return def;
} }
@@ -53,32 +64,43 @@ async function initialize() {
await getOrSetDefault('profiles', [{name: 'Default', privKey: generatePrivateKey(), hosts: []}]); await getOrSetDefault('profiles', [{name: 'Default', privKey: generatePrivateKey(), hosts: []}]);
} }
async function getProfileIndex() { async function getPrivKey() {
return (await storage.get('profileIndex')).profileIndex; let profile = await currentProfile();
return profile.privKey;
}
async function getPubKey() {
let privKey = await getPrivKey();
return getPublicKey(privKey);
}
async function getHosts() {
let profile = await currentProfile();
return profile.hosts;
}
async function getName() {
let profile = await currentProfile();
return profile.name;
}
async function getProfileNames() {
let profiles = await get('profiles');
return profiles.map(p => p.name);
} }
async function setProfileIndex(profileIndex) { async function setProfileIndex(profileIndex) {
await storage.set({profileIndex}); await storage.set({profileIndex});
} }
async function currentProfile() { async function getProfileIndex() {
let index = (await storage.get('profileIndex')).profileIndex; return await get('profileIndex');
let profiles = (await storage.get('profiles')).profiles;
if (!profiles || !profiles[index]) {
let newProfile = {name: 'Default', privKey: generatePrivateKey(), hosts: []};
await storage.set({profileIndex: 0});
await storage.set({profiles: [newProfile]});
return newProfile;
}
return profiles[index];
} }
async function getProfileNames() { async function currentProfile() {
let profiles = (await storage.get({profiles: []})).profiles; let index = await get('profileIndex');
console.log('Profiles: ', profiles); let profiles = await get('profiles');
return profiles.map(p => p.name); return profiles[index];
} }
async function newProfile() { async function newProfile() {
@@ -86,5 +108,5 @@ async function newProfile() {
const newProfile = {name: 'New Profile', privKey: generatePrivateKey(), hosts: []}; const newProfile = {name: 'New Profile', privKey: generatePrivateKey(), hosts: []};
profiles.push(newProfile); profiles.push(newProfile);
await storage.set({profiles}); await storage.set({profiles});
return profiles.index - 1; return profiles.length - 1;
} }

View File

@@ -8,8 +8,8 @@
<body x-data="popup"> <body x-data="popup">
<div class="profiles"> <div class="profiles">
<label for="profile">Profile</label> <label for="profile">Profile</label>
<select x-model="profileIndex" name="profile" id="profile" @change="setProfileIndex()"> <select x-model.number="profileIndex" name="profile" id="profile">
<template x-for="(prof, index) in profiles"> <template x-for="(prof, index) in profileNames" :key="index">
<option x-text="prof" :value="index"></option> <option x-text="prof" :value="index"></option>
</template> </template>
</select> </select>
@@ -18,17 +18,18 @@
<div class="profile-name"> <div class="profile-name">
<label for="profile-name">Profile Name</label> <label for="profile-name">Profile Name</label>
<input type="text" id="profile-name" x-model="activeProfile.name"> <input type="text" id="profile-name" x-model="name">
</div> </div>
<div class="key"> <div class="key">
<label for="priv-key">Private Key</label> <label for="priv-key">Private Key</label>
<input id="priv-key" x-model="activeProfile.privKey" :type="visibleKey ? 'text' : 'password'"> <input id="priv-key" x-model="privKey" :type="visibleKey ? 'text' : 'password'">
</div> </div>
<div class="buttons"> <div class="buttons">
<button @click="visibleKey = !visibleKey" x-text="visibleKey ? 'Hide' : 'Show'"></button> <button @click="visibleKey = !visibleKey" x-text="visibleKey ? 'Hide' : 'Show'"></button>
<button @click="saveKey()">Save</button> <button @click="saveKey()">Save</button>
<button @click="profileIndex = 1">Profile Index</button>
</div> </div>
<div x-show="hasValidPubKey"> <div x-show="hasValidPubKey">

View File

@@ -2,78 +2,84 @@ import Alpine from "alpinejs";
window.Alpine = Alpine; window.Alpine = Alpine;
Alpine.data('popup', () => ({ Alpine.data('popup', () => ({
privKey: '',
pubKey: '', pubKey: '',
profiles: [], hosts: [],
name: '',
profileNames: ['Default'],
profileIndex: 0, profileIndex: 0,
activeProfile: {hosts: []},
visibleKey: false, visibleKey: false,
async init() { async init() {
console.log("Initializing backend."); console.log("Initializing backend.");
await browser.runtime.sendMessage({kind: 'init'}); await browser.runtime.sendMessage({kind: 'init'});
console.log('Getting profile list.')
await this.getProfiles(); this.$watch('profileIndex', async () => {
console.log('Getting active profile.'); await this.setProfileIndex();
await this.getActiveProfile(); await this.refreshProfile();
console.log('Getting pub key.'); });
// Even though getProfileIndex will immediately trigger a profile refresh, we still
// need to do an initial profile refresh first. This will pull the latest data from
// the background scripts. Specifically, this pulls the list of profile names,
// otherwise it generates a rendering error where it may not show the correct selected
// profile when first loading the popup.
await this.refreshProfile();
await this.getProfileIndex();
},
async refreshProfile() {
await this.getPrivKey();
await this.getPubKey(); await this.getPubKey();
console.log('profiles: ', this.profiles); await this.getHosts();
}, await this.getName();
await this.getProfileNames();
async saveKey() {},
async setProfileByIndex(index) {
this.profileIndex = index;
await this.resetProfile();
},
async resetProfile() {
await this.setProfileIndex();
await this.getActiveProfile();
await this.getPubKey();
await this.getProfiles();
},
async getActiveProfile() {
this.activeProfile = await browser.runtime.sendMessage({kind: 'getActiveProfile'});
},
async getProfiles() {
this.profiles = await browser.runtime.sendMessage({kind: 'getProfiles'});
this.profileIndex = await browser.runtime.sendMessage({kind: 'getProfileIndex'});
}, },
async setProfileIndex() { async setProfileIndex() {
await browser.runtime.sendMessage({kind: 'setProfileIndex', payload: this.profileIndex}); // Becauset the popup state resets every time it open, we use null as a guard. That way
// whenever the user opens the popup, it doesn't automatically reset the current profile
if (this.profileIndex !== null) {
await browser.runtime.sendMessage({kind: 'setProfileIndex', payload: this.profileIndex});
}
}, },
async deleteSite(index) { async getPrivKey() {
let newSites = [...this.hosts]; this.privKey = await browser.runtime.sendMessage({kind: 'getPrivKey'});
newSites.splice(index, 1);
this.hosts = newSites;
}, },
async getPubKey() { async getPubKey() {
this.pubKey = await browser.runtime.sendMessage({kind: 'getPubKey', payload: this.activeProfile.privKey}); this.pubKey = await browser.runtime.sendMessage({kind: 'getPubKey'});
},
async getHosts() {
this.hosts = await browser.runtime.sendMessage({kind: 'getHosts'});
},
async getProfileNames() {
this.profileNames = await browser.runtime.sendMessage({kind: 'getProfileNames'});
},
async getName() {
this.name = await browser.runtime.sendMessage({kind: 'getName'});
},
async getProfileIndex() {
this.profileIndex = await browser.runtime.sendMessage({kind: 'getProfileIndex'});
}, },
async newProfile() { async newProfile() {
const newIndex = await browser.runtime.sendMessage({kind: 'newProfile'}); let newIndex = await browser.runtime.sendMessage({kind: 'newProfile'});
await this.setProfileByIndex(newIndex); await this.refreshProfile();
this.profileIndex = newIndex;
}, },
// Properties
get hasValidPubKey() { get hasValidPubKey() {
return typeof(this.pubKey) === 'string' && this.pubKey.length > 0; return typeof(this.pubKey) === 'string' && this.pubKey.length > 0;
}, },
get hosts() {
return this.activeProfile.hosts;
},
set hosts(value) {
this.activeProfile.hosts = value;
}
})); }));
Alpine.start(); Alpine.start();