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.
This commit is contained in:
Keegan George 2024-11-14 00:19:34 +09:00 committed by GitHub
parent 9551b1a4d1
commit f75b13c4fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 31 deletions

View File

@ -18,19 +18,20 @@ export default class SemanticSearch extends Component {
return siteSettings.ai_embeddings_semantic_search_enabled; return siteSettings.ai_embeddings_semantic_search_enabled;
} }
@service router;
@service appEvents; @service appEvents;
@service siteSettings; @service siteSettings;
@service searchPreferencesManager; @service searchPreferencesManager;
@tracked searching = false; @tracked searching = false;
@tracked AIResults = []; @tracked AiResults = [];
@tracked showingAIResults = false; @tracked showingAiResults = false;
initialSearchTerm = this.args.outletArgs.search; initialSearchTerm = this.args.outletArgs.search;
get disableToggleSwitch() { get disableToggleSwitch() {
if ( if (
this.searching || this.searching ||
this.AIResults.length === 0 || this.AiResults.length === 0 ||
this.args.outletArgs.sortOrder !== 0 this.args.outletArgs.sortOrder !== 0
) { ) {
return true; return true;
@ -39,19 +40,19 @@ export default class SemanticSearch extends Component {
get searchStateText() { get searchStateText() {
// Search results: // Search results:
if (this.AIResults.length > 0) { if (this.AiResults.length > 0) {
if (this.showingAIResults) { if (this.showingAiResults) {
return I18n.t( return I18n.t(
"discourse_ai.embeddings.semantic_search_results.toggle", "discourse_ai.embeddings.semantic_search_results.toggle",
{ {
count: this.AIResults.length, count: this.AiResults.length,
} }
); );
} else { } else {
return I18n.t( return I18n.t(
"discourse_ai.embeddings.semantic_search_results.toggle_hidden", "discourse_ai.embeddings.semantic_search_results.toggle_hidden",
{ {
count: this.AIResults.length, count: this.AiResults.length,
} }
); );
} }
@ -64,14 +65,14 @@ export default class SemanticSearch extends Component {
// Typing to search: // Typing to search:
if ( if (
this.AIResults.length === 0 && this.AiResults.length === 0 &&
this.searchTerm !== this.initialSearchTerm this.searchTerm !== this.initialSearchTerm
) { ) {
return I18n.t("discourse_ai.embeddings.semantic_search_results.new"); return I18n.t("discourse_ai.embeddings.semantic_search_results.new");
} }
// No results: // No results:
if (this.AIResults.length === 0) { if (this.AiResults.length === 0) {
return I18n.t("discourse_ai.embeddings.semantic_search_results.none"); return I18n.t("discourse_ai.embeddings.semantic_search_results.none");
} }
} }
@ -93,19 +94,19 @@ export default class SemanticSearch extends Component {
} }
@action @action
toggleAIResults() { toggleAiResults() {
if (this.showingAIResults) { if (this.showingAiResults) {
this.args.outletArgs.addSearchResults([], "topic_id"); this.args.outletArgs.addSearchResults([], "topic_id");
} else { } else {
this.args.outletArgs.addSearchResults(this.AIResults, "topic_id"); this.args.outletArgs.addSearchResults(this.AiResults, "topic_id");
} }
this.showingAIResults = !this.showingAIResults; this.showingAiResults = !this.showingAiResults;
} }
@action @action
resetAIResults() { resetAiResults() {
this.AIResults = []; this.AiResults = [];
this.showingAIResults = false; this.showingAiResults = false;
this.args.outletArgs.addSearchResults([], "topic_id"); this.args.outletArgs.addSearchResults([], "topic_id");
} }
@ -119,19 +120,12 @@ export default class SemanticSearch extends Component {
return this.performHyDESearch(); return this.performHyDESearch();
} }
withPluginApi("1.15.0", (api) => { this.#resetAndSearchOnEvent();
api.onAppEvent("full-page-search:trigger-search", () => {
if (!this.searching) {
this.resetAIResults();
return this.performHyDESearch();
}
});
});
} }
performHyDESearch() { performHyDESearch() {
this.searching = true; this.searching = true;
this.resetAIResults(); this.resetAiResults();
ajax("/discourse-ai/embeddings/semantic-search", { ajax("/discourse-ai/embeddings/semantic-search", {
data: { q: this.searchTerm }, data: { q: this.searchTerm },
@ -145,15 +139,37 @@ export default class SemanticSearch extends Component {
} }
model.posts.forEach((post) => { model.posts.forEach((post) => {
post.generatedByAI = true; post.generatedByAi = true;
}); });
this.AIResults = model.posts; this.AiResults = model.posts;
}) })
.finally(() => (this.searching = false)); .finally(() => (this.searching = false));
} }
#resetAndSearchOnEvent() {
return withPluginApi("1.15.0", (api) => {
api.onAppEvent("full-page-search:trigger-search", () => {
if (!this.searching) {
this.resetAiResults();
return this.performHyDESearch();
}
});
});
}
@action
checkQueryParamsAndSearch() {
// This check is necessary because handleSearch() isn't called
// if query params are present and a new search has appended text.
// It ensures AiResults are reset and searched for properly
const searchQueryParam = this.router.currentRoute?.queryParams?.q;
if (searchQueryParam) {
this.#resetAndSearchOnEvent();
}
}
<template> <template>
<span {{didInsert this.checkQueryParamsAndSearch}}></span>
{{#if this.searchEnabled}} {{#if this.searchEnabled}}
<div class="semantic-search__container search-results" role="region"> <div class="semantic-search__container search-results" role="region">
<div class="semantic-search__results" {{didInsert this.handleSearch}}> <div class="semantic-search__results" {{didInsert this.handleSearch}}>
@ -163,10 +179,9 @@ export default class SemanticSearch extends Component {
> >
<DToggleSwitch <DToggleSwitch
disabled={{this.disableToggleSwitch}} disabled={{this.disableToggleSwitch}}
@state={{this.showingAIResults}} @state={{this.showingAiResults}}
title="AI search results hidden"
class="semantic-search__results-toggle" class="semantic-search__results-toggle"
{{on "click" this.toggleAIResults}} {{on "click" this.toggleAiResults}}
/> />
<div class="semantic-search__searching-text"> <div class="semantic-search__searching-text">

View File

@ -4,6 +4,6 @@ export default apiInitializer("1.15.0", (api) => {
api.modifyClass("component:search-result-entry", { api.modifyClass("component:search-result-entry", {
pluginId: "discourse-ai", pluginId: "discourse-ai",
classNameBindings: ["bulkSelectEnabled", "post.generatedByAI:ai-result"], classNameBindings: ["bulkSelectEnabled", "post.generatedByAi:ai-result"],
}); });
}); });