FEATURE: Support tag and replace in watched words in test modal (#13100)

FEATURE: Support tag and replace in watched words in test modal (#13100)

The modal showed only the matches, without the replacement or tags.

diff --git a/app/assets/javascripts/admin/addon/components/admin-watched-word.js b/app/assets/javascripts/admin/addon/components/admin-watched-word.js
index fb24a6f..1dcee04 100644
--- a/app/assets/javascripts/admin/addon/components/admin-watched-word.js
+++ b/app/assets/javascripts/admin/addon/components/admin-watched-word.js
@@ -1,10 +1,20 @@
 import Component from "@ember/component";
-import I18n from "I18n";
+import { equal } from "@ember/object/computed";
 import bootbox from "bootbox";
+import discourseComputed from "discourse-common/utils/decorators";
+import I18n from "I18n";
 
 export default Component.extend({
   classNames: ["watched-word"],
 
+  isReplace: equal("actionKey", "replace"),
+  isTag: equal("actionKey", "tag"),
+
+  @discourseComputed("word.replacement")
+  tags(replacement) {
+    return replacement.split(",");
+  },
+
   click() {
     this.word
       .destroy()
diff --git a/app/assets/javascripts/admin/addon/controllers/modals/admin-watched-word-test.js b/app/assets/javascripts/admin/addon/controllers/modals/admin-watched-word-test.js
index 215cc08..3dfbd27 100644
--- a/app/assets/javascripts/admin/addon/controllers/modals/admin-watched-word-test.js
+++ b/app/assets/javascripts/admin/addon/controllers/modals/admin-watched-word-test.js
@@ -1,14 +1,48 @@
 import Controller from "@ember/controller";
 import ModalFunctionality from "discourse/mixins/modal-functionality";
 import discourseComputed from "discourse-common/utils/decorators";
+import { equal } from "@ember/object/computed";
 
 export default Controller.extend(ModalFunctionality, {
-  @discourseComputed("value", "model.compiledRegularExpression")
-  matches(value, regexpString) {
+  isReplace: equal("model.nameKey", "replace"),
+  isTag: equal("model.nameKey", "tag"),
+
+  @discourseComputed(
+    "value",
+    "model.compiledRegularExpression",
+    "model.words",
+    "isReplace",
+    "isTag"
+  )
+  matches(value, regexpString, words, isReplace, isTag) {
     if (!value || !regexpString) {
       return;
     }
-    let censorRegexp = new RegExp(regexpString, "ig");
-    return value.match(censorRegexp);
+
+    const regexp = new RegExp(regexpString, "ig");
+    const matches = value.match(regexp) || [];
+
+    if (isReplace) {
+      return matches.map((match) => ({
+        match,
+        replacement: words.find((word) =>
+          new RegExp(word.regexp, "ig").test(match)
+        ).replacement,
+      }));
+    } else if (isTag) {
+      return matches.map((match) => {
+        const tags = new Set();
+
+        words.forEach((word) => {
+          if (new RegExp(word.regexp, "ig").test(match)) {
+            word.replacement.split(",").forEach((tag) => tags.add(tag));
+          }
+        });
+
+        return { match, tags: Array.from(tags) };
+      });
+    }
+
+    return matches;
   },
 });
diff --git a/app/assets/javascripts/admin/addon/templates/components/admin-watched-word.hbs b/app/assets/javascripts/admin/addon/templates/components/admin-watched-word.hbs
index cc03496..26f8a9e 100644
--- a/app/assets/javascripts/admin/addon/templates/components/admin-watched-word.hbs
+++ b/app/assets/javascripts/admin/addon/templates/components/admin-watched-word.hbs
@@ -1 +1,9 @@
-{{d-icon "times"}} {{word.word}} {{#if word.replacement}}&rarr; <span class="replacement">{{word.replacement}}</span>{{/if}}
+{{d-icon "times"}} {{word.word}}
+{{#if isReplace}}
+  &rarr; <span class="replacement">{{word.replacement}}</span>
+{{else if isTag}}
+  &rarr;
+  {{#each tags as |tag|}}
+    <span class="tag">{{tag}}</span>
+  {{/each}}
+{{/if}}
diff --git a/app/assets/javascripts/admin/addon/templates/components/watched-word-form.hbs b/app/assets/javascripts/admin/addon/templates/components/watched-word-form.hbs
index dbc7c86..8d10aeb 100644
--- a/app/assets/javascripts/admin/addon/templates/components/watched-word-form.hbs
+++ b/app/assets/javascripts/admin/addon/templates/components/watched-word-form.hbs
@@ -6,14 +6,14 @@
 {{#if canReplace}}
   <div class="watched-word-input">
     <label for="watched-replacement">{{i18n "admin.watched_words.form.replacement_label"}}</label>
-    {{text-field id="watched-replacement" value=replacement disabled=formSubmitted class="watched-word-input-replace" autocorrect="off" autocapitalize="off" placeholderKey="admin.watched_words.form.replacement_placeholder"}}
+    {{text-field id="watched-replacement" value=replacement disabled=formSubmitted class="watched-word-input-field" autocorrect="off" autocapitalize="off" placeholderKey="admin.watched_words.form.replacement_placeholder"}}
   </div>
 {{/if}}
 
 {{#if canTag}}
   <div class="watched-word-input">
     <label for="watched-tag">{{i18n "admin.watched_words.form.tag_label"}}</label>
-    {{text-field id="watched-tag" value=replacement disabled=formSubmitted class="watched-word-input" autocorrect="off" autocapitalize="off" placeholderKey="admin.watched_words.form.tag_placeholder"}}
+    {{text-field id="watched-tag" value=replacement disabled=formSubmitted class="watched-word-input-field" autocorrect="off" autocapitalize="off" placeholderKey="admin.watched_words.form.tag_placeholder"}}
   </div>
 {{/if}}
 
diff --git a/app/assets/javascripts/admin/addon/templates/modal/admin-watched-word-test.hbs b/app/assets/javascripts/admin/addon/templates/modal/admin-watched-word-test.hbs
index ef56567..2b2361b 100644
--- a/app/assets/javascripts/admin/addon/templates/modal/admin-watched-word-test.hbs
+++ b/app/assets/javascripts/admin/addon/templates/modal/admin-watched-word-test.hbs
@@ -5,9 +5,29 @@
     <p>
       {{i18n "admin.watched_words.test.found_matches"}}
       <ul>
-        {{#each matches as |match|}}
-          <li>{{match}}</li>
-        {{/each}}
+        {{#if isReplace}}
+          {{#each matches as |match|}}
+            <li>
+              <span class="match">{{match.match}}</span>
+              &rarr;
+              <span class="replacement">{{match.replacement}}</span>
+            </li>
+          {{/each}}
+        {{else if isTag}}
+          {{#each matches as |match|}}
+            <li>
+              <span class="match">{{match.match}}</span>
+              &rarr;
+              {{#each match.tags as |tag|}}
+                <span class="tag">{{tag}}</span>
+              {{/each}}
+            </li>
+          {{/each}}
+        {{else}}
+          {{#each matches as |match|}}
+            <li>{{match}}</li>
+          {{/each}}
+        {{/if}}
       </ul>
     </p>
   {{else}}
diff --git a/app/assets/javascripts/admin/addon/templates/watched-words-action.hbs b/app/assets/javascripts/admin/addon/templates/watched-words-action.hbs
index d17ce9e..e6b9c77 100644
--- a/app/assets/javascripts/admin/addon/templates/watched-words-action.hbs
+++ b/app/assets/javascripts/admin/addon/templates/watched-words-action.hbs
@@ -8,6 +8,7 @@
   {{watched-word-uploader uploading=uploading actionKey=actionNameKey done=(action "uploadComplete")}}
 
   {{d-button
+    class="watched-word-test"
     label="admin.watched_words.test.button_label"
     icon="far-eye"
     action=(action "test")}}
@@ -37,7 +38,7 @@
 {{#if showWordsList}}
   <div class="watched-words-list">
     {{#each filteredContent as |word| }}
-      <div class="watched-word-box">{{admin-watched-word word=word action=(action "recordRemoved")}}</div>
+      <div class="watched-word-box">{{admin-watched-word actionKey=actionNameKey word=word action=(action "recordRemoved")}}</div>
     {{/each}}
   </div>
 {{/if}}
diff --git a/app/assets/javascripts/discourse/tests/acceptance/admin-watched-words-test.js b/app/assets/javascripts/discourse/tests/acceptance/admin-watched-words-test.js
index 1a3162e..4094f6b 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/admin-watched-words-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/admin-watched-words-test.js
@@ -83,4 +83,20 @@ acceptance("Admin - Watched Words", function (needs) {
 

[... diff too long, it was truncated ...]

GitHub sha: f700f3ef

This commit appears in #13100 which was approved by riking. It was merged by nbianca.