-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add browser notification (#528)
- Loading branch information
Showing
11 changed files
with
169 additions
and
48 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useGlobalContext } from "../context/Global"; | ||
import { registerNotifications } from "../utils/notification"; | ||
|
||
const BrowserNotification = () => { | ||
const { browserNotification, setBrowserNotification, t, notify } = | ||
useGlobalContext(); | ||
|
||
const toggle = (evt: MouseEvent) => { | ||
// When disabled, we try to request permission and enable them | ||
if (!browserNotification()) { | ||
registerNotifications().then((state: boolean) => { | ||
setBrowserNotification(state); | ||
if (state === false) { | ||
notify("error", t("browsernotification_error")); | ||
} | ||
}); | ||
evt.stopPropagation(); | ||
return; | ||
} | ||
// When enabled, we disable sending them | ||
setBrowserNotification(false); | ||
evt.stopPropagation(); | ||
}; | ||
|
||
return ( | ||
<> | ||
<div | ||
class="browser-notification toggle" | ||
title={t("browsernotification_tooltip")} | ||
onClick={toggle}> | ||
<span class={browserNotification() ? "active" : ""}> | ||
{t("on")} | ||
</span> | ||
<span class={!browserNotification() ? "active" : ""}> | ||
{t("off")} | ||
</span> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default BrowserNotification; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import log from "loglevel"; | ||
|
||
export const registerNotifications = () => { | ||
return new Promise<boolean>((resolve) => { | ||
Notification.requestPermission().then((result) => { | ||
log.info("Notification permission: ", result); | ||
resolve(result === "granted"); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters