mod api; mod components; mod pages; mod types; use yew::prelude::*; use yew_router::prelude::*; use components::navbar::Navbar; use pages::{switch, Route}; use types::UserInfo; #[derive(Clone, PartialEq)] enum AuthState { Loading, NeedsSetup, NeedsLogin, Authenticated(UserInfo), } #[function_component(App)] fn app() -> Html { let auth = use_state(|| AuthState::Loading); // Check auth state on mount { let auth = auth.clone(); use_effect_with((), move |_| { wasm_bindgen_futures::spawn_local(async move { match api::get_me().await { Ok(user) => auth.set(AuthState::Authenticated(user)), Err(_) => { // Not logged in — check if setup is needed match api::check_setup_required().await { Ok(sr) if sr.required => auth.set(AuthState::NeedsSetup), _ => auth.set(AuthState::NeedsLogin), } } } }); }); } let on_auth_success = { let auth = auth.clone(); Callback::from(move |_: ()| { let auth = auth.clone(); wasm_bindgen_futures::spawn_local(async move { if let Ok(user) = api::get_me().await { auth.set(AuthState::Authenticated(user)); } }); }) }; match &*auth { AuthState::Loading => html! {
{ "Loading..." }