From 5e769e5f8e4a042ef00cd59f3f2c5ed5ab41bda1 Mon Sep 17 00:00:00 2001 From: canove Date: Sun, 18 Oct 2020 12:04:28 -0300 Subject: [PATCH] fix: notifications not working sometimes --- frontend/package.json | 1 + .../components/NotificationsPopOver/index.js | 84 +++++++++++++------ 2 files changed, 58 insertions(+), 27 deletions(-) diff --git a/frontend/package.json b/frontend/package.json index 32dd972..0908c23 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -26,6 +26,7 @@ "react-toastify": "^6.0.9", "recharts": "^1.8.5", "socket.io-client": "^2.3.1", + "use-sound": "^1.0.2", "yup": "^0.29.3" }, "scripts": { diff --git a/frontend/src/components/NotificationsPopOver/index.js b/frontend/src/components/NotificationsPopOver/index.js index c4ca8cb..38aa822 100644 --- a/frontend/src/components/NotificationsPopOver/index.js +++ b/frontend/src/components/NotificationsPopOver/index.js @@ -3,6 +3,7 @@ import React, { useState, useRef, useEffect } from "react"; import { useHistory } from "react-router-dom"; import { format } from "date-fns"; import openSocket from "socket.io-client"; +import useSound from "use-sound"; import Popover from "@material-ui/core/Popover"; import IconButton from "@material-ui/core/IconButton"; @@ -16,6 +17,7 @@ import ChatIcon from "@material-ui/icons/Chat"; import TicketListItem from "../TicketListItem"; import { i18n } from "../../translate/i18n"; import useTickets from "../../hooks/useTickets"; +import alertSound from "../../assets/sound.mp3"; const useStyles = makeStyles(theme => ({ tabContainer: { @@ -42,22 +44,27 @@ const NotificationsPopOver = () => { const history = useHistory(); const userId = +localStorage.getItem("userId"); - const soundAlert = useRef(new Audio(require("../../assets/sound.mp3"))); const ticketIdUrl = +history.location.pathname.split("/")[2]; const ticketIdRef = useRef(ticketIdUrl); const anchorEl = useRef(); const [isOpen, setIsOpen] = useState(false); const [notifications, setNotifications] = useState([]); + const [, setDesktopNotifications] = useState([]); + const { tickets } = useTickets({ withUnreadMessages: "true" }); + const [play] = useSound(alertSound); + const soundAlertRef = useRef(); useEffect(() => { + soundAlertRef.current = play; + if (!("Notification" in window)) { console.log("This browser doesn't support notifications"); } else { Notification.requestPermission(); } - }, []); + }, [play]); useEffect(() => { setNotifications(tickets); @@ -82,6 +89,18 @@ const NotificationsPopOver = () => { } return prevState; }); + + setDesktopNotifications(prevState => { + const notfiticationIndex = prevState.findIndex( + n => n.tag === String(data.ticketId) + ); + if (notfiticationIndex !== -1) { + prevState[notfiticationIndex].close(); + prevState.splice(notfiticationIndex, 1); + return [...prevState]; + } + return prevState; + }); } }); @@ -108,31 +127,7 @@ const NotificationsPopOver = () => { if (shouldNotNotificate) return; - // show desktop notification - const { message, contact, ticket } = data; - const options = { - body: `${message.body} - ${format(new Date(), "HH:mm")}`, - icon: contact.profilePicUrl, - tag: ticket.id, - }; - let notification = new Notification( - `${i18n.t("tickets.notification.message")} ${contact.name}`, - options - ); - - notification.onclick = function (event) { - event.preventDefault(); // - window.focus(); - history.push(`/tickets/${ticket.id}`); - }; - - document.addEventListener("visibilitychange", () => { - if (document.visibilityState === "visible") { - notification.close(); - } - }); - - soundAlert.current.play(); + handleNotifications(data, history); } }); @@ -141,6 +136,41 @@ const NotificationsPopOver = () => { }; }, [history, userId]); + const handleNotifications = (data, history) => { + const { message, contact, ticket } = data; + + const options = { + body: `${message.body} - ${format(new Date(), "HH:mm")}`, + icon: contact.profilePicUrl, + tag: ticket.id, + renotify: true, + }; + + const notification = new Notification( + `${i18n.t("tickets.notification.message")} ${contact.name}`, + options + ); + + notification.onclick = e => { + e.preventDefault(); + window.focus(); + history.push(`/tickets/${ticket.id}`); + }; + + setDesktopNotifications(prevState => { + const notfiticationIndex = prevState.findIndex( + n => n.tag === notification.tag + ); + if (notfiticationIndex !== -1) { + prevState[notfiticationIndex] = notification; + return [...prevState]; + } + return [notification, ...prevState]; + }); + + soundAlertRef.current(); + }; + const handleClick = () => { setIsOpen(prevState => !prevState); };