Skip to content
Snippets Groups Projects
Unverified Commit 22fdf235 authored by Anton Kulyk's avatar Anton Kulyk Committed by GitHub
Browse files

Handle not available Notifications API (#22366)

parent 8d5f3dd3
No related branches found
No related tags found
No related merge requests found
......@@ -106,7 +106,7 @@ const DashboardApp = props => {
const [isShowingToaster, setIsShowingToaster] = useState(false);
const onTimeout = useCallback(() => {
if (Notification.permission === "default") {
if ("Notification" in window && Notification.permission === "default") {
setIsShowingToaster(true);
}
}, []);
......@@ -123,7 +123,11 @@ const DashboardApp = props => {
useEffect(() => {
if (isLoadingComplete) {
setIsShowingToaster(false);
if (Notification.permission === "granted" && document.hidden) {
if (
"Notification" in window &&
Notification.permission === "granted" &&
document.hidden
) {
showNotification(
t`All Set! ${dashboard?.name} is ready.`,
t`All questions loaded`,
......
import React, { useCallback } from "react";
import { useCallback } from "react";
const hasNotificationAPI = "Notification" in window;
export function useWebNotification() {
const requestPermission = useCallback(async () => {
if (!hasNotificationAPI) {
return "denied";
}
const permission = await Notification.requestPermission();
return permission;
}, []);
const showNotification = useCallback((title: string, body: string) => {
if (!hasNotificationAPI) {
return;
}
const notification = new Notification(title, {
body,
icon: "app/assets/img/favicon-32x32.png",
......
......@@ -373,7 +373,7 @@ function QueryBuilder(props) {
const { isRunning } = uiControls;
const onTimeout = useCallback(() => {
if (Notification.permission === "default") {
if ("Notification" in window && Notification.permission === "default") {
setIsShowingToaster(true);
}
}, []);
......@@ -389,7 +389,11 @@ function QueryBuilder(props) {
if (isLoadingComplete) {
setIsShowingToaster(false);
if (Notification.permission === "granted" && document.hidden) {
if (
"Notification" in window &&
Notification.permission === "granted" &&
document.hidden
) {
showNotification(
t`All Set! Your question is ready.`,
t`${card.name} is loaded.`,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment