From 24eddead28768ebf64f709c2015c28a69443d2e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C5=ABdolfs=20O=C5=A1i=C5=86=C5=A1?= Date: Fri, 20 Nov 2026 10:55:28 +0100 Subject: [PATCH] desktop: Sync the notification count The backend now tells the frontend whenever the notifications database changes, which covers notifications the node writes as it fetches as well as clears by another instance of the app or the `rad` CLI, which have no node event. The inbox reloads in place, keeping the rows in view where they are. --- .../radicle-tauri/src/commands/startup.rs | 1 + desktop/crates/radicle-tauri/src/lib.rs | 1 + .../crates/radicle-tauri/src/notifications.rs | 37 +++++++ .../radicle-types/src/outbound/sqlite.rs | 23 ++++ desktop/src/components/AppSidebar.svelte | 5 +- desktop/src/components/InboxList.svelte | 47 +++++--- .../src/components/NotificationTeaser.svelte | 1 + .../src/components/NotificationsByRepo.svelte | 3 +- desktop/src/lib/latestOnly.ts | 12 ++ desktop/src/lib/router/definitions.ts | 4 + desktop/src/lib/startup.svelte.ts | 6 +- desktop/src/lib/writes.ts | 3 + desktop/src/views/Inbox.svelte | 104 ++++++++++++------ desktop/tests/unit/latestOnly.test.ts | 37 +++++++ desktop/tests/unit/writeCoverage.test.ts | 1 - shared/core/src/keys.ts | 5 + 16 files changed, 233 insertions(+), 57 deletions(-) create mode 100644 desktop/crates/radicle-tauri/src/notifications.rs create mode 100644 desktop/src/lib/latestOnly.ts create mode 100644 desktop/tests/unit/latestOnly.test.ts diff --git a/desktop/crates/radicle-tauri/src/commands/startup.rs b/desktop/crates/radicle-tauri/src/commands/startup.rs index 99077584f..493142469 100644 --- a/desktop/crates/radicle-tauri/src/commands/startup.rs +++ b/desktop/crates/radicle-tauri/src/commands/startup.rs @@ -43,6 +43,7 @@ pub(crate) fn startup(app: AppHandle) -> Result { let cobs_db = radicle_types::outbound::sqlite::Sqlite::reader(profile.cobs().join(COBS_DB_FILE))?; + crate::notifications::spawn(app.app_handle().clone(), inbox_db.clone()); let inbox_service = domain::inbox::service::Service::new(inbox_db); let patch_service = domain::patch::service::Service::new(cobs_db.clone()); let issue_service = domain::issue::service::Service::new(cobs_db); diff --git a/desktop/crates/radicle-tauri/src/lib.rs b/desktop/crates/radicle-tauri/src/lib.rs index fd644e6e2..f2fccb1b4 100644 --- a/desktop/crates/radicle-tauri/src/lib.rs +++ b/desktop/crates/radicle-tauri/src/lib.rs @@ -1,4 +1,5 @@ mod commands; +mod notifications; mod ref_updates; use std::sync::atomic::{AtomicBool, Ordering}; diff --git a/desktop/crates/radicle-tauri/src/notifications.rs b/desktop/crates/radicle-tauri/src/notifications.rs new file mode 100644 index 000000000..7f80ec4b3 --- /dev/null +++ b/desktop/crates/radicle-tauri/src/notifications.rs @@ -0,0 +1,37 @@ +use std::sync::Once; +use std::time::Duration; + +use radicle_types::outbound::sqlite::Sqlite; +use tauri::{AppHandle, Emitter}; + +/// Tells the frontend when another process changes the notifications, such as +/// the `rad` CLI or another instance of the app clearing some. The node's +/// events only cover notifications it adds while fetching. +pub(crate) fn spawn(app: AppHandle, db: Sqlite) { + static WATCHING: Once = Once::new(); + WATCHING.call_once(|| { + std::thread::spawn(move || { + let mut last = None; + let mut failing = false; + loop { + match db.data_version() { + Ok(version) => { + if last.is_some_and(|last| last != version) { + let _ = app.emit("notifications_changed", ()); + } + last = Some(version); + failing = false; + } + // Logged once until it works again. + Err(e) => { + if !failing { + log::warn!("Not able to watch the notifications: {e}"); + } + failing = true; + } + } + std::thread::sleep(Duration::from_secs(2)); + } + }); + }); +} diff --git a/desktop/crates/radicle-types/src/outbound/sqlite.rs b/desktop/crates/radicle-types/src/outbound/sqlite.rs index 5bc933cb6..44bf98165 100644 --- a/desktop/crates/radicle-types/src/outbound/sqlite.rs +++ b/desktop/crates/radicle-types/src/outbound/sqlite.rs @@ -22,6 +22,14 @@ pub struct Sqlite { } impl Sqlite { + /// A number that changes whenever another connection commits to the + /// database, such as another process on the same profile. + pub fn data_version(&self) -> Result { + let mut statement = self.db.prepare("PRAGMA data_version")?; + statement.next()?; + Ok(statement.read::(0)?) + } + /// How long to wait for the database lock to be released before failing a read. const DB_READ_TIMEOUT: time::Duration = time::Duration::from_secs(3); @@ -318,6 +326,21 @@ mod test { use super::Sqlite; + #[test] + fn data_version_changes_with_another_connections_writes() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("notifications.db"); + let writer = sql::Connection::open(&path).unwrap(); + writer.execute("CREATE TABLE t (x INTEGER)").unwrap(); + + let reader = Sqlite::reader(&path).unwrap(); + let before = reader.data_version().unwrap(); + assert_eq!(reader.data_version().unwrap(), before); + + writer.execute("INSERT INTO t VALUES (1)").unwrap(); + assert_ne!(reader.data_version().unwrap(), before); + } + const RID: &str = "rad:z3fpY7nttPPa6MBnAv2DccHzQJnqe"; const NID: &str = "z6MktULudTtAsAhRegYPiZ6631RV3viv12qd4GQF8z1xB22S"; const COMMIT: &str = "52b0a6dfb600be4e8fe61d70ca40cead71fb03a8"; diff --git a/desktop/src/components/AppSidebar.svelte b/desktop/src/components/AppSidebar.svelte index 1a3d2d3fb..42341cbf0 100644 --- a/desktop/src/components/AppSidebar.svelte +++ b/desktop/src/components/AppSidebar.svelte @@ -273,8 +273,9 @@ } }); - // The node writes notifications as it fetches, so its events say when to - // count them again. Without them, the count is polled. Either way it is + // The backend says when the notifications change, whether the node wrote + // them while fetching or another process cleared some. Without the node's + // events, such as in the test backend, the count is polled. Either way it is // fetched once at start, since events can already be live then, such as // after the webview reloads. onMount(() => { diff --git a/desktop/src/components/InboxList.svelte b/desktop/src/components/InboxList.svelte index 90fea298b..aec6574a6 100644 --- a/desktop/src/components/InboxList.svelte +++ b/desktop/src/components/InboxList.svelte @@ -12,12 +12,12 @@ import ConfirmClear from "@app/components/ConfirmClear.svelte"; import FuzzySearch from "@app/components/FuzzySearch.svelte"; import NotificationsByRepoComponent from "@app/components/NotificationsByRepo.svelte"; + import { getScrollViewport } from "@app/components/ScrollArea.svelte"; interface Props { clearAll: () => Promise; clearByIds: (ids: string[]) => Promise; clearByRepo: (rid: string) => Promise; - loadNew: () => Promise; notificationCount: number | undefined; notificationsByRepo: NotificationsByRepo[]; showAll: (rid: string) => Promise; @@ -27,7 +27,6 @@ clearAll, clearByIds, clearByRepo, - loadNew, notificationCount, notificationsByRepo, showAll, @@ -118,11 +117,36 @@ ), ); - function loadedNotificationCount() { - return notificationsByRepo.reduce((acc, repo) => { - return acc + repo.count; - }, 0); - } + // Notifications arriving or being cleared elsewhere update the list in + // place. Unless the list is scrolled to the top, keep the first visible row + // where it is, as browsers' scroll anchoring does, so that nothing moves + // under the user's cursor. WebKit doesn't anchor on its own. + const viewport = getScrollViewport(); + let anchors: { el: Element; top: number }[] = []; + + $effect.pre(() => { + void notificationsByRepo; + const v = viewport(); + if (!v || v.scrollTop === 0) { + anchors = []; + return; + } + const viewTop = v.getBoundingClientRect().top; + anchors = [...v.querySelectorAll("[data-inbox-anchor]")] + .map(el => ({ el, top: el.getBoundingClientRect().top })) + .filter(a => a.top >= viewTop); + }); + + $effect(() => { + void notificationsByRepo; + const v = viewport(); + // The first row still shown, since cleared rows are gone. + const anchor = anchors.find(a => a.el.isConnected); + anchors = []; + if (v && anchor) { + v.scrollTop += anchor.el.getBoundingClientRect().top - anchor.top; + } + });