Improve App initialization logic with better error handling and logging to prevent infinite loading state
All checks were successful
Build MIPS Binary / build (push) Successful in 4m6s

This commit is contained in:
spinline
2026-02-07 15:45:51 +03:00
parent a948215538
commit 3c13f99652

View File

@@ -22,44 +22,64 @@ pub fn App() -> impl IntoView {
let (is_loading, set_is_loading) = create_signal(true); let (is_loading, set_is_loading) = create_signal(true);
let (is_authenticated, set_is_authenticated) = create_signal(false); let (is_authenticated, set_is_authenticated) = create_signal(false);
// Check Auth & Setup Status on load // Check Auth & Setup Status on load
create_effect(move |_| { create_effect(move |_| {
spawn_local(async move { spawn_local(async move {
// 1. Check Setup Status logging::log!("App initialization started...");
let setup_res = gloo_net::http::Request::get("/api/setup/status").send().await;
if let Ok(resp) = setup_res {
if let Ok(status) = resp.json::<SetupStatus>().await {
if !status.completed {
// Redirect to setup if not completed
let navigate = use_navigate();
navigate("/setup", Default::default());
set_is_loading.set(false);
return;
}
}
}
// 2. Check Auth Status // 1. Check Setup Status
let auth_res = gloo_net::http::Request::get("/api/auth/check").send().await; logging::log!("Checking setup status...");
if let Ok(resp) = auth_res { let setup_res = gloo_net::http::Request::get("/api/setup/status").send().await;
if resp.status() == 200 {
set_is_authenticated.set(true);
// Initialize push notifications logic only if authenticated match setup_res {
// ... (Push notification logic moved here or kept global but guarded) Ok(resp) => {
} else { if resp.ok() {
let navigate = use_navigate(); match resp.json::<SetupStatus>().await {
// If we are already on login or setup, don't redirect loop Ok(status) => {
let pathname = window().location().pathname().unwrap_or_default(); logging::log!("Setup status: completed={}", status.completed);
if pathname != "/login" && pathname != "/setup" { if !status.completed {
navigate("/login", Default::default()); logging::log!("Setup not completed, redirecting to /setup");
let navigate = use_navigate();
navigate("/setup", Default::default());
set_is_loading.set(false);
return;
}
}
Err(e) => logging::error!("Failed to parse setup status: {}", e),
}
} else {
logging::error!("Setup status request failed: {}", resp.status());
}
} }
Err(e) => logging::error!("Network error checking setup status: {}", e),
} }
}
set_is_loading.set(false); // 2. Check Auth Status
logging::log!("Checking auth status...");
let auth_res = gloo_net::http::Request::get("/api/auth/check").send().await;
match auth_res {
Ok(resp) => {
logging::log!("Auth check status: {}", resp.status());
if resp.status() == 200 {
logging::log!("Authenticated!");
set_is_authenticated.set(true);
} else {
logging::log!("Not authenticated, checking if redirect needed");
let navigate = use_navigate();
let pathname = window().location().pathname().unwrap_or_default();
if pathname != "/login" && pathname != "/setup" {
navigate("/login", Default::default());
}
}
}
Err(e) => logging::error!("Network error checking auth status: {}", e),
}
logging::log!("App initialization finished, disabling loader.");
set_is_loading.set(false);
});
}); });
});
// Initialize push notifications after user grants permission (Only if authenticated) // Initialize push notifications after user grants permission (Only if authenticated)
create_effect(move |_| { create_effect(move |_| {
if is_authenticated.get() { if is_authenticated.get() {