Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
banners: Redesign desktop notifications navbar banner.
Sayam Samal028e9dc58a15
diff --git a/templates/zerver/app/index.html b/templates/zerver/app/index.htmlindex 2c167b4aecd..cbe5a544818 100644--- a/templates/zerver/app/index.html+++ b/templates/zerver/app/index.html@@ -225,7 +225,7 @@</div><div id="navbar-fixed-container">- <div id="navbar_alerts_wrapper"></div>+ <div id="navbar_alerts_wrapper" class="banner-wrapper"></div><div id="header-container"></div></div>diff --git a/tools/test-js-with-node b/tools/test-js-with-nodeindex 00653e7c5fd..a8c85f884d6 100755--- a/tools/test-js-with-node+++ b/tools/test-js-with-node@@ -57,6 +57,7 @@ EXEMPT_FILES = make_set("web/src/attachments_ui.ts","web/src/audible_notifications.ts","web/src/avatar.ts",+ "web/src/banners.ts","web/src/base_page_params.ts","web/src/blueslip.ts","web/src/blueslip_stacktrace.ts",diff --git a/web/src/banners.ts b/web/src/banners.tsnew file mode 100644index 00000000000..bacc21b5a1d--- /dev/null+++ b/web/src/banners.ts@@ -0,0 +1,52 @@+import type Handlebars from "handlebars/runtime.js";+import $ from "jquery";++import render_banner from "../templates/components/banner.hbs";++type ComponentIntent = "neutral" | "brand" | "info" | "success" | "warning" | "danger";++type ActionButton = {+ type: "primary" | "quiet" | "borderless";+ intent?: ComponentIntent;+ label: string;+ icon?: string;+ custom_classes?: string;+};++export type Banner = {+ intent: ComponentIntent;+ label: string | Handlebars.SafeString;+ buttons: ActionButton[];+ close_button: boolean;+ custom_classes?: string;+};++export type AlertBanner = Banner & {+ process: string;+};++export function open(banner: Banner | AlertBanner, $banner_container: JQuery): void {+ const banner_html = render_banner(banner);+ $banner_container.html(banner_html);+ $(window).trigger("resize");+}++export function append(banner: Banner | AlertBanner, $banner_container: JQuery): void {+ const $banner_html = render_banner(banner);+ $banner_container.append($banner_html);+ $(window).trigger("resize");+}++export function close($banner: JQuery): void {+ $banner.remove();+ $(window).trigger("resize");+}++export function initialize(): void {+ $("body").on("click", ".banner .banner-close-action", function (this: HTMLElement, e) {+ e.preventDefault();+ e.stopPropagation();+ const $banner = $(this).closest(".banner");+ close($banner);+ });+}diff --git a/web/src/bundles/app.ts b/web/src/bundles/app.tsindex d4c4f3199cf..4936bb173dd 100644--- a/web/src/bundles/app.ts+++ b/web/src/bundles/app.ts@@ -26,6 +26,7 @@ import "../../styles/typeahead.css";import "../../styles/app_variables.css";import "../../styles/tooltips.css";import "../../styles/buttons.css";+import "../../styles/banners.css";import "../../styles/components.css";import "../../styles/app_components.css";import "../../styles/rendered_markdown.css";diff --git a/web/src/navbar_alerts.ts b/web/src/navbar_alerts.tsindex 86837438cbb..b1d67fd241e 100644--- a/web/src/navbar_alerts.ts+++ b/web/src/navbar_alerts.ts@@ -5,7 +5,6 @@ import assert from "minimalistic-assert";import render_bankruptcy_alert_content from "../templates/navbar_alerts/bankruptcy.hbs";import render_configure_email_alert_content from "../templates/navbar_alerts/configure_outgoing_email.hbs";import render_demo_organization_deadline_content from "../templates/navbar_alerts/demo_organization_deadline.hbs";-import render_desktop_notifications_alert_content from "../templates/navbar_alerts/desktop_notifications.hbs";import render_empty_required_profile_fields from "../templates/navbar_alerts/empty_required_profile_fields.hbs";import render_insecure_desktop_app_alert_content from "../templates/navbar_alerts/insecure_desktop_app.hbs";import render_navbar_alert_wrapper from "../templates/navbar_alerts/navbar_alert_wrapper.hbs";@@ -13,6 +12,8 @@ import render_profile_incomplete_alert_content from "../templates/navbar_alerts/import render_server_needs_upgrade_alert_content from "../templates/navbar_alerts/server_needs_upgrade.hbs";import render_time_zone_update_offer_content from "../templates/navbar_alerts/time_zone_update_offer.hbs";+import * as banners from "./banners.ts";+import type {AlertBanner} from "./banners.ts";import * as channel from "./channel.ts";import * as desktop_notifications from "./desktop_notifications.ts";import * as feedback_widget from "./feedback_widget.ts";@@ -39,11 +40,7 @@ const show_step = function ($process: JQuery, step: number): void {.show();};-const get_step = function ($process: JQuery): number {- return Number($process.find("[data-step]:visible").attr("data-step"));-};--export function should_show_notifications(ls: LocalStorage): boolean {+export function should_show_desktop_notifications_banner(ls: LocalStorage): boolean {// if the user said to never show banner on this computer again, it will// be stored as `true` so we want to negate that.if (localstorage.supported() && ls.get("dontAskForNotifications") === true) {@@ -162,6 +159,34 @@ export function should_offer_to_update_timezone(): boolean {);}+const DESKTOP_NOTIFICATIONS_BANNER: AlertBanner = {+ process: "desktop-notifications",+ intent: "brand",+ label: $t({+ defaultMessage:+ "Zulip needs your permission to enable desktop notifications for important messages.",+ }),+ buttons: [+ {+ type: "primary",+ label: $t({defaultMessage: "Enable notifications"}),+ custom_classes: "request-desktop-notifications",+ },+ {+ type: "quiet",+ label: $t({defaultMessage: "Customize notifications"}),+ custom_classes: "customize-desktop-notifications",+ },+ {+ type: "borderless",+ label: $t({defaultMessage: "Never ask on this computer"}),+ custom_classes: "reject-desktop-notifications",+ },+ ],+ close_button: true,+ custom_classes: "navbar-alert-banner",+};+export function initialize(): void {const ls = localstorage();const browser_time_zone = timerender.browser_time_zone();@@ -203,11 +228,8 @@ export function initialize(): void {custom_class: "red",rendered_alert_content_html: render_configure_email_alert_content(),});- } else if (should_show_notifications(ls)) {- open({- data_process: "notifications",- rendered_alert_content_html: render_desktop_notifications_alert_content(),- });+ } else if (should_show_desktop_notifications_banner(ls)) {+ banners.open(DESKTOP_NOTIFICATIONS_BANNER, $("#navbar_alerts_wrapper"));} else if (unread_ui.should_display_bankruptcy_banner()) {const old_unreads_missing = unread.old_unreads_missing;const unread_msgs_count = unread.get_unread_message_count();@@ -229,19 +251,30 @@ export function initialize(): void {}// Configure click handlers.- $(".request-desktop-notifications").on("click", function (e) {- e.preventDefault();- $(this).closest(".alert").hide();- desktop_notifications.request_desktop_notifications_permission();- $(window).trigger("resize");- });+ $("#navbar_alerts_wrapper").on(+ "click",+ ".request-desktop-notifications",+ function (this: HTMLElement) {+ const $banner = $(this).closest(".banner");+ banners.close($banner);+ desktop_notifications.request_desktop_notifications_permission();+ },+ );- $(".reject-notifications").on("click", function () {- $(this).closest(".alert").hide();- ls.set("dontAskForNotifications", true);- $(window).trigger("resize");+ $("#navbar_alerts_wrapper").on("click", ".customize-desktop-notifications", () => {+ window.location.hash = "#settings/notifications";});+ $("#navbar_alerts_wrapper").on(+ "click",+ ".reject-desktop-notifications",+ function (this: HTMLElement) {+ const $banner = $(this).closest(".banner");+ banners.close($banner);+ ls.set("dontAskForNotifications", true);+ },+ );+$(".accept-bankruptcy").on("click", function (e) {e.preventDefault();const $process = $(this).closest("[data-process]");@@ -262,13 +295,9 @@ export function initialize(): void {function (this: HTMLElement, e) {e.stopPropagation();const $process = $(this).closest("[data-process]");- if (get_step($process) === 1 && $process.attr("data-process") === "notifications") {- show_step($process, 2);- } else {- $(this).closest(".alert").hide();- if ($process.attr("data-process") !== "profile-missing-required") {- maybe_show_empty_required_profile_fields_alert();- }+ $(this).closest(".alert").hide();+ if ($process.attr("data-process") !== "profile-missing-required") {+ maybe_show_empty_required_profile_fields_alert();}$(window).trigger("resize");},diff --git a/web/src/ui_init.js b/web/src/ui_init.jsindex 19a8fe21ef2..1ea3d5649e2 100644--- a/web/src/ui_init.js+++ b/web/src/ui_init.js@@ -16,6 +16,7 @@ import * as add_stream_options_popover from "./add_stream_options_popover.ts";import * as alert_words from "./alert_words.ts";import {all_messages_data} from "./all_messages_data.ts";import * as audible_notifications from "./audible_notifications.ts";+import * as banners from "./banners.ts";import * as blueslip from "./blueslip.ts";import * as bot_data from "./bot_data.ts";import * as channel from "./channel.ts";@@ -515,6 +516,7 @@ export function initialize_everything(state_data) {emojisets.initialize();scroll_bar.initialize();message_viewport.initialize();+ banners.initialize();navbar_alerts.initialize();message_list_hover.initialize();initialize_kitchen_sink_stuff();diff --git a/web/styles/banners.css b/web/styles/banners.cssindex 2ca002ad2e3..966f7b1a4b1 100644--- a/web/styles/banners.css+++ b/web/styles/banners.css@@ -56,7 +56,7 @@}@container (width >= 44em) and (width < 63em) {- .navbar-alert-banner[data-process="notifications"] {+ .navbar-alert-banner[data-process="desktop-notifications"] {grid-template: var(--banner-grid-template-rows-md) / var(--banner-grid-template-columns-md);diff --git a/web/templates/components/banner.hbs b/web/templates/components/banner.hbsindex b20340b8bce..4a893280ebc 100644--- a/web/templates/components/banner.hbs+++ b/web/templates/components/banner.hbs@@ -4,10 +4,14 @@</span><span class="banner-action-buttons">{{#each buttons}}- {{> action-button .}}+ {{#if this.intent}}+ {{> action-button .}}+ {{else}}+ {{> action-button . intent=../intent}}+ {{/if}}{{/each}}</span>{{#if close_button}}- {{> icon-button custom_classes="banner-close-button" icon="close" intent=intent}}+ {{> icon-button custom_classes="banner-close-action banner-close-button" icon="close" intent=intent}}{{/if}}</div>diff --git a/web/templates/navbar_alerts/desktop_notifications.hbs b/web/templates/navbar_alerts/desktop_notifications.hbsdeleted file mode 100644index 1f05477edd2..00000000000--- a/web/templates/navbar_alerts/desktop_notifications.hbs+++ /dev/null@@ -1,15 +0,0 @@-<div data-step="1">- {{#tr}}Zulip needs your permission to <z-link>enable desktop notifications.</z-link>- {{#*inline "z-link"}}<a class="request-desktop-notifications alert-link" role="button" tabindex=0>{{> @partial-block}}</a>{{/inline}}- {{/tr}}-</div>-<div data-step="2" style="display: none">- {{t "We strongly recommend enabling desktop notifications. They help Zulip keep your team connected." }}- <span class="buttons">- <a class="alert-link request-desktop-notifications" role="button" tabindex=0>{{t "Enable notifications" }}</a>- •- <a class="alert-link exit" role="button" tabindex=0>{{t "Ask me later" }}</a>- •- <a class="alert-link reject-notifications" role="button" tabindex=0>{{t "Never ask on this computer" }}</a>- </span>-</div>diff --git a/web/tests/navbar_alerts.test.cjs b/web/tests/navbar_alerts.test.cjsindex 8a286cfd055..277ec12b304 100644--- a/web/tests/navbar_alerts.test.cjs+++ b/web/tests/navbar_alerts.test.cjs@@ -8,8 +8,6 @@ const {mock_esm, zrequire} = require("./lib/namespace.cjs");const {run_test} = require("./lib/test.cjs");const {page_params} = require("./lib/zpage_params.cjs");-page_params.is_spectator = false;-const desktop_notifications = mock_esm("../src/desktop_notifications");const util = mock_esm("../src/util");const timerender = mock_esm("../src/timerender");@@ -30,43 +28,43 @@ function test(label, f) {});}-test("allow_notification_alert", ({disallow, override}) => {+test("should_show_desktop_notifications_banner", ({override}) => {const ls = localstorage();- // Show alert.- assert.equal(ls.get("dontAskForNotifications"), undefined);+ // Show desktop notifications banner when following conditions are suitable:+ // - Notification permission is not already granted.+ // - The user is not a spectator.+ // - The device is not mobile.+ // - Notification permission is not denied.+ // - The user has not said to never show banner on this device again.+ ls.set("dontAskForNotifications", undefined);+ page_params.is_spectator = false;override(util, "is_mobile", () => false);override(desktop_notifications, "granted_desktop_notifications_permission", () => false);- override(desktop_notifications, "permission_state", () => "granted");- assert.equal(navbar_alerts.should_show_notifications(ls), true);+ override(desktop_notifications, "permission_state", () => "default");+ assert.equal(navbar_alerts.should_show_desktop_notifications_banner(ls), true);- // Avoid showing if the user said to never show alert on this computer again.+ // Don't ask for permission if user has said to never show banner on this device again.ls.set("dontAskForNotifications", true);- assert.equal(navbar_alerts.should_show_notifications(ls), false);-- // Avoid showing if device is mobile.+ assert.equal(navbar_alerts.should_show_desktop_notifications_banner(ls), false);ls.set("dontAskForNotifications", undefined);- assert.equal(navbar_alerts.should_show_notifications(ls), true);- override(util, "is_mobile", () => true);- assert.equal(navbar_alerts.should_show_notifications(ls), false);- // Avoid showing if notification permission is denied.+ // Don't ask for permission if device is mobile.+ override(util, "is_mobile", () => true);+ assert.equal(navbar_alerts.should_show_desktop_notifications_banner(ls), false);override(util, "is_mobile", () => false);- assert.equal(navbar_alerts.should_show_notifications(ls), true);++ // Don't ask for permission if notification is denied by user.override(desktop_notifications, "permission_state", () => "denied");- assert.equal(navbar_alerts.should_show_notifications(ls), false);+ assert.equal(navbar_alerts.should_show_desktop_notifications_banner(ls), false);- // Avoid showing if notification is already granted.- disallow(desktop_notifications, "permission_state");- override(desktop_notifications, "granted_desktop_notifications_permission", () => "granted");- assert.equal(navbar_alerts.should_show_notifications(ls), false);+ // Don't ask for permission if notification is already granted by user.+ override(desktop_notifications, "granted_desktop_notifications_permission", () => true);+ assert.equal(navbar_alerts.should_show_desktop_notifications_banner(ls), false);- // Don't ask for permission to spectator.- disallow(util, "is_mobile");- disallow(desktop_notifications, "granted_desktop_notifications_permission");- disallow(desktop_notifications, "permission_state");+ // Don't ask for permission if user is a spectator.page_params.is_spectator = true;- assert.equal(navbar_alerts.should_show_notifications(ls), false);+ assert.equal(navbar_alerts.should_show_desktop_notifications_banner(ls), false);});test("profile_incomplete_alert", ({override}) => {