fix: notification redirect

This commit is contained in:
Tristan Yang
2022-10-25 10:33:17 +08:00
parent ea98e2a308
commit 82cdd6a66d
+15 -15
View File
@@ -1,12 +1,12 @@
import { useEffect, memo } from "react"; import { useEffect, memo } from "react";
import { useNavigate } from "react-router-dom"; // import { useNavigate } from "react-router-dom";
import useDeviceToken from "./useDeviceToken"; import useDeviceToken from "./useDeviceToken";
import { vapidKey } from "../../../app/config"; import { vapidKey } from "../../../app/config";
import { useUpdateDeviceTokenMutation } from "../../../app/services/auth"; import { useUpdateDeviceTokenMutation } from "../../../app/services/auth";
let updated = false; let updated = false;
let updating = false; let updating = false;
const Notification = () => { const Notification = () => {
const navigateTo = useNavigate(); // const navigateTo = useNavigate();
const token = useDeviceToken(vapidKey); const token = useDeviceToken(vapidKey);
const [updateDeviceToken] = useUpdateDeviceTokenMutation(); const [updateDeviceToken] = useUpdateDeviceTokenMutation();
useEffect(() => { useEffect(() => {
@@ -24,19 +24,19 @@ const Notification = () => {
updateToken(token as string); updateToken(token as string);
}, [token]); }, [token]);
useEffect(() => { // useEffect(() => {
const handleServiceWorkerMessage = (event: MessageEvent) => { // const handleServiceWorkerMessage = (event: MessageEvent) => {
const { newPath } = event.data; // const { newPath } = event.data;
if (newPath) { // if (newPath && document.hidden) {
navigateTo(newPath); // navigateTo(newPath);
} // }
}; // };
// https only // // https only
navigator.serviceWorker?.addEventListener("message", handleServiceWorkerMessage); // navigator.serviceWorker?.addEventListener("message", handleServiceWorkerMessage);
return () => { // return () => {
navigator.serviceWorker?.removeEventListener("message", handleServiceWorkerMessage); // navigator.serviceWorker?.removeEventListener("message", handleServiceWorkerMessage);
}; // };
}, []); // }, []);
return null; return null;
}; };