From 477f85145c6bb6eec7e86796651afaa35da0e7d2 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sun, 18 May 2025 15:02:18 +0200 Subject: webui live updating --- src/webui.rs | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'src/webui.rs') diff --git a/src/webui.rs b/src/webui.rs index 5a66ffc..cd7c1de 100644 --- a/src/webui.rs +++ b/src/webui.rs @@ -1,4 +1,7 @@ -use crate::{State, helper::Css}; +use crate::{ + State, + helper::{Css, Javascript}, +}; use axum::extract::State as S; use axum::response::Html; use markup::doctype; @@ -13,6 +16,13 @@ pub(crate) async fn webui_style() -> Css { include_str!("style.css").to_string() }) } +pub(crate) async fn webui_script() -> Javascript { + Javascript(if cfg!(debug_assertions) { + read_to_string("src/webui_live.js").await.unwrap() + } else { + include_str!("webui_live.js").to_string() + }) +} pub(crate) async fn webui(S(state): S>>) -> Html { let g = state.read().await; @@ -25,19 +35,20 @@ pub(crate) async fn webui(S(state): S>>) -> Html { head { meta[charset="UTF-8"]; link[rel="stylesheet", href="/style.css"]; + script[src="/webui_live.js", defer] {} title { "Queue-Server" } } body { - section { + section.workers { h2 { "Workers"} ul { @for (id, w) in &g.workers { li { @Worker { id: *id, w } } }} } section.tasks { - @Taskbin {title: "Queued", class: "task queue", set: &g.queue, default, g } - @Taskbin {title: "Loading", class: "task loading", set: &g.loading, default, g } - @Taskbin {title: "Completed", class: "task complete", set: &g.complete, default, g } + @Taskbin {title: "Queued", state: "queue", set: &g.queue, default, g } + @Taskbin {title: "Loading", state: "loading", set: &g.loading, default, g } + @Taskbin {title: "Completed", state: "complete", set: &g.complete, default, g } } } } @@ -46,17 +57,18 @@ pub(crate) async fn webui(S(state): S>>) -> Html { } markup::define!( - Taskbin<'a>(title: &'a str, class: &'a str, set: &'a HashSet, g: &'a State, default: &'a Map) { - div { + Taskbin<'a>(title: &'a str, state: &'a str, set: &'a HashSet, g: &'a State, default: &'a Map) { + div[id=format!("bin-{state}")] { h2 { @title } p.count { @set.len() " tasks" } + @let class = format!("task {state}"); ul { @for key in set.iter().take(128) { - li { @Task { key, data: g.metadata.get(key).unwrap_or(&default), class } } + li { @Task { key, data: g.metadata.get(key).unwrap_or(&default), class: &class } } }} } } Task<'a>(key: &'a str, data: &'a Map, class: &'a str) { - div[class=class] { + div[class=class, id=key] { // @if let Some(url) = data.get("thumbnail").and_then(Value::as_str) { // img[src=url, loading="lazy"]; // } @@ -68,7 +80,7 @@ markup::define!( } } Worker<'a>(id: u64, w: &'a crate::Worker) { - div[class=worker_class(w)] { + div[class=worker_class(w), id=format!("worker-{id}")] { h3 { @w.name } span { "ID: " @id } ", " @if !w.assigned_tasks.is_empty() { -- cgit v1.2.3-70-g09d2