Notification.tsx 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright 2021-2024 Avaiga Private Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  5. * the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. */
  13. import React, { useCallback, useEffect, useMemo } from "react";
  14. import { SnackbarKey, useSnackbar, VariantType } from "notistack";
  15. import IconButton from "@mui/material/IconButton";
  16. import CloseIcon from "@mui/icons-material/Close";
  17. import { NotificationMessage, createDeleteAlertAction } from "../../context/taipyReducers";
  18. import { useDispatch } from "../../utils/hooks";
  19. interface NotificationProps {
  20. notifications: NotificationMessage[];
  21. }
  22. const TaipyNotification = ({ notifications }: NotificationProps) => {
  23. const notification = notifications.length ? notifications[0] : undefined;
  24. const { enqueueSnackbar, closeSnackbar } = useSnackbar();
  25. const dispatch = useDispatch();
  26. const resetNotification = useCallback(
  27. (key: SnackbarKey) => () => {
  28. closeSnackbar(key);
  29. },
  30. [closeSnackbar]
  31. );
  32. const notificationAction = useCallback(
  33. (key: SnackbarKey) => (
  34. <IconButton size="small" aria-label="close" color="inherit" onClick={resetNotification(key)}>
  35. <CloseIcon fontSize="small" />
  36. </IconButton>
  37. ),
  38. [resetNotification]
  39. );
  40. const faviconUrl = useMemo(() => {
  41. const nodeList = document.getElementsByTagName("link");
  42. for (let i = 0; i < nodeList.length; i++) {
  43. if (nodeList[i].getAttribute("rel") == "icon" || nodeList[i].getAttribute("rel") == "shortcut icon") {
  44. return nodeList[i].getAttribute("href") || "/favicon.png";
  45. }
  46. }
  47. return "/favicon.png";
  48. }, []);
  49. useEffect(() => {
  50. if (notification) {
  51. const notificationId = notification.notificationId || "";
  52. if (notification.atype === "") {
  53. closeSnackbar(notificationId);
  54. } else {
  55. enqueueSnackbar(notification.message, {
  56. variant: notification.atype as VariantType,
  57. action: notificationAction,
  58. autoHideDuration: notification.duration,
  59. key: notificationId,
  60. });
  61. notification.system &&
  62. new Notification(document.title || "Taipy", { body: notification.message, icon: faviconUrl });
  63. }
  64. dispatch(createDeleteAlertAction(notificationId));
  65. }
  66. }, [notification, enqueueSnackbar, closeSnackbar, notificationAction, faviconUrl, dispatch]);
  67. useEffect(() => {
  68. notification?.system && window.Notification && Notification.requestPermission();
  69. }, [notification?.system]);
  70. return null;
  71. };
  72. export default TaipyNotification;