From b04613407be73832d620cb127154e864c6303e75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Av=C3=A9?= Date: Wed, 5 Nov 2025 15:50:17 +0700 Subject: [PATCH] Add error handling to notifications --- home/ags/files/notifications/Notification.tsx | 38 +++++++++++++++++-- .../notifications/NotificationPopups.tsx | 26 ++++++++++--- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/home/ags/files/notifications/Notification.tsx b/home/ags/files/notifications/Notification.tsx index 08a90cd..6312b07 100644 --- a/home/ags/files/notifications/Notification.tsx +++ b/home/ags/files/notifications/Notification.tsx @@ -39,8 +39,38 @@ export default function Notification({ onHoverLost: () => void; }) { const timer = timeout(3000, () => { - onHoverLost(); + try { + onHoverLost(); + } catch (error) { + console.error("Failed to handle onHoverLost:", error); + } }); + + const handleDismiss = () => { + try { + n.dismiss(); + } catch (error) { + console.error("Failed to dismiss notification:", error); + } + }; + + const handleInvoke = (id: string) => { + try { + n.invoke(id); + } catch (error) { + console.error(`Failed to invoke action ${id}:`, error); + } + }; + + const handleTime = (timeValue: number) => { + try { + return time(timeValue); + } catch (error) { + console.error("Failed to format time:", error); + return "Invalid time"; + } + }; + return ( - @@ -114,7 +144,7 @@ export default function Notification({ {n.actions.length > 0 && ( {n.actions.map(({ label, id }) => ( - ))} diff --git a/home/ags/files/notifications/NotificationPopups.tsx b/home/ags/files/notifications/NotificationPopups.tsx index 484b17d..f392ace 100644 --- a/home/ags/files/notifications/NotificationPopups.tsx +++ b/home/ags/files/notifications/NotificationPopups.tsx @@ -14,17 +14,31 @@ export default function NotificationPopups() { ); const notifiedHandler = notifd.connect("notified", (_, id, replaced) => { - const notification = notifd.get_notification(id); + try { + const notification = notifd.get_notification(id); + if (!notification) { + console.error(`Failed to get notification with id: ${id}`); + return; + } - if (replaced && notifications.get().some((n) => n.id === id)) { - setNotifications((ns) => ns.map((n) => (n.id === id ? notification : n))); - } else { - setNotifications((ns) => [notification, ...ns]); + if (replaced && notifications.get().some((n) => n.id === id)) { + setNotifications((ns) => + ns.map((n) => (n.id === id ? notification : n)), + ); + } else { + setNotifications((ns) => [notification, ...ns]); + } + } catch (error) { + console.error("Failed to handle notification:", error); } }); const resolvedHandler = notifd.connect("resolved", (_, id) => { - setNotifications((ns) => ns.filter((n) => n.id !== id)); + try { + setNotifications((ns) => ns.filter((n) => n.id !== id)); + } catch (error) { + console.error("Failed to resolve notification:", error); + } }); // technically, we don't need to cleanup because in this example this is a root component