From 9ec657f1fda3188b5b6593449b5439e1892228ef Mon Sep 17 00:00:00 2001 From: Selase Krakani <849886+s3lase@users.noreply.github.com> Date: Wed, 8 Mar 2023 13:31:25 +0000 Subject: [PATCH] DEV: Make global search context suggestion first (#20581) Currently, the global search context suggestion("in all posts and topics") which also doubles as the default context on pressing Enter is displayed as the second item in the initial search options suggested. This changes makes it the first item in the suggested options. --- .../app/widgets/search-menu-results.js | 31 +++++++------ .../discourse/tests/acceptance/search-test.js | 46 ++++++++++--------- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/app/assets/javascripts/discourse/app/widgets/search-menu-results.js b/app/assets/javascripts/discourse/app/widgets/search-menu-results.js index bfb7044ff77..70f20a255fd 100644 --- a/app/assets/javascripts/discourse/app/widgets/search-menu-results.js +++ b/app/assets/javascripts/discourse/app/widgets/search-menu-results.js @@ -486,18 +486,6 @@ createWidget("search-menu-assistant", { this.router.currentRouteName.startsWith("topic.") ) { const user = attrs.results[0]; - content.push( - this.attach("search-menu-assistant-item", { - prefix, - user, - setTopicContext: true, - slug: `${prefix}@${user.username}`, - suffix: h( - "span.label-suffix", - ` ${I18n.t("search.in_this_topic")}` - ), - }) - ); content.push( this.attach("search-menu-assistant-item", { extraHint: I18n.t("search.enter_hint"), @@ -510,6 +498,18 @@ createWidget("search-menu-assistant", { ), }) ); + content.push( + this.attach("search-menu-assistant-item", { + prefix, + user, + setTopicContext: true, + slug: `${prefix}@${user.username}`, + suffix: h( + "span.label-suffix", + ` ${I18n.t("search.in_this_topic")}` + ), + }) + ); } else { attrs.results.forEach((user) => { content.push( @@ -552,6 +552,10 @@ createWidget("search-menu-initial-options", { const content = []; if (attrs.term || ctx) { + if (attrs.term) { + content.push(this.defaultRow(attrs.term, { withLabel: true })); + } + if (ctx) { const term = attrs.term || ""; switch (ctx.type) { @@ -650,9 +654,6 @@ createWidget("search-menu-initial-options", { } } - if (attrs.term) { - content.push(this.defaultRow(attrs.term, { withLabel: true })); - } return content; } diff --git a/app/assets/javascripts/discourse/tests/acceptance/search-test.js b/app/assets/javascripts/discourse/tests/acceptance/search-test.js index 62806071af3..87741e151a2 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/search-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/search-test.js @@ -3,6 +3,7 @@ import { count, exists, query, + queryAll, } from "discourse/tests/helpers/qunit-helpers"; import { click, @@ -131,14 +132,13 @@ acceptance("Search - Anonymous", function (needs) { }); test("search scope", async function (assert) { - const firstResult = - ".search-menu .results .search-menu-assistant-item:first-child"; + const contextSelector = ".search-menu .results .search-menu-assistant-item"; await visit("/tag/important"); await click("#search-button"); assert.strictEqual( - query(firstResult).textContent.trim(), + queryAll(contextSelector)[0].firstChild.textContent.trim(), `${I18n.t("search.in")} test`, "contextual tag search is first available option with no term" ); @@ -146,22 +146,22 @@ acceptance("Search - Anonymous", function (needs) { await fillIn("#search-term", "smth"); assert.strictEqual( - query(firstResult).textContent.trim(), + queryAll(contextSelector)[1].firstChild.textContent.trim(), `smth ${I18n.t("search.in")} test`, - "tag-scoped search is first available option" + "tag-scoped search is second available option" ); await visit("/c/bug"); await click("#search-button"); assert.strictEqual( - query(firstResult).textContent.trim(), + queryAll(contextSelector)[1].firstChild.textContent.trim(), `smth ${I18n.t("search.in")} bug`, - "category-scoped search is first available option" + "category-scoped search is first available option with no search term" ); assert.ok( - exists(`${firstResult} span.badge-wrapper`), + exists(`${contextSelector} span.badge-wrapper`), "category badge is a span (i.e. not a link)" ); @@ -169,20 +169,20 @@ acceptance("Search - Anonymous", function (needs) { await click("#search-button"); assert.strictEqual( - query(firstResult).textContent.trim(), + queryAll(contextSelector)[1].firstChild.textContent.trim(), `smth ${I18n.t("search.in_this_topic")}`, - "topic-scoped search is first available option" + "topic-scoped search is first available option with no search term" ); await visit("/u/eviltrout"); await click("#search-button"); assert.strictEqual( - query(firstResult).textContent.trim(), + queryAll(contextSelector)[1].firstChild.textContent.trim(), `smth ${I18n.t("search.in_posts_by", { username: "eviltrout", })}`, - "user-scoped search is first available option" + "user-scoped search is first available option with no search term" ); }); @@ -197,17 +197,18 @@ acceptance("Search - Anonymous", function (needs) { assert.strictEqual( query(firstResult).textContent.trim(), I18n.t("search.in_this_topic"), - "contextual topic search is first available option" + "contextual topic search is first available option with no search term" ); await fillIn("#search-term", "a proper"); await query("input#search-term").focus(); await triggerKeyEvent(".search-menu", "keydown", "ArrowDown"); + await triggerKeyEvent(".search-menu", "keydown", "ArrowDown"); await click(document.activeElement); assert.ok( exists(".search-menu .search-result-post ul li"), - "clicking first option formats results as posts" + "clicking second option scopes search to current topic" ); assert.strictEqual( @@ -233,6 +234,7 @@ acceptance("Search - Anonymous", function (needs) { await fillIn("#search-term", "dev"); await query("input#search-term").focus(); await triggerKeyEvent(".search-menu", "keydown", "ArrowDown"); + await triggerKeyEvent(".search-menu", "keydown", "ArrowDown"); await click(document.activeElement); assert.ok( @@ -255,18 +257,18 @@ acceptance("Search - Anonymous", function (needs) { await click("#search-button"); - const firstResult = - ".search-menu .results .search-menu-assistant-item:first-child"; + const contextSelector = ".search-menu .results .search-menu-assistant-item"; assert.strictEqual( - query(firstResult).textContent.trim(), + queryAll(contextSelector)[0].firstChild.textContent.trim(), I18n.t("search.in_this_topic"), - "contextual topic search is first available option" + "contextual topic search is first available option with no search term" ); await fillIn("#search-term", "proper"); await query("input#search-term").focus(); await triggerKeyEvent(".search-menu", "keydown", "ArrowDown"); + await triggerKeyEvent(".search-menu", "keydown", "ArrowDown"); await click(document.activeElement); assert.ok( @@ -300,16 +302,16 @@ acceptance("Search - Anonymous", function (needs) { query( ".search-menu-assistant-item:first-child .search-item-user .label-suffix" ).textContent.trim(), - I18n.t("search.in_this_topic"), - "first result hints in this topic search" + I18n.t("search.in_topics_posts"), + "first result hints at global search" ); assert.strictEqual( query( ".search-menu-assistant-item:nth-child(2) .search-item-user .label-suffix" ).textContent.trim(), - I18n.t("search.in_topics_posts"), - "second result hints global search" + I18n.t("search.in_this_topic"), + "second result hints at search within current topic" ); });