aboutsummaryrefslogtreecommitdiff
path: root/ui/client-scripts/src/import_live.ts
blob: 54966ebcccf8e1d13ba3deefa0c863d2100264d1 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2026 metamuffin <metamuffin.org>
*/
/// <reference lib="dom" />

import { OVar } from "./jshelper/mod.ts";
import { e } from "./jshelper/src/element.ts";

interface ImportProgress {
    stage: string
    total_items: number
    finished_items: number
    tasks: string[]
}

function progress_bar(progress: OVar<number>, text: OVar<string>): HTMLElement {
    const bar_inner = e("div")
    bar_inner.style.height = "100%"
    bar_inner.style.backgroundColor = "#444"
    bar_inner.style.position = "absolute"
    bar_inner.style.top = "0px"
    bar_inner.style.left = "0px"
    bar_inner.style.zIndex = "2"
    const bar_text = e("div")
    bar_text.style.position = "absolute"
    bar_text.style.top = "0px"
    bar_text.style.left = "0px"
    bar_text.style.color = "white"
    bar_text.style.zIndex = "3"
    const bar_outer = e("div", bar_inner, bar_text)
    bar_outer.style.position = "relative"
    bar_outer.style.width = "100%"
    bar_outer.style.height = "2em"
    bar_outer.style.backgroundColor = "black"
    bar_outer.style.borderRadius = "5px"
    progress.onchangeinit(v => bar_inner.style.width = `${v * 100}%`)
    text.onchangeinit(v => bar_text.textContent = v)
    return bar_outer
}

globalThis.addEventListener("DOMContentLoaded", () => {
    if (!document.getElementById("admin_import")) return
    const el = document.getElementById("admin_import")!

    const ws = new WebSocket(`/admin/import`)
    ws.onopen = () => console.log("live progress connected");
    ws.onclose = () => console.log("live progress disconnected");
    ws.onerror = e => console.log("live progress ws error", e);


    const progress = new OVar(0)
    const progress_text = new OVar("")
    const pre = new OVar("")
    const stage = new OVar("")

    el.append(
        e("h2", stage),
        progress_bar(progress, progress_text),
        e("pre", pre)
    )

    ws.onmessage = msg => {
        if (msg.data == "done") return location.reload()
        const p: ImportProgress = JSON.parse(msg.data)
        stage.value = p.stage
        progress_text.value = `${p.finished_items} / ${p.total_items}`
        progress.value = p.finished_items / p.total_items
        pre.value = p.tasks.map((e, i) => `thread ${("#" + i).padStart(3)}: ${e}`).join("\n")
    }
})