proper reactivity on config load
This commit is contained in:
+25
-23
@@ -9,7 +9,7 @@ use sir::{css, global_css};
|
||||
use std::collections::HashMap;
|
||||
use tracing::error;
|
||||
|
||||
use crate::{imp, CONFIG};
|
||||
use crate::imp;
|
||||
|
||||
pub type ChannelId = u32;
|
||||
pub type UserId = u32;
|
||||
@@ -26,6 +26,7 @@ pub enum Command {
|
||||
Connect {
|
||||
address: String,
|
||||
username: String,
|
||||
config: GuiConfig,
|
||||
},
|
||||
SendChat {
|
||||
markdown: String,
|
||||
@@ -436,9 +437,7 @@ pub fn ChatView() -> Element {
|
||||
//},
|
||||
|
||||
#[component]
|
||||
pub fn ControlView() -> Element {
|
||||
let config_future = use_resource(|| CONFIG.get());
|
||||
|
||||
pub fn ControlView(config: Resource<GuiConfig>) -> Element {
|
||||
let net: Coroutine<Command> = use_coroutine_handle();
|
||||
let status = &STATE.status;
|
||||
let server = STATE.server.read();
|
||||
@@ -457,7 +456,7 @@ pub fn ControlView() -> Element {
|
||||
|
||||
let current_channel_name = server.channels[&channel].name.clone();
|
||||
|
||||
let Some(proxy_url) = config_future
|
||||
let Some(proxy_url) = config
|
||||
.read_unchecked()
|
||||
.as_ref()
|
||||
.and_then(|gui_config| gui_config.proxy_url.clone())
|
||||
@@ -706,7 +705,7 @@ pub fn ControlView() -> Element {
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn ServerView() -> Element {
|
||||
pub fn ServerView(config: Resource<GuiConfig>) -> Element {
|
||||
let net: Coroutine<Command> = use_coroutine_handle();
|
||||
let server = STATE.server.read();
|
||||
let Some(&UserState {
|
||||
@@ -797,25 +796,26 @@ pub fn ServerView() -> Element {
|
||||
}
|
||||
div {
|
||||
class: "{control_box}",
|
||||
ControlView {}
|
||||
ControlView { config }
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn LoginView() -> Element {
|
||||
pub fn LoginView(config: Resource<GuiConfig>) -> Element {
|
||||
let net: Coroutine<Command> = use_coroutine_handle();
|
||||
|
||||
let config_future = use_resource(|| CONFIG.get());
|
||||
|
||||
let default_address = &*config_future
|
||||
.read_unchecked()
|
||||
.as_ref()
|
||||
.and_then(|gui_config| gui_config.proxy_url.clone())
|
||||
.unwrap_or("".to_string());
|
||||
|
||||
let mut address = use_signal(|| default_address.to_string());
|
||||
let mut address_input = use_signal(|| None::<String>);
|
||||
let mut address = use_memo(move || {
|
||||
if let Some(addr) = address_input() {
|
||||
addr.clone()
|
||||
} else {
|
||||
config()
|
||||
.and_then(|c| c.proxy_url.clone())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
});
|
||||
|
||||
let previous_username = imp::load_username();
|
||||
let mut username = use_signal(|| previous_username.unwrap_or(String::new()));
|
||||
@@ -869,6 +869,7 @@ pub fn LoginView() -> Element {
|
||||
net.send(Connect {
|
||||
address: address.read().clone(),
|
||||
username: username.read().clone(),
|
||||
config: config.read().clone().unwrap_or_default(),
|
||||
})
|
||||
};
|
||||
let status = &STATE.status;
|
||||
@@ -918,7 +919,7 @@ pub fn LoginView() -> Element {
|
||||
placeholder: "server address",
|
||||
value: "{address.read()}",
|
||||
autofocus: "true",
|
||||
oninput: move |evt| address.set(evt.value().clone()),
|
||||
oninput: move |evt| address_input.set(Some(evt.value().clone())),
|
||||
}
|
||||
{bottom}
|
||||
}
|
||||
@@ -927,9 +928,10 @@ pub fn LoginView() -> Element {
|
||||
|
||||
pub fn app() -> Element {
|
||||
use_coroutine(|rx: UnboundedReceiver<Command>| super::network_entrypoint(rx));
|
||||
use_future(|| async move {
|
||||
if let Err(err) = imp::load_config().await {
|
||||
error!("{}", err)
|
||||
let config = use_resource(|| async move {
|
||||
match imp::load_config().await {
|
||||
Ok(config) => config,
|
||||
Err(_) => GuiConfig::default(),
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1006,8 +1008,8 @@ pub fn app() -> Element {
|
||||
|
||||
sir::AppStyle { }
|
||||
match *STATE.status.read() {
|
||||
Connected => rsx!(ServerView {}),
|
||||
_ => rsx!(LoginView {}),
|
||||
Connected => rsx!(ServerView { config }),
|
||||
_ => rsx!(LoginView { config }),
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user