-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathindex.website.js
30 lines (27 loc) · 1.14 KB
/
index.website.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Web browsers have a tab title and favicon which can be updated to show there are unread comments
*/
import CONFIG from '../../../CONFIG';
let unreadTotalCount = 0;
/**
* Set the page title on web
*
* @param {Number} totalCount
*/
function updateUnread(totalCount) {
const hasUnread = totalCount !== 0;
unreadTotalCount = totalCount;
// This setTimeout is required because due to how react rendering messes with the DOM, the document title can't be modified synchronously, and we must wait until all JS is done
// running before setting the title.
setTimeout(() => {
// There is a Chrome browser bug that causes the title to revert back to the previous when we are navigating back. Setting the title to an empty string
// seems to improve this issue.
document.title = '';
document.title = hasUnread ? `(${totalCount}) ${CONFIG.SITE_TITLE}` : CONFIG.SITE_TITLE;
document.getElementById('favicon').href = hasUnread ? CONFIG.FAVICON.UNREAD : CONFIG.FAVICON.DEFAULT;
}, 0);
}
window.addEventListener('popstate', () => {
updateUnread(unreadTotalCount);
});
export default updateUnread;