FIX: Discard old search results if search term changes when moving posts to a different topic (#14117)

FIX: Discard old search results if search term changes when moving posts to a different topic (#14117)

This also fixes an incorrect usage of debounce.

Meta topic: Odd search behaviour when moving messages (as a staff member) - bug - Discourse Meta.

diff --git a/app/assets/javascripts/discourse/app/components/choose-topic.js b/app/assets/javascripts/discourse/app/components/choose-topic.js
index d7d164d..6454ae0 100644
--- a/app/assets/javascripts/discourse/app/components/choose-topic.js
+++ b/app/assets/javascripts/discourse/app/components/choose-topic.js
@@ -4,6 +4,7 @@ import discourseDebounce from "discourse-common/lib/debounce";
 import { isEmpty } from "@ember/utils";
 import { next, schedule } from "@ember/runloop";
 import { searchForTerm } from "discourse/lib/search";
+import { INPUT_DELAY } from "discourse-common/config/environment";
 
 export default Component.extend({
   loading: null,
@@ -68,7 +69,7 @@ export default Component.extend({
       oldTopicTitle: this.topicTitle,
     });
 
-    this.search(this.topicTitle);
+    this.searchDebounced(this.topicTitle);
   },
 
   @discourseComputed("label")
@@ -85,48 +86,48 @@ export default Component.extend({
     this.set("loading", false);
   },
 
+  searchDebounced(title) {
+    discourseDebounce(this, this.search, title, INPUT_DELAY);
+  },
+
   search(title) {
-    discourseDebounce(
-      this,
-      function () {
-        if (!this.element || this.isDestroying || this.isDestroyed) {
-          return;
-        }
+    if (!this.element || this.isDestroying || this.isDestroyed) {
+      return;
+    }
 
-        if (isEmpty(title) && isEmpty(this.additionalFilters)) {
-          this.setProperties({ topics: null, loading: false });
-          return;
-        }
+    if (isEmpty(title) && isEmpty(this.additionalFilters)) {
+      this.setProperties({ topics: null, loading: false });
+      return;
+    }
 
-        const currentTopicId = this.currentTopicId;
-        const titleWithFilters = `${title} ${this.additionalFilters}`;
-        let searchParams = {};
+    const currentTopicId = this.currentTopicId;
+    const titleWithFilters = `${title} ${this.additionalFilters}`;
+    let searchParams = {};
 
-        if (!isEmpty(title)) {
-          searchParams.typeFilter = "topic";
-          searchParams.restrictToArchetype = "regular";
-          searchParams.searchForId = true;
-        }
+    if (!isEmpty(title)) {
+      searchParams.typeFilter = "topic";
+      searchParams.restrictToArchetype = "regular";
+      searchParams.searchForId = true;
+    }
 
-        searchForTerm(titleWithFilters, searchParams).then((results) => {
-          if (results && results.posts && results.posts.length > 0) {
-            this.set(
-              "topics",
-              results.posts
-                .mapBy("topic")
-                .filter((t) => t.id !== currentTopicId)
-            );
-            if (this.topics.length === 1) {
-              this.send("chooseTopic", this.topics[0]);
-            }
-          } else {
-            this.setProperties({ topics: null, loading: false });
-          }
-        });
-      },
-      title,
-      300
-    );
+    searchForTerm(titleWithFilters, searchParams).then((results) => {
+      // search term changed after the request was fired but before we
+      // got a response, ignore results.
+      if (title !== this.topicTitle) {
+        return;
+      }
+      if (results && results.posts && results.posts.length > 0) {
+        this.set(
+          "topics",
+          results.posts.mapBy("topic").filter((t) => t.id !== currentTopicId)
+        );
+        if (this.topics.length === 1) {
+          this.send("chooseTopic", this.topics[0]);
+        }
+      } else {
+        this.setProperties({ topics: null, loading: false });
+      }
+    });
   },
 
   actions: {

GitHub sha: 19632ecfbbb454bf6a64e06f13745d62c7353ec8

This commit appears in #14117 which was approved by eviltrout. It was merged by OsamaSayegh.

This commit has been mentioned on Discourse Meta. There might be relevant details there: