Skip to content

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

UX: in combined AI search only show the count on other tabs (#45035)

Krisb9d2b1b15992
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search/ai-search-tabs.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search/ai-search-tabs.gjs
index 94e9c812435..a196cd4aeb3 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search/ai-search-tabs.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search/ai-search-tabs.gjs
@@ -10,8 +10,8 @@ import { i18n } from "discourse-i18n";
/**
* The kinds of result a combined search fetched, as pills that switch between
- * them. Each keyword tab says how much it found, so an empty one is plain
- * before it is opened.
+ * them. Each keyword tab that isn't open says how much it found, so an empty
+ * one is plain before it is opened.
*/
export default class AiSearchTabs extends Component {
@service aiSearchSession;
@@ -44,10 +44,14 @@ export default class AiSearchTabs extends Component {
empty: this.session.answerFailed,
});
- return tabs.map((tab) => ({
- ...tab,
- selected: this.session.selectedTab === tab.kind,
- }));
+ const selectedTab = this.session.selectedTab;
+ return tabs.map((tab) => {
+ const selected = selectedTab === tab.kind;
+ // the count flags what was found beside the tab in view, so it waits
+ // for one to open and stays off that one
+ const showCount = selectedTab !== null && !selected;
+ return { ...tab, selected, count: showCount ? tab.count : null };
+ });
}
get tabsKey() {
diff --git a/plugins/discourse-ai/spec/system/ai_combined_search_spec.rb b/plugins/discourse-ai/spec/system/ai_combined_search_spec.rb
index d4784a8e3bc..32937a09ac2 100644
--- a/plugins/discourse-ai/spec/system/ai_combined_search_spec.rb
+++ b/plugins/discourse-ai/spec/system/ai_combined_search_spec.rb
@@ -308,11 +308,13 @@ describe "AI combined search" do
ask(query)
expect_tab("topics")
- expect(page).to have_css("#{tab("topics")} .ai-search-tabs__count", text: "1")
expect(page).to have_css(".search-menu .search-result-topic", text: topic_1.title)
+ # counts wait for a tab to open, then show only on the others
+ expect(page).to have_no_css("#{tab("topics")} .ai-search-tabs__count")
# the answer was asked for with the rest, so switching asks nothing more
select_tab("ask")
+ expect(page).to have_css("#{tab("topics")} .ai-search-tabs__count", text: "1")
expect(page).to have_css(".ai-search-answer")
expect(page).to have_no_css(".search-menu .search-result-topic")
publish(
@@ -610,7 +612,7 @@ describe "AI combined search" do
expect(page).to have_css(tab("context"), text: category.name)
expect_tab("context")
expect(scopes.last).to eq("category:#{category.id}")
- expect(page).to have_css("#{tab("context")} .ai-search-tabs__count", text: "1")
+ expect(page).to have_no_css("#{tab("context")} .ai-search-tabs__count")
expect(page).to have_css("#{tab("topics")} .ai-search-tabs__count", text: "2")
# the counts hang off the pills rather than widening them
widths = page.evaluate_script(<<~JS)
@@ -622,6 +624,7 @@ describe "AI combined search" do
select_tab("topics")
expect(page).to have_css(".search-menu .search-result-topic", text: outside.title)
+ expect(page).to have_css("#{tab("context")} .ai-search-tabs__count", text: "1")
expect(request_ids.size).to eq(1)
end
diff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-search-tabs-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-search-tabs-test.gjs
new file mode 100644
index 00000000000..ce760da8a6c
--- /dev/null
+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-search-tabs-test.gjs
@@ -0,0 +1,48 @@
+import { tracked } from "@glimmer/tracking";
+import Service from "@ember/service";
+import { render, settled } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import AiSearchTabs from "discourse/plugins/discourse-ai/discourse/components/ai-search/ai-search-tabs";
+
+module("Integration | Component | AiSearchTabs", function (hooks) {
+ setupRenderingTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.owner.register(
+ "service:ai-search-session",
+ class extends Service {
+ @tracked selectedTab = null;
+ @tracked topicsResults = null;
+ @tracked contextResults = null;
+ @tracked answerFailed = false;
+ scope = null;
+
+ selectTab(tab) {
+ this.selectedTab = tab;
+ }
+ }
+ );
+ this.session = this.owner.lookup("service:ai-search-session");
+ });
+
+ test("counts only the tabs beside the one that opened", async function (assert) {
+ this.session.topicsResults = { posts: [{ id: 1 }, { id: 2 }] };
+
+ await render(<template><AiSearchTabs /></template>);
+
+ assert
+ .dom(".ai-search-tabs__count")
+ .doesNotExist("waits for a tab to be chosen");
+
+ this.session.selectedTab = "ask";
+ await settled();
+ assert.dom(".--topics .ai-search-tabs__count").hasText("2");
+
+ this.session.selectedTab = "topics";
+ await settled();
+ assert
+ .dom(".--topics .ai-search-tabs__count")
+ .doesNotExist("the open tab shows its results instead");
+ });
+});