A platform for community discussion. Free, open, simple.
DEV: Add sidebar-discovery block outlet to discovery layout (#38937)
Sérgio Saquetim103408f0eb01
diff --git a/frontend/discourse/app/blocks/block-outlet.gjs b/frontend/discourse/app/blocks/block-outlet.gjsindex b3764ca3613..6f7f2030354 100644--- a/frontend/discourse/app/blocks/block-outlet.gjs+++ b/frontend/discourse/app/blocks/block-outlet.gjs@@ -380,12 +380,13 @@ export function _renderBlocks(outletName, layout, owner, callSiteError = null) {/*** Checks if a layout has been registered for a given outlet.- * Used in templates to conditionally render content based on block presence.+ *+ * @internal This is an internal API. Use the `blocks` service's `hasLayout()` method instead.** @param {string} outletName - The outlet identifier to check.* @returns {boolean} True if a layout is registered for this outlet.*/-function hasLayout(outletName) {+export function _hasLayout(outletName) {return outletLayouts.has(outletName);}@@ -560,7 +561,7 @@ export default class BlockOutlet extends Component {{{! yield to :before block with hasLayout boolean for conditional renderingThis allows block outlets to wrap other elements and conditionally render them based onthe presence of a registered layout if necessary }}- {{yield (hasLayout this.outletName) to="before"}}+ {{yield (_hasLayout this.outletName) to="before"}}{{#let(if@@ -631,7 +632,7 @@ export default class BlockOutlet extends Component {{{! yield to :after block with hasLayout boolean for conditional renderingThis allows block outlets to wrap other elements and conditionally render them based onthe presence of a registered layout if necessary }}- {{yield (hasLayout this.outletName) to="after"}}+ {{yield (_hasLayout this.outletName) to="after"}}</template>}diff --git a/frontend/discourse/app/components/discovery/layout.gjs b/frontend/discourse/app/components/discovery/layout.gjsindex 1024a912c43..f9200f868eb 100644--- a/frontend/discourse/app/components/discovery/layout.gjs+++ b/frontend/discourse/app/components/discovery/layout.gjs@@ -1,92 +1,146 @@+import Component from "@glimmer/component";+import { service } from "@ember/service";+import BlockOutlet from "discourse/blocks/block-outlet";import CategoryReadOnlyBanner from "discourse/components/category-read-only-banner";import DiscourseBanner from "discourse/components/discourse-banner";import PluginOutlet from "discourse/components/plugin-outlet";import concatClass from "discourse/helpers/concat-class";import lazyHash from "discourse/helpers/lazy-hash";-const Layout = <template>- <DiscourseBanner />- {{#if @model.category}}- <CategoryReadOnlyBanner- @category={{@model.category}}- @readOnly={{@createTopicDisabled}}- />+/* Renders its content inside a <div> with the given class when @when is true,+ or renders the content directly when false. */+const ConditionalWrap = <template>+ {{#if @when}}+ <div class={{@class}}>{{yield}}</div>+ {{else}}+ {{yield}}{{/if}}+</template>;++export default class Layout extends Component {+ @service blocks;- <PluginOutlet- @name="discovery-list-controls-above"- @connectorTagName="div"- @outletArgs={{lazyHash- toggleTagInfo=@toggleTagInfo- }}- />+ get hasSidebarLayout() {+ return this.blocks.hasLayout("sidebar-discovery");+ }- <div class="list-controls">- <PluginOutlet- @name="discovery-navigation-bar-above"- @connectorTagName="div"- @outletArgs={{lazyHash [email protected] [email protected]}}- />- <div class="container">- {{yield to="navigation"}}- </div>- </div>+ <template>+ <ConditionalWrap @when={{this.hasSidebarLayout}} @class="discovery-layout">+ <DiscourseBanner />- <PluginOutlet- @name="discovery-above"- @connectorTagName="div"- @outletArgs={{lazyHash- model=@model- }}- />+ <ConditionalWrap+ @when={{this.hasSidebarLayout}}+ @class="discovery-layout__category-header"+ >+ {{#if @model.category}}+ <CategoryReadOnlyBanner+ @category={{@model.category}}+ @readOnly={{@createTopicDisabled}}+ />+ {{/if}}+ </ConditionalWrap>- <div class={{concatClass "container list-container" @listClass}}>- <div class="row full-width">- <div id="header-list-area">- {{yield to="header"}}+ <ConditionalWrap+ @when={{this.hasSidebarLayout}}+ @class="discovery-layout__navigation"+ ><PluginOutlet- @name="header-list-container-bottom"- @outletArgs={{lazyHash [email protected] [email protected]}}+ @name="discovery-list-controls-above"+ @connectorTagName="div"+ @outletArgs={{lazyHash+ toggleTagInfo=@toggleTagInfo+ }}/>- </div>- </div>- <div class="row full-width">-- <PluginOutlet- @name="before-list-area"- @outletArgs={{lazyHash [email protected] [email protected]}}- />+ <div class="list-controls">+ <PluginOutlet+ @name="discovery-navigation-bar-above"+ @connectorTagName="div"+ @outletArgs={{lazyHash [email protected] [email protected]}}+ />+ <div class="container">+ {{yield to="navigation"}}+ </div>+ </div>+ </ConditionalWrap>- <div id="list-area">+ <ConditionalWrap+ @when={{this.hasSidebarLayout}}+ @class="discovery-layout__content"+ ><PluginOutlet- @name="discovery-list-area"+ @name="discovery-above"+ @connectorTagName="div"@outletArgs={{lazyHashmodel=@model}}- @defaultGlimmer={{true}}+ />++ <ConditionalWrap+ @when={{this.hasSidebarLayout}}+ @class="discovery-layout__list">- <PluginOutlet- @name="discovery-list-container-top"- @connectorTagName="span"- @outletArgs={{lazyHash [email protected] [email protected]}}- />- {{yield to="list"}}- </PluginOutlet>- </div>- </div>- </div>+ <div class={{concatClass "container list-container" @listClass}}>+ <div class="row full-width">+ <div id="header-list-area">+ {{yield to="header"}}+ <PluginOutlet+ @name="header-list-container-bottom"+ @outletArgs={{lazyHash+ }}+ />+ </div>+ </div>+ <div class="row full-width">+ <PluginOutlet+ @name="before-list-area"+ @outletArgs={{lazyHash [email protected] [email protected]}}+ />+ <div id="list-area">+ <PluginOutlet+ @name="discovery-list-area"+ @outletArgs={{lazyHash+ model=@model+ }}+ @defaultGlimmer={{true}}+ >+ <PluginOutlet+ @name="discovery-list-container-top"+ @connectorTagName="span"+ @outletArgs={{lazyHash+ }}+ />+ {{yield to="list"}}+ </PluginOutlet>+ </div>+ </div>+ </div>+ </ConditionalWrap>- <PluginOutlet- @name="discovery-below"- @connectorTagName="div"- @outletArgs={{lazyHash [email protected] [email protected]}}- />-</template>;+ {{#if this.hasSidebarLayout}}+ <div class="discovery-layout__sidebar">+ <BlockOutlet+ @name="sidebar-discovery"+ @outletArgs={{lazyHash [email protected] [email protected]}}+ />+ </div>+ {{/if}}-export default Layout;+ <PluginOutlet+ @name="discovery-below"+ @connectorTagName="div"+ @outletArgs={{lazyHash [email protected] [email protected]}}+ />+ </ConditionalWrap>+ </ConditionalWrap>+ </template>+}diff --git a/frontend/discourse/app/lib/registry/block-outlets.js b/frontend/discourse/app/lib/registry/block-outlets.jsindex 5cf7d441a3a..17c54b86e64 100644--- a/frontend/discourse/app/lib/registry/block-outlets.js+++ b/frontend/discourse/app/lib/registry/block-outlets.js@@ -41,6 +41,7 @@ export const BLOCK_OUTLETS = Object.freeze(["homepage-blocks","main-outlet-blocks","sidebar-blocks",+ "sidebar-discovery",]);// Validate outlet names follow the kebab-case pattern.diff --git a/frontend/discourse/app/services/blocks.js b/frontend/discourse/app/services/blocks.jsindex 64a4f8da257..cb6f9eeb4ce 100644--- a/frontend/discourse/app/services/blocks.js+++ b/frontend/discourse/app/services/blocks.js@@ -1,6 +1,8 @@// @ts-checkimport { getOwner, setOwner } from "@ember/owner";import Service from "@ember/service";+/** @type {import("discourse/blocks/block-outlet.gjs")} */+import { _hasLayout } from "discourse/blocks/block-outlet";import { debugHooks } from "discourse/lib/blocks/-internals/debug-hooks";import { getBlockMetadata } from "discourse/lib/blocks/-internals/decorator";import { evaluateConditions } from "discourse/lib/blocks/-internals/matching/condition-evaluator";@@ -98,6 +100,27 @@ export default class Blocks extends Service {return getAllOutlets();}+ /**+ * Checks if a layout has been registered for a given block outlet.+ *+ * A layout is registered when a plugin or theme calls `api.renderBlocks()`+ * for an outlet. This method allows checking layout presence outside of+ * `BlockOutlet` templates.+ *+ * @param {string} outletName - The outlet identifier to check.+ * @returns {boolean} True if a layout is registered for this outlet.+ *+ * @example+ * ```javascript+ * if (this.blocks.hasLayout("homepage-blocks")) {+ * // Outlet has blocks registered+ * }+ * ```+ */+ hasLayout(outletName) {+ return _hasLayout(outletName);+ }+/** Block Registry Methods*/diff --git a/frontend/discourse/tests/acceptance/discovery-layout-test.gjs b/frontend/discourse/tests/acceptance/discovery-layout-test.gjsnew file mode 100644index 00000000000..a3a6e4198d1--- /dev/null+++ b/frontend/discourse/tests/acceptance/discovery-layout-test.gjs@@ -0,0 +1,52 @@+import Component from "@glimmer/component";+import { visit } from "@ember/test-helpers";+import { test } from "qunit";+import { block } from "discourse/blocks";+import { _renderBlocks } from "discourse/blocks/block-outlet";+import {+ registerBlock,+ withTestBlockRegistration,+} from "discourse/tests/helpers/block-testing";+import { acceptance } from "discourse/tests/helpers/qunit-helpers";++acceptance("Discovery Layout", function () {+ test("does not render the discovery-layout wrapper when no blocks are registered", async function (assert) {+ await visit("/latest");++ assert.dom("#list-area").exists();+ assert.dom(".discovery-layout").doesNotExist();+ assert.dom(".discovery-layout__category-header").doesNotExist();+ assert.dom(".discovery-layout__navigation").doesNotExist();+ assert.dom(".discovery-layout__content").doesNotExist();+ assert.dom(".discovery-layout__list").doesNotExist();+ assert.dom(".discovery-layout__sidebar").doesNotExist();+ });++ test("renders the discovery-layout wrapper with sidebar when blocks are registered", async function (assert) {+ @block("sidebar-test-block")+ class SidebarTestBlock extends Component {+ <template>+ <div class="sidebar-test-content">Sidebar Content</div>+ </template>+ }++ withTestBlockRegistration(() => {+ registerBlock(SidebarTestBlock);+ });++ _renderBlocks("sidebar-discovery", [{ block: SidebarTestBlock }]);++ await visit("/latest");++ assert.dom(".discovery-layout").exists();+ assert.dom(".discovery-layout__category-header").exists();+ assert.dom(".discovery-layout__navigation").exists();+ assert.dom(".discovery-layout__navigation .list-controls").exists();+ assert.dom(".discovery-layout__content").exists();+ assert.dom(".discovery-layout__list #list-area").exists();+ assert.dom(".discovery-layout__sidebar").exists();+ assert+ .dom(".discovery-layout__sidebar .sidebar-test-content")+ .hasText("Sidebar Content");+ });+});diff --git a/frontend/discourse/tests/unit/services/blocks-test.js b/frontend/discourse/tests/unit/services/blocks-test.jsindex 80fbe7421c2..94767eba049 100644--- a/frontend/discourse/tests/unit/services/blocks-test.js+++ b/frontend/discourse/tests/unit/services/blocks-test.js@@ -3,6 +3,7 @@ import { getOwner } from "@ember/owner";import { setupTest } from "ember-qunit";import { module, test } from "qunit";import { block } from "discourse/blocks";+import { _renderBlocks } from "discourse/blocks/block-outlet";import { BlockCondition, blockCondition } from "discourse/blocks/conditions";import { getBlockMetadata } from "discourse/lib/blocks/-internals/decorator";import {@@ -99,6 +100,25 @@ module("Unit | Service | blocks", function (hooks) {});});+ module("block outlet", function () {+ test("hasLayout returns false when no layout is registered", function (assert) {+ assert.false(this.blocks.hasLayout("hero-blocks"));+ });++ test("hasLayout returns true after a layout is registered", function (assert) {+ @block("has-layout-test-block")+ class HasLayoutTestBlock extends Component {}++ withTestBlockRegistration(() => {+ registerBlock(HasLayoutTestBlock);+ });++ _renderBlocks("hero-blocks", [{ block: HasLayoutTestBlock }]);++ assert.true(this.blocks.hasLayout("hero-blocks"));+ });+ });+module("built-in conditions", function () {test("registers built-in condition types", function (assert) {assert.true(this.blocks.hasConditionType("route"));