From c1e62808ed37f88d690a6c8f517287936f7c5a86 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Tue, 31 Jul 2018 01:01:03 +0530 Subject: [PATCH] FIX: Top site categories are displayed in random order --- .../discourse/widgets/hamburger-menu.js.es6 | 38 +++++++++---------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/app/assets/javascripts/discourse/widgets/hamburger-menu.js.es6 b/app/assets/javascripts/discourse/widgets/hamburger-menu.js.es6 index e8e2fed85c6..0c361e7c1f7 100644 --- a/app/assets/javascripts/discourse/widgets/hamburger-menu.js.es6 +++ b/app/assets/javascripts/discourse/widgets/hamburger-menu.js.es6 @@ -179,40 +179,36 @@ export default createWidget("hamburger-menu", { listCategories() { const maxCategoriesToDisplay = this.siteSettings .hamburger_menu_categories_count; - const categoriesByCount = this.site.get("categoriesByCount"); - let categories = categoriesByCount.slice(); + let categories = this.site.get("categoriesByCount"); if (this.currentUser) { - let unreadCategoryIds = []; - let topCategoryIds = this.currentUser.get("top_category_ids") || []; - let i = 0; + const allCategories = this.site + .get("categories") + .filter(c => c.notification_level !== NotificationLevels.MUTED); - categoriesByCount + categories = allCategories + .filter(c => c.get("newTopics") > 0 || c.get("unreadTopics") > 0) .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); - categories.splice(i, 0, category); - i += 1; + const topCategoryIds = this.currentUser.get("top_category_ids") || []; + topCategoryIds.forEach(id => { + const category = allCategories.find(c => c.id === id); + if (category && !categories.includes(category)) { + categories.push(category); } }); + + categories = categories.concat( + allCategories + .filter(c => !categories.includes(c)) + .sort((a, b) => b.topic_count - a.topic_count) + ); } const moreCount = categories.length - maxCategoriesToDisplay;