feat: use web badge API to show notifications/follow requests (#2005)

* feat: use web badge API to show notifications/follow requests

Fixes #1900

* fix: change detection logic

* fix: add UA check

* fix: tweak
This commit is contained in:
Nolan Lawson 2021-03-19 08:00:59 -07:00 committed by GitHub
parent 66cfc342f0
commit 75458a3410
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 0 deletions

View File

@ -25,4 +25,9 @@ export function badgeComputations (store) {
['numberOfFollowRequests'],
(numberOfFollowRequests) => !!numberOfFollowRequests
)
store.compute('badgeNumber',
['numberOfFollowRequests', 'numberOfNotifications'],
(numberOfFollowRequests, numberOfNotifications) => (numberOfFollowRequests + numberOfNotifications)
)
}

View File

@ -0,0 +1,19 @@
import { store } from '../store'
import { isChromePre87 } from '../../_utils/userAgent/isChromePre87'
export function badgeObservers () {
if (!process.browser) {
return
}
// Chrome 86 on Linux in Circle CI seems to hang just by checking for this... not worth supporting.
if (isChromePre87() || !('setAppBadge' in navigator)) {
return
}
store.observe('badgeNumber', badgeNumber => {
if (badgeNumber) {
navigator.setAppBadge(badgeNumber)
} else {
navigator.clearAppBadge()
}
})
}

View File

@ -8,6 +8,7 @@ import { customEmojiObservers } from './customEmojiObservers'
import { cleanup } from './cleanup'
import { wordFilterObservers } from './wordFilterObservers'
import { showShareDialogObservers } from './showShareDialogObservers'
import { badgeObservers } from './badgeObservers'
// These observers can be lazy-loaded when the user is actually logged in.
// Prevents circular dependencies and reduces the size of main.js
@ -21,5 +22,6 @@ export function loggedInObservers () {
customScrollbarObservers()
customEmojiObservers()
showShareDialogObservers()
badgeObservers()
cleanup()
}

View File

@ -0,0 +1,5 @@
import { isChrome } from './isChrome'
import { thunk } from '../thunk'
// https://caniuse.com/cookie-store-api
export const isChromePre87 = thunk(() => (process.browser && isChrome() && typeof cookieStore === 'undefined'))