A platform for community discussion. Free, open, simple.
DEV: Modernize topic-status (#30754)
Jarek Radosz6cb8b576d0c7
diff --git a/app/assets/javascripts/discourse/app/components/header/topic/info.gjs b/app/assets/javascripts/discourse/app/components/header/topic/info.gjsindex d619d5c9a26..cb7a60a0aa9 100644--- a/app/assets/javascripts/discourse/app/components/header/topic/info.gjs+++ b/app/assets/javascripts/discourse/app/components/header/topic/info.gjs@@ -5,6 +5,7 @@ import { action } from "@ember/object";import { service } from "@ember/service";import { htmlSafe } from "@ember/template";import { and, gt, not, or } from "truth-helpers";+import TopicStatus from "discourse/components/topic-status";import categoryLink from "discourse/helpers/category-link";import concatClass from "discourse/helpers/concat-class";import icon from "discourse/helpers/d-icon";@@ -15,7 +16,6 @@ import { i18n } from "discourse-i18n";import PluginOutlet from "../../plugin-outlet";import FeaturedLink from "./featured-link";import Participant from "./participant";-import Status from "./status";export default class Info extends Component {@service currentUser;@@ -105,8 +105,8 @@ export default class Info extends Component {{{/if}}{{#if (and @topicInfo.fancyTitle @topicInfo.url)}}- <Status- @topicInfo={{@topicInfo}}+ <TopicStatus+ @topic={{@topicInfo}}@disableActions={{@disableActions}}/>diff --git a/app/assets/javascripts/discourse/app/components/header/topic/status.gjs b/app/assets/javascripts/discourse/app/components/header/topic/status.gjsdeleted file mode 100644index f0beede7949..00000000000--- a/app/assets/javascripts/discourse/app/components/header/topic/status.gjs+++ /dev/null@@ -1,62 +0,0 @@-import Component from "@glimmer/component";-import { on } from "@ember/modifier";-import { action } from "@ember/object";-import { service } from "@ember/service";-import concatClass from "discourse/helpers/concat-class";-import icon from "discourse/helpers/d-icon";-import TopicStatusIcons from "discourse/helpers/topic-status-icons";-import { escapeExpression } from "discourse/lib/utilities";-import { i18n } from "discourse-i18n";--export default class Status extends Component {- @service currentUser;-- get canAct() {- return this.currentUser && !this.args.disableActions;- }-- get topicStatuses() {- let topicStatuses = [];- TopicStatusIcons.render(this.args.topicInfo, (name, key) => {- const iconArgs = { class: key === "unpinned" ? "unpinned" : null };- const statusIcon = { name, iconArgs };-- const attributes = {- title: escapeExpression(i18n(`topic_statuses.${key}.help`)),- };- let klass = ["topic-status"];- if (key === "unpinned" || key === "pinned") {- klass.push("pin-toggle-button", key);- klass = klass.join(" ");- }- topicStatuses.push({ attributes, klass, icon: statusIcon });- });-- return topicStatuses;- }-- @action- togglePinnedForUser(e) {- if (!this.canAct) {- return;- }- const parent = e.target.closest(".topic-statuses");- if (parent?.querySelector(".pin-toggle-button")?.contains(e.target)) {- this.args.topicInfo.togglePinnedForUser();- }- }-- <template>- <span class="topic-statuses">- {{#each this.topicStatuses as |status|}}- {{! template-lint-disable no-invalid-interactive }}- <span- class={{concatClass status.klass "topic-status"}}- {{on "click" this.togglePinnedForUser}}- >- {{icon status.icon.name class=status.icon.iconArgs.class}}- </span>- {{/each}}- </span>- </template>-}diff --git a/app/assets/javascripts/discourse/app/components/topic-list-item.js b/app/assets/javascripts/discourse/app/components/topic-list-item.jsindex 213cf8a8da7..d02d2ae86e1 100644--- a/app/assets/javascripts/discourse/app/components/topic-list-item.js+++ b/app/assets/javascripts/discourse/app/components/topic-list-item.js@@ -332,14 +332,6 @@ export default class TopicListItem extends Component {return this.navigateToTopic(topic, topic.lastUnreadUrl);}- if (- classList.contains("d-icon-thumbtack") &&- target.closest("a.topic-status")- ) {- this.topic.togglePinnedForUser();- return false;- }-return this.unhandledRowClick(e, topic);}diff --git a/app/assets/javascripts/discourse/app/components/topic-status.gjs b/app/assets/javascripts/discourse/app/components/topic-status.gjsindex 9e7cc23d907..b81f0375267 100644--- a/app/assets/javascripts/discourse/app/components/topic-status.gjs+++ b/app/assets/javascripts/discourse/app/components/topic-status.gjs@@ -1,30 +1,45 @@import Component from "@glimmer/component";-import { hash } from "@ember/helper";+import { concat, get, hash } from "@ember/helper";import { on } from "@ember/modifier";import { action } from "@ember/object";import { service } from "@ember/service";import { and } from "truth-helpers";import PluginOutlet from "discourse/components/plugin-outlet";import icon from "discourse/helpers/d-icon";+import element from "discourse/helpers/element";+import TopicStatusIcons from "discourse/helpers/topic-status-icons";import { i18n } from "discourse-i18n";export default class TopicStatus extends Component {@service currentUser;+ @service site;++ get wrapperElement() {+ return element(this.args.tagName ?? "span");+ }get canAct() {+ // TODO: @disableActions -> !@interactivereturn this.currentUser && !this.args.disableActions;}@actiontogglePinned(e) {- const { topic } = this.args;- topic.pinned ? topic.clearPin() : topic.rePin();e.preventDefault();+ this.args.topic.togglePinnedForUser();}<template>{{~! no whitespace ~}}- <span class="topic-statuses">+ <this.wrapperElement class="topic-statuses">+ {{~#if @topic.bookmarked~}}+ <a+ href={{@topic.url}}+ title={{i18n "topic_statuses.bookmarked.help"}}+ class="topic-status"+ >{{icon "bookmark"}}</a>+ {{~/if~}}+{{~#if (and @topic.closed @topic.archived)~}}<spantitle={{i18n "topic_statuses.locked_and_archived.help"}}@@ -60,12 +75,12 @@ export default class TopicStatus extends Component {{{on "click" this.togglePinned}}hreftitle={{i18n "topic_statuses.pinned.help"}}- class="topic-status"+ class="topic-status pinned pin-toggle-button">{{icon "thumbtack"}}</a>{{~else~}}<spantitle={{i18n "topic_statuses.pinned.help"}}- class="topic-status"+ class="topic-status pinned">{{icon "thumbtack"}}</span>{{~/if~}}{{~else if @topic.unpinned~}}@@ -74,12 +89,12 @@ export default class TopicStatus extends Component {{{on "click" this.togglePinned}}hreftitle={{i18n "topic_statuses.unpinned.help"}}- class="topic-status"+ class="topic-status unpinned pin-toggle-button">{{icon "thumbtack" class="unpinned"}}</a>{{~else~}}<spantitle={{i18n "topic_statuses.unpinned.help"}}- class="topic-status"+ class="topic-status unpinned">{{icon "thumbtack" class="unpinned"}}</span>{{~/if~}}{{~/if~}}@@ -94,12 +109,23 @@ export default class TopicStatus extends Component {>{{icon "far-eye-slash"}}</span>{{~/if~}}+ {{~#unless this.site.useGlimmerTopicList~}}+ {{~#each TopicStatusIcons.entries as |entry|~}}+ {{~#if (get @topic entry.attribute)~}}+ <span+ title={{i18n (concat "topic_statuses." entry.titleKey "help")}}+ class="topic-status"+ >{{icon entry.iconName}}</span>+ {{~/if~}}+ {{~/each~}}+ {{~/unless~}}+<PluginOutlet@name="after-topic-status"@outletArgs={{hash topic=@topic}}/>{{~! no whitespace ~}}- </span>+ </this.wrapperElement>{{~! no whitespace ~}}</template>}diff --git a/app/assets/javascripts/discourse/app/helpers/topic-status-icons.js b/app/assets/javascripts/discourse/app/helpers/topic-status-icons.jsindex 1b685b7f5d0..3efe797cb1e 100644--- a/app/assets/javascripts/discourse/app/helpers/topic-status-icons.js+++ b/app/assets/javascripts/discourse/app/helpers/topic-status-icons.js@@ -1,30 +1,18 @@-import ArrayProxy from "@ember/array/proxy";+import deprecated from "discourse/lib/deprecated";+import { RAW_TOPIC_LIST_DEPRECATION_OPTIONS } from "discourse/lib/plugin-api";-class TopicStatusIconArrayProxy extends ArrayProxy {- render(topic, renderIcon) {- const renderIconIf = (conditionProp, name, key) => {- if (!topic.get(conditionProp)) {- return;- }- renderIcon(name, key);- };+const TopicStatusIcons = new (class {+ entries = [];- if (topic.get("closed") && topic.get("archived")) {- renderIcon("lock", "locked_and_archived");- } else {- renderIconIf("closed", "lock", "locked");- renderIconIf("archived", "lock", "archived");- }+ addObject(entry) {+ deprecated(+ "TopicStatusIcons is deprecated. Use 'after-topic-status' plugin outlet instead.",+ RAW_TOPIC_LIST_DEPRECATION_OPTIONS+ );- this.forEach((args) => renderIconIf(...args));+ const [attribute, iconName, titleKey] = entry;+ this.entries.push({ attribute, iconName, titleKey });}-}+})();-export default TopicStatusIconArrayProxy.create({- content: [- ["is_warning", "envelope", "warning"],- ["pinned", "thumbtack", "pinned"],- ["unpinned", "thumbtack", "unpinned"],- ["invisible", "far-eye-slash", "unlisted"],- ],-});+export default TopicStatusIcons;diff --git a/app/assets/javascripts/discourse/app/widgets/topic-status.js b/app/assets/javascripts/discourse/app/widgets/topic-status.jsindex daf5062edd3..5ccc916fcc8 100644--- a/app/assets/javascripts/discourse/app/widgets/topic-status.js+++ b/app/assets/javascripts/discourse/app/widgets/topic-status.js@@ -1,40 +1,12 @@-import { h } from "virtual-dom";-import TopicStatusIcons from "discourse/helpers/topic-status-icons";-import { iconNode } from "discourse/lib/icon-library";-import { escapeExpression } from "discourse/lib/utilities";-import { createWidget } from "discourse/widgets/widget";-import { i18n } from "discourse-i18n";--export default createWidget("topic-status", {- tagName: "span.topic-statuses",-- html(attrs) {- const topic = attrs.topic;- const canAct = this.currentUser && !attrs.disableActions;-- const result = [];-- TopicStatusIcons.render(topic, function (name, key) {- const iconArgs = { class: key === "unpinned" ? "unpinned" : null };- const icon = iconNode(name, iconArgs);-- const attributes = {- title: escapeExpression(i18n(`topic_statuses.${key}.help`)),- };- let klass = "topic-status";- if (key === "unpinned" || key === "pinned") {- klass += `.pin-toggle-button.${key}`;- }- result.push(h(`${canAct ? "a" : "span"}.${klass}`, attributes, icon));- });-- return result;- },-- click(e) {- const parent = e.target.closest(".topic-statuses");- if (parent?.querySelector(".pin-toggle-button")?.contains(e.target)) {- this.attrs.topic.togglePinnedForUser();- }- },-});+import { hbs } from "ember-cli-htmlbars";+import { registerWidgetShim } from "discourse/widgets/render-glimmer";++registerWidgetShim(+ "topic-status",+ "span.topic-statuses",+ hbs`<TopicStatus+ @topic={{@data.topic}}+ @disableActions={{@data.disableActions}}+ @tagName=""+ />`+);diff --git a/app/assets/javascripts/discourse/tests/integration/components/widgets/topic-status-test.js b/app/assets/javascripts/discourse/tests/integration/components/widgets/topic-status-test.gjssimilarity index 62%rename from app/assets/javascripts/discourse/tests/integration/components/widgets/topic-status-test.jsrename to app/assets/javascripts/discourse/tests/integration/components/widgets/topic-status-test.gjsindex 8ea21e52b4d..735e3b90706 100644--- a/app/assets/javascripts/discourse/tests/integration/components/widgets/topic-status-test.js+++ b/app/assets/javascripts/discourse/tests/integration/components/widgets/topic-status-test.gjs@@ -1,8 +1,9 @@import { getOwner } from "@ember/owner";import { click, render } from "@ember/test-helpers";-import { hbs } from "ember-cli-htmlbars";import { module, test } from "qunit";+import MountWidget from "discourse/components/mount-widget";import TopicStatusIcons from "discourse/helpers/topic-status-icons";+import { withSilencedDeprecations } from "discourse/lib/deprecated";import { setupRenderingTest } from "discourse/tests/helpers/component-test";module("Integration | Component | Widget | topic-status", function (hooks) {@@ -10,62 +11,64 @@ module("Integration | Component | Widget | topic-status", function (hooks) {test("basics", async function (assert) {const store = getOwner(this).lookup("service:store");- this.set("args", {+ const args = {topic: store.createRecord("topic", { closed: true }),disableActions: true,- });+ };- await render(- hbs`<MountWidget @widget="topic-status" @args={{this.args}} />`- );+ await render(<template>+ <MountWidget @widget="topic-status" @args={{args}} />+ </template>);assert.dom(".topic-status .d-icon-lock").exists();});test("extendability", async function (assert) {+ this.siteSettings.glimmer_topic_list_mode = "disabled";+ withSilencedDeprecations("discourse.hbr-topic-list-overrides", () => {+ TopicStatusIcons.addObject([+ "has_accepted_answer",+ "far-square-check",+ "solved",+ ]);+ });+const store = getOwner(this).lookup("service:store");- TopicStatusIcons.addObject([- "has_accepted_answer",- "far-square-check",- "solved",- ]);- this.set("args", {- topic: store.createRecord("topic", {- has_accepted_answer: true,- }),+ const args = {+ topic: store.createRecord("topic", { has_accepted_answer: true }),disableActions: true,- });+ };- await render(- hbs`<MountWidget @widget="topic-status" @args={{this.args}} />`- );+ await render(<template>+ <MountWidget @widget="topic-status" @args={{args}} />+ </template>);assert.dom(".topic-status .d-icon-far-square-check").exists();});test("toggling pin status", async function (assert) {const store = getOwner(this).lookup("service:store");- this.set("args", {+ const args = {topic: store.createRecord("topic", { closed: true, pinned: true }),- });+ };- await render(- hbs`<MountWidget @widget="topic-status" @args={{this.args}} />`- );+ await render(<template>+ <MountWidget @widget="topic-status" @args={{args}} />+ </template>);assert.dom(".topic-statuses .pinned").exists("pinned icon is shown");assert.dom(".topic-statuses .unpinned").doesNotExist("unpinned icon is not shown");- await click(".topic-statuses .pin-toggle-button");+ await click(".topic-statuses a.pin-toggle-button");assert.dom(".topic-statuses .pinned").doesNotExist("pinned icon is not shown");assert.dom(".topic-statuses .unpinned").exists("unpinned icon is shown");- await click(".topic-statuses .pin-toggle-button");+ await click(".topic-statuses a.pin-toggle-button");assert.dom(".topic-statuses .pinned").exists("pinned icon is shown");assert