From 920ef598f5ed0f2ef919c417005985319889c047 Mon Sep 17 00:00:00 2001 From: alemi Date: Mon, 15 Apr 2024 23:11:36 +0200 Subject: [PATCH] fix(web): proper caching of users --- web/src/lib.rs | 31 ++++++++++++++++++------------- web/src/main.rs | 4 ++-- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/web/src/lib.rs b/web/src/lib.rs index 457364aa..5d2aa7ad 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -167,26 +167,31 @@ pub fn ActorBanner(object: serde_json::Value) -> impl IntoView { } #[component] -pub fn Actor() -> impl IntoView { +pub fn UserPage() -> impl IntoView { let params = use_params_map(); - let actor = create_local_resource(move || params.get().get("id").cloned().unwrap_or_default(), |uid| { + let actor = create_local_resource(move || params.get().get("id").cloned().unwrap_or_default(), |id| { async move { - let uid = format!("{URL_BASE}/users/{uid}"); - match CTX.cache.actors.get(&uid) { - Some(x) => x.clone(), - None => reqwest::get(uid) - .await - .unwrap() - .json::() - .await - .unwrap(), + let uri = web_uri("users", &id); + match CTX.cache.actors.get(&uri) { + Some(x) => Some(x.clone()), + None => { + let user = reqwest::get(&uri) + .await + .ok()? + .json::() + .await + .ok()?; + CTX.cache.actors.insert(uri, user.clone()); + Some(user) + }, } } }); view! { {move || match actor.get() { None => view! {

loading...

}.into_view(), - Some(x) => view! { + Some(None) => view! {

error loading

}.into_view(), + Some(Some(x)) => view! {

().await?; - CTX.cache.actors.insert(uid, actor.clone()); + CTX.cache.actors.insert(web_uri("users", &uid), actor.clone()); out.push(x.set_actor(apb::Node::object(actor))) } diff --git a/web/src/main.rs b/web/src/main.rs index 8cf76818..2ffd2476 100644 --- a/web/src/main.rs +++ b/web/src/main.rs @@ -3,7 +3,7 @@ use leptos_router::*; use leptos_use::{use_cookie, utils::FromToStringCodec}; use upub_web::{ - Actor, LoginBox, ObjectPage, PostBox, Timeline + LoginBox, ObjectPage, UserPage, PostBox, Timeline }; @@ -44,7 +44,7 @@ fn main() { } /> } /> - +