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 })[]>([]) const addNotification = (newNotif: Notification) => { const { message, type, duration = 5000 } = newNotif const id = crypto.randomUUID() setNotifications((prev) => [...prev, { id, message, type, duration }]) if (duration > 0) { setTimeout(() => { removeNotification(id) }, duration) } } // 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)) } const removeAllNotifications = () => { setNotifications([]) } const Icon = ({ type }: { type: string }) => { switch (type) { case 'error': return case 'success': return case 'info': return default: return } } return ( {children} {notifications.map((notification) => ( removeNotification(notification.id)} > {notification.message} ))} ) } export default NotificationsProvider
{notification.message}