tweaking the UI, not working quite yet. may totally revise

This commit is contained in:
Ryan Breen
2023-01-15 21:48:00 -05:00
parent abc84d4694
commit b84be8c351
3 changed files with 108 additions and 42 deletions

View File

@@ -1,38 +1,90 @@
import { generatePrivateKey, getPublicKey } from "nostr-tools"; import { generatePrivateKey , getPublicKey } from "nostr-tools";
const storage = browser.storage.local; const storage = browser.storage.local;
let profiles = [
{name: 'Default', privKey: generatePrivateKey(), hosts: [
{host: 'yosup.app', allowed: true},
{host: 'iris.to', allowed: false},
]},
{name: 'Extra', privKey: generatePrivateKey(), hosts: []},
];
let profileIndex = 0;
browser.runtime.onMessage.addListener(async (message, _sender, sendResponse) => { browser.runtime.onMessage.addListener(async (message, _sender, sendResponse) => {
console.log(message); console.log(message);
if (message.kind === 'getPubKey') {
const privKey = getPublicKey(message.payload); switch (message.kind) {
sendResponse(privKey); case 'init':
} else if (message.kind === 'newKey') { await initialize();
const privKey = generatePrivateKey(); break;
sendResponse(privKey); case 'getProfiles':
} else if (message.kind === 'getProfiles') { let names = await getProfileNames();
sendResponse(profiles); sendResponse(names);
} else if (message.kind === 'getProfileIndex') { break;
sendResponse(await getProfileIndex()); case 'getProfileIndex':
} else if (message.kind === 'setProfileIndex') { sendResponse(await getProfileIndex());
await setProfileIndex(message.payload); break;
case 'setProfileIndex':
await setProfileIndex(message.payload);
break;
case 'getActiveProfile':
let ap = await currentProfile();
sendResponse(ap);
break;
case 'newKey':
sendResponse(generatePrivateKey());
break;
case 'newProfile':
sendResponse(await newProfile());
break;
default:
break;
} }
}); });
async function get(item) {
return (await storage.get(item))[item];
}
async function getOrSetDefault(key, def) {
let val = storage.get(key)[key];
if (!val) {
await storage.set({[key]: def});
return def;
}
return val;
}
async function initialize() {
await getOrSetDefault('profileIndex', 0);
await getOrSetDefault('profiles', [{name: 'Default', privKey: generatePrivateKey(), hosts: []}]);
}
async function getProfileIndex() { async function getProfileIndex() {
return await storage.get('profileIndex').profileIndex; return (await storage.get('profileIndex')).profileIndex;
} }
async function setProfileIndex(profileIndex) { async function setProfileIndex(profileIndex) {
await storage.set({profileIndex}); await storage.set({profileIndex});
}
async function currentProfile() {
let index = (await storage.get('profileIndex')).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() {
let profiles = (await storage.get({profiles: []})).profiles;
console.log('Profiles: ', profiles);
return profiles.map(p => p.name);
}
async function newProfile() {
let profiles = await get('profiles');
const newProfile = {name: 'New Profile', privKey: generatePrivateKey(), hosts: []};
profiles.push(newProfile);
await storage.set({profiles});
return profiles.index - 1;
} }

View File

@@ -10,7 +10,7 @@
<label for="profile">Profile</label> <label for="profile">Profile</label>
<select x-model="profileIndex" name="profile" id="profile" @change="setProfileIndex()"> <select x-model="profileIndex" name="profile" id="profile" @change="setProfileIndex()">
<template x-for="(prof, index) in profiles"> <template x-for="(prof, index) in profiles">
<option x-text="prof.name" :value="index"></option> <option x-text="prof" :value="index"></option>
</template> </template>
</select> </select>
<button @click="newProfile">New</button> <button @click="newProfile">New</button>
@@ -18,12 +18,12 @@
<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="profile.name"> <input type="text" id="profile-name" x-model="activeProfile.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="profile.privKey" :type="visibleKey ? 'text' : 'password'"> <input id="priv-key" x-model="activeProfile.privKey" :type="visibleKey ? 'text' : 'password'">
</div> </div>
<div class="buttons"> <div class="buttons">

View File

@@ -3,17 +3,39 @@ window.Alpine = Alpine;
Alpine.data('popup', () => ({ Alpine.data('popup', () => ({
pubKey: '', pubKey: '',
profiles: [{name: 'Default', privKey: '', hosts: []}], profiles: [],
profileIndex: 0, profileIndex: 0,
activeProfile: {hosts: []},
visibleKey: false, visibleKey: false,
async init() { async init() {
console.log("Initializing backend.");
await browser.runtime.sendMessage({kind: 'init'});
console.log('Getting profile list.')
await this.getProfiles(); await this.getProfiles();
console.log('Getting active profile.');
await this.getActiveProfile();
console.log('Getting pub key.');
await this.getPubKey(); await this.getPubKey();
console.log('profiles: ', this.profiles);
}, },
async saveKey() { async saveKey() {},
async setProfileByIndex(index) {
this.profileIndex = index;
await this.resetProfile();
},
async resetProfile() {
await this.setProfileIndex();
await this.getActiveProfile();
await this.getPubKey(); await this.getPubKey();
await this.getProfiles();
},
async getActiveProfile() {
this.activeProfile = await browser.runtime.sendMessage({kind: 'getActiveProfile'});
}, },
async getProfiles() { async getProfiles() {
@@ -23,7 +45,6 @@ Alpine.data('popup', () => ({
async setProfileIndex() { async setProfileIndex() {
await browser.runtime.sendMessage({kind: 'setProfileIndex', payload: this.profileIndex}); await browser.runtime.sendMessage({kind: 'setProfileIndex', payload: this.profileIndex});
await this.getPubKey();
}, },
async deleteSite(index) { async deleteSite(index) {
@@ -33,31 +54,24 @@ Alpine.data('popup', () => ({
}, },
async getPubKey() { async getPubKey() {
this.pubKey = await browser.runtime.sendMessage({kind: 'getPubKey', payload: this.profile.privKey}); this.pubKey = await browser.runtime.sendMessage({kind: 'getPubKey', payload: this.activeProfile.privKey});
}, },
async newProfile() { async newProfile() {
let newKey = await browser.runtime.sendMessage({kind: 'newKey'}); const newIndex = await browser.runtime.sendMessage({kind: 'newProfile'});
const newProfile = {name: 'New Profile', privKey: newKey}; await this.setProfileByIndex(newIndex);
this.profiles.push(newProfile);
this.profileIndex = this.profiles.length - 1;
await this.setProfileIndex();
}, },
get hasValidPubKey() { get hasValidPubKey() {
return typeof(this.pubKey) === 'string' && this.pubKey.length > 0; return typeof(this.pubKey) === 'string' && this.pubKey.length > 0;
}, },
get profile() {
return this.profiles[this.profileIndex];
},
get hosts() { get hosts() {
return this.profile.hosts; return this.activeProfile.hosts;
}, },
set hosts(value) { set hosts(value) {
this.profiles[this.profileIndex].hosts = value; this.activeProfile.hosts = value;
} }
})); }));