Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

search_suggestion: Prevent duplicate terms in default suggestion.

Evy Kassirerf0210fd6e80e
diff --git a/web/src/search_suggestion.ts b/web/src/search_suggestion.ts
index 7cfa0300318..77951ff3a1a 100644
--- a/web/src/search_suggestion.ts
+++ b/web/src/search_suggestion.ts
@@ -421,7 +421,16 @@ function get_default_suggestion_line(terms: NarrowTerm[]): SuggestionLine {
if (terms.length === 0) {
return [{description_html: "", search_string: "", is_people: false}];
}
- return terms.map((term) => format_as_suggestion([term]));
+ const suggestion_line = [];
+ const suggestion_strings = new Set();
+ for (const term of terms) {
+ const suggestion = format_as_suggestion([term]);
+ if (!suggestion_strings.has(suggestion.search_string)) {
+ suggestion_line.push(suggestion);
+ suggestion_strings.add(suggestion.search_string);
+ }
+ }
+ return suggestion_line;
}
export function get_topic_suggestions_from_candidates({
diff --git a/web/tests/search_suggestion.test.cjs b/web/tests/search_suggestion.test.cjs
index 458dd28dae4..57ebf0c07db 100644
--- a/web/tests/search_suggestion.test.cjs
+++ b/web/tests/search_suggestion.test.cjs
@@ -117,6 +117,18 @@ test("basic_get_suggestions_for_spectator", () => {
page_params.is_spectator = false;
});
+test("get_suggestions deduplication", () => {
+ let query = "has:attachment";
+ let suggestions = get_suggestions(query, query);
+ let expected = ["has:attachment"];
+ assert.deepEqual(suggestions.strings, expected);
+
+ query = "has:attachment has:attachment";
+ suggestions = get_suggestions(query);
+ expected = ["has:attachment"];
+ assert.deepEqual(suggestions.strings, expected);
+});
+
test("get_is_suggestions_for_spectator", () => {
page_params.is_spectator = true;