From ca67087622086d107a58b25b52fe504c016533d0 Mon Sep 17 00:00:00 2001 From: wanhose Date: Tue, 15 Oct 2024 15:34:42 +0200 Subject: [PATCH] feat(browser-extension): add extension update banner --- packages/browser-extension/src/popup.html | 26 ++++++++++++++++++- .../src/scripts/background.js | 8 +++--- .../browser-extension/src/scripts/popup.js | 11 +++++++- .../browser-extension/src/styles/popup.css | 15 ++++++++--- 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/packages/browser-extension/src/popup.html b/packages/browser-extension/src/popup.html index c2af4b3..352f20c 100644 --- a/packages/browser-extension/src/popup.html +++ b/packages/browser-extension/src/popup.html @@ -49,7 +49,7 @@
- +
} */ async function getState(hostname) { - const { [hostname]: state = stateByDefault } = await storage.get(hostname); + const keys = [hostname, 'updateAvailable']; + const { [hostname]: state = stateByDefault, updateAvailable } = await storage.get(keys); if ((state.issue && Date.now() > state.issue.expiresIn) || !state.issue?.expiresIn) { state.issue = await refreshIssue(hostname); } - return { ...stateByDefault, ...state }; + return { ...stateByDefault, ...state, updateAvailable }; } /** @@ -222,7 +224,7 @@ async function refreshData() { */ async function refreshIssue(hostname) { try { - const { data } = await requestManager.fetchData(`${apiUrl}/issues/${hostname}`); + const { data } = await requestManager.fetchData(`${apiUrl}/issues/${hostname}/`); if (Object.keys(data).length === 0) { await updateStore(hostname, { issue: { expiresIn: Date.now() + 8 * 60 * 60 * 1000 } }); diff --git a/packages/browser-extension/src/scripts/popup.js b/packages/browser-extension/src/scripts/popup.js index af819e5..21e6412 100644 --- a/packages/browser-extension/src/scripts/popup.js +++ b/packages/browser-extension/src/scripts/popup.js @@ -1,7 +1,8 @@ /** * @typedef {Object} ExtensionState - * @property {boolean} on * @property {ExtensionIssue} [issue] + * @property {boolean} on + * @property {string} [updateAvailable] */ /** @@ -128,6 +129,14 @@ async function handleContentLoaded() { submitButtonElement?.addEventListener('click', handleSubmitButtonClick); } + if (state.updateAvailable) { + const updateBanner = document.getElementById('update-banner'); + updateBanner.removeAttribute('aria-hidden'); + + const updateBannerUrl = document.getElementById('update-banner-url'); + updateBannerUrl.href += `/tag/${state.updateAvailable}`; + } + const hostTextElement = document.getElementById('host'); hostTextElement.innerText = hostname ?? 'unknown'; diff --git a/packages/browser-extension/src/styles/popup.css b/packages/browser-extension/src/styles/popup.css index 6607cc3..bcf210c 100644 --- a/packages/browser-extension/src/styles/popup.css +++ b/packages/browser-extension/src/styles/popup.css @@ -89,8 +89,6 @@ header { } main > .banner { - background-color: #f39c12; - color: #c0392b; margin: 0px; padding: 16px; @@ -98,7 +96,18 @@ main > .banner { display: none; } - & #issue-banner-url { + &[data-variant='notice'] { + background-color: #2196f3; + color: var(--color-white); + } + + &[data-variant='warning'] { + background-color: #f39c12; + color: #c0392b; + } + + & #issue-banner-url, + #update-banner-url { color: inherit; display: inline-block; vertical-align: middle;