149 lines
4.3 KiB
TypeScript
149 lines
4.3 KiB
TypeScript
import adapter, {Adapter} from "./adapter";
|
|
|
|
function _s(key: string, value: any | null) {
|
|
const x = <any>window;
|
|
x[key] = value;
|
|
}
|
|
|
|
function _g(key: string): any | null {
|
|
const x = <any>window;
|
|
return x[key];
|
|
}
|
|
|
|
function $(id: string): HTMLElement | null {
|
|
return document.getElementById(id);
|
|
}
|
|
|
|
function main():void {
|
|
_s("settings", JSON.parse(localStorage.getItem("settings") ?? "{}"));
|
|
_s("adapters", []);
|
|
|
|
const settings = _g("settings");
|
|
const adapters = _g("adapters");
|
|
if (settings != null) {
|
|
for (let s of settings.adapters) {
|
|
switch (s.protocol) {
|
|
case "nostr":
|
|
let a = adapter.toNostrAdapter(adapter.createAdapter(), s);
|
|
adapters.push(a);
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
console.log("no settings exist for this client");
|
|
_s("settings", { adapters: [] });
|
|
}
|
|
};
|
|
|
|
function showSettings():void {
|
|
// tab bar hidden
|
|
const tabbar = $("tabbar");
|
|
if (tabbar) {
|
|
tabbar.style.display = "none";
|
|
}
|
|
|
|
// tabcontent to show settings ui
|
|
const tabcontent = $("tabcontent");
|
|
|
|
if (tabcontent) {
|
|
let html = "<p>this is our settings dialogue</p>";
|
|
html += "<button onclick='addAdapter()'>New</button>";
|
|
html += _g("adapters").reduce((self: string, a: Adapter) => {
|
|
self += `<li><a href='#' onclick='editAdapter(${a.nickname})'>${a.nickname}</a></li>`
|
|
return self;
|
|
}, "<ul id='settings_adapterlist'>");
|
|
html += "</ul>";
|
|
html += "<button onclick='saveSettings()'>save</button>";
|
|
tabcontent.innerHTML = html;
|
|
}
|
|
}
|
|
|
|
function addAdapter(): void {
|
|
const tabcontent = $("tabcontent");
|
|
if (tabcontent) {
|
|
// dropdown for protocol
|
|
let html = "<select id='settings_newadapter_protocolselect' onchange='fillAdapterProtocolOptions()'>";
|
|
html += [ "nostr" ].reduce((self, p)=>{
|
|
self += `<option value='${p}'>${p}</option>`;
|
|
return self;
|
|
}, "");
|
|
html += "</select>";
|
|
// depending on protocol, different fields
|
|
// nostr: privkey, initial relays
|
|
html += "<div id='settings_newadapter_protocoloptions'>";
|
|
html += " <input id='settings_newadapter_nickname'/>";
|
|
html += " <input id='settings_newadapter_nostr_privkey'/>";
|
|
html += " <input id='settings_newadapter_nostr_default_relays'/>";
|
|
html += "</div>";
|
|
// masto/AP: server, username, pw/apikey
|
|
// save button, back button
|
|
html += "<button onclick='saveAdapter()'>Add</button>";
|
|
html += "<button onclick='showSettings()'>Back</button>";
|
|
|
|
tabcontent.innerHTML = html;
|
|
}
|
|
}
|
|
|
|
function fillAdapterProtocolOptions(): void {
|
|
const proto = $("settings_newadapter_protocolselect") as HTMLSelectElement;
|
|
|
|
|
|
|
|
console.log(proto?.options[proto.selectedIndex] ?? "no proto");
|
|
}
|
|
|
|
function saveSettings(): void {
|
|
const settings = _g("settings");
|
|
if (settings) {
|
|
localStorage.setItem("settings", JSON.stringify(settings));
|
|
}
|
|
// tab bar hidden
|
|
const tabbar = $("tabbar");
|
|
if (tabbar) {
|
|
tabbar.style.display = "block";
|
|
}
|
|
|
|
// tabcontent to show settings ui
|
|
const tabcontent = $("tabcontent");
|
|
if (tabcontent) {
|
|
tabcontent.innerHTML = "";
|
|
}
|
|
}
|
|
|
|
function saveAdapter(): void {
|
|
let self: any = {};
|
|
// get selected adapter protocol
|
|
const proto = $("settings_newadapter_protocolselect") as HTMLSelectElement;
|
|
console.log(proto.options[proto.selectedIndex]);
|
|
|
|
// switch protocol
|
|
switch (proto.options[proto.selectedIndex].value) {
|
|
// nostr: save privkey
|
|
// save relays
|
|
case "nostr":
|
|
const privkey = ($("settings_newadapter_nostr_privkey") as HTMLInputElement)?.value ?? "";
|
|
const relays = ($("settings_newadapter_nostr_default_relays") as HTMLInputElement)?.value ?? "";
|
|
const nickname = ($("settings_newadapter_nickname") as HTMLInputElement)?.value ?? "" ;
|
|
self = { nickname: nickname, protocol: "nostr", privkey: privkey, relays: relays.split(",").map(r=>r.trim()) };
|
|
break;
|
|
// AP/masto: whatever
|
|
case "ap":
|
|
break;
|
|
}
|
|
const settings = _g("settings");
|
|
const adapters = _g("adapters");
|
|
if (settings && adapters) {
|
|
settings.adapters.push(self);
|
|
adapters.push(adapter.toNostrAdapter(adapter.createAdapter(), self));
|
|
localStorage.setItem("settings", JSON.stringify(settings));
|
|
showSettings();
|
|
}
|
|
}
|
|
|
|
_s("addAdapter", addAdapter);
|
|
_s("saveAdapter", saveAdapter);
|
|
_s("fillAdapterProtocolOptions", fillAdapterProtocolOptions);
|
|
_s("showSettings", showSettings);
|
|
_s("saveSettings", saveSettings);
|
|
|
|
main();
|