From 63135594093dd3f23cf58dfcf414c741db300b21 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 15 Oct 2021 12:42:42 -0500 Subject: [PATCH] Favicon unread badge: improve behavior & style --- app/soapbox/components/helmet.js | 30 +++++++++++++++++++++------- app/soapbox/main.js | 4 ---- app/soapbox/utils/favicon_service.js | 4 ++-- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/app/soapbox/components/helmet.js b/app/soapbox/components/helmet.js index a677ce2bf..266baf0b8 100644 --- a/app/soapbox/components/helmet.js +++ b/app/soapbox/components/helmet.js @@ -7,6 +7,8 @@ import { getSettings } from 'soapbox/actions/settings'; import sourceCode from 'soapbox/utils/code'; import FaviconService from 'soapbox/utils/favicon_service'; +FaviconService.initFaviconService(); + const getNotifTotals = state => { const notifications = state.getIn(['notifications', 'unread'], 0); const chats = state.get('chats').reduce((acc, curr) => acc + Math.min(curr.get('unread', 0), 1), 0); @@ -34,16 +36,30 @@ class SoapboxHelmet extends React.Component { demetricator: PropTypes.bool, }; - addCounter = title => { + hasUnread = () => { const { unreadCount, demetricator } = this.props; + return !(unreadCount < 1 || demetricator); + } - if (unreadCount < 1 || demetricator) { - // Erase badge when there are no notifications - FaviconService.clearFaviconBadge(); - return title; - } else { + addCounter = title => { + const { unreadCount } = this.props; + const hasUnread = this.hasUnread(); + return hasUnread ? `(${unreadCount}) ${title}` : title; + } + + updateFaviconBadge = () => { + const hasUnread = this.hasUnread(); + + if (hasUnread) { FaviconService.drawFaviconBadge(); - return `(${unreadCount}) ${title}`; + } else { + FaviconService.clearFaviconBadge(); + } + } + + componentDidUpdate(prevProps) { + if (this.props.unreadCount !== prevProps.unreadCount || this.props.demetricator !== prevProps.demetricator) { + this.updateFaviconBadge(); } } diff --git a/app/soapbox/main.js b/app/soapbox/main.js index 95e1b13fc..91822efef 100644 --- a/app/soapbox/main.js +++ b/app/soapbox/main.js @@ -12,7 +12,6 @@ import * as perf from './performance'; import * as monitoring from './monitoring'; import ready from './ready'; import { NODE_ENV } from 'soapbox/build_config'; -import FaviconService from 'soapbox/utils/favicon_service'; function main() { perf.start('main()'); @@ -20,9 +19,6 @@ function main() { // Sentry monitoring.start(); - // Favicon unread badge - FaviconService.initFaviconService(); - ready(() => { const mountNode = document.getElementById('soapbox'); diff --git a/app/soapbox/utils/favicon_service.js b/app/soapbox/utils/favicon_service.js index 9e14aad96..d009bca60 100644 --- a/app/soapbox/utils/favicon_service.js +++ b/app/soapbox/utils/favicon_service.js @@ -5,7 +5,7 @@ const createFaviconService = () => { const favicons = []; const faviconWidth = 128; const faviconHeight = 128; - const badgeRadius = 32; + const badgeRadius = 24; const initFaviconService = () => { const nodes = document.querySelectorAll('link[rel="icon"]'); @@ -44,7 +44,7 @@ const createFaviconService = () => { if (!favimg || !favcontext || !favcontext) return; const style = getComputedStyle(document.body); - const badgeColor = `${style.getPropertyValue('--badgeNotification') || 'rgb(240, 100, 100)'}`; + const badgeColor = `${style.getPropertyValue('--badge-notification') || 'rgb(255, 0, 0)'}`; if (isImageLoaded(favimg)) { favcontext.drawImage(favimg, 0, 0, favimg.width, favimg.height, 0, 0, faviconWidth, faviconHeight);