mirror of
https://github.com/Crosstalk-Solutions/project-nomad.git
synced 2026-03-28 03:29:25 +01:00
feat: improve global error reporting with user notifs
This commit is contained in:
parent
52e90041f4
commit
fcc749ec57
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user