Skip to content

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

UX: make sure search context is kept when navigating (#33016)

Renato Atilio3a01b69b761a
diff --git a/app/assets/javascripts/discourse/app/components/search-menu.gjs b/app/assets/javascripts/discourse/app/components/search-menu.gjs
index 84f4ade7076..a91bdf9cd6b 100644
--- a/app/assets/javascripts/discourse/app/components/search-menu.gjs
+++ b/app/assets/javascripts/discourse/app/components/search-menu.gjs
@@ -44,8 +44,7 @@ export default class SearchMenu extends Component {
@service appEvents;
@tracked loading = false;
- @tracked
- inPMInboxContext = this.search.searchContext?.type === "private_messages";
+ @tracked isPMInboxCleared = false;
@tracked typeFilter = DEFAULT_TYPE_FILTER;
@tracked suggestionKeyword = false;
@tracked suggestionResults = [];
@@ -53,6 +52,7 @@ export default class SearchMenu extends Component {
@tracked menuPanelOpen = false;
searchInputId = this.args.searchInputId ?? "search-term";
+
_debouncer = null;
_activeSearch = null;
@@ -114,6 +114,13 @@ export default class SearchMenu extends Component {
return false;
}
+ get inPMInboxContext() {
+ return (
+ !this.isPMInboxCleared &&
+ this.search.searchContext?.type === "private_messages"
+ );
+ }
+
@action
onKeydown(event) {
if (event.key === "Escape") {
@@ -196,6 +203,9 @@ export default class SearchMenu extends Component {
if (opts.setTopicContext) {
this.search.inTopicContext = true;
}
+ if (opts.setPMInboxContext) {
+ this.isPMInboxCleared = false;
+ }
this.search.activeGlobalSearchTerm = term;
this.triggerSearch();
}
@@ -216,7 +226,7 @@ export default class SearchMenu extends Component {
@action
clearPMInboxContext() {
- this.inPMInboxContext = false;
+ this.isPMInboxCleared = true;
}
@action
diff --git a/app/assets/javascripts/discourse/app/components/search-menu/results/assistant-item.gjs b/app/assets/javascripts/discourse/app/components/search-menu/results/assistant-item.gjs
index 7562f923432..25d6ff22c77 100644
--- a/app/assets/javascripts/discourse/app/components/search-menu/results/assistant-item.gjs
+++ b/app/assets/javascripts/discourse/app/components/search-menu/results/assistant-item.gjs
@@ -122,10 +122,14 @@ export default class AssistantItem extends Component {
return;
}
+ const inPMInboxContext =
+ this.search.searchContext?.type === "private_messages";
this.args.searchTermChanged(updatedTerm, {
searchTopics,
...(inTopicContext &&
!this.args.searchAllTopics && { setTopicContext: true }),
+ ...(!this.args.searchAllTopics &&
+ inPMInboxContext && { setPMInboxContext: true }),
});
this.search.focusSearchInput();
}
diff --git a/app/assets/javascripts/discourse/tests/integration/components/search-menu-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/search-menu-test.gjs
index f80b0dd6b76..bd28d2061a7 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/search-menu-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/search-menu-test.gjs
@@ -1,4 +1,10 @@
-import { click, fillIn, render, triggerKeyEvent } from "@ember/test-helpers";
+import {
+ click,
+ fillIn,
+ render,
+ settled,
+ triggerKeyEvent,
+} from "@ember/test-helpers";
import { module, test } from "qunit";
import SearchMenu, {
DEFAULT_TYPE_FILTER,
@@ -106,4 +112,57 @@ module("Integration | Component | search-menu", function (hooks) {
.dom("#search-term.search-term__input")
.exists("input defaults to id of search-term");
});
+
+ test("search-context state changes updates the UI", async function (assert) {
+ const searchService = this.owner.lookup("service:search");
+
+ searchService.searchContext = null;
+ searchService.inTopicContext = false;
+ await render(<template><SearchMenu @location="test" /></template>);
+
+ assert
+ .dom(".search-context")
+ .doesNotExist("no search context button when searchContext is null");
+
+ searchService.searchContext = { type: "private_messages" };
+ await settled();
+
+ assert
+ .dom(".search-context")
+ .exists(
+ "PM context button appears when searchContext.type changes to private_messages"
+ );
+
+ await click(".search-context");
+
+ assert
+ .dom(".search-context")
+ .doesNotExist("PM context button disappears when clear btn is pressed");
+ });
+
+ test("PM inbox context can be restored after being cleared", async function (assert) {
+ const searchService = this.owner.lookup("service:search");
+
+ searchService.searchContext = { type: "private_messages" };
+ searchService.inTopicContext = false;
+
+ await render(<template><SearchMenu @location="test" /></template>);
+
+ assert.dom(".search-context").exists("PM context button appears initially");
+
+ await click(".search-context");
+ assert
+ .dom(".search-context")
+ .doesNotExist("PM context button disappears when cleared");
+
+ await click("#search-term");
+
+ await click(".search-menu-assistant-item .search-item-slug");
+
+ assert
+ .dom(".search-context")
+ .exists(
+ "PM context button reappears after selecting 'in:messages' suggestion"
+ );
+ });
});