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

AI-generated summary

The discussion revolves around viewing categories and the “latest” topics on mobile. Aurora initially asks how to achieve this and shares a screenshot of the Let’s Talk app. Arkshine provides a custom JavaScript code to force the “categories and latest topics” view, which Aurora tries but reports that it doesn’t work. Arkshine suggests checking the code placement, theme attachment, site settings, and browser console for errors. After Aurora shares a screenshot of their code, Arkshine identifies a duplicate code snippet as the likely cause of the issue. Upon removing the duplicate, Aurora reports that something is now working, but not when using the “modern category and group” theme, and shares screenshots of the page with German text describing neurodiverse self-help groups and an interface with illustrations about different types of regions.

How can I view categories and “latest” on mobile?

Like this?

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.

Thank you very much. I tried, but it didn’t change anything. :thinking:

I would check the following, make sure:

  • Code is in the JS tab (and not others places)
  • Your code is enabled (if you created a TC, make sure it’s attached to the theme)
  • You have the desktop_category_page_style site setting to categories_and_latest_topics
  • If you have errors in the browser’s console

Let’s see if we can figure out why nothing happens.

I think I did it all that way. Is it correct copied?

You can remove the duplicate. That’s likely the reason it doesn’t work (these 5 lines):

Now something is coming! But not when I use “modern category and group” Thing.

:sweat_smile: