Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit f75b13c

Browse files
authored
FIX: results not being reset when appending to query param (#912)
This PR fixes an issue where the AI search results were not being reset when you append your search to an existing query param (typically when you've come from quick search). This is because `handleSearch()` doesn't get called in this situation. So here we explicitly check for query params, trigger a reset and search for those occasions.
1 parent 9551b1a commit f75b13c

File tree

2 files changed

+46
-31
lines changed

2 files changed

+46
-31
lines changed

assets/javascripts/discourse/connectors/full-page-search-below-search-header/semantic-search.gjs

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@ export default class SemanticSearch extends Component {
1818
return siteSettings.ai_embeddings_semantic_search_enabled;
1919
}
2020

21+
@service router;
2122
@service appEvents;
2223
@service siteSettings;
2324
@service searchPreferencesManager;
2425

2526
@tracked searching = false;
26-
@tracked AIResults = [];
27-
@tracked showingAIResults = false;
27+
@tracked AiResults = [];
28+
@tracked showingAiResults = false;
2829
initialSearchTerm = this.args.outletArgs.search;
2930

3031
get disableToggleSwitch() {
3132
if (
3233
this.searching ||
33-
this.AIResults.length === 0 ||
34+
this.AiResults.length === 0 ||
3435
this.args.outletArgs.sortOrder !== 0
3536
) {
3637
return true;
@@ -39,19 +40,19 @@ export default class SemanticSearch extends Component {
3940

4041
get searchStateText() {
4142
// Search results:
42-
if (this.AIResults.length > 0) {
43-
if (this.showingAIResults) {
43+
if (this.AiResults.length > 0) {
44+
if (this.showingAiResults) {
4445
return I18n.t(
4546
"discourse_ai.embeddings.semantic_search_results.toggle",
4647
{
47-
count: this.AIResults.length,
48+
count: this.AiResults.length,
4849
}
4950
);
5051
} else {
5152
return I18n.t(
5253
"discourse_ai.embeddings.semantic_search_results.toggle_hidden",
5354
{
54-
count: this.AIResults.length,
55+
count: this.AiResults.length,
5556
}
5657
);
5758
}
@@ -64,14 +65,14 @@ export default class SemanticSearch extends Component {
6465

6566
// Typing to search:
6667
if (
67-
this.AIResults.length === 0 &&
68+
this.AiResults.length === 0 &&
6869
this.searchTerm !== this.initialSearchTerm
6970
) {
7071
return I18n.t("discourse_ai.embeddings.semantic_search_results.new");
7172
}
7273

7374
// No results:
74-
if (this.AIResults.length === 0) {
75+
if (this.AiResults.length === 0) {
7576
return I18n.t("discourse_ai.embeddings.semantic_search_results.none");
7677
}
7778
}
@@ -93,19 +94,19 @@ export default class SemanticSearch extends Component {
9394
}
9495

9596
@action
96-
toggleAIResults() {
97-
if (this.showingAIResults) {
97+
toggleAiResults() {
98+
if (this.showingAiResults) {
9899
this.args.outletArgs.addSearchResults([], "topic_id");
99100
} else {
100-
this.args.outletArgs.addSearchResults(this.AIResults, "topic_id");
101+
this.args.outletArgs.addSearchResults(this.AiResults, "topic_id");
101102
}
102-
this.showingAIResults = !this.showingAIResults;
103+
this.showingAiResults = !this.showingAiResults;
103104
}
104105

105106
@action
106-
resetAIResults() {
107-
this.AIResults = [];
108-
this.showingAIResults = false;
107+
resetAiResults() {
108+
this.AiResults = [];
109+
this.showingAiResults = false;
109110
this.args.outletArgs.addSearchResults([], "topic_id");
110111
}
111112

@@ -119,19 +120,12 @@ export default class SemanticSearch extends Component {
119120
return this.performHyDESearch();
120121
}
121122

122-
withPluginApi("1.15.0", (api) => {
123-
api.onAppEvent("full-page-search:trigger-search", () => {
124-
if (!this.searching) {
125-
this.resetAIResults();
126-
return this.performHyDESearch();
127-
}
128-
});
129-
});
123+
this.#resetAndSearchOnEvent();
130124
}
131125

132126
performHyDESearch() {
133127
this.searching = true;
134-
this.resetAIResults();
128+
this.resetAiResults();
135129

136130
ajax("/discourse-ai/embeddings/semantic-search", {
137131
data: { q: this.searchTerm },
@@ -145,15 +139,37 @@ export default class SemanticSearch extends Component {
145139
}
146140

147141
model.posts.forEach((post) => {
148-
post.generatedByAI = true;
142+
post.generatedByAi = true;
149143
});
150144

151-
this.AIResults = model.posts;
145+
this.AiResults = model.posts;
152146
})
153147
.finally(() => (this.searching = false));
154148
}
155149

150+
#resetAndSearchOnEvent() {
151+
return withPluginApi("1.15.0", (api) => {
152+
api.onAppEvent("full-page-search:trigger-search", () => {
153+
if (!this.searching) {
154+
this.resetAiResults();
155+
return this.performHyDESearch();
156+
}
157+
});
158+
});
159+
}
160+
161+
@action
162+
checkQueryParamsAndSearch() {
163+
// This check is necessary because handleSearch() isn't called
164+
// if query params are present and a new search has appended text.
165+
// It ensures AiResults are reset and searched for properly
166+
const searchQueryParam = this.router.currentRoute?.queryParams?.q;
167+
if (searchQueryParam) {
168+
this.#resetAndSearchOnEvent();
169+
}
170+
}
156171
<template>
172+
<span {{didInsert this.checkQueryParamsAndSearch}}></span>
157173
{{#if this.searchEnabled}}
158174
<div class="semantic-search__container search-results" role="region">
159175
<div class="semantic-search__results" {{didInsert this.handleSearch}}>
@@ -163,10 +179,9 @@ export default class SemanticSearch extends Component {
163179
>
164180
<DToggleSwitch
165181
disabled={{this.disableToggleSwitch}}
166-
@state={{this.showingAIResults}}
167-
title="AI search results hidden"
182+
@state={{this.showingAiResults}}
168183
class="semantic-search__results-toggle"
169-
{{on "click" this.toggleAIResults}}
184+
{{on "click" this.toggleAiResults}}
170185
/>
171186

172187
<div class="semantic-search__searching-text">

assets/javascripts/initializers/ai-semantic-search.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ export default apiInitializer("1.15.0", (api) => {
44
api.modifyClass("component:search-result-entry", {
55
pluginId: "discourse-ai",
66

7-
classNameBindings: ["bulkSelectEnabled", "post.generatedByAI:ai-result"],
7+
classNameBindings: ["bulkSelectEnabled", "post.generatedByAi:ai-result"],
88
});
99
});

0 commit comments

Comments
 (0)