How can I view categories and "latest" on mobile?

I looked, and they used some custom JS code to force the “categories and latest topics” view.

I did not check if there was a better way; here is what they do (I modernized the code and added some comments):

import { apiInitializer } from "discourse/lib/api";
import CategoriesAndLatestTopics from "discourse/components/categories-and-latest-topics";
import CategoryList from "discourse/models/category-list";
import { MAX_UNOPTIMIZED_CATEGORIES } from "discourse/lib/constants";

export default apiInitializer((api) => {
  // Forces the Categories and latests topic view.
  api.modifyClass(
    "component:discovery/categories-display",
    (Superclass) =>
      class extends Superclass {
        get categoriesComponent() {
          if (this.args.parentCategory) {
            return super.categoriesComponent;
          } else {
            return CategoriesAndLatestTopics;
          }
        }
      }
  );

  // Same as original, minor the "this.site.desktopView" check,
  // so it finds the latest topics for mobile as well.
  api.modifyClass(
    "route:discovery-categories",
    (Superclass) =>
      class extends Superclass {
        async findCategories(parentCategory) {
          let model;

          let style = this.siteSettings.desktop_category_page_style;
          if (this.site.categories.length > MAX_UNOPTIMIZED_CATEGORIES) {
            style = "categories_only";
          }

          if (
            style === "categories_and_latest_topics" ||
            style === "categories_and_latest_topics_created_date"
          ) {
            model = await this._findCategoriesAndTopics(
              "latest",
              parentCategory
            );
          } else if (style === "categories_and_top_topics") {
            model = await this._findCategoriesAndTopics(
              "top",
              parentCategory
            );
          } else {
            PreloadStore.remove("topic_list");
            model = await CategoryList.list(this.store, parentCategory);
          }

          return model;
        }
      }
  );
})

You can try to paste the code into a new component → Edit code → JS tab.
Let me know if it helps.

2 Likes