1
0
mirror of https://github.com/0rangebananaspy/authelia.git synced 2024-09-14 22:47:21 +07:00
authelia/web/src/hooks/NotificationsContext.ts
Amir Zarrinkafsh 6b7b08d800
refactor(web): replace incorrect use of usecallback ()
* refactor(web): replace incorrect use of usecallback

Replaces incorrect usage of useCallback with useRef.

* refactor(web): onsignin...ref -> onsignin...callback

* fix(web): fix lint errors
2021-09-04 22:31:24 +10:00

49 lines
1.6 KiB
TypeScript

import { createContext, useContext, useRef } from "react";
import { Level } from "@components/ColoredSnackbarContent";
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: Level) => {
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,
};
}