Skip to content

Commit eb19018

Browse files
rngus2344martin-brennan
authored andcommitted
DEV: Add applyValueTransformer to poster name (#33086)
## Changes This PR adds applyValueTransformer to poster name.
1 parent 4a3c960 commit eb19018

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

app/assets/javascripts/discourse/app/components/post/meta-data/poster-name.gjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ export default class PostMetaDataPosterName extends Component {
8282
);
8383
}
8484

85+
get additionalClasses() {
86+
return applyValueTransformer("poster-name-class", [], {
87+
user: this.user,
88+
});
89+
}
90+
8591
@bind
8692
refreshUserStatus() {
8793
this.#stopTrackingUserStatus();
@@ -139,6 +145,7 @@ export default class PostMetaDataPosterName extends Component {
139145
@post.primary_group_name
140146
(concat "group--" @post.primary_group_name)
141147
)
148+
this.additionalClasses
142149
}}
143150
>
144151
{{! use the position argument to choose between the first and second name if needed}}

app/assets/javascripts/discourse/app/lib/transformer/registry.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export const VALUE_TRANSFORMERS = Object.freeze([
5050
"post-small-action-custom-component",
5151
"post-small-action-icon",
5252
"post-text-selection-prevent-close",
53+
"poster-name-class",
5354
"site-setting-enable-welcome-banner",
5455
"site-setting-search-experience",
5556
"small-user-attrs",

app/assets/javascripts/discourse/app/widgets/poster-name.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { h } from "virtual-dom";
33
import getURL from "discourse/lib/get-url";
44
import { iconNode } from "discourse/lib/icon-library";
55
import { prioritizeNameInUx } from "discourse/lib/settings";
6+
import { applyValueTransformer } from "discourse/lib/transformer";
67
import { formatUsername } from "discourse/lib/utilities";
78
import RenderGlimmer from "discourse/widgets/render-glimmer";
89
import { applyDecorators, createWidget } from "discourse/widgets/widget";
@@ -105,6 +106,12 @@ export default createWidget("poster-name", {
105106
);
106107
},
107108

109+
additionalClasses(attrs) {
110+
return applyValueTransformer("poster-name-class", [], {
111+
user: attrs.user,
112+
});
113+
},
114+
108115
html(attrs) {
109116
const username = attrs.username;
110117
const name = attrs.name;
@@ -129,6 +136,7 @@ export default createWidget("poster-name", {
129136
if (attrs.new_user) {
130137
classNames.push("new-user");
131138
}
139+
classNames.push(...this.additionalClasses(attrs));
132140

133141
const primaryGroupName = attrs.primary_group_name;
134142
if (primaryGroupName && primaryGroupName.length) {

app/assets/javascripts/discourse/tests/integration/components/post/meta-data/poster-name-test.gjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,5 +187,27 @@ module(
187187
.dom("span.poster-icon.test-smile > a > .emoji[alt='smile']")
188188
.exists();
189189
});
190+
191+
test("poster name additional classes", async function (assert) {
192+
this.post.username = "eviltrout";
193+
this.post.user = this.store.createRecord("user", {
194+
username: "eviltrout",
195+
});
196+
withPluginApi((api) => {
197+
api.registerValueTransformer(
198+
"poster-name-class",
199+
({ value, context }) => {
200+
if (context.user.username === "eviltrout") {
201+
value.push(...["custom-class", "another-class"]);
202+
}
203+
return value;
204+
}
205+
);
206+
});
207+
208+
await renderComponent(this.post);
209+
assert.dom("span.custom-class").exists();
210+
assert.dom("span.another-class").exists();
211+
});
190212
}
191213
);

0 commit comments

Comments
 (0)