Skip to content

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

DEV: Allow a simplier way to register a link under sidebar topics section (#16916)

Alan Guo Xiang Tan037436047d35
diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.js b/app/assets/javascripts/discourse/app/lib/plugin-api.js
index e586be839c6..6c4c308bde4 100644
--- a/app/assets/javascripts/discourse/app/lib/plugin-api.js
+++ b/app/assets/javascripts/discourse/app/lib/plugin-api.js
@@ -1634,11 +1634,15 @@ class PluginApi {
* api.addTopicsSectionLink((baseSectionLink) => {
* return class CustomSectionLink extends baseSectionLink {
* get name() {
- * returns "bookmarked"
+ * returns "bookmarked";
* }
*
* get route() {
- * returns "userActivity.bookmarks"
+ * returns "userActivity.bookmarks";
+ * }
+ *
+ * get model() {
+ * return this.currentUser;
* }
*
* get title() {
@@ -1652,14 +1656,29 @@ class PluginApi {
* })
* ```
*
- * @callback addTopicsSectionLinkCallback
- * @param {BaseSectionLink} baseSectionLink Factory class to inherit from.
- * @returns {BaseSectionLink} A class that extends BaseSectionLink.
+ * or
*
- * @param {addTopicsSectionLinkCallback} callback
+ * ```
+ * api.addTopicsSectionLink({
+ * name: "unread",
+ * route: "discovery.unread",
+ * title: I18n.t("some.unread.title"),
+ * text: I18n.t("some.unread.text")
+ * })
+ * ```
+ *
+ * @callback addTopicsSectionLinkCallback
+ * @param {BaseSectionLink} baseSectionLink - Factory class to inherit from.
+ * @returns {BaseSectionLink} - A class that extends BaseSectionLink.
+ *
+ * @param {(addTopicsSectionLinkCallback|Object)} arg - A callback function or an Object.
+ * @param {string} arg.name - The name of the link. Needs to be dasherized and lowercase.
+ * @param {string} arg.route - The Ember route of the link.
+ * @param {string} arg.title - The title attribute for the link.
+ * @param {string} arg.text - The text to display for the link.
*/
- async addTopicsSectionLink(callback) {
- addSectionLink(callback);
+ addTopicsSectionLink(arg) {
+ addSectionLink(arg);
}
}
diff --git a/app/assets/javascripts/discourse/app/lib/sidebar/custom-topics-section-links.js b/app/assets/javascripts/discourse/app/lib/sidebar/custom-topics-section-links.js
index 22543a0a043..3d5384c80b0 100644
--- a/app/assets/javascripts/discourse/app/lib/sidebar/custom-topics-section-links.js
+++ b/app/assets/javascripts/discourse/app/lib/sidebar/custom-topics-section-links.js
@@ -8,10 +8,36 @@ export let customSectionLinks = [];
* @param {BaseSectionLink} baseSectionLink Factory class to inherit from.
* @returns {BaseSectionLink} A class that extends BaseSectionLink.
*
- * @param {addTopicsSectionLinkCallback} callback
+ * @param {(addTopicsSectionLinkCallback|Object)} arg - A callback function or an Object.
+ * @param {string} arg.name - The name of the link. Needs to be dasherized and lowercase.
+ * @param {string} arg.route - The Ember route of the link.
+ * @param {string} arg.title - The title attribute for the link.
+ * @param {string} arg.text - The text to display for the link.
*/
-export function addSectionLink(callback) {
- customSectionLinks.push(callback.call(this, BaseSectionLink));
+export function addSectionLink(arg) {
+ if (typeof arg === "function") {
+ customSectionLinks.push(arg.call(this, BaseSectionLink));
+ } else {
+ const klass = class extends BaseSectionLink {
+ get name() {
+ return arg.name;
+ }
+
+ get route() {
+ return arg.route;
+ }
+
+ get text() {
+ return arg.text;
+ }
+
+ get title() {
+ return arg.title;
+ }
+ };
+
+ customSectionLinks.push(klass);
+ }
}
export function resetDefaultSectionLinks() {
diff --git a/app/assets/javascripts/discourse/app/lib/sidebar/topics-section/base-section-link.js b/app/assets/javascripts/discourse/app/lib/sidebar/topics-section/base-section-link.js
index 61ed6b53ac4..3712d0a5659 100644
--- a/app/assets/javascripts/discourse/app/lib/sidebar/topics-section/base-section-link.js
+++ b/app/assets/javascripts/discourse/app/lib/sidebar/topics-section/base-section-link.js
@@ -8,7 +8,7 @@ export default class BaseSectionLink {
}
/**
- * @returns {string} The name of the section link
+ * @returns {string} The name of the section link. Needs to be dasherized and lowercase.
*/
get name() {
this._notImplemented();
diff --git a/app/assets/javascripts/discourse/tests/acceptance/sidebar-topics-section-test.js b/app/assets/javascripts/discourse/tests/acceptance/sidebar-topics-section-test.js
index 9bcfe629223..d518a5a5d30 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/sidebar-topics-section-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/sidebar-topics-section-test.js
@@ -410,7 +410,40 @@ acceptance("Sidebar - Topics Section", function (needs) {
);
conditionalTest(
- "adding section link via plugin API",
+ "adding section link via plugin API with Object",
+ !isLegacyEmber(),
+ async function (assert) {
+ withPluginApi("1.2.0", (api) => {
+ api.addTopicsSectionLink({
+ name: "unread",
+ route: "discovery.unread",
+ text: "unread topics",
+ title: "List of unread topics",
+ });
+ });
+
+ await visit("/");
+
+ assert.strictEqual(
+ query(".sidebar-section-link-unread").textContent.trim(),
+ "unread topics",
+ "displays the right text for the link"
+ );
+
+ assert.strictEqual(
+ query(".sidebar-section-link-unread").title,
+ "List of unread topics",
+ "displays the right title for the link"
+ );
+
+ await click(".sidebar-section-link-unread");
+
+ assert.strictEqual(currentURL(), "/unread", "links to the right URL");
+ }
+ );
+
+ conditionalTest(
+ "adding section link via plugin API with callback function",
!isLegacyEmber(),
async function (assert) {
withPluginApi("1.2.0", (api) => {