From dc96b6e953993afb752ecc037f4102ae7b99391b Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Thu, 31 Oct 2024 01:06:02 +0100 Subject: [PATCH] DEV: Add `owner` arg to plugin connector's `shouldRender` callbacks (#29488) --- .../discourse/app/components/plugin-outlet.js | 4 +++- .../discourse/app/lib/plugin-connectors.js | 8 +++++--- .../components/plugin-outlet-test.gjs | 19 +++++++++++++++++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/discourse/app/components/plugin-outlet.js b/app/assets/javascripts/discourse/app/components/plugin-outlet.js index a35ca161023..aa50a47a5b1 100644 --- a/app/assets/javascripts/discourse/app/components/plugin-outlet.js +++ b/app/assets/javascripts/discourse/app/components/plugin-outlet.js @@ -1,6 +1,7 @@ import { cached } from "@glimmer/tracking"; import ClassicComponent from "@ember/component"; import { get } from "@ember/object"; +import { getOwner } from "@ember/owner"; import { service } from "@ember/service"; import GlimmerComponentWithDeprecatedParentView from "discourse/components/glimmer-component-with-deprecated-parent-view"; import { @@ -88,7 +89,8 @@ export default class PluginOutletComponent extends GlimmerComponentWithDeprecate const connectors = renderedConnectorsFor( this.args.name, this.outletArgsWithDeprecations, - this.context + this.context, + getOwner(this) ); if (connectors.length > 1 && hasBlock) { const message = `Multiple connectors were registered for the ${this.args.name} outlet. Using the first.`; diff --git a/app/assets/javascripts/discourse/app/lib/plugin-connectors.js b/app/assets/javascripts/discourse/app/lib/plugin-connectors.js index 8e33c5c1cdf..dcd0f7be676 100644 --- a/app/assets/javascripts/discourse/app/lib/plugin-connectors.js +++ b/app/assets/javascripts/discourse/app/lib/plugin-connectors.js @@ -224,10 +224,12 @@ export function connectorsFor(outletName) { return _connectorCache[outletName] || []; } -export function renderedConnectorsFor(outletName, args, context) { +export function renderedConnectorsFor(outletName, args, context, owner) { return connectorsFor(outletName).filter((con) => { - const shouldRender = con.connectorClass?.shouldRender; - return !shouldRender || shouldRender(args, context); + return ( + !con.connectorClass?.shouldRender || + con.connectorClass?.shouldRender(args, context, owner) + ); }); } diff --git a/app/assets/javascripts/discourse/tests/integration/components/plugin-outlet-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/plugin-outlet-test.gjs index cd6a55ffc49..009945fb681 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/plugin-outlet-test.gjs +++ b/app/assets/javascripts/discourse/tests/integration/components/plugin-outlet-test.gjs @@ -60,8 +60,12 @@ module("Integration | Component | plugin-outlet", function (hooks) { }); registerTemporaryModule(`${CLASS_PREFIX}/test-name/conditional-render`, { - shouldRender(args, context) { - return args.shouldDisplay || context.siteSettings.always_display; + shouldRender(args, context, owner) { + return ( + args.shouldDisplay || + context.siteSettings.always_display || + owner.lookup("service:site-settings").alternativeAccess + ); }, }); @@ -407,6 +411,17 @@ module("Integration | Component | plugin-outlet", function (hooks) { assert.dom(".conditional-render").exists("renders conditional outlet"); }); + test("shouldRender receives an owner argument", async function (assert) { + await render(hbs``); + assert + .dom(".conditional-render") + .doesNotExist("doesn't render conditional outlet"); + + getOwner(this).lookup("service:site-settings").alternativeAccess = true; + await settled(); + assert.dom(".conditional-render").exists("renders conditional outlet"); + }); + test("Other outlets are not re-rendered", async function (assert) { this.set("shouldDisplay", false); await render(hbs`