Skip to content

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

FIX: Avoid draft conflicts when creating a topic from the search page

Régis Hanole0271836b01f
diff --git a/frontend/discourse/app/controllers/full-page-search.js b/frontend/discourse/app/controllers/full-page-search.js
index 59cbd65bab0..c18c1050ca4 100644
--- a/frontend/discourse/app/controllers/full-page-search.js
+++ b/frontend/discourse/app/controllers/full-page-search.js
@@ -32,7 +32,6 @@ import {
import userSearch from "discourse/lib/user-search";
import { escapeExpression } from "discourse/lib/utilities";
import Category from "discourse/models/category";
-import Composer from "discourse/models/composer";
import { i18n } from "discourse-i18n";
export const SEARCH_TYPE_DEFAULT = "topics_posts";
@@ -506,20 +505,9 @@ export default class FullPageSearchController extends Controller {
}
@action
- createTopic(searchTerm, event) {
+ createTopic(event) {
event?.preventDefault();
- let topicCategory;
- if (searchTerm.includes("category:")) {
- const match = searchTerm.match(/category:(\S*)/);
- if (match && match[1]) {
- topicCategory = match[1];
- }
- }
- this.composer.open({
- action: Composer.CREATE_TOPIC,
- draftKey: Composer.NEW_TOPIC_KEY,
- topicCategory,
- });
+ this.composer.openNewTopic();
}
@action
diff --git a/frontend/discourse/app/templates/full-page-search.gjs b/frontend/discourse/app/templates/full-page-search.gjs
index 3bbf029477d..99798717599 100644
--- a/frontend/discourse/app/templates/full-page-search.gjs
+++ b/frontend/discourse/app/templates/full-page-search.gjs
@@ -280,15 +280,9 @@ export default <template>
<div class="no-results-suggestion">
{{i18n "search.cant_find"}}
{{#if @controller.canCreateTopic}}
- <a
- href
- {{on
- "click"
- (fn
- @controller.createTopic @controller.searchTerm
- )
- }}
- >{{i18n "search.start_new_topic"}}</a>
+ <a href {{on "click" @controller.createTopic}}>{{i18n
+ "search.start_new_topic"
+ }}</a>
{{#unless @controller.siteSettings.login_required}}
{{i18n "search.or_search_google"}}
{{/unless}}
diff --git a/frontend/discourse/tests/acceptance/search-full-test.js b/frontend/discourse/tests/acceptance/search-full-test.js
index 365d6f56140..d1b004bec32 100644
--- a/frontend/discourse/tests/acceptance/search-full-test.js
+++ b/frontend/discourse/tests/acceptance/search-full-test.js
@@ -5,6 +5,7 @@ import {
SEARCH_TYPE_DEFAULT,
SEARCH_TYPE_USERS,
} from "discourse/controllers/full-page-search";
+import pretender, { response } from "discourse/tests/helpers/create-pretender";
import { acceptance, selectDate } from "discourse/tests/helpers/qunit-helpers";
import selectKit from "discourse/tests/helpers/select-kit-helper";
@@ -120,6 +121,31 @@ acceptance("Search - Full Page", function (needs) {
assert.dom(".fps-topic").exists({ count: 1 }, "has one post");
});
+ test("starting a new topic from no results opens a fresh draft", async function (assert) {
+ pretender.get("/search", () =>
+ response({
+ grouped_search_result: { term: "none", can_create_topic: true },
+ })
+ );
+
+ await visit("/search?q=none");
+ await click(".no-results-suggestion a");
+
+ assert
+ .dom(".composer-action-create-topic")
+ .exists("opens the composer in new topic mode");
+ const composerModel = this.owner.lookup("service:composer").model;
+ assert.true(
+ /^new_topic_\d+$/.test(composerModel.draftKey),
+ "uses a unique draft key"
+ );
+ assert.strictEqual(
+ composerModel.draftSequence,
+ 0,
+ "starts a fresh draft sequence"
+ );
+ });
+
test("unknown search types fall back to post results", async function (assert) {
await visit("/search?q=consectetur&search_type=unavailable_type");