underbbs/ts/index.ts

150 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-04-27 16:50:27 +00:00
import adapter, {Adapter} from "./adapter";
2024-04-22 00:02:44 +00:00
2024-04-27 16:50:27 +00:00
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];
}
2024-04-22 00:02:44 +00:00
2024-04-27 16:50:27 +00:00
function $(id: string): HTMLElement | null {
2024-04-22 00:32:14 +00:00
return document.getElementById(id);
}
2024-04-27 16:50:27 +00:00
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) {
2024-04-22 00:02:44 +00:00
switch (s.protocol) {
case "nostr":
2024-04-22 00:32:14 +00:00
let a = adapter.toNostrAdapter(adapter.createAdapter(), s);
2024-04-27 16:50:27 +00:00
adapters.push(a);
2024-04-22 00:02:44 +00:00
break;
}
}
} else {
console.log("no settings exist for this client");
2024-04-27 16:50:27 +00:00
_s("settings", { adapters: [] });
2024-04-22 00:02:44 +00:00
}
};
2024-04-27 16:50:27 +00:00
function showSettings():void {
2024-04-22 00:32:14 +00:00
// tab bar hidden
2024-04-22 02:11:37 +00:00
const tabbar = $("tabbar");
2024-04-27 16:50:27 +00:00
if (tabbar) {
2024-04-22 00:32:14 +00:00
tabbar.style.display = "none";
2024-04-27 16:50:27 +00:00
}
2024-04-22 00:32:14 +00:00
// tabcontent to show settings ui
2024-04-22 02:11:37 +00:00
const tabcontent = $("tabcontent");
2024-04-22 00:32:14 +00:00
2024-04-27 16:50:27 +00:00
if (tabcontent) {
2024-04-22 02:11:37 +00:00
let html = "<p>this is our settings dialogue</p>";
html += "<button onclick='addAdapter()'>New</button>";
2024-04-27 16:50:27 +00:00
html += _g("adapters").reduce((self: string, a: Adapter) => {
2024-04-22 02:11:37 +00:00
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;
2024-04-27 16:50:27 +00:00
}
2024-04-22 02:11:37 +00:00
}
2024-04-27 16:50:27 +00:00
function addAdapter(): void {
2024-04-22 02:11:37 +00:00
const tabcontent = $("tabcontent");
2024-04-27 16:50:27 +00:00
if (tabcontent) {
2024-04-22 02:11:37 +00:00
// 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>";
2024-04-22 00:32:14 +00:00
2024-04-22 02:11:37 +00:00
tabcontent.innerHTML = html;
2024-04-27 16:50:27 +00:00
}
2024-04-22 02:11:37 +00:00
}
2024-04-27 16:50:27 +00:00
function fillAdapterProtocolOptions(): void {
const proto = $("settings_newadapter_protocolselect") as HTMLSelectElement;
console.log(proto?.options[proto.selectedIndex] ?? "no proto");
2024-04-22 00:32:14 +00:00
}
2024-04-27 16:50:27 +00:00
function saveSettings(): void {
const settings = _g("settings");
if (settings) {
localStorage.setItem("settings", JSON.stringify(settings));
2024-04-22 00:32:14 +00:00
}
2024-04-22 02:11:37 +00:00
// tab bar hidden
const tabbar = $("tabbar");
2024-04-27 16:50:27 +00:00
if (tabbar) {
tabbar.style.display = "block";
}
2024-04-22 00:32:14 +00:00
// tabcontent to show settings ui
2024-04-22 02:11:37 +00:00
const tabcontent = $("tabcontent");
2024-04-27 16:50:27 +00:00
if (tabcontent) {
tabcontent.innerHTML = "";
}
2024-04-22 00:32:14 +00:00
}
2024-04-27 16:50:27 +00:00
function saveAdapter(): void {
let self: any = {};
2024-04-22 02:11:37 +00:00
// get selected adapter protocol
2024-04-27 16:50:27 +00:00
const proto = $("settings_newadapter_protocolselect") as HTMLSelectElement;
2024-04-22 02:11:37 +00:00
console.log(proto.options[proto.selectedIndex]);
// switch protocol
switch (proto.options[proto.selectedIndex].value) {
// nostr: save privkey
// save relays
case "nostr":
2024-04-27 16:50:27 +00:00
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 ?? "" ;
2024-04-22 02:11:37 +00:00
self = { nickname: nickname, protocol: "nostr", privkey: privkey, relays: relays.split(",").map(r=>r.trim()) };
break;
// AP/masto: whatever
case "ap":
break;
}
2024-04-27 16:50:27 +00:00
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();
}
2024-04-22 02:11:37 +00:00
}
2024-04-27 16:50:27 +00:00
_s("addAdapter", addAdapter);
_s("saveAdapter", saveAdapter);
_s("fillAdapterProtocolOptions", fillAdapterProtocolOptions);
_s("showSettings", showSettings);
_s("saveSettings", saveSettings);
2024-04-22 00:32:14 +00:00
2024-04-27 16:50:27 +00:00
main();