Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
search_pill: Prevent adding duplicate search pills.
Evy Kassirerd1023660da62
diff --git a/web/src/search_pill.ts b/web/src/search_pill.tsindex fc6241e5fbb..bbcdc4b7699 100644--- a/web/src/search_pill.ts+++ b/web/src/search_pill.ts@@ -178,9 +178,13 @@ export function set_search_bar_contents(let partial_pill = "";const invalid_inputs = [];const search_operator_strings = [];+ const added_pills_as_input_strings = new Set(); // to prevent duplicating termsfor (const term of search_terms) {const input = Filter.unparse([term]);+ if (added_pills_as_input_strings.has(input)) {+ return;+ }// If the last term looks something like `dm:`, we// don't want to make it a pill, since it isn't isn't@@ -215,10 +219,13 @@ export function set_search_bar_contents(return user;});append_user_pill(users, pill_widget, term.operator, term.negated ?? false);+ added_pills_as_input_strings.add(input);} else if (term.operator === "search") {+ // This isn't a pill, so we don't add it to `added_pills_as_input_strings`search_operator_strings.push(input);} else {pill_widget.appendValue(input);+ added_pills_as_input_strings.add(input);}}pill_widget.clear_text();diff --git a/web/tests/search.test.cjs b/web/tests/search.test.cjsindex 7f3149a8f1c..f063d939db7 100644--- a/web/tests/search.test.cjs+++ b/web/tests/search.test.cjs@@ -384,3 +384,32 @@ run_test("initiate_search", ({override_rewire}) => {assert.ok(search_bar_opened);assert.equal($("#search_query").text(), "");});++run_test("set_search_bar_contents with duplicate pills", () => {+ const duplicate_attachment_terms = [+ {+ negated: false,+ operator: "has",+ operand: "attachment",+ },+ {+ negated: false,+ operator: "has",+ operand: "attachment",+ },+ ];+ search_pill.set_search_bar_contents(+ duplicate_attachment_terms,+ search.search_pill_widget,+ false,+ noop,+ );+ const pills = search.search_pill_widget._get_pills_for_testing();+ assert.equal(pills.length, 1);+ assert.deepEqual(pills[0].item, {+ type: "search",+ operator: "has",+ operand: "attachment",+ negated: false,+ });+});