import {Adapter} from "./adapter"; function _(key: string, value: any | null | undefined = undefined): any | null { const x = window; if (value !== undefined) { x[key] = value; } return x[key]; } function $(id: string): HTMLElement | null { return document.getElementById(id); } function main():void { const settings = _("settings", JSON.parse(localStorage.getItem("settings") ?? "{}")); const adapters = _("adapters", []); if (settings != null) { for (let s of settings.adapters) { let a: Adapter = Adapter.create() switch (s.protocol) { case "nostr": adapters.push(Adapter.toNostr(a, s)); break; case "mastodon": adapters.push(Adapter.toMasto(a, s)); } } } else { console.log("no settings exist for this client"); _("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"); const adapters = _("adapters") as Adapter[]; if (tabcontent) { let html = "

this is our settings dialogue

"; html += ""; html += adapters.reduce((self: string, a: Adapter) => { self += `
  • ${a.nickname}
  • ` return self; }, ""; html += ""; tabcontent.innerHTML = html; } } function addAdapter(): void { const tabcontent = $("tabcontent"); if (tabcontent) { // dropdown for protocol let html = ""; // depending on protocol, different fields // nostr: privkey, initial relays html += "
    "; html += " "; html += " "; html += " "; html += "
    "; // masto/AP: server, username, pw/apikey // save button, back button html += ""; html += ""; tabcontent.innerHTML = html; } } function fillAdapterProtocolOptions(): void { const proto = $("settings_newadapter_protocolselect") as HTMLSelectElement; let html = ""; switch(proto?.options[proto.selectedIndex].value) { case "nostr": html += " "; html += " "; html += " "; break; case "mastodon": html += " "; html += " "; html += " "; break; } const div = $("settings_newadapter_protocoloptions"); if (div) { div.innerHTML = html; } } function saveSettings(): void { const settings = _("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]); const nickname = ($("settings_newadapter_nickname") as HTMLInputElement)?.value ?? "" ; // switch protocol switch (proto.options[proto.selectedIndex].value) { case "nostr": const privkey = ($("settings_newadapter_nostr_privkey") as HTMLInputElement)?.value ?? ""; const relays = ($("settings_newadapter_nostr_default_relays") as HTMLInputElement)?.value ?? ""; self = { nickname: nickname, protocol: "nostr", privkey: privkey, relays: relays.split(",").map(r=>r.trim()) }; break; case "mastodon": const server = ($("settings_newadapter_masto_server") as HTMLInputElement)?.value ?? ""; const apiKey = ($("settings_newadapter_masto_apikey") as HTMLInputElement)?.value ?? ""; self = { nickname: nickname, protocol: "mastodon", server: server, apiKey: apiKey }; break; } const settings = _("settings"); const adapters = _("adapters"); if (settings && adapters) { settings.adapters.push(self); let a: Adapter = Adapter.create(); switch (self.protocol) { case "nostr": adapters.push(Adapter.toNostr(a, self)); break; case "mastodon": adapters.push(Adapter.toMasto(a, self)); break; } localStorage.setItem("settings", JSON.stringify(settings)); showSettings(); } } _("addAdapter", addAdapter); _("saveAdapter", saveAdapter); _("fillAdapterProtocolOptions", fillAdapterProtocolOptions); _("showSettings", showSettings); _("saveSettings", saveSettings); main();