proper reactivity on config load

This commit is contained in:
2025-10-25 19:03:02 -06:00
parent 20ec64cf1c
commit d9695be153
5 changed files with 59 additions and 54 deletions
+21 -24
View File
@@ -1,5 +1,4 @@
use crate::app::Command;
use crate::CONFIG;
use color_eyre::eyre::{bail, eyre, Error};
use dioxus::prelude::*;
use futures::{AsyncRead, AsyncWrite};
@@ -9,7 +8,9 @@ use mumble_protocol::control::{ClientControlCodec, ControlPacket};
use mumble_protocol::voice::{VoicePacket, VoicePacketPayload};
use mumble_protocol::Serverbound;
use mumble_web2_common::GuiConfig;
use reqwest::Url;
use std::time::Duration;
use tracing::level_filters::LevelFilter;
use tracing::{debug, error, info, instrument};
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::JsFuture;
@@ -308,8 +309,9 @@ pub async fn network_connect(
address: String,
username: String,
event_rx: &mut UnboundedReceiver<Command>,
gui_config: &GuiConfig,
) -> Result<(), Error> {
info!("Rust via WASM!");
info!("connecting");
let object = web_sys::js_sys::Object::new();
@@ -320,8 +322,7 @@ pub async fn network_connect(
)
.ey()?;
if let Some(server_hash) = &CONFIG.try_get().and_then(|cfg| cfg.cert_hash) {
error!("{:?}", server_hash);
if let Some(server_hash) = &gui_config.cert_hash {
let hash = web_sys::js_sys::Uint8Array::from(server_hash.as_slice());
web_sys::js_sys::Reflect::set(&object, &"value".into(), &hash).ey()?;
}
@@ -387,27 +388,20 @@ pub fn load_username() -> Option<String> {
.ok()?
}
fn load_config_from_window() -> Option<GuiConfig> {
serde_wasm_bindgen::from_value(Reflect::get(window()?.as_ref(), &"config".into()).ok()?).ok()
}
pub async fn load_config() -> color_eyre::Result<GuiConfig> {
let config_url = match option_env!("MUMBLE_WEB2_GUI_CONFIG_URL") {
Some(url) => Url::parse(url)?,
None => {
let window: web_sys::Window = web_sys::window().expect("no global `window` exists");
let location = window.location();
Url::parse(&location.href().ey()?)?.join("config")?
}
};
info!("loading config from {}", config_url);
fn load_config_from_env() -> Option<GuiConfig> {
serde_json::from_str(option_env!("MUMBLE_WEB2_GUI_CONFIG")?).ok()?
}
let config = reqwest::get(config_url).await?.json::<GuiConfig>().await?;
pub async fn load_config() -> color_eyre::Result<()> {
let config_url = option_env!("MUMBLE_WEB2_GUI_CONFIG_URL").ok_or(eyre!("foo"))?;
let config = reqwest::get(config_url)
.await
.unwrap()
.json::<GuiConfig>()
.await
.unwrap();
crate::CONFIG.set(config);
Ok(())
Ok(config)
}
pub fn init_logging() {
@@ -420,11 +414,14 @@ pub fn init_logging() {
let fmt_layer = tracing_subscriber::fmt::layer()
.with_ansi(false) // Only partially supported across browsers
.without_time() // std::time is not available in browsers
.with_writer(MakeWebConsoleWriter::new()); // write events to the console
.with_writer(MakeWebConsoleWriter::new()) // write events to the console
.with_filter(LevelFilter::DEBUG);
let perf_layer = performance_layer().with_details_from_fields(Pretty::default());
tracing_subscriber::registry()
.with(fmt_layer)
.with(perf_layer)
.init();
info!("logging initiated");
}