FEATURE: Conditionally force optimized category style (#29473)

The `categories_only_optimized` category page style has been introduced
in commit d37a0d401c. This commit makes
sure that style is enforced for users who can see over 1000 categories
in order to keep `/categories` page functional.
This commit is contained in:
Bianca Nenciu 2024-10-30 16:46:56 +02:00 committed by GitHub
parent 2e3f1a6a62
commit 430c42acde
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 33 additions and 10 deletions

View File

@ -12,6 +12,7 @@ import ConditionalLoadingSpinner from "discourse/components/conditional-loading-
import LoadMore from "discourse/components/load-more"; import LoadMore from "discourse/components/load-more";
import PluginOutlet from "discourse/components/plugin-outlet"; import PluginOutlet from "discourse/components/plugin-outlet";
import SubcategoriesWithFeaturedTopics from "discourse/components/subcategories-with-featured-topics"; import SubcategoriesWithFeaturedTopics from "discourse/components/subcategories-with-featured-topics";
import { MAX_UNOPTIMIZED_CATEGORIES } from "discourse/lib/constants";
const mobileCompatibleViews = [ const mobileCompatibleViews = [
"categories_with_featured_topics", "categories_with_featured_topics",
@ -57,15 +58,22 @@ export default class CategoriesDisplay extends Component {
return component; return component;
} }
get #globalComponent() { get style() {
let style = this.siteSettings.desktop_category_page_style; let style = this.siteSettings.desktop_category_page_style;
if (this.site.mobileView && !mobileCompatibleViews.includes(style)) { if (this.site.mobileView && !mobileCompatibleViews.includes(style)) {
style = mobileCompatibleViews[0]; style = mobileCompatibleViews[0];
} }
const component = globalComponents[style]; if (this.site.categories.length > MAX_UNOPTIMIZED_CATEGORIES) {
style = "categories_only_optimized";
}
return style;
}
get #globalComponent() {
const component = globalComponents[this.style];
if (!component) { if (!component) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.error("Unknown category list style: " + style); console.error("Unknown category list style: " + this.style);
return CategoriesOnly; return CategoriesOnly;
} }
@ -87,8 +95,7 @@ export default class CategoriesDisplay extends Component {
return ( return (
this.args.loadMore && this.args.loadMore &&
(this.site.lazy_load_categories || (this.site.lazy_load_categories ||
this.siteSettings.desktop_category_page_style === this.style === "categories_only_optimized")
"categories_only_optimized")
); );
} }

View File

@ -97,3 +97,5 @@ export const SITE_SETTING_REQUIRES_CONFIRMATION_TYPES = {
simple: "simple", simple: "simple",
user_option: "user_option", user_option: "user_option",
}; };
export const MAX_UNOPTIMIZED_CATEGORIES = 1000;

View File

@ -2,6 +2,7 @@ import { action } from "@ember/object";
import { service } from "@ember/service"; import { service } from "@ember/service";
import { hash } from "rsvp"; import { hash } from "rsvp";
import { ajax } from "discourse/lib/ajax"; import { ajax } from "discourse/lib/ajax";
import { MAX_UNOPTIMIZED_CATEGORIES } from "discourse/lib/constants";
import PreloadStore from "discourse/lib/preload-store"; import PreloadStore from "discourse/lib/preload-store";
import { defaultHomepage } from "discourse/lib/utilities"; import { defaultHomepage } from "discourse/lib/utilities";
import Category from "discourse/models/category"; import Category from "discourse/models/category";
@ -21,8 +22,11 @@ export default class DiscoveryCategoriesRoute extends DiscourseRoute {
async findCategories(parentCategory) { async findCategories(parentCategory) {
let model; let model;
const style = let style =
this.site.desktopView && this.siteSettings.desktop_category_page_style; this.site.desktopView && this.siteSettings.desktop_category_page_style;
if (this.site.categories.length > MAX_UNOPTIMIZED_CATEGORIES) {
style = "categories_only_optimized";
}
if ( if (
style === "categories_and_latest_topics" || style === "categories_and_latest_topics" ||

View File

@ -4,6 +4,9 @@ class CategoryList
CATEGORIES_PER_PAGE = 20 CATEGORIES_PER_PAGE = 20
SUBCATEGORIES_PER_CATEGORY = 5 SUBCATEGORIES_PER_CATEGORY = 5
# Maximum number of categories before the optimized category page style is enforced
MAX_UNOPTIMIZED_CATEGORIES = 1000
include ActiveModel::Serialization include ActiveModel::Serialization
cattr_accessor :preloaded_topic_custom_fields cattr_accessor :preloaded_topic_custom_fields
@ -139,9 +142,14 @@ class CategoryList
query = query.where(parent_category_id: @options[:parent_category_id]) query = query.where(parent_category_id: @options[:parent_category_id])
end end
style =
if Category.secured(@guardian).count > MAX_UNOPTIMIZED_CATEGORIES
"categories_only_optimized"
else
SiteSetting.desktop_category_page_style
end
page = [1, @options[:page].to_i].max page = [1, @options[:page].to_i].max
if SiteSetting.desktop_category_page_style == "categories_only_optimized" || if style == "categories_only_optimized" || @guardian.can_lazy_load_categories?
@guardian.can_lazy_load_categories?
query = query.limit(CATEGORIES_PER_PAGE).offset((page - 1) * CATEGORIES_PER_PAGE) query = query.limit(CATEGORIES_PER_PAGE).offset((page - 1) * CATEGORIES_PER_PAGE)
elsif page > 1 elsif page > 1
# Pagination is supported only when lazy load is enabled. If it is not, # Pagination is supported only when lazy load is enabled. If it is not,

View File

@ -160,9 +160,11 @@ task "javascript:update_constants" => :environment do
export const TOPIC_VISIBILITY_REASONS = #{Topic.visibility_reasons.to_json}; export const TOPIC_VISIBILITY_REASONS = #{Topic.visibility_reasons.to_json};
export const SYSTEM_FLAG_IDS = #{PostActionType.types.to_json} export const SYSTEM_FLAG_IDS = #{PostActionType.types.to_json};
export const SITE_SETTING_REQUIRES_CONFIRMATION_TYPES = #{SiteSettings::TypeSupervisor::REQUIRES_CONFIRMATION_TYPES.to_json} export const SITE_SETTING_REQUIRES_CONFIRMATION_TYPES = #{SiteSettings::TypeSupervisor::REQUIRES_CONFIRMATION_TYPES.to_json};
export const MAX_UNOPTIMIZED_CATEGORIES = #{CategoryList::MAX_UNOPTIMIZED_CATEGORIES};
JS JS
pretty_notifications = Notification.types.map { |n| " #{n[0]}: #{n[1]}," }.join("\n") pretty_notifications = Notification.types.map { |n| " #{n[0]}: #{n[1]}," }.join("\n")