This commit is contained in:
Matthias Hochmeister
2026-02-23 17:08:58 +01:00
commit f09748f4a1
97 changed files with 17729 additions and 0 deletions

View 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;
};