A platform for community discussion. Free, open, simple.
REFACTOR: Prioritize unread categories in hamburger menu
Vinoth Kannanc54b5824d474
diff --git a/app/assets/javascripts/discourse/widgets/hamburger-menu.js.es6 b/app/assets/javascripts/discourse/widgets/hamburger-menu.js.es6index 3838d675e60..e8e2fed85c6 100644--- a/app/assets/javascripts/discourse/widgets/hamburger-menu.js.es6+++ b/app/assets/javascripts/discourse/widgets/hamburger-menu.js.es6@@ -3,6 +3,7 @@ import { h } from "virtual-dom";import DiscourseURL from "discourse/lib/url";import { ajax } from "discourse/lib/ajax";import { userPath } from "discourse/lib/url";+import { NotificationLevels } from "discourse/lib/notification-levels";const flatten = array => [].concat.apply([], array);@@ -178,15 +179,33 @@ export default createWidget("hamburger-menu", {listCategories() {const maxCategoriesToDisplay = this.siteSettings.hamburger_menu_categories_count;- const categoriesList = this.site.get("categoriesByCount");- let categories = categoriesList.slice();+ const categoriesByCount = this.site.get("categoriesByCount");+ let categories = categoriesByCount.slice();if (this.currentUser) {- let categoryIds = this.currentUser.get("top_category_ids") || [];+ let unreadCategoryIds = [];+ let topCategoryIds = this.currentUser.get("top_category_ids") || [];let i = 0;- const mutedCategoryIds = this.currentUser.get("muted_category_ids") || [];- categories = categories.filter(c => !mutedCategoryIds.includes(c.id));- categoryIds.forEach(id => {++ categoriesByCount+ .sort((a, b) => {+ return (+ b.get("newTopics") ++ b.get("unreadTopics") -+ (a.get("newTopics") + a.get("unreadTopics"))+ );+ })+ .forEach(c => {+ if (c.get("newTopics") > 0 || c.get("unreadTopics") > 0) {+ unreadCategoryIds.push(c.id);+ }+ });++ categories = categories.filter(+ c => c.notification_level !== NotificationLevels.MUTED+ );++ [...unreadCategoryIds, ...topCategoryIds].uniq().forEach(id => {const category = categories.find(c => c.id === id);if (category) {categories = categories.filter(c => c.id !== id);diff --git a/test/javascripts/widgets/hamburger-menu-test.js.es6 b/test/javascripts/widgets/hamburger-menu-test.js.es6index ddb39f219d4..c50323bf189 100644--- a/test/javascripts/widgets/hamburger-menu-test.js.es6+++ b/test/javascripts/widgets/hamburger-menu-test.js.es6@@ -1,9 +1,11 @@import { moduleForWidget, widgetTest } from "helpers/widget-test";+import { NotificationLevels } from "discourse/lib/notification-levels";moduleForWidget("hamburger-menu");const topCategoryIds = [2, 3, 1];let mutedCategoryIds = [];+let unreadCategoryIds = [];let categoriesByCount = [];widgetTest("prioritize faq", {@@ -155,16 +157,24 @@ widgetTest("top categories", {beforeEach() {this.siteSettings.hamburger_menu_categories_count = 8;maxCategoriesToDisplay = this.siteSettings.hamburger_menu_categories_count;- categoriesByCount = this.site.get("categoriesByCount");+ categoriesByCount = this.site.get("categoriesByCount").slice();categoriesByCount.every(c => {if (!topCategoryIds.includes(c.id)) {- mutedCategoryIds.push(c.id);- return false;+ if (mutedCategoryIds.length === 0) {+ mutedCategoryIds.push(c.id);+ c.set("notification_level", NotificationLevels.MUTED);+ } else if (unreadCategoryIds.length === 0) {+ unreadCategoryIds.push(c.id);+ c.set("unreadTopics", 5);+ } else {+ unreadCategoryIds.splice(0, 0, c.id);+ c.set("newTopics", 10);+ return false;+ }}return true;});this.currentUser.set("top_category_ids", topCategoryIds);- this.currentUser.set("muted_category_ids", mutedCategoryIds);},test(assert) {@@ -173,8 +183,11 @@ widgetTest("top categories", {categoriesByCount = categoriesByCount.filter(c => !mutedCategoryIds.includes(c.id));- let ids = topCategoryIds- .concat(categoriesByCount.map(c => c.id))+ let ids = [+ ...unreadCategoryIds,+ ...topCategoryIds,+ ...categoriesByCount.map(c => c.id)+ ].uniq().slice(0, maxCategoriesToDisplay);