import util from "./util" import { Message } from "./message" import { BatchTimer } from "./batch-timer" import { AdapterState } from "./adapter" export class MessageElement extends HTMLElement { static observedAttributes = [ "data-target", "data-latest", "data-adapter", "data-replyCt", "data-reactionCt", "data-boostCt" ] private _id: string | null = null; private _adapter: string | null = null; private _message: Message | null = null; private _messageTimer: BatchTimer | null = null; constructor() { super(); this.innerHTML = `
` } connectedCallback() { this._id = this.getAttribute("data-target"); this._adapter = this.getAttribute("data-adapter"); const gateway = this.getAttribute("data-gateway") ?? ""; this._messageTimer = new BatchTimer((ids: string[])=>{ let url = `${gateway}/api/adapters/${this._adapter}/fetch?entity_type=message`; for (let id of ids) { url += `&entity_id=${id}`; } util.authorizedFetch("GET", url, null); }); } attributeChangedCallback(attr: string, prev: string, next: string) { switch (attr) { case "data-target": if (!next) { return } this._id = next; if (this._messageTimer) { this._messageTimer.queue(next, 100); } break; case "data-latest": let datastore = AdapterState._instance.data.get(this._adapter ?? ""); if (!datastore) { console.log("no data yet, wait for some to come in maybe..."); return; } let msg = datastore.messages.get(next); console.log("MessageElement.attributeChangedCallback: " + JSON.stringify(msg)); if (msg) { this._message = msg; const metadata = this.querySelector(".message_metadata"); const content = this.querySelector(".message_content"); const attachments = this.querySelector(".message_attachments"); if (metadata) { metadata.innerHTML = ` ` } if (content) { content.innerHTML = this._message.content; } if (attachments && this._message.attachments.length > 0) { let html = "