yak shaving to store and display messages
This commit is contained in:
parent
2c35e9c304
commit
2244bbcf7a
3 changed files with 49 additions and 25 deletions
40
ts/index.ts
40
ts/index.ts
|
@ -1,17 +1,9 @@
|
|||
import {Adapter} from "./adapter";
|
||||
import {Message, Attachment} from "./message"
|
||||
import util from "./util"
|
||||
|
||||
function _(key: string, value: any | null | undefined = undefined): any | null {
|
||||
const x = <any>window;
|
||||
if (value !== undefined) {
|
||||
x[key] = value;
|
||||
}
|
||||
return x[key];
|
||||
}
|
||||
|
||||
function $(id: string): HTMLElement | null {
|
||||
return document.getElementById(id);
|
||||
}
|
||||
var $ = util.$
|
||||
var _ = util._
|
||||
|
||||
function main():void {
|
||||
const settings = _("settings", JSON.parse(localStorage.getItem("settings") ?? "{}"));
|
||||
|
@ -179,24 +171,12 @@ function saveAdapter(): void {
|
|||
break;
|
||||
}
|
||||
const settings = _("settings");
|
||||
let adapters = _("adapters");
|
||||
if (settings) {
|
||||
if (!settings.adapters) {
|
||||
settings.adapters = [];
|
||||
}
|
||||
settings.adapters.push(self);
|
||||
if (!adapters) {
|
||||
adapters = []
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
@ -217,7 +197,6 @@ async function authorizedFetch(method: string, uri: string, body: any): Promise<
|
|||
|
||||
function connect() {
|
||||
|
||||
// open the websocket connection with settings as subprotocol
|
||||
const wsProto = location.protocol == "https:" ? "wss" : "ws";
|
||||
_conn = new WebSocket(`${wsProto}://${location.host}/subscribe`, "underbbs");
|
||||
_conn.addEventListener("open", (e: any) => {
|
||||
|
@ -226,11 +205,22 @@ function connect() {
|
|||
});
|
||||
_conn.addEventListener("message", (e: any) => {
|
||||
|
||||
// debugging
|
||||
console.log(e)
|
||||
|
||||
// now we'll figure out what to do with it
|
||||
const data = JSON.parse(e.data);
|
||||
if (data.key) {
|
||||
_("skey", data.key)
|
||||
authorizedFetch("POST", "/api/adapters", JSON.stringify(_("settings").adapters))
|
||||
} else {
|
||||
// typeswitch on the incoming data and adapters
|
||||
|
||||
// if it's a regular message, add it to the store
|
||||
// if the adapter is active, inject the web components
|
||||
// FOR HOTFETCHED DATA:
|
||||
// before fetching, we can set properties on the DOM,
|
||||
// so when those data return to us we know where to inject components!
|
||||
}
|
||||
});
|
||||
_conn.addEventListener("error", (e: any) => {
|
||||
|
|
20
ts/message-element.ts
Normal file
20
ts/message-element.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
import util from "./util"
|
||||
var _ = util._
|
||||
|
||||
export class MessageElement extends HTMLElement {
|
||||
static observedAttributes = [ "data-id", "data-adapter", "data-replyCt", "data-reactionCt", "data-boostCt" ]
|
||||
|
||||
private _id: string | null = null;
|
||||
private _adapter: any;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this._id = this.getAttribute("data-id");
|
||||
this._adapter = _("settings").adapters.filter((a: any)=>a.nickname == this.getAttribute("data-adapter"))[0];
|
||||
|
||||
// grab message content from the store and format our innerHTML
|
||||
}
|
||||
}
|
14
ts/util.ts
Normal file
14
ts/util.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
|
||||
function _(key: string, value: any | null | undefined = undefined): any | null {
|
||||
const x = <any>window;
|
||||
if (value !== undefined) {
|
||||
x[key] = value;
|
||||
}
|
||||
return x[key];
|
||||
}
|
||||
|
||||
function $(id: string): HTMLElement | null {
|
||||
return document.getElementById(id);
|
||||
}
|
||||
|
||||
export default { _, $ }
|
Loading…
Reference in a new issue