Skip to content

Commit b71a64b

Browse files
committed
DEV: Introduce block registration API and deprecate ember resolution
In the near future, runtime resolution of components via the ember resolver will no longer be possible. This commit introduces a dedicated list of Right Sidebar Blocks components, and allows other themes/plugins to register additional ones. This also avoids the issue of normal component names clashing with rsb block names, so specific handling of cases like `custom-html-rsb` is no longer necessary. Other themes/plugins can register new blocks via the new `right-sidebar-blocks` value transformer ```js api.registerValueTransformer( "right-sidebar-blocks", ({ value: blocks }) => blocks.set("my-amazing-block", MyAmazingBlock) ); ``` For now, resolution of components via the Ember resolver is still supported, but will print a deprecation message to the console with upgrade instructions.
1 parent fd584ed commit b71a64b

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

javascripts/discourse/components/right-sidebar-blocks.gjs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
import Component from "@glimmer/component";
2-
import { tracked } from "@glimmer/tracking";
2+
import { cached } from "@glimmer/tracking";
33
import { getOwner } from "@ember/owner";
44
import curryComponent from "ember-curry-component";
55
import PluginOutlet from "discourse/components/plugin-outlet";
66
import lazyHash from "discourse/helpers/lazy-hash";
7-
import CustomHtmlRsb from "./custom-html-rsb";
7+
import deprecated from "discourse/lib/deprecated";
8+
import { getAvailableBlocks } from "../pre-initializers/right-sidebar-blocks-registry";
89

910
export default class RightSidebarBlocks extends Component {
10-
@tracked blocks = [];
11-
12-
constructor() {
13-
super(...arguments);
11+
@cached
12+
get blocks() {
13+
const availableBlocks = getAvailableBlocks();
1414

1515
const blocksArray = [];
1616

1717
JSON.parse(settings.blocks).forEach((block) => {
18-
if (block.name === "custom-html") {
19-
block.component = CustomHtmlRsb;
18+
const rsbRegistryResult = availableBlocks.get(block.name);
19+
if (rsbRegistryResult) {
20+
block.component = rsbRegistryResult;
2021
} else {
21-
block.component = getOwner(this).resolveRegistration(
22+
const emberRegistryResult = getOwner(this).resolveRegistration(
2223
`component:${block.name}`
2324
);
25+
if (emberRegistryResult) {
26+
block.component = emberRegistryResult;
27+
deprecated(
28+
`The block "${block.name}" is not registered in the right-sidebar-blocks registry. Register it using \`api.registerValueTransformer("right-sidebar-blocks", ({ value: blocks }) => blocks.set("${block.name}", MyImportantComponent));\``,
29+
{ id: "discourse.right-sidebar-blocks.component-resolution" }
30+
);
31+
}
2432
}
2533

2634
if (block.component) {
@@ -41,7 +49,7 @@ export default class RightSidebarBlocks extends Component {
4149
}
4250
});
4351

44-
this.blocks = blocksArray;
52+
return blocksArray;
4553
}
4654

4755
<template>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import SignupCta from "discourse/components/signup-cta";
2+
import { withPluginApi } from "discourse/lib/plugin-api";
3+
import { applyValueTransformer } from "discourse/lib/transformer";
4+
import CategoryList from "../components/category-list";
5+
import CategoryTopics from "../components/category-topics";
6+
import CustomHtmlRsb from "../components/custom-html-rsb";
7+
import PopularTags from "../components/popular-tags";
8+
import RecentReplies from "../components/recent-replies";
9+
import SubcategoryList from "../components/subcategory-list";
10+
import TopContributors from "../components/top-contributors";
11+
12+
const TRANSFORMER_NAME = "right-sidebar-blocks";
13+
14+
const DefaultBlocks = new Map();
15+
DefaultBlocks.set("popular-tags", PopularTags);
16+
DefaultBlocks.set("top-contributers", TopContributors);
17+
DefaultBlocks.set("recent-replies", RecentReplies);
18+
DefaultBlocks.set("category-topics", CategoryTopics);
19+
DefaultBlocks.set("custom-html", CustomHtmlRsb);
20+
DefaultBlocks.set("category-list", CategoryList);
21+
DefaultBlocks.set("subcategory-list", SubcategoryList);
22+
DefaultBlocks.set("signup-cta", SignupCta);
23+
24+
export function getAvailableBlocks() {
25+
const results = new Map(DefaultBlocks);
26+
return applyValueTransformer(TRANSFORMER_NAME, results, null, {
27+
mutable: true,
28+
});
29+
}
30+
31+
export default {
32+
before: "freeze-valid-transformers",
33+
initialize() {
34+
withPluginApi((api) => api.addValueTransformerName(TRANSFORMER_NAME));
35+
},
36+
};

0 commit comments

Comments
 (0)