2022-01-06 01:26:15 +00:00
|
|
|
import React, { useState, useRef, useEffect, useContext } 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";
|
2022-04-28 20:57:57 +00:00
|
|
|
//import IconButton from "@material-ui/core/IconButton";
|
2022-01-06 01:26:15 +00:00
|
|
|
import List from "@material-ui/core/List";
|
|
|
|
import ListItem from "@material-ui/core/ListItem";
|
|
|
|
import ListItemText from "@material-ui/core/ListItemText";
|
|
|
|
import { makeStyles } from "@material-ui/core/styles";
|
2022-04-28 20:57:57 +00:00
|
|
|
//import Badge from "@material-ui/core/Badge";
|
|
|
|
//import ChatIcon from "@material-ui/icons/Chat";
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
import TicketListItem from "../TicketListItem";
|
|
|
|
import { i18n } from "../../translate/i18n";
|
|
|
|
import useTickets from "../../hooks/useTickets";
|
|
|
|
import alertSound from "../../assets/sound.mp3";
|
|
|
|
import { AuthContext } from "../../context/Auth/AuthContext";
|
|
|
|
|
|
|
|
const useStyles = makeStyles(theme => ({
|
|
|
|
tabContainer: {
|
|
|
|
overflowY: "auto",
|
|
|
|
maxHeight: 350,
|
|
|
|
...theme.scrollbarStyles,
|
|
|
|
},
|
|
|
|
popoverPaper: {
|
|
|
|
width: "100%",
|
|
|
|
maxWidth: 350,
|
|
|
|
marginLeft: theme.spacing(2),
|
|
|
|
marginRight: theme.spacing(1),
|
|
|
|
[theme.breakpoints.down("sm")]: {
|
|
|
|
maxWidth: 270,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
noShadow: {
|
|
|
|
boxShadow: "none !important",
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
|
|
|
|
let _fifo
|
|
|
|
|
|
|
|
// const onlineEmitter = async (socket, user) => {
|
|
|
|
|
|
|
|
// try {
|
|
|
|
// clearInterval(_fifo);
|
|
|
|
|
|
|
|
// socket.emit("online", user.id)
|
|
|
|
|
|
|
|
// } catch (error) {
|
|
|
|
// console.log('error on onlineEmitter: ', error)
|
|
|
|
// }
|
|
|
|
// finally {
|
|
|
|
// _fifo = setInterval(onlineEmitter, 3000);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// _fifo = setInterval(onlineEmitter, 3000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const NotificationsPopOver = () => {
|
|
|
|
const classes = useStyles();
|
|
|
|
|
|
|
|
const history = useHistory();
|
|
|
|
const { user } = useContext(AuthContext);
|
|
|
|
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();
|
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
const historyRef = useRef(history);
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
const { handleLogout } = useContext(AuthContext);
|
2022-05-25 20:19:38 +00:00
|
|
|
|
2022-07-14 23:00:35 +00:00
|
|
|
// const [lastRef] = useState(+history.location.pathname.split("/")[2])
|
2022-05-25 20:19:38 +00:00
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
|
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
useEffect(() => {
|
|
|
|
soundAlertRef.current = play;
|
|
|
|
|
|
|
|
if (!("Notification" in window)) {
|
2022-12-27 14:25:50 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
} else {
|
|
|
|
Notification.requestPermission();
|
|
|
|
}
|
|
|
|
}, [play]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setNotifications(tickets);
|
|
|
|
}, [tickets]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-05-25 20:19:38 +00:00
|
|
|
|
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
|
|
|
|
ticketIdRef.current = ticketIdUrl;
|
2022-01-06 01:26:15 +00:00
|
|
|
}, [ticketIdUrl]);
|
|
|
|
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2022-05-06 22:49:45 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
const socket = openSocket(process.env.REACT_APP_BACKEND_URL);
|
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
socket.on("reload_page", (data) => {
|
|
|
|
|
|
|
|
console.log('UPDATING THE PAGE: ', data.userId, ' | user.id: ', user.id)
|
|
|
|
|
|
|
|
if (user.id === data.userId) {
|
|
|
|
|
|
|
|
window.location.reload(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
socket.on("onlineStatus", (data) => {
|
|
|
|
|
|
|
|
if (data.action === "logout") {
|
2022-05-16 02:48:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
if (`${user.id}` === data.userOnlineTime['userId']) {
|
|
|
|
|
|
|
|
socket.emit("online", { logoutUserId: user.id })
|
2022-05-16 02:48:06 +00:00
|
|
|
handleLogout();
|
2022-12-27 14:25:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
|
|
|
|
});
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
// socket.on("isOnline", (data) => {
|
|
|
|
|
|
|
|
// if (data.action === "online") {
|
|
|
|
|
|
|
|
// if (data.userId === user.id) {
|
|
|
|
|
|
|
|
// socket.emit("online", user.id)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
// });
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
if (user.profile === 'user') {
|
2022-12-27 14:25:50 +00:00
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
if (_fifo) {
|
|
|
|
clearInterval(_fifo);
|
2022-12-27 14:25:50 +00:00
|
|
|
}
|
2023-02-07 15:47:40 +00:00
|
|
|
|
|
|
|
_fifo = setInterval(() => {
|
2022-12-27 14:25:50 +00:00
|
|
|
console.log('user.id: ', user.id)
|
2022-05-06 22:49:45 +00:00
|
|
|
socket.emit("online", user.id)
|
2022-12-27 14:25:50 +00:00
|
|
|
}, 3000);
|
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 22:49:45 +00:00
|
|
|
|
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-05-06 22:49:45 +00:00
|
|
|
return () => {
|
2022-12-27 14:25:50 +00:00
|
|
|
socket.disconnect();
|
2022-05-06 22:49:45 +00:00
|
|
|
};
|
2023-02-07 15:47:40 +00:00
|
|
|
}, [user.id, handleLogout, user.profile]);
|
2022-05-06 22:49:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const socket = openSocket(process.env.REACT_APP_BACKEND_URL);
|
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
socket.on("connect", () => socket.emit("joinNotification"));
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
socket.on("ticket", data => {
|
|
|
|
if (data.action === "updateUnread" || data.action === "delete") {
|
2022-05-25 20:19:38 +00:00
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
|
2022-05-25 20:19:38 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
setNotifications(prevState => {
|
|
|
|
const ticketIndex = prevState.findIndex(t => t.id === data.ticketId);
|
|
|
|
if (ticketIndex !== -1) {
|
|
|
|
prevState.splice(ticketIndex, 1);
|
|
|
|
return [...prevState];
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("appMessage", data => {
|
2022-07-14 23:00:35 +00:00
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
|
2022-07-14 23:00:35 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
if (
|
|
|
|
data.action === "create" &&
|
|
|
|
!data.message.read &&
|
|
|
|
(data.ticket.userId === user?.id || !data.ticket.userId)
|
|
|
|
) {
|
2022-04-28 20:57:57 +00:00
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
|
|
|
|
|
2022-04-28 20:57:57 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
setNotifications(prevState => {
|
2022-05-25 20:19:38 +00:00
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
|
2022-05-25 20:19:38 +00:00
|
|
|
|
|
|
|
// prevState.forEach((e)=>{
|
2022-10-25 14:16:36 +00:00
|
|
|
//
|
2022-05-25 20:19:38 +00:00
|
|
|
// })
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const ticketIndex = prevState.findIndex(t => t.id === data.ticket.id);
|
|
|
|
if (ticketIndex !== -1) {
|
2022-12-27 14:25:50 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
prevState[ticketIndex] = data.ticket;
|
|
|
|
return [...prevState];
|
|
|
|
}
|
2022-12-27 14:25:50 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
return [data.ticket, ...prevState];
|
2022-12-27 14:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2022-05-25 20:19:38 +00:00
|
|
|
const shouldNotNotificate = (data.message.ticketId === ticketIdRef.current && document.visibilityState === "visible") ||
|
2022-12-27 14:25:50 +00:00
|
|
|
(data.ticket.userId && data.ticket.userId !== user?.id) ||
|
2022-04-28 20:57:57 +00:00
|
|
|
data.ticket.isGroup || !data.ticket.userId;
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
if (shouldNotNotificate) return;
|
|
|
|
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
handleNotifications(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
socket.disconnect();
|
|
|
|
};
|
|
|
|
}, [user]);
|
|
|
|
|
|
|
|
const handleNotifications = data => {
|
|
|
|
const { message, contact, ticket } = data;
|
2022-12-27 14:25:50 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
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();
|
|
|
|
historyRef.current.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();
|
|
|
|
};
|
|
|
|
|
2022-04-28 20:57:57 +00:00
|
|
|
// const handleClick = () => {
|
|
|
|
// setIsOpen(prevState => !prevState);
|
|
|
|
// };
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const handleClickAway = () => {
|
|
|
|
setIsOpen(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
const NotificationTicket = ({ children }) => {
|
|
|
|
return <div onClick={handleClickAway}>{children}</div>;
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-04-28 20:57:57 +00:00
|
|
|
{/* <IconButton
|
2022-01-06 01:26:15 +00:00
|
|
|
onClick={handleClick}
|
2022-02-27 20:05:21 +00:00
|
|
|
// buttonRef={anchorEl}
|
|
|
|
ref={anchorEl}
|
2022-01-06 01:26:15 +00:00
|
|
|
aria-label="Open Notifications"
|
|
|
|
color="inherit"
|
|
|
|
>
|
|
|
|
<Badge badgeContent={notifications.length} color="secondary">
|
|
|
|
<ChatIcon />
|
|
|
|
</Badge>
|
2022-04-28 20:57:57 +00:00
|
|
|
</IconButton> */}
|
2022-01-06 01:26:15 +00:00
|
|
|
<Popover
|
|
|
|
disableScrollLock
|
|
|
|
open={isOpen}
|
|
|
|
anchorEl={anchorEl.current}
|
|
|
|
anchorOrigin={{
|
|
|
|
vertical: "bottom",
|
|
|
|
horizontal: "right",
|
|
|
|
}}
|
|
|
|
transformOrigin={{
|
|
|
|
vertical: "top",
|
|
|
|
horizontal: "right",
|
|
|
|
}}
|
|
|
|
classes={{ paper: classes.popoverPaper }}
|
|
|
|
onClose={handleClickAway}
|
|
|
|
>
|
|
|
|
<List dense className={classes.tabContainer}>
|
2022-12-27 14:25:50 +00:00
|
|
|
{notifications.length === 0 ? (
|
|
|
|
<ListItem>
|
2022-01-06 01:26:15 +00:00
|
|
|
<ListItemText>{i18n.t("notifications.noTickets")}</ListItemText>
|
|
|
|
</ListItem>
|
2022-12-27 14:25:50 +00:00
|
|
|
) : (
|
2022-05-25 20:19:38 +00:00
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
notifications.map(ticket => (
|
2022-01-06 01:26:15 +00:00
|
|
|
<NotificationTicket key={ticket.id}>
|
|
|
|
<TicketListItem ticket={ticket} />
|
|
|
|
</NotificationTicket>
|
|
|
|
))
|
|
|
|
)}
|
|
|
|
</List>
|
2022-05-25 20:19:38 +00:00
|
|
|
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
</Popover>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default NotificationsPopOver;
|