inital
This commit is contained in:
109
frontend/src/contexts/NotificationContext.tsx
Normal file
109
frontend/src/contexts/NotificationContext.tsx
Normal file
@@ -0,0 +1,109 @@
|
||||
import React, { createContext, useContext, useState, ReactNode, useCallback } from 'react';
|
||||
import { Snackbar, Alert, AlertColor } from '@mui/material';
|
||||
|
||||
interface Notification {
|
||||
id: number;
|
||||
message: string;
|
||||
severity: AlertColor;
|
||||
}
|
||||
|
||||
interface NotificationContextType {
|
||||
showSuccess: (message: string) => void;
|
||||
showError: (message: string) => void;
|
||||
showWarning: (message: string) => void;
|
||||
showInfo: (message: string) => void;
|
||||
}
|
||||
|
||||
const NotificationContext = createContext<NotificationContextType | undefined>(undefined);
|
||||
|
||||
interface NotificationProviderProps {
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
export const NotificationProvider: React.FC<NotificationProviderProps> = ({ children }) => {
|
||||
const [notifications, setNotifications] = useState<Notification[]>([]);
|
||||
const [currentNotification, setCurrentNotification] = useState<Notification | null>(null);
|
||||
|
||||
const addNotification = useCallback((message: string, severity: AlertColor) => {
|
||||
const id = Date.now();
|
||||
const notification: Notification = { id, message, severity };
|
||||
|
||||
setNotifications((prev) => [...prev, notification]);
|
||||
|
||||
// If no notification is currently displayed, show this one immediately
|
||||
if (!currentNotification) {
|
||||
setCurrentNotification(notification);
|
||||
}
|
||||
}, [currentNotification]);
|
||||
|
||||
const showSuccess = useCallback((message: string) => {
|
||||
addNotification(message, 'success');
|
||||
}, [addNotification]);
|
||||
|
||||
const showError = useCallback((message: string) => {
|
||||
addNotification(message, 'error');
|
||||
}, [addNotification]);
|
||||
|
||||
const showWarning = useCallback((message: string) => {
|
||||
addNotification(message, 'warning');
|
||||
}, [addNotification]);
|
||||
|
||||
const showInfo = useCallback((message: string) => {
|
||||
addNotification(message, 'info');
|
||||
}, [addNotification]);
|
||||
|
||||
const handleClose = (_event?: React.SyntheticEvent | Event, reason?: string) => {
|
||||
if (reason === 'clickaway') {
|
||||
return;
|
||||
}
|
||||
|
||||
setCurrentNotification(null);
|
||||
|
||||
// Show next notification after a short delay
|
||||
setTimeout(() => {
|
||||
setNotifications((prev) => {
|
||||
const remaining = prev.filter((n) => n.id !== currentNotification?.id);
|
||||
if (remaining.length > 0) {
|
||||
setCurrentNotification(remaining[0]);
|
||||
}
|
||||
return remaining;
|
||||
});
|
||||
}, 200);
|
||||
};
|
||||
|
||||
const value: NotificationContextType = {
|
||||
showSuccess,
|
||||
showError,
|
||||
showWarning,
|
||||
showInfo,
|
||||
};
|
||||
|
||||
return (
|
||||
<NotificationContext.Provider value={value}>
|
||||
{children}
|
||||
<Snackbar
|
||||
open={currentNotification !== null}
|
||||
autoHideDuration={6000}
|
||||
onClose={handleClose}
|
||||
anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }}
|
||||
>
|
||||
<Alert
|
||||
onClose={handleClose}
|
||||
severity={currentNotification?.severity || 'info'}
|
||||
variant="filled"
|
||||
sx={{ width: '100%' }}
|
||||
>
|
||||
{currentNotification?.message}
|
||||
</Alert>
|
||||
</Snackbar>
|
||||
</NotificationContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useNotification = (): NotificationContextType => {
|
||||
const context = useContext(NotificationContext);
|
||||
if (context === undefined) {
|
||||
throw new Error('useNotification must be used within a NotificationProvider');
|
||||
}
|
||||
return context;
|
||||
};
|
||||
Reference in New Issue
Block a user