all repos — underbbs @ 0106b445b5f13dfd99f7f8f40743639204ae472e

decentralized social media client

frontend/ts/index.ts (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import util from "./util"
import {AdapterState, AdapterData} from "./adapter";
import {Message, Attachment, Author} from "./message"
import {Settings} from "./settings"
import { TabBarElement } from "./tabbar-element"
import { MessageElement } from "./message-element"
import { SettingsElement } from "./settings-element"
import { AdapterElement } from "./adapter-element"
import { ThreadSummaryElement } from "./thread-summary-element"

function main()  {
  const saveData = localStorage.getItem("settings");
  Settings._instance = saveData ? <Settings>JSON.parse(saveData) : new Settings();
  
  customElements.define("underbbs-tabbar", TabBarElement);
  customElements.define("underbbs-message", MessageElement);
  customElements.define("underbbs-settings", SettingsElement);
  customElements.define("underbbs-adapter", AdapterElement);
  customElements.define("underbbs-thread-summary", ThreadSummaryElement);
  
  util._("closeErr", util.closeErr);

  tabbarInit(Settings._instance.adapters?.map(a=>a.nickname) ?? []);
  
  registerServiceWorker();
}

function tabbarInit(adapters: string[]) {
  const nav = util.$("tabbar_injectparent");
  if (nav) {
    nav.innerHTML = `<underbbs-tabbar data-adapters="" data-currentadapter=""></underbbs-tabbar>`;
  }
}

async function registerServiceWorker() {
  if ("serviceWorker" in navigator) {
    try {
      const registration = await navigator.serviceWorker.register("/serviceWorker.js", {
        scope: "/",
      });
      if (registration.installing) {
        console.log("Service worker installing");
      } else if (registration.waiting) {
        console.log("Service worker installed");
      } else if (registration.active) {
        console.log("Service worker active");
      }
    } catch (error) {
      console.error(`Registration failed with ${error}`);
    }
    const registration = await navigator.serviceWorker.ready;
    (registration as any).sync.register("testdata").then((r:any)=>{console.log("but i will see this!")});
  }
}

main();