Notification.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 { AlertMessage, createDeleteAlertAction } from "../../context/taipyReducers";
  18. import { useDispatch } from "../../utils/hooks";
  19. interface NotificationProps {
  20. alerts: AlertMessage[];
  21. }
  22. const TaipyNotification = ({ alerts }: NotificationProps) => {
  23. const alert = alerts.length ? alerts[0] : undefined;
  24. const { enqueueSnackbar, closeSnackbar } = useSnackbar();
  25. const dispatch = useDispatch();
  26. const resetAlert = useCallback(
  27. (key: SnackbarKey) => () => {
  28. closeSnackbar(key);
  29. },
  30. [closeSnackbar]
  31. );
  32. const notifAction = useCallback(
  33. (key: SnackbarKey) => (
  34. <IconButton size="small" aria-label="close" color="inherit" onClick={resetAlert(key)}>
  35. <CloseIcon fontSize="small" />
  36. </IconButton>
  37. ),
  38. [resetAlert]
  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 (alert) {
  51. const notificationId = alert.notificationId || "";
  52. if (alert.atype === "") {
  53. closeSnackbar(notificationId);
  54. } else {
  55. enqueueSnackbar(alert.message, {
  56. variant: alert.atype as VariantType,
  57. action: notifAction,
  58. autoHideDuration: alert.duration,
  59. key: notificationId,
  60. });
  61. alert.system && new Notification(document.title || "Taipy", { body: alert.message, icon: faviconUrl });
  62. }
  63. dispatch(createDeleteAlertAction(notificationId));
  64. }
  65. }, [alert, enqueueSnackbar, closeSnackbar, notifAction, faviconUrl, dispatch]);
  66. useEffect(() => {
  67. alert?.system && window.Notification && Notification.requestPermission();
  68. }, [alert?.system]);
  69. return null;
  70. };
  71. export default TaipyNotification;