Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

feat: persist notification history to disk instead of localforage

Louis Beaumontc6d8868d254c
diff --git a/apps/screenpipe-app-tauri/components/notification-bell.tsx b/apps/screenpipe-app-tauri/components/notification-bell.tsx
index 4459fbb76..06b3520df 100644
--- a/apps/screenpipe-app-tauri/components/notification-bell.tsx
+++ b/apps/screenpipe-app-tauri/components/notification-bell.tsx
@@ -6,9 +6,7 @@
import { useState, useEffect, useCallback } from "react";
import { Bell, ChevronRight, ChevronDown, MessageSquare } from "lucide-react";
-import localforage from "localforage";
import ReactMarkdown from "react-markdown";
-import { listen } from "@tauri-apps/api/event";
import {
Popover,
PopoverContent,
@@ -27,17 +25,23 @@ interface NotificationEntry {
read: boolean;
}
+const API_BASE = "http://localhost:11435";
+
export function NotificationBell() {
const [history, setHistory] = useState<NotificationEntry[]>([]);
const [open, setOpen] = useState(false);
const [expandedId, setExpandedId] = useState<string | null>(null);
const loadHistory = useCallback(async () => {
- const entries =
- (await localforage.getItem<NotificationEntry[]>(
- "notification-history"
- )) || [];
- setHistory(entries);
+ try {
+ const res = await fetch(`${API_BASE}/notifications`);
+ if (res.ok) {
+ const entries: NotificationEntry[] = await res.json();
+ setHistory(entries);
+ }
+ } catch {
+ // server not ready yet
+ }
}, []);
useEffect(() => {
@@ -46,45 +50,20 @@ export function NotificationBell() {
return () => clearInterval(interval);
}, [loadHistory]);
- // Listen for native notifications so they appear in history even if the
- // overlay window (which has NotificationHandler) isn't mounted.
- useEffect(() => {
- const unlisten = listen<string>("native-notification-shown", async (event) => {
- try {
- const data = JSON.parse(event.payload);
- const history = await localforage.getItem<NotificationEntry[]>("notification-history") || [];
- // Skip if already saved (dedup by id)
- if (history.some((n) => n.id === data.id)) return;
- const entry: NotificationEntry = {
- id: data.id,
- type: data.type,
- title: data.title,
- body: data.body,
- pipe_name: data.pipe_name,
- timestamp: new Date().toISOString(),
- read: false,
- };
- const updated = [entry, ...history].slice(0, 100);
- await localforage.setItem("notification-history", updated);
- setHistory(updated);
- } catch (e) {
- console.error("failed to save notification to history:", e);
- }
- });
- return () => { unlisten.then((u) => u()); };
- }, []);
-
const unreadCount = history.filter((n) => !n.read).length;
const markAllRead = async () => {
- const updated = history.map((n) => ({ ...n, read: true }));
- setHistory(updated);
- await localforage.setItem("notification-history", updated);
+ setHistory((prev) => prev.map((n) => ({ ...n, read: true })));
+ try {
+ await fetch(`${API_BASE}/notifications`, { method: "POST" });
+ } catch {}
};
const clearAll = async () => {
setHistory([]);
- await localforage.setItem("notification-history", []);
+ try {
+ await fetch(`${API_BASE}/notifications`, { method: "DELETE" });
+ } catch {}
};
const formatTime = (ts: string) => {
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/server.rs b/apps/screenpipe-app-tauri/src-tauri/src/server.rs
index 34aad3b10..8fc64b27c 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/server.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/server.rs
@@ -14,6 +14,7 @@ use axum::{
use http::header::{HeaderValue, CONTENT_TYPE};
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;
+use std::path::PathBuf;
use tauri::Emitter;
use tauri::Manager;
use tokio::sync::mpsc;
@@ -59,6 +60,39 @@ struct ApiResponse {
message: String,
}
+#[derive(Serialize, Deserialize, Debug, Clone)]
+struct NotificationHistoryEntry {
+ id: String,
+ #[serde(rename = "type")]
+ notification_type: String,
+ title: String,
+ body: String,
+ pipe_name: Option<String>,
+ timestamp: String,
+ read: bool,
+}
+
+const MAX_NOTIFICATION_HISTORY: usize = 100;
+
+fn notifications_path() -> PathBuf {
+ screenpipe_core::paths::default_screenpipe_data_dir().join("notifications.json")
+}
+
+fn read_notification_history() -> Vec<NotificationHistoryEntry> {
+ let path = notifications_path();
+ match std::fs::read_to_string(&path) {
+ Ok(data) => serde_json::from_str(&data).unwrap_or_default(),
+ Err(_) => Vec::new(),
+ }
+}
+
+fn write_notification_history(entries: &[NotificationHistoryEntry]) {
+ let path = notifications_path();
+ if let Ok(data) = serde_json::to_string(entries) {
+ let _ = std::fs::write(&path, data);
+ }
+}
+
#[derive(Serialize, Deserialize, Debug)]
struct InboxMessagePayload {
title: String,
@@ -247,12 +281,18 @@ pub async fn run_server(app_handle: tauri::AppHandle, port: u16) {
let cors = CorsLayer::new()
.allow_origin("*".parse::<HeaderValue>().unwrap())
- .allow_methods([Method::GET, Method::POST, Method::OPTIONS])
+ .allow_methods([Method::GET, Method::POST, Method::DELETE, Method::OPTIONS])
.allow_headers(Any)
.allow_credentials(false);
let app = Router::new()
.route("/notify", axum::routing::post(send_notification))
+ .route(
+ "/notifications",
+ axum::routing::get(list_notifications)
+ .post(mark_notifications_read)
+ .delete(clear_notifications),
+ )
.route("/inbox", axum::routing::post(send_inbox_message))
.route("/log", axum::routing::post(log_message))
.route("/auth", axum::routing::post(handle_auth))
@@ -322,6 +362,21 @@ async fn send_notification(
"autoDismissMs": dismiss_ms,
});
+ // Persist to disk before attempting to show — survives crashes/restarts
+ let entry = NotificationHistoryEntry {
+ id: panel_id.clone(),
+ notification_type: panel_payload["type"].as_str().unwrap_or("pipe").to_string(),
+ title: payload.title.clone(),
+ body: payload.body.clone(),
+ pipe_name: None,
+ timestamp: chrono::Utc::now().to_rfc3339(),
+ read: false,
+ };
+ let mut history = read_notification_history();
+ history.insert(0, entry);
+ history.truncate(MAX_NOTIFICATION_HISTORY);
+ write_notification_history(&history);
+
let panel_json = panel_payload.to_string();
// Use the show_notification_panel command directly
@@ -343,6 +398,30 @@ async fn send_notification(
}
}
+async fn list_notifications() -> Json<Vec<NotificationHistoryEntry>> {
+ Json(read_notification_history())
+}
+
+async fn mark_notifications_read() -> Json<ApiResponse> {
+ let mut history = read_notification_history();
+ for entry in &mut history {
+ entry.read = true;
+ }
+ write_notification_history(&history);
+ Json(ApiResponse {
+ success: true,
+ message: "all notifications marked as read".to_string(),
+ })
+}
+
+async fn clear_notifications() -> Json<ApiResponse> {
+ write_notification_history(&[]);
+ Json(ApiResponse {
+ success: true,
+ message: "notification history cleared".to_string(),
+ })
+}
+
async fn send_inbox_message(
State(state): State<ServerState>,
Json(payload): Json<InboxMessagePayload>,