mirror of
https://github.com/Crosstalk-Solutions/project-nomad.git
synced 2026-04-03 07:19:27 +02: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 {
|
export function capitalizeFirstLetter(str?: string | null): string {
|
||||||
if (!str) return ''
|
if (!str) return ''
|
||||||
|
|
@ -68,6 +76,16 @@ export function catchInternal<Fn extends (...args: any[]) => any>(fn: Fn): (...a
|
||||||
return await fn(...args)
|
return await fn(...args)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Internal error caught:', 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
|
return undefined
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import { useState } from 'react'
|
import { useState, useEffect } from 'react'
|
||||||
import { NotificationContext, Notification } from '../context/NotificationContext'
|
import { NotificationContext, Notification } from '../context/NotificationContext'
|
||||||
import { IconExclamationCircle, IconCircleCheck, IconInfoCircle } from '@tabler/icons-react'
|
import { IconExclamationCircle, IconCircleCheck, IconInfoCircle } from '@tabler/icons-react'
|
||||||
|
import { setGlobalNotificationCallback } from '~/lib/util'
|
||||||
|
|
||||||
const NotificationsProvider = ({ children }: { children: React.ReactNode }) => {
|
const NotificationsProvider = ({ children }: { children: React.ReactNode }) => {
|
||||||
const [notifications, setNotifications] = useState<(Notification & { id: string })[]>([])
|
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) => {
|
const removeNotification = (id: string) => {
|
||||||
setNotifications((prev) => prev.filter((n) => n.id !== id))
|
setNotifications((prev) => prev.filter((n) => n.id !== id))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user