From c00c748c1ef9491c480cbf44c0f2861e7be1acae Mon Sep 17 00:00:00 2001 From: tophf Date: Tue, 9 Feb 2021 12:37:36 +0300 Subject: [PATCH] simplify/deduplicate badge error logic also correctly restore the real badge info when error is cleared --- background/icon-manager.js | 6 ++++-- background/sync-manager.js | 27 +++++++++++---------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/background/icon-manager.js b/background/icon-manager.js index 5e13a15c..85a2be2d 100644 --- a/background/icon-manager.js +++ b/background/icon-manager.js @@ -67,13 +67,15 @@ const iconMan = (() => { refreshIconBadgeColor(); setBadgeText({text}); for (const tabId of tabMan.list()) { - if (badgeOvr) { + if (text) { setBadgeText({tabId, text}); } else { refreshIconBadgeText(tabId); } } - chrome.browserAction.setTitle({title}); + chrome.browserAction.setTitle({ + title: title && chrome.i18n.getMessage(title) || title || '', + }); }, }; diff --git a/background/sync-manager.js b/background/sync-manager.js index db816a1e..3fbb72e6 100644 --- a/background/sync-manager.js +++ b/background/sync-manager.js @@ -209,22 +209,7 @@ const syncMan = (() => { function emitStatusChange() { msg.broadcastExtension({method: 'syncStatusUpdate', status}); - - if (status.state !== STATES.connected || !lastError || isNetworkError(lastError)) { - iconMan.overrideBadge({}); - } else if (isGrantError(lastError)) { - iconMan.overrideBadge({ - text: 'x', - color: '#F00', - title: chrome.i18n.getMessage('syncErrorRelogin'), - }); - } else { - iconMan.overrideBadge({ - text: 'x', - color: '#F00', - title: chrome.i18n.getMessage('syncError'), - }); - } + iconMan.overrideBadge(getErrorBadge()); } function isNetworkError(err) { @@ -237,6 +222,16 @@ const syncMan = (() => { return false; } + function getErrorBadge() { + if (status.state === STATES.connected && lastError && !isNetworkError(lastError)) { + return { + text: 'x', + color: '#F00', + title: isGrantError(lastError) ? 'syncErrorRelogin' : 'syncError', + }; + } + } + function getDrive(name) { if (name === 'dropbox' || name === 'google' || name === 'onedrive') { return dbToCloud.drive[name]({