Skip to content

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

DEV: add valueTransformer customization points to flag.gjs (#33113)

marstallc2b707306a64
diff --git a/app/assets/javascripts/discourse/app/components/post/menu/buttons/flag.gjs b/app/assets/javascripts/discourse/app/components/post/menu/buttons/flag.gjs
index da81b5354ad..2f612aca63f 100644
--- a/app/assets/javascripts/discourse/app/components/post/menu/buttons/flag.gjs
+++ b/app/assets/javascripts/discourse/app/components/post/menu/buttons/flag.gjs
@@ -3,17 +3,33 @@ import { action } from "@ember/object";
import { gt } from "truth-helpers";
import DButton from "discourse/components/d-button";
import concatClass from "discourse/helpers/concat-class";
+import { applyValueTransformer } from "discourse/lib/transformer";
import DiscourseURL from "discourse/lib/url";
export default class PostMenuFlagButton extends Component {
static shouldRender(args, helper) {
- const { reviewable_id, canFlag, hidden } = args.post;
- return (
+ const { post } = args;
+ const { reviewable_id, canFlag, hidden } = post;
+
+ let show =
reviewable_id ||
(canFlag && !hidden) ||
(helper.siteSettings.allow_all_users_to_flag_illegal_content &&
- !helper.currentUser)
- );
+ !helper.currentUser);
+
+ return applyValueTransformer("flag-button-render-decision", show, { post });
+ }
+
+ get dynamicFlagButtonClass() {
+ return applyValueTransformer("flag-button-dynamic-class", "", {
+ post: this.args.post,
+ });
+ }
+
+ get isFlagButtonDisabled() {
+ return applyValueTransformer("flag-button-disabled-state", false, {
+ post: this.args.post,
+ });
}
get title() {
@@ -42,12 +58,16 @@ export default class PostMenuFlagButton extends Component {
</DButton>
{{/if}}
<DButton
- class="post-action-menu__flag create-flag"
+ class={{concatClass
+ "post-action-menu__flag create-flag"
+ this.dynamicFlagButtonClass
+ }}
...attributes
@action={{@buttonActions.showFlags}}
@icon="flag"
@label={{if @showLabel "post.controls.flag_action"}}
@title={{this.title}}
+ @disabled={{this.isFlagButtonDisabled}}
/>
</div>
</template>
diff --git a/app/assets/javascripts/discourse/app/components/post/menu/buttons/like.gjs b/app/assets/javascripts/discourse/app/components/post/menu/buttons/like.gjs
index 680cdd22f89..1c6725b786f 100644
--- a/app/assets/javascripts/discourse/app/components/post/menu/buttons/like.gjs
+++ b/app/assets/javascripts/discourse/app/components/post/menu/buttons/like.gjs
@@ -6,11 +6,15 @@ import DButton from "discourse/components/d-button";
import concatClass from "discourse/helpers/concat-class";
import icon from "discourse/helpers/d-icon";
import discourseLater from "discourse/lib/later";
+import { applyValueTransformer } from "discourse/lib/transformer";
import { i18n } from "discourse-i18n";
export default class PostMenuLikeButton extends Component {
static shouldRender(args) {
- return args.post.showLike || args.post.likeCount > 0;
+ const show = args.post.showLike || args.post.likeCount > 0;
+ return applyValueTransformer("like-button-render-decision", show, {
+ post: args.post,
+ });
}
@service currentUser;
diff --git a/app/assets/javascripts/discourse/app/lib/transformer/registry.js b/app/assets/javascripts/discourse/app/lib/transformer/registry.js
index 5bbfda345c4..91027444789 100644
--- a/app/assets/javascripts/discourse/app/lib/transformer/registry.js
+++ b/app/assets/javascripts/discourse/app/lib/transformer/registry.js
@@ -20,6 +20,9 @@ export const VALUE_TRANSFORMERS = Object.freeze([
"composer-save-button-label",
"composer-service-cannot-submit-post",
"create-topic-label",
+ "flag-button-render-decision",
+ "flag-button-dynamic-class",
+ "flag-button-disabled-state",
"flag-description",
"flag-custom-placeholder",
"flag-formatted-name",
@@ -30,6 +33,7 @@ export const VALUE_TRANSFORMERS = Object.freeze([
"home-logo-minimized",
"invite-simple-mode-topic",
"latest-topic-list-item-class",
+ "like-button-render-decision",
"mentions-class",
"more-topics-tabs",
"move-to-topic-merge-options",
diff --git a/app/assets/javascripts/discourse/tests/integration/components/post/menu/buttons/flag-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/post/menu/buttons/flag-test.gjs
new file mode 100644
index 00000000000..48b98b682dd
--- /dev/null
+++ b/app/assets/javascripts/discourse/tests/integration/components/post/menu/buttons/flag-test.gjs
@@ -0,0 +1,83 @@
+import { render } from "@ember/test-helpers";
+import { hbs } from "ember-cli-htmlbars";
+import { setupRenderingTest } from "ember-qunit";
+import { module, test } from "qunit";
+import PostMenuFlagButton from "discourse/components/post/menu/buttons/flag"; // Corrected path
+import { withPluginApi } from "discourse/lib/plugin-api";
+
+module("Integration | Component | Post Menu Flag Button", function (hooks) {
+ setupRenderingTest(hooks);
+
+ hooks.beforeEach(function () {
+ // Baseline args and helper for shouldRender to be true initially
+ this.args = {
+ post: {
+ canFlag: true,
+ hidden: false,
+ reviewable_id: null, // Not relying on this for baseline true
+ // For rendered component tests
+ reviewable_score_pending_count: 0,
+ reviewable_score_count: 0,
+ currentUser: false, // post.currentUser (user has not flagged this post)
+ },
+ };
+
+ this.helper = {
+ siteSettings: { reviewable_claiming_enabled: true },
+ currentUser: { id: 1, username: "test_user" }, // Updated mock user object
+ };
+ });
+
+ test("shouldRender returns true with baseline args and helper", function (assert) {
+ assert.true(
+ PostMenuFlagButton.shouldRender(this.args, this.helper),
+ "shouldRender is true by default with baseline setup"
+ );
+ });
+
+ test("shouldRender returns false when 'flag-button-render-decision' transformer returns false", function (assert) {
+ withPluginApi("0.1", (api) => {
+ api.registerValueTransformer("flag-button-render-decision", () => false);
+ });
+ assert.false(
+ PostMenuFlagButton.shouldRender(this.args, this.helper),
+ "shouldRender is false due to transformer"
+ );
+ });
+
+ test("'flag-button-disabled-state' transformer to true causes DButton to be disabled", async function (assert) {
+ withPluginApi("0.1", (api) => {
+ api.registerValueTransformer("flag-button-render-decision", () => true); // Ensure component renders
+ api.registerValueTransformer("flag-button-disabled-state", () => true);
+ });
+
+ this.set("postForRender", this.args.post);
+ await render(
+ hbs`<Post::Menu::Buttons::Flag @post={{this.postForRender}} />`
+ );
+
+ assert
+ .dom("button.create-flag")
+ .isDisabled("Button is disabled by transformer");
+ });
+
+ test("'flag-button-dynamic-class' transformer applies the given style", async function (assert) {
+ const testClass = "my-special-flag-style";
+ withPluginApi("0.1", (api) => {
+ api.registerValueTransformer("flag-button-render-decision", () => true); // Ensure component renders
+ api.registerValueTransformer(
+ "flag-button-dynamic-class",
+ () => testClass
+ );
+ });
+
+ this.set("postForRender", this.args.post);
+ await render(
+ hbs`<Post::Menu::Buttons::Flag @post={{this.postForRender}} />`
+ );
+
+ assert
+ .dom("button.create-flag")
+ .hasClass(testClass, "Button has the custom class from transformer");
+ });
+});