Skip to content

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

DEV: Upgrade AvatarFlair to Glimmer, enhance avatar handling (#33695)

Sérgio Saquetim6698a0288fe9
diff --git a/app/assets/javascripts/discourse/app/components/avatar-flair.gjs b/app/assets/javascripts/discourse/app/components/avatar-flair.gjs
new file mode 100644
index 00000000000..8c38a1d1488
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/components/avatar-flair.gjs
@@ -0,0 +1,60 @@
+import Component from "@glimmer/component";
+import { concat } from "@ember/helper";
+import { htmlSafe } from "@ember/template";
+import concatClass from "discourse/helpers/concat-class";
+import icon from "discourse/helpers/d-icon";
+import { convertIconClass } from "discourse/lib/icon-library";
+import { escapeExpression } from "discourse/lib/utilities";
+
+export default class AvatarFlair extends Component {
+ get icon() {
+ return convertIconClass(this.args.flairUrl);
+ }
+
+ get isIcon() {
+ return this.args.flairUrl && !this.args.flairUrl.includes("/");
+ }
+
+ get style() {
+ const css = [];
+
+ if (!this.isIcon) {
+ css.push(
+ "background-image: url(" + escapeExpression(this.args.flairUrl) + ")"
+ );
+ }
+
+ if (this.args.flairBgColor) {
+ css.push(
+ "background-color: #" + escapeExpression(this.args.flairBgColor)
+ );
+ }
+
+ if (this.args.flairColor) {
+ css.push("color: #" + escapeExpression(this.args.flairColor));
+ }
+
+ return css.length > 0 ? htmlSafe(css.join("; ")) : null;
+ }
+
+ get title() {
+ return this.args.flairName;
+ }
+
+ <template>
+ <div
+ class={{concatClass
+ "avatar-flair"
+ (concat "avatar-flair-" @flairName)
+ (if @flairBgColor "rounded")
+ (unless this.isIcon "avatar-flair-image")
+ }}
+ style={{this.style}}
+ title={{this.title}}
+ >
+ {{#if this.isIcon}}
+ {{icon this.icon}}
+ {{/if}}
+ </div>
+ </template>
+}
diff --git a/app/assets/javascripts/discourse/app/components/avatar-flair.js b/app/assets/javascripts/discourse/app/components/avatar-flair.js
deleted file mode 100644
index 7c2ceff2a32..00000000000
--- a/app/assets/javascripts/discourse/app/components/avatar-flair.js
+++ /dev/null
@@ -1,21 +0,0 @@
-import { observes } from "@ember-decorators/object";
-import MountWidget from "discourse/components/mount-widget";
-
-// TODO (glimmer-post-stream): this component needs to be converted to Glimmer
-export default class AvatarFlair extends MountWidget {
- widget = "avatar-flair";
-
- @observes("flairName", "flairUrl", "flairBgColor", "flairColor")
- _rerender() {
- this.queueRerender();
- }
-
- buildArgs() {
- return {
- flair_name: this.flairName,
- flair_url: this.flairUrl,
- flair_bg_color: this.flairBgColor,
- flair_color: this.flairColor,
- };
- }
-}
diff --git a/app/assets/javascripts/discourse/app/components/post/avatar.gjs b/app/assets/javascripts/discourse/app/components/post/avatar.gjs
index a243ee6843d..f99695283cd 100644
--- a/app/assets/javascripts/discourse/app/components/post/avatar.gjs
+++ b/app/assets/javascripts/discourse/app/components/post/avatar.gjs
@@ -10,7 +10,11 @@ import { applyValueTransformer } from "discourse/lib/transformer";
export default class PostAvatar extends Component {
get size() {
- return this.args.size || "large";
+ return applyValueTransformer(
+ "post-avatar-size",
+ this.args.size || "large",
+ { post: this.args.post }
+ );
}
@cached
diff --git a/app/assets/javascripts/discourse/app/lib/transformer/registry.js b/app/assets/javascripts/discourse/app/lib/transformer/registry.js
index e189cd696f1..7e461464460 100644
--- a/app/assets/javascripts/discourse/app/lib/transformer/registry.js
+++ b/app/assets/javascripts/discourse/app/lib/transformer/registry.js
@@ -45,6 +45,7 @@ export const VALUE_TRANSFORMERS = Object.freeze([
"parent-category-row-class-mobile",
"post-article-class",
"post-avatar-class",
+ "post-avatar-size",
"post-avatar-template",
"post-class",
"post-event-listener",
diff --git a/app/assets/javascripts/discourse/tests/integration/components/avatar-flair-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/avatar-flair-test.gjs
new file mode 100644
index 00000000000..e091e380eb9
--- /dev/null
+++ b/app/assets/javascripts/discourse/tests/integration/components/avatar-flair-test.gjs
@@ -0,0 +1,65 @@
+import { render } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import AvatarFlair from "discourse/components/avatar-flair";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+
+function renderComponent(flairArgs) {
+ return render(
+ <template>
+ <AvatarFlair
+ @flairBgColor={{flairArgs.flair_bg_color}}
+ @flairColor={{flairArgs.flair_color}}
+ @flairName={{flairArgs.flair_name}}
+ @flairUrl={{flairArgs.flair_url}}
+ />
+ </template>
+ );
+}
+
+module("Integration | Component | AvatarFlair", function (hooks) {
+ setupRenderingTest(hooks);
+
+ test("avatar flair with an icon", async function (assert) {
+ const flairArgs = {
+ flair_url: "bars",
+ flair_bg_color: "CC0000",
+ flair_color: "FFFFFF",
+ flair_name: "icon-avatar-flair",
+ };
+
+ await renderComponent(flairArgs);
+
+ assert
+ .dom(".avatar-flair")
+ .exists("has the tag")
+ .hasAttribute(
+ "title",
+ "icon-avatar-flair",
+ "the title attribute is set correctly"
+ );
+ assert.dom("svg.d-icon-bars").exists("has the svg icon");
+ assert.dom(".avatar-flair").hasStyle({
+ backgroundColor: "rgb(204, 0, 0)",
+ color: "rgb(255, 255, 255)",
+ });
+ });
+
+ test("avatar flair with an image", async function (assert) {
+ const flairArgs = {
+ flair_url: "/images/avatar.png",
+ flair_name: "image-avatar-flair",
+ };
+
+ await renderComponent(flairArgs);
+
+ assert
+ .dom(".avatar-flair")
+ .exists("has the tag")
+ .hasAttribute(
+ "title",
+ "image-avatar-flair",
+ "the title attribute is set correctly"
+ );
+ assert.dom("svg").doesNotExist("does not have an svg icon");
+ });
+});
diff --git a/app/assets/javascripts/discourse/tests/integration/components/post/avatar-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/post/avatar-test.gjs
index ef3a37b4ffd..76343a3789f 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/post/avatar-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/post/avatar-test.gjs
@@ -23,6 +23,9 @@ module("Integration | Component | Post | PostAvatar", function (hooks) {
topic,
like_count: 3,
actions_summary: [{ id: 2, count: 1, hidden: false, can_act: true }],
+ user_id: 1,
+ username: "eviltrout",
+ avatar_template: "/letter_avatar_proxy/v4/letter/e/eviltrout/{size}.png",
});
this.post = post;
@@ -42,4 +45,33 @@ module("Integration | Component | Post | PostAvatar", function (hooks) {
.dom(".topic-avatar.custom-class")
.exists("applies the custom classes to the component");
});
+
+ test("can change the size of the avatar", async function (assert) {
+ withPluginApi((api) => {
+ api.registerValueTransformer("post-avatar-size", () => {
+ return "100";
+ });
+ });
+
+ await renderComponent(this.post);
+
+ assert
+ .dom("img.avatar")
+ .hasAttribute("height", "100", "changes the height of the avatar")
+ .hasAttribute("width", "100", "changes the width of the avatar");
+ });
+
+ test("can change the template of the avatar", async function (assert) {
+ withPluginApi((api) => {
+ api.registerValueTransformer("post-avatar-template", () => {
+ return "/letter_avatar_proxy/v4/letter/e/eviltrout/{size}.gif";
+ });
+ });
+
+ await renderComponent(this.post);
+
+ assert
+ .dom("img.avatar")
+ .hasAttribute("src", /.gif$/, "changes the avatar url");
+ });
});