From 38e1706b6146bf50702c3836b8ce905f0dc6332e Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Fri, 12 Apr 2024 11:56:32 +0300 Subject: [PATCH] FIX: Update category breadcrumbs more reliably (#26608) The breadcrumbs were updated everytime there were changes to the categories which was not efficient and caused unnecessary rerendering of the CategoryDrop elements when "lazy load categories" is enabled. This commit also ensures that all category fields are serialized for ancestors too for the categories#search endpoint. --- .../discourse/app/components/bread-crumbs.js | 43 ++++++++++--------- app/controllers/categories_controller.rb | 1 + 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/app/assets/javascripts/discourse/app/components/bread-crumbs.js b/app/assets/javascripts/discourse/app/components/bread-crumbs.js index 3c5e92e7e26..27721f4a7f2 100644 --- a/app/assets/javascripts/discourse/app/components/bread-crumbs.js +++ b/app/assets/javascripts/discourse/app/components/bread-crumbs.js @@ -10,30 +10,31 @@ export default Component.extend({ editingCategory: false, editingCategoryTab: null, - @discourseComputed("category.ancestors", "categories", "noSubcategories") - categoryBreadcrumbs(categoryAncestors, filteredCategories, noSubcategories) { - categoryAncestors = categoryAncestors || []; - const parentCategories = [undefined, ...categoryAncestors]; - const categories = [...categoryAncestors, undefined]; - const zipped = parentCategories.map((x, i) => [x, categories[i]]); + @discourseComputed("category", "categories", "noSubcategories") + categoryBreadcrumbs(category, filteredCategories, noSubcategories) { + const ancestors = category?.ancestors || []; + const parentCategories = [undefined, ...ancestors]; + const categories = [...ancestors, undefined]; - return zipped.map((record) => { - const [parentCategory, category] = record; + return parentCategories + .map((x, i) => [x, categories[i]]) + .map((record) => { + const [parentCategory, subCategory] = record; - const options = filteredCategories.filter( - (c) => - c.get("parentCategory.id") === (parentCategory && parentCategory.id) - ); + const options = filteredCategories.filter( + (c) => + c.get("parentCategory.id") === (parentCategory && parentCategory.id) + ); - return { - category, - parentCategory, - options, - isSubcategory: !!parentCategory, - noSubcategories: !category && noSubcategories, - hasOptions: !parentCategory || parentCategory.has_children, - }; - }); + return { + category: subCategory, + parentCategory, + options, + isSubcategory: !!parentCategory, + noSubcategories: !subCategory && noSubcategories, + hasOptions: !parentCategory || parentCategory.has_children, + }; + }); }, @discourseComputed("siteSettings.tagging_enabled", "editingCategory") diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 3d6da8e307a..8cb422cabde 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -436,6 +436,7 @@ class CategoriesController < ApplicationController if include_ancestors ancestors = Category.secured(guardian).ancestors_of(categories.map(&:id)) + Category.preload_user_fields!(guardian, ancestors) response[:ancestors] = serialize_data(ancestors, SiteCategorySerializer, scope: guardian) end