use leptos::*; use leptos_router::*; use reqwest::Method; use crate::prelude::*; use leptos_use::{storage::use_local_storage, use_cookie, utils::{FromToStringCodec, JsonCodec}}; #[component] pub fn App() -> impl IntoView { let (token, set_token) = use_cookie::("token"); let (userid, set_userid) = use_cookie::("user_id"); let (config, set_config, _) = use_local_storage::("config"); let auth = Auth { token, userid }; provide_context(auth); provide_context(config); let username = auth.userid.get_untracked() .map(|x| x.split('/').last().unwrap_or_default().to_string()) .unwrap_or_default(); let home_tl = Timeline::new(format!("{URL_BASE}/users/{username}/inbox/page")); let server_tl = Timeline::new(format!("{URL_BASE}/inbox/page")); let local_tl = Timeline::new(format!("{URL_BASE}/outbox/page")); let user_tl = Timeline::new(format!("{URL_BASE}/users/{username}/outbox/page")); let context_tl = Timeline::new(format!("{URL_BASE}/outbox/page")); let reply_controls = ReplyControls::default(); provide_context(reply_controls); let screen_width = window().screen().map(|x| x.avail_width().unwrap_or_default()).unwrap_or_default(); let (menu, set_menu) = create_signal(screen_width <= 786); let (advanced, set_advanced) = create_signal(false); let auth_present = auth.token.get_untracked().is_some(); // skip helper to use get_untracked let title_target = move || if auth_present { "/web/home" } else { "/web/server" }; if let Some(tok) = token.get_untracked() { spawn_local(async move { // refresh token first, or verify that we're still authed match reqwest::Client::new() .request(Method::PATCH, format!("{URL_BASE}/auth")) .json(&serde_json::json!({"token": tok})) .send() .await { Err(e) => tracing::error!("could not refresh token: {e}"), Ok(res) => match res.error_for_status() { Err(e) => tracing::error!("server rejected refresh: {e}"), Ok(doc) => match doc.json::().await { Err(e) => tracing::error!("failed parsing auth response: {e}"), Ok(auth) => { set_token.set(Some(auth.token)); set_userid.set(Some(auth.user)); }, } } } server_tl.more(auth); if auth_present { home_tl.more(auth) }; }) }; view! {


{move || if advanced.get() { view! { }} else { view! { }}}
404

nothing to see here!

}.into_view() > // TODO this is kind of ugly: the whole router gets rebuilt every time we log in/out // in a sense it's what we want: refreshing the home tl is main purpose, but also // server tl may contain stuff we can no longer see, or otherwise we may now be // entitled to see new posts. so while being ugly it's techically correct ig? {move || { view! {
} } else { view! { } } /> } /> } /> } /> } /> } /> } /> } />
} }}
} }