authelia/web/src/hooks/NotificationsContext.ts
dependabot-preview[bot] e6f4768961
[MISC] (deps): Bump react-scripts from 3.4.4 to 4.0.0 in /web (#1403)
Bumps [react-scripts](https://github.com/facebook/create-react-app/tree/HEAD/packages/react-scripts) from 3.4.4 to 4.0.0.
- [Release notes](https://github.com/facebook/create-react-app/releases)
- [Changelog](https://github.com/facebook/create-react-app/blob/master/CHANGELOG-3.x.md)
- [Commits](https://github.com/facebook/create-react-app/commits/react-scripts@4.0.0/packages/react-scripts)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Address CRA breaking changes

This is related to [breaking changes](https://github.com/facebook/create-react-app/blob/master/CHANGELOG.md#breaking-changes) in CRA specific to ESLint.

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
2020-11-07 13:06:18 +11:00

51 lines
1.7 KiB
TypeScript

import { Level } from "../components/ColoredSnackbarContent";
import { useCallback, createContext, useContext } from "react";
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);
/* eslint-disable react-hooks/exhaustive-deps */
const createInfoNotification = useCallback(notificationBuilder("info"), []);
const createSuccessNotification = useCallback(notificationBuilder("success"), []);
const createWarnNotification = useCallback(notificationBuilder("warning"), []);
const createErrorNotification = useCallback(notificationBuilder("error"), []);
/* eslint-enable react-hooks/exhaustive-deps */
const isActive = useNotificationsProps.notification !== null;
return {
notification: useNotificationsProps.notification,
resetNotification,
createInfoNotification,
createSuccessNotification,
createWarnNotification,
createErrorNotification,
isActive
}
}