From 45392081c7a5624f41fe8c2a2021c1ce5e36a033 Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 15 Aug 2024 04:48:39 +0200 Subject: [PATCH] feat(web): show unread notifications count --- web/src/app.rs | 24 ++++++++++++++++++++---- web/src/components/navigation.rs | 4 ++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/web/src/app.rs b/web/src/app.rs index 96a3c0d..35cf6a1 100644 --- a/web/src/app.rs +++ b/web/src/app.rs @@ -1,3 +1,4 @@ +use apb::Collection; use leptos::*; use leptos_router::*; use crate::prelude::*; @@ -84,9 +85,24 @@ pub fn App() -> impl IntoView { let title_target = move || if auth.present() { "/web/home" } else { "/web/global" }; // refresh token immediately and every hour - let refresh = move || spawn_local(async move { Auth::refresh(auth.token, set_token, set_userid).await; }); - refresh(); - set_interval(refresh, std::time::Duration::from_secs(3600)); + let refresh_token = move || spawn_local(async move { Auth::refresh(auth.token, set_token, set_userid).await; }); + refresh_token(); + set_interval(refresh_token, std::time::Duration::from_secs(3600)); + + // refresh notifications + let (notifications, set_notifications) = create_signal(0); + let fetch_notifications = move || spawn_local(async move { + let actor_id = userid.get().unwrap_or_default(); + let notif_url = format!("/actors/{actor_id}/notifications"); + match Http::fetch::(¬if_url, auth).await { + Err(e) => tracing::error!("failed fetching notifications: {e}"), + Ok(doc) => if let Ok(count) = doc.total_items() { + set_notifications.set(count); + }, + } + }); + fetch_notifications(); + set_interval(fetch_notifications, std::time::Duration::from_secs(60)); view! {