How can I edit sidebar links that are added with api.addCommunitySectionLink?

I’d like to add a new sidebar link with a component. I’m using this code:

      api.addCommunitySectionLink({
        name: "custom-homepage",
        route: "discovery.custom",
        title: "Home",
        text: "Home",
        icon: "house",
      });

This adds the link, but on the customize modal it doesn’t show up. So I can’t edit the default link or drag it to a different position.

I see the same is true for a link added by the Calendar plugin. Here’s the link:

But on the customize modal the link doesn’t show up:

So I wonder if this is a bug. Or if there’s another approach?

3 Likes

You can’t unfortunately,

4 Likes

Would the changes be reverted on every component or plugin update?

I guess the approach is then to not add a default link at all.

2 Likes

only if you’re overwriting the component/plugin directly… if you want to change the order in the existing section, it’s probably easiest to reorder with CSS

#sidebar-section-content-community {
  display: flex;
  flex-direction: column;
  
  li {
    order: 2
  }
  
  [data-list-item-name="upcoming-events"] {
    order: 1
  }
}

If you want to move the item to the “more” drawer, hide the original and duplicate it in a separate component:

#sidebar-section-content-community {
  [data-list-item-name="upcoming-events"] {
    display: none;
  }
}
import { apiInitializer } from "discourse/lib/api";
import { i18n } from "discourse-i18n";

export default apiInitializer((api) => {
  const siteSettings = api.container.lookup("service:site-settings");
  if (
    siteSettings.discourse_post_event_enabled &&
    siteSettings.sidebar_show_upcoming_events
  ) {
    api.addCommunitySectionLink(
      {
        name: "custom-upcoming-events",
        route: "discourse-post-event-upcoming-events",
        text: i18n("discourse_post_event.upcoming_events.title"),
        title: i18n("discourse_post_event.upcoming_events.title"),
        icon: "calendar-days",
      },
      true,
    );
  }
});
2 Likes

If the new links can’t be modified on the customize modal, I guess I’ll drop the idea of adding them for now. It just seems confusing to add a link that is elusive on the customize modal then.

However, I’d want the link (if added manually) be highlighted when on the related route, so I’ll add this now:

import { apiInitializer } from "discourse/lib/api";

export default apiInitializer("0.8", (api) => {
  const router = api.container.lookup("service:router");

  api.onPageChange(() => {
    const currentRoute = router.currentRoute.name;
    const customHomepageLink = document.querySelector(
      '.sidebar-section-link[href="/custom"]'
    );

    if (currentRoute === "discovery.custom") {
      if (customHomepageLink) {
        customHomepageLink.classList.add("active");
      }
    } else {
      if (customHomepageLink) {
        customHomepageLink.classList.remove("active");
      }
    }
  });
});
1 Like