Skip to content

Commit a97d879

Browse files
committed
refactor: simplify inapp banner code
1 parent 0097dfc commit a97d879

File tree

4 files changed

+30
-90
lines changed

4 files changed

+30
-90
lines changed

src/extensionsIntegrated/InAppNotifications/banner.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ define(function (require, exports, module) {
3030
PreferencesManager = require("preferences/PreferencesManager"),
3131
ExtensionUtils = require("utils/ExtensionUtils"),
3232
Metrics = require("utils/Metrics"),
33-
utils = require("./utils"),
33+
semver = require("thirdparty/semver.browser"),
3434
NotificationBarHtml = require("text!./htmlContent/notificationContainer.html");
3535

3636
ExtensionUtils.loadStyleSheet(module, "styles/styles.css");
@@ -44,6 +44,26 @@ define(function (require, exports, module) {
4444
PreferencesManager.stateManager.definePreference(IN_APP_NOTIFICATIONS_BANNER_SHOWN_STATE,
4545
"object", {});
4646

47+
function _isValidForThisVersion(versionFilter) {
48+
return semver.satisfies(brackets.metadata.apiVersion, versionFilter);
49+
}
50+
51+
// platformFilter is a string subset of
52+
// "mac,win,linux,allDesktop,firefox,chrome,safari,allBrowser,all"
53+
function _isValidForThisPlatform(platformFilter) {
54+
platformFilter = platformFilter.split(",");
55+
if(platformFilter.includes("all")
56+
|| (platformFilter.includes(brackets.platform) && Phoenix.isNativeApp) // win linux and mac is only for tauri and not for browser in platform
57+
|| (platformFilter.includes("allDesktop") && Phoenix.isNativeApp)
58+
|| (platformFilter.includes("firefox") && Phoenix.browser.desktop.isFirefox && !Phoenix.isNativeApp)
59+
|| (platformFilter.includes("chrome") && Phoenix.browser.desktop.isChromeBased && !Phoenix.isNativeApp)
60+
|| (platformFilter.includes("safari") && Phoenix.browser.desktop.isSafari && !Phoenix.isNativeApp)
61+
|| (platformFilter.includes("allBrowser") && !Phoenix.isNativeApp)){
62+
return true;
63+
}
64+
return false;
65+
}
66+
4767
/**
4868
* If there are multiple notifications, thew will be shown one after the other and not all at once.
4969
* A sample notifications is as follows:
@@ -86,21 +106,18 @@ define(function (require, exports, module) {
86106
for(const notificationID of Object.keys(notifications)){
87107
if(!_InAppBannerShownAndDone[notificationID]) {
88108
const notification = notifications[notificationID];
89-
if(!utils.isValidForThisVersion(notification.FOR_VERSIONS)){
109+
if(!_isValidForThisVersion(notification.FOR_VERSIONS)){
90110
continue;
91111
}
92-
if(!utils.isValidForThisPlatform(notification.PLATFORM)){
112+
if(!_isValidForThisPlatform(notification.PLATFORM)){
93113
continue;
94114
}
95-
if(!notification.HTML_CONTENT.includes(NOTIFICATION_ACK_CLASS)
96-
&& !notification.DANGER_SHOW_ON_EVERY_BOOT){
115+
if(!notification.DANGER_SHOW_ON_EVERY_BOOT){
97116
// One time notification. mark as shown and never show again
117+
// all notifications are one time, we track metrics for each notification separately
98118
_markAsShownAndDone(notificationID);
99119
}
100120
await showBannerAndWaitForDismiss(notification.HTML_CONTENT, notificationID);
101-
if(!notification.DANGER_SHOW_ON_EVERY_BOOT){
102-
_markAsShownAndDone(notificationID);
103-
}
104121
}
105122
}
106123
}
@@ -185,25 +202,21 @@ define(function (require, exports, module) {
185202
$closeIcon = $notificationBar.find('.close-icon');
186203

187204
$notificationContent.append($htmlContent);
188-
Metrics.countEvent(Metrics.EVENT_TYPE.NOTIFICATIONS, "banner-"+notificationID,
189-
"shown");
205+
Metrics.countEvent(Metrics.EVENT_TYPE.NOTIFICATIONS, "banner-shown", notificationID);
190206

191207
// Click handlers on actionable elements
192208
if ($closeIcon.length > 0) {
193209
$closeIcon.click(function () {
194210
cleanNotificationBanner();
195-
Metrics.countEvent(Metrics.EVENT_TYPE.NOTIFICATIONS, "banner-"+notificationID,
196-
"closeClick");
211+
Metrics.countEvent(Metrics.EVENT_TYPE.NOTIFICATIONS, "banner-close", notificationID);
197212
!resolved && resolve($htmlContent);
198213
resolved = true;
199214
});
200215
}
201216

202217
$notificationBar.find(`.${NOTIFICATION_ACK_CLASS}`).click(function() {
203-
// Your click event handler logic here
204218
cleanNotificationBanner();
205-
Metrics.countEvent(Metrics.EVENT_TYPE.NOTIFICATIONS, "banner-"+notificationID,
206-
"ackClick");
219+
Metrics.countEvent(Metrics.EVENT_TYPE.NOTIFICATIONS, "banner-ack", notificationID);
207220
!resolved && resolve($htmlContent);
208221
resolved = true;
209222
});

src/extensionsIntegrated/InAppNotifications/utils.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/utils/Metrics.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ define(function (require, exports, module) {
104104
PROJECT: "project",
105105
THEMES: "themes",
106106
EXTENSIONS: "extensions",
107-
NOTIFICATIONS: "notifications",
107+
NOTIFICATIONS: "notify",
108108
UI: "UI",
109109
UI_MENU: "UIMenu",
110110
UI_DIALOG: "ui-dialog",

test/spec/Extn-InAppNotifications-integ-test.js

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,9 @@ define(function (require, exports, module) {
165165
expect(testWindow.$(id).length).toEqual(1);
166166
});
167167

168-
it("Should show notification if not acknowledged with close click", async function () {
168+
it("Should show notification only once", async function () {
169169
banner.cleanNotificationBanner();
170170
const {notification, id} = getRandomNotification("all", false, true);
171-
banner._renderNotifications(notification);
172-
173-
// clear notification without clicking close
174-
banner.cleanNotificationBanner();
175171

176172
// show the same banner again
177173
banner._renderNotifications(notification);
@@ -186,27 +182,5 @@ define(function (require, exports, module) {
186182
banner._renderNotifications(notification);
187183
expect(testWindow.$(id).length).toEqual(0);
188184
});
189-
190-
it("Should show notification if not acknowledged with click on item with notification ack class", async function () {
191-
banner.cleanNotificationBanner();
192-
const {notification, id} = getRandomNotification("all", false, true);
193-
banner._renderNotifications(notification);
194-
195-
// clear notification without clicking close
196-
banner.cleanNotificationBanner();
197-
198-
// show the same banner again
199-
banner._renderNotifications(notification);
200-
expect(testWindow.$(id).length).toEqual(1);
201-
202-
// now close the notification by clicking the close icon
203-
testWindow.$(".notification_ack").click();
204-
expect(testWindow.$(id).length).toEqual(0);
205-
206-
await awaits(300);
207-
// acknowledged banner should not show the same banner again
208-
banner._renderNotifications(notification);
209-
expect(testWindow.$(id).length).toEqual(0);
210-
});
211185
});
212186
});

0 commit comments

Comments
 (0)