import { DcEvent, DeltaChat } from "../deltachat.js"; var SELECTED_ACCOUNT = 0; window.addEventListener("DOMContentLoaded", (_event) => { (window as any).selectDeltaAccount = (id: string) => { SELECTED_ACCOUNT = Number(id); window.dispatchEvent(new Event("account-changed")); }; console.log("launch run script..."); run().catch((err) => console.error("run failed", err)); }); async function run() { const $main = document.getElementById("main")!; const $side = document.getElementById("side")!; const $head = document.getElementById("header")!; const client = new DeltaChat("ws://localhost:20808/ws"); (window as any).client = client.rpc; client.on("ALL", (accountId, event) => { onIncomingEvent(accountId, event); }); window.addEventListener("account-changed", async (_event: Event) => { listChatsForSelectedAccount(); }); await Promise.all([loadAccountsInHeader(), listChatsForSelectedAccount()]); async function loadAccountsInHeader() { console.log("load accounts"); const accounts = await client.rpc.getAllAccounts(); console.log("accounts loaded", accounts); for (const account of accounts) { if (account.kind === "Configured") { write( $head, ` ${account.id}: ${account.addr!} ` ); } else { write( $head, ` ${account.id}: (unconfigured) ` ); } } } async function listChatsForSelectedAccount() { clear($main); const selectedAccount = SELECTED_ACCOUNT; const info = await client.rpc.getAccountInfo(selectedAccount); if (info.kind !== "Configured") { return write($main, "Account is not configured"); } write($main, `
${message.text}
`); else write($main, `loading error: ${message.error}
`); } } } function onIncomingEvent(accountId: number, event: DcEvent) { write( $side, ` ` ); } } function write(el: HTMLElement, html: string) { el.innerHTML += html; } function clear(el: HTMLElement) { el.innerHTML = ""; }