mirror of
https://github.com/0rangebananaspy/authelia.git
synced 2024-09-14 22:47:21 +07:00
50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
import { createContext, useContext, useRef } from "react";
|
|
|
|
import { AlertColor } from "@mui/material";
|
|
|
|
import { Notification } from "@models/Notifications";
|
|
|
|
const defaultOptions = {
|
|
timeout: 5,
|
|
};
|
|
|
|
interface NotificationContextProps {
|
|
notification: Notification | null;
|
|
setNotification: (n: Notification | null) => void;
|
|
}
|
|
|
|
const NotificationsContext = createContext<NotificationContextProps>({ notification: null, setNotification: () => {} });
|
|
|
|
export default NotificationsContext;
|
|
|
|
export function useNotifications() {
|
|
let useNotificationsProps = useContext(NotificationsContext);
|
|
|
|
const notificationBuilder = (level: AlertColor) => {
|
|
return (message: string, timeout?: number) => {
|
|
useNotificationsProps.setNotification({
|
|
level,
|
|
message,
|
|
timeout: timeout ? timeout : defaultOptions.timeout,
|
|
});
|
|
};
|
|
};
|
|
|
|
const resetNotification = () => useNotificationsProps.setNotification(null);
|
|
const createInfoNotification = useRef(notificationBuilder("info")).current;
|
|
const createSuccessNotification = useRef(notificationBuilder("success")).current;
|
|
const createWarnNotification = useRef(notificationBuilder("warning")).current;
|
|
const createErrorNotification = useRef(notificationBuilder("error")).current;
|
|
const isActive = useNotificationsProps.notification !== null;
|
|
|
|
return {
|
|
notification: useNotificationsProps.notification,
|
|
resetNotification,
|
|
createInfoNotification,
|
|
createSuccessNotification,
|
|
createWarnNotification,
|
|
createErrorNotification,
|
|
isActive,
|
|
};
|
|
}
|