Skip to content

Project NOMAD is an offline-first knowledge and education server. Wikipedia, thousands of books, courses, maps, and optional local AI, all running on hardware you own with no internet required.

TypeScript

feat: improve global error reporting with user notifs

Jake Turnerfcc749ec571d
diff --git a/admin/inertia/lib/util.ts b/admin/inertia/lib/util.ts
index baad6a63..89246818 100644
--- a/admin/inertia/lib/util.ts
+++ b/admin/inertia/lib/util.ts
@@ -1,3 +1,11 @@
+import { Notification } from "~/context/NotificationContext"
+
+// Global notification callback that can be set by the NotificationProvider
+let globalNotificationCallback: ((notification: Notification) => void) | null = null
+
+export function setGlobalNotificationCallback(callback: (notification: Notification) => void) {
+ globalNotificationCallback = callback
+}
export function capitalizeFirstLetter(str?: string | null): string {
if (!str) return ''
@@ -68,6 +76,16 @@ export function catchInternal<Fn extends (...args: any[]) => any>(fn: Fn): (...a
return await fn(...args)
} catch (error) {
console.error('Internal error caught:', error)
+
+ if (globalNotificationCallback) {
+ const errorMessage = 'An internal error occurred. Please try again or check the console for details. ' + (error instanceof Error ? String(error.message).slice(0, 50) : '')
+ globalNotificationCallback({
+ message: errorMessage,
+ type: 'error',
+ duration: 5000
+ })
+ }
+
return undefined
}
}
diff --git a/admin/inertia/providers/NotificationProvider.tsx b/admin/inertia/providers/NotificationProvider.tsx
index 4a7763a3..1c4f4090 100644
--- a/admin/inertia/providers/NotificationProvider.tsx
+++ b/admin/inertia/providers/NotificationProvider.tsx
@@ -1,6 +1,7 @@
-import { useState } from 'react'
+import { useState, useEffect } from 'react'
import { NotificationContext, Notification } from '../context/NotificationContext'
import { IconExclamationCircle, IconCircleCheck, IconInfoCircle } from '@tabler/icons-react'
+import { setGlobalNotificationCallback } from '~/lib/util'
const NotificationsProvider = ({ children }: { children: React.ReactNode }) => {
const [notifications, setNotifications] = useState<(Notification & { id: string })[]>([])
@@ -17,6 +18,14 @@ const NotificationsProvider = ({ children }: { children: React.ReactNode }) => {
}
}
+ // Set the global notification callback when provider mounts
+ useEffect(() => {
+ setGlobalNotificationCallback(addNotification)
+ return () => {
+ setGlobalNotificationCallback(() => {})
+ }
+ }, [])
+
const removeNotification = (id: string) => {
setNotifications((prev) => prev.filter((n) => n.id !== id))
}