feat(pwa): add PWA support and browser notifications for critical events
This commit is contained in:
@@ -213,7 +213,30 @@ pub fn provide_torrent_store() {
|
||||
global_stats.set(stats);
|
||||
}
|
||||
AppEvent::Notification(n) => {
|
||||
show_toast_with_signal(notifications, n.level, n.message);
|
||||
// Show toast notification
|
||||
show_toast_with_signal(notifications, n.level.clone(), n.message.clone());
|
||||
|
||||
// Show browser notification for critical events
|
||||
// (torrent completed, connection lost/restored, errors)
|
||||
let is_critical = n.message.contains("tamamlandı")
|
||||
|| n.message.contains("Reconnected")
|
||||
|| n.message.contains("yeniden kuruldu")
|
||||
|| n.message.contains("Lost connection")
|
||||
|| n.level == shared::NotificationLevel::Error;
|
||||
|
||||
if is_critical {
|
||||
let title = match n.level {
|
||||
shared::NotificationLevel::Success => "✅ VibeTorrent",
|
||||
shared::NotificationLevel::Error => "❌ VibeTorrent",
|
||||
shared::NotificationLevel::Warning => "⚠️ VibeTorrent",
|
||||
shared::NotificationLevel::Info => "ℹ️ VibeTorrent",
|
||||
};
|
||||
|
||||
crate::utils::notification::show_notification_if_enabled(
|
||||
title,
|
||||
&n.message
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use tailwind_fuse::merge::tw_merge;
|
||||
|
||||
pub mod notification;
|
||||
|
||||
pub fn cn(classes: impl AsRef<str>) -> String {
|
||||
tw_merge(classes.as_ref())
|
||||
}
|
||||
|
||||
113
frontend/src/utils/notification.rs
Normal file
113
frontend/src/utils/notification.rs
Normal file
@@ -0,0 +1,113 @@
|
||||
use wasm_bindgen::prelude::*;
|
||||
use web_sys::{Notification, NotificationOptions};
|
||||
|
||||
/// Request browser notification permission from user
|
||||
pub async fn request_notification_permission() -> bool {
|
||||
let window = web_sys::window().expect("no global window");
|
||||
|
||||
// Check if Notification API is available
|
||||
if js_sys::Reflect::has(&window, &JsValue::from_str("Notification")).unwrap_or(false) {
|
||||
let notification = js_sys::Reflect::get(&window, &JsValue::from_str("Notification"))
|
||||
.expect("Notification should exist");
|
||||
|
||||
// Request permission
|
||||
let promise = js_sys::Reflect::get(¬ification, &JsValue::from_str("requestPermission"))
|
||||
.expect("requestPermission should exist");
|
||||
|
||||
if let Ok(function) = promise.dyn_into::<js_sys::Function>() {
|
||||
if let Ok(promise) = function.call0(¬ification) {
|
||||
if let Ok(promise) = promise.dyn_into::<js_sys::Promise>() {
|
||||
let result = wasm_bindgen_futures::JsFuture::from(promise).await;
|
||||
|
||||
if let Ok(permission) = result {
|
||||
let permission_str = permission.as_string().unwrap_or_default();
|
||||
return permission_str == "granted";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
/// Check if browser notifications are supported and permitted
|
||||
pub fn is_notification_supported() -> bool {
|
||||
let window = web_sys::window().expect("no global window");
|
||||
js_sys::Reflect::has(&window, &JsValue::from_str("Notification")).unwrap_or(false)
|
||||
}
|
||||
|
||||
/// Get current notification permission status
|
||||
pub fn get_notification_permission() -> String {
|
||||
if !is_notification_supported() {
|
||||
return "unsupported".to_string();
|
||||
}
|
||||
|
||||
let window = web_sys::window().expect("no global window");
|
||||
let notification = js_sys::Reflect::get(&window, &JsValue::from_str("Notification"))
|
||||
.expect("Notification should exist");
|
||||
|
||||
let permission = js_sys::Reflect::get(¬ification, &JsValue::from_str("permission"))
|
||||
.unwrap_or(JsValue::from_str("default"));
|
||||
|
||||
permission.as_string().unwrap_or("default".to_string())
|
||||
}
|
||||
|
||||
/// Show a browser notification
|
||||
/// Returns true if notification was shown successfully
|
||||
pub fn show_browser_notification(title: &str, body: &str, icon: Option<&str>) -> bool {
|
||||
// Check permission first
|
||||
let permission = get_notification_permission();
|
||||
if permission != "granted" {
|
||||
log::warn!("Notification permission not granted: {}", permission);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create notification options
|
||||
let opts = NotificationOptions::new();
|
||||
opts.set_body(body);
|
||||
opts.set_icon(icon.unwrap_or("/icon-192.png"));
|
||||
opts.set_badge("/icon-192.png");
|
||||
opts.set_tag("vibetorrent");
|
||||
opts.set_require_interaction(false);
|
||||
opts.set_silent(Some(false));
|
||||
|
||||
// Create and show notification
|
||||
match Notification::new_with_options(title, &opts) {
|
||||
Ok(notification) => {
|
||||
log::info!("Browser notification shown: {}", title);
|
||||
|
||||
// Auto-close after 5 seconds
|
||||
let _ = gloo_timers::callback::Timeout::new(5000, move || {
|
||||
notification.close();
|
||||
}).forget();
|
||||
|
||||
true
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Failed to create notification: {:?}", e);
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Show notification only if enabled in settings and permission granted
|
||||
pub fn show_notification_if_enabled(title: &str, body: &str) -> bool {
|
||||
// Check localStorage for user preference
|
||||
let window = web_sys::window().expect("no global window");
|
||||
let storage = window.local_storage().ok().flatten();
|
||||
|
||||
if let Some(storage) = storage {
|
||||
let enabled = storage
|
||||
.get_item("vibetorrent_browser_notifications")
|
||||
.ok()
|
||||
.flatten()
|
||||
.unwrap_or("true".to_string());
|
||||
|
||||
if enabled == "true" {
|
||||
return show_browser_notification(title, body, None);
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
Reference in New Issue
Block a user