import { Alert, Snackbar, useTheme } from "@mui/material"; import { FC, ReactNode, createContext, useContext, useState } from "react"; import { v4 } from "uuid"; type Notification = { id: string; message: string; color?: string; duration?: number; }; type NotificationContextProps = { notifications: Notification[]; openNotification: (message: string, color?: string, duration?: number) => void; closeNotification: (id: string) => void; }; const NotificationContext = createContext(undefined); export const NotificationProvider: FC<{ children: ReactNode }> = ({ children }) => { const theme = useTheme(); const [notifications, setNotifications] = useState([]); const openNotification = (message: string, color?: string, duration: number = 3000) => { const id = v4(); setNotifications((prevNotifications) => [...prevNotifications, { id, message, color, duration }]); setTimeout(() => { closeNotification(id); }, duration); }; const closeNotification = (id: string) => { setNotifications((prevNotifications) => prevNotifications.filter((notification) => notification.id !== id)); }; return ( {children} {notifications.map((notification) => ( {notification.message} ))} ); }; export const useNotification = () => { const context = useContext(NotificationContext); if (context === undefined) { throw new Error("useNotification must be used within a NotificationProvider"); } return context; };