Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add ability to render empty state for custom sidebar section (#32518)

Mark VanLandingham56edbcd9e278
diff --git a/app/assets/javascripts/discourse/app/components/sidebar/api-section.gjs b/app/assets/javascripts/discourse/app/components/sidebar/api-section.gjs
index 2b5d117cd5d..403e5d3a111 100644
--- a/app/assets/javascripts/discourse/app/components/sidebar/api-section.gjs
+++ b/app/assets/javascripts/discourse/app/components/sidebar/api-section.gjs
@@ -1,4 +1,4 @@
-import { and, eq } from "truth-helpers";
+import { and, eq, not } from "truth-helpers";
import Section from "./section";
import SectionLink from "./section-link";
@@ -19,6 +19,12 @@ const SidebarApiSection = <template>
@expandWhenActive={{@expandWhenActive}}
@scrollActiveLinkIntoView={{@scrollActiveLinkIntoView}}
>
+ {{#if
+ (and @section.emptyStateComponent (not @section.filteredLinks.length))
+ }}
+ <@section.emptyStateComponent />
+ {{/if}}
+
{{#each @section.filteredLinks key="name" as |link|}}
<SectionLink
@linkName={{link.name}}
diff --git a/app/assets/javascripts/discourse/tests/acceptance/sidebar-plugin-api-test.js b/app/assets/javascripts/discourse/tests/acceptance/sidebar-plugin-api-test.gjs
similarity index 96%
rename from app/assets/javascripts/discourse/tests/acceptance/sidebar-plugin-api-test.js
rename to app/assets/javascripts/discourse/tests/acceptance/sidebar-plugin-api-test.gjs
index 9a9252010f5..b1c46b7bc11 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/sidebar-plugin-api-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/sidebar-plugin-api-test.gjs
@@ -1,3 +1,4 @@
+import Component from "@glimmer/component";
import { click, settled, visit } from "@ember/test-helpers";
import { test } from "qunit";
import { PLUGIN_API_VERSION, withPluginApi } from "discourse/lib/plugin-api";
@@ -309,6 +310,34 @@ acceptance("Sidebar - Plugin API", function (needs) {
.doesNotExist("displays no links");
});
+ test("Empty state when no links are present", async function (assert) {
+ const emptyStateComponent = class EmptyStateTestComponent extends Component {
+ <template>
+ <div class="test-empty-state">Empty</div>
+ </template>
+ };
+
+ withPluginApi(PLUGIN_API_VERSION, (api) => {
+ api.addSidebarSection((BaseCustomSidebarSection) => {
+ return class extends BaseCustomSidebarSection {
+ name = "test-empty-state";
+ text = "random text";
+ links = [];
+
+ get emptyStateComponent() {
+ return emptyStateComponent;
+ }
+ };
+ });
+ });
+ await visit("/");
+ assert
+ .dom(
+ ".sidebar-section[data-section-name='test-empty-state'] #sidebar-section-content-test-empty-state .test-empty-state"
+ )
+ .hasText("Empty");
+ });
+
test("Section that is not displayed via displaySection", async function (assert) {
withPluginApi(PLUGIN_API_VERSION, (api) => {
api.addSidebarSection((BaseCustomSidebarSection) => {