From 6e5f6d9b9c6fedb4ab80190c156595d321d33bbf Mon Sep 17 00:00:00 2001 From: metamuffin Date: Thu, 11 Dec 2025 01:20:17 +0100 Subject: refactor import plugins part 3 --- web/script/import_live.ts | 64 +++++++++++++++++++++++++++++++++++++++++++++++ web/script/log_live.ts | 22 ++++++++++++++++ web/script/log_stream.ts | 22 ---------------- web/script/main.ts | 3 ++- 4 files changed, 88 insertions(+), 23 deletions(-) create mode 100644 web/script/import_live.ts create mode 100644 web/script/log_live.ts delete mode 100644 web/script/log_stream.ts (limited to 'web') diff --git a/web/script/import_live.ts b/web/script/import_live.ts new file mode 100644 index 0000000..cc8c846 --- /dev/null +++ b/web/script/import_live.ts @@ -0,0 +1,64 @@ +/* + 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) 2025 metamuffin +*/ +/// + +import { OVar } from "./jshelper/mod.ts"; +import { e } from "./jshelper/src/element.ts"; + +interface ImportProgress { + total_items: number + finished_items: number + tasks: string[] +} + +function progress_bar(progress: OVar, text: OVar): 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 log connected"); + ws.onclose = () => console.log("live log disconnected"); + ws.onerror = e => console.log("live log ws error", e); + + + const progress = new OVar(0) + const text = new OVar("") + const pre = e("pre") + el.append(progress_bar(progress, text), pre) + + ws.onmessage = msg => { + if (msg.data == "done") return location.reload() + const p: ImportProgress = JSON.parse(msg.data) + text.value = `${p.finished_items} / ${p.total_items}` + progress.value = p.finished_items / p.total_items + pre.textContent = p.tasks.map((e, i) => `thread ${("#" + i).padStart(3)}: ${e}`).join("\n") + } +}) diff --git a/web/script/log_live.ts b/web/script/log_live.ts new file mode 100644 index 0000000..053c110 --- /dev/null +++ b/web/script/log_live.ts @@ -0,0 +1,22 @@ +/* + 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) 2025 metamuffin +*/ +/// +globalThis.addEventListener("DOMContentLoaded", () => { + if (!document.body.classList.contains("admin_log")) return + const log = document.getElementById("log")! + + const warnonly = new URL(globalThis.location.href).searchParams.get("warnonly") == "true" + const ws = new WebSocket(`/admin/log?stream&warnonly=${warnonly}&html=true`) + ws.onopen = () => console.log("live log connected"); + ws.onclose = () => console.log("live log disconnected"); + ws.onerror = e => console.log(`live log ws error: ${e}`); + + ws.onmessage = msg => { + log.children[0].children[0].innerHTML += msg.data + while (log.children[0].children[0].children.length > 1024) + log.children[0].children[0].children[0].remove() + } +}) diff --git a/web/script/log_stream.ts b/web/script/log_stream.ts deleted file mode 100644 index 053c110..0000000 --- a/web/script/log_stream.ts +++ /dev/null @@ -1,22 +0,0 @@ -/* - 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) 2025 metamuffin -*/ -/// -globalThis.addEventListener("DOMContentLoaded", () => { - if (!document.body.classList.contains("admin_log")) return - const log = document.getElementById("log")! - - const warnonly = new URL(globalThis.location.href).searchParams.get("warnonly") == "true" - const ws = new WebSocket(`/admin/log?stream&warnonly=${warnonly}&html=true`) - ws.onopen = () => console.log("live log connected"); - ws.onclose = () => console.log("live log disconnected"); - ws.onerror = e => console.log(`live log ws error: ${e}`); - - ws.onmessage = msg => { - log.children[0].children[0].innerHTML += msg.data - while (log.children[0].children[0].children.length > 1024) - log.children[0].children[0].children[0].remove() - } -}) diff --git a/web/script/main.ts b/web/script/main.ts index d7a36cb..d5905d3 100644 --- a/web/script/main.ts +++ b/web/script/main.ts @@ -8,4 +8,5 @@ import "./player/mod.ts" import "./transition.ts" import "./backbutton.ts" import "./dangerbutton.ts" -import "./log_stream.ts" +import "./log_live.ts" +import "./import_live.ts" -- cgit v1.3