/*
    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" />
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()
    }
})
