diff --git a/admin/inertia/lib/util.ts b/admin/inertia/lib/util.ts index baad6a6..8924681 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 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 4a7763a..1c4f409 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)) }